@shikijs/core 2.3.2 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { CreatedBundledHighlighterOptions, CreateHighlighterFactory, LanguageInput, ThemeInput, HighlighterCoreOptions, CodeToHastOptions, CodeToTokensOptions, TokensResult, RequireKeys, CodeToTokensBaseOptions, ThemedToken, CodeToTokensWithThemesOptions, ThemedTokenWithVariants, BundledHighlighterOptions, HighlighterGeneric, GrammarState, HighlighterCore, ShikiInternal, RegexEngine, LoadWasmOptions, ShikiTransformerContextCommon, CodeToHastRenderOptions, ShikiTransformerContextSource, ThemeRegistrationResolved, TokenizeWithThemeOptions, Grammar, ThemeRegistrationAny, ThemeRegistration, ShikiTransformer, MaybeArray, PlainTextLanguage, SpecialLanguage, SpecialTheme, MaybeGetter, TokenStyles, Position } from '@shikijs/types';
1
+ import { CreatedBundledHighlighterOptions, CreateHighlighterFactory, LanguageInput, ThemeInput, HighlighterCoreOptions, CodeToHastOptions, CodeToTokensOptions, TokensResult, RequireKeys, CodeToTokensBaseOptions, ThemedToken, CodeToTokensWithThemesOptions, ThemedTokenWithVariants, BundledHighlighterOptions, HighlighterGeneric, GrammarState, Awaitable, HighlighterCore, ShikiInternal, RegexEngine, LoadWasmOptions, ShikiTransformerContextCommon, CodeToHastRenderOptions, ShikiTransformerContextSource, ThemeRegistrationResolved, TokenizeWithThemeOptions, Grammar, ThemeRegistrationAny, ThemeRegistration, ShikiTransformer, MaybeArray, PlainTextLanguage, SpecialLanguage, SpecialTheme, MaybeGetter, TokenStyles, Position } from '@shikijs/types';
2
2
  export * from '@shikijs/types';
3
3
  import { Root, Element } from 'hast';
4
4
  import { JavaScriptRegexEngineOptions } from '@shikijs/engine-javascript';
@@ -79,7 +79,13 @@ interface ShorthandsBundle<L extends string, T extends string> {
79
79
  getLastGrammarState: ((element: ThemedToken[][] | Root) => GrammarState) | ((code: string, options: CodeToTokensBaseOptions<L, T>) => Promise<GrammarState>);
80
80
  }
81
81
  declare function makeSingletonHighlighter<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
82
- declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): ShorthandsBundle<L, T>;
82
+ interface CreateSingletonShorthandsOptions<L extends string, T extends string> {
83
+ /**
84
+ * A custom function to guess embedded languages to be loaded.
85
+ */
86
+ guessEmbeddedLanguages?: (code: string, lang: string | undefined, highlighter: HighlighterGeneric<L, T>) => Awaitable<string[] | undefined>;
87
+ }
88
+ declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>, config?: CreateSingletonShorthandsOptions<L, T>): ShorthandsBundle<L, T>;
83
89
 
84
90
  /**
85
91
  * Create a Shiki core highlighter instance, with no languages or themes bundled.
@@ -277,6 +283,12 @@ declare function createPositionConverter(code: string): {
277
283
  indexToPos: (index: number) => Position;
278
284
  posToIndex: (line: number, character: number) => number;
279
285
  };
286
+ /**
287
+ * Guess embedded languages from given code and highlighter.
288
+ *
289
+ * When highlighter is provided, only bundled languages will be included.
290
+ */
291
+ declare function guessEmbeddedLanguages(code: string, _lang: string | undefined, highlighter?: HighlighterGeneric<any, any>): string[];
280
292
 
281
293
  type DeprecationTarget = 3;
282
294
  /**
@@ -292,4 +304,4 @@ declare function enableDeprecationWarnings(emitDeprecation?: DeprecationTarget |
292
304
  */
293
305
  declare function warnDeprecated(message: string, version?: DeprecationTarget): void;
294
306
 
295
- export { type CssVariablesThemeOptions, type ShorthandsBundle, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createJavaScriptRegexEngine, createOnigurumaEngine, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createWasmOnigEngine, createdBundledHighlighter, defaultJavaScriptRegexConstructor, enableDeprecationWarnings, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations, warnDeprecated };
307
+ export { type CreateSingletonShorthandsOptions, type CssVariablesThemeOptions, type ShorthandsBundle, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createJavaScriptRegexEngine, createOnigurumaEngine, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createWasmOnigEngine, createdBundledHighlighter, defaultJavaScriptRegexConstructor, enableDeprecationWarnings, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, guessEmbeddedLanguages, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations, warnDeprecated };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CreatedBundledHighlighterOptions, CreateHighlighterFactory, LanguageInput, ThemeInput, HighlighterCoreOptions, CodeToHastOptions, CodeToTokensOptions, TokensResult, RequireKeys, CodeToTokensBaseOptions, ThemedToken, CodeToTokensWithThemesOptions, ThemedTokenWithVariants, BundledHighlighterOptions, HighlighterGeneric, GrammarState, HighlighterCore, ShikiInternal, RegexEngine, LoadWasmOptions, ShikiTransformerContextCommon, CodeToHastRenderOptions, ShikiTransformerContextSource, ThemeRegistrationResolved, TokenizeWithThemeOptions, Grammar, ThemeRegistrationAny, ThemeRegistration, ShikiTransformer, MaybeArray, PlainTextLanguage, SpecialLanguage, SpecialTheme, MaybeGetter, TokenStyles, Position } from '@shikijs/types';
1
+ import { CreatedBundledHighlighterOptions, CreateHighlighterFactory, LanguageInput, ThemeInput, HighlighterCoreOptions, CodeToHastOptions, CodeToTokensOptions, TokensResult, RequireKeys, CodeToTokensBaseOptions, ThemedToken, CodeToTokensWithThemesOptions, ThemedTokenWithVariants, BundledHighlighterOptions, HighlighterGeneric, GrammarState, Awaitable, HighlighterCore, ShikiInternal, RegexEngine, LoadWasmOptions, ShikiTransformerContextCommon, CodeToHastRenderOptions, ShikiTransformerContextSource, ThemeRegistrationResolved, TokenizeWithThemeOptions, Grammar, ThemeRegistrationAny, ThemeRegistration, ShikiTransformer, MaybeArray, PlainTextLanguage, SpecialLanguage, SpecialTheme, MaybeGetter, TokenStyles, Position } from '@shikijs/types';
2
2
  export * from '@shikijs/types';
3
3
  import { Root, Element } from 'hast';
4
4
  import { JavaScriptRegexEngineOptions } from '@shikijs/engine-javascript';
@@ -79,7 +79,13 @@ interface ShorthandsBundle<L extends string, T extends string> {
79
79
  getLastGrammarState: ((element: ThemedToken[][] | Root) => GrammarState) | ((code: string, options: CodeToTokensBaseOptions<L, T>) => Promise<GrammarState>);
80
80
  }
81
81
  declare function makeSingletonHighlighter<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
82
- declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): ShorthandsBundle<L, T>;
82
+ interface CreateSingletonShorthandsOptions<L extends string, T extends string> {
83
+ /**
84
+ * A custom function to guess embedded languages to be loaded.
85
+ */
86
+ guessEmbeddedLanguages?: (code: string, lang: string | undefined, highlighter: HighlighterGeneric<L, T>) => Awaitable<string[] | undefined>;
87
+ }
88
+ declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>, config?: CreateSingletonShorthandsOptions<L, T>): ShorthandsBundle<L, T>;
83
89
 
84
90
  /**
85
91
  * Create a Shiki core highlighter instance, with no languages or themes bundled.
@@ -277,6 +283,12 @@ declare function createPositionConverter(code: string): {
277
283
  indexToPos: (index: number) => Position;
278
284
  posToIndex: (line: number, character: number) => number;
279
285
  };
286
+ /**
287
+ * Guess embedded languages from given code and highlighter.
288
+ *
289
+ * When highlighter is provided, only bundled languages will be included.
290
+ */
291
+ declare function guessEmbeddedLanguages(code: string, _lang: string | undefined, highlighter?: HighlighterGeneric<any, any>): string[];
280
292
 
281
293
  type DeprecationTarget = 3;
282
294
  /**
@@ -292,4 +304,4 @@ declare function enableDeprecationWarnings(emitDeprecation?: DeprecationTarget |
292
304
  */
293
305
  declare function warnDeprecated(message: string, version?: DeprecationTarget): void;
294
306
 
295
- export { type CssVariablesThemeOptions, type ShorthandsBundle, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createJavaScriptRegexEngine, createOnigurumaEngine, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createWasmOnigEngine, createdBundledHighlighter, defaultJavaScriptRegexConstructor, enableDeprecationWarnings, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations, warnDeprecated };
307
+ export { type CreateSingletonShorthandsOptions, type CssVariablesThemeOptions, type ShorthandsBundle, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createJavaScriptRegexEngine, createOnigurumaEngine, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createWasmOnigEngine, createdBundledHighlighter, defaultJavaScriptRegexConstructor, enableDeprecationWarnings, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, guessEmbeddedLanguages, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations, warnDeprecated };
package/dist/index.mjs CHANGED
@@ -172,6 +172,22 @@ function createPositionConverter(code) {
172
172
  posToIndex
173
173
  };
174
174
  }
175
+ function guessEmbeddedLanguages(code, _lang, highlighter) {
176
+ const langs = /* @__PURE__ */ new Set();
177
+ for (const match of code.matchAll(/lang=["']([\w-]+)["']/g)) {
178
+ langs.add(match[1]);
179
+ }
180
+ for (const match of code.matchAll(/(?:```|~~~)([\w-]+)/g)) {
181
+ langs.add(match[1]);
182
+ }
183
+ for (const match of code.matchAll(/\\begin\{([\w-]+)\}/g)) {
184
+ langs.add(match[1]);
185
+ }
186
+ if (!highlighter)
187
+ return Array.from(langs);
188
+ const bundle = highlighter.getBundledLanguages();
189
+ return Array.from(langs).filter((l) => l && bundle[l]);
190
+ }
175
191
 
176
192
  class ShikiError extends Error {
177
193
  constructor(message) {
@@ -284,7 +300,7 @@ function transformerDecorations() {
284
300
  };
285
301
  } else {
286
302
  const line = converter.lines[p.line];
287
- if (line === undefined)
303
+ if (line === void 0)
288
304
  throw new ShikiError(`Invalid decoration position ${JSON.stringify(p)}. Lines length: ${converter.lines.length}`);
289
305
  if (p.character < 0 || p.character > line.length)
290
306
  throw new ShikiError(`Invalid decoration position ${JSON.stringify(p)}. Line ${p.line} length: ${line.length}`);
@@ -466,52 +482,52 @@ var decorations = {
466
482
  3: "italic",
467
483
  4: "underline",
468
484
  7: "reverse",
485
+ 8: "hidden",
469
486
  9: "strikethrough"
470
487
  };
471
488
 
472
489
  // src/parser.ts
473
490
  function findSequence(value, position) {
474
- const nextEscape = value.indexOf("\x1B[", position);
491
+ const nextEscape = value.indexOf("\x1B", position);
475
492
  if (nextEscape !== -1) {
476
- const nextClose = value.indexOf("m", nextEscape);
477
- return {
478
- sequence: value.substring(nextEscape + 2, nextClose).split(";"),
479
- startPosition: nextEscape,
480
- position: nextClose + 1
481
- };
493
+ if (value[nextEscape + 1] === "[") {
494
+ const nextClose = value.indexOf("m", nextEscape);
495
+ if (nextClose !== -1) {
496
+ return {
497
+ sequence: value.substring(nextEscape + 2, nextClose).split(";"),
498
+ startPosition: nextEscape,
499
+ position: nextClose + 1
500
+ };
501
+ }
502
+ }
482
503
  }
483
504
  return {
484
505
  position: value.length
485
506
  };
486
507
  }
487
- function parseColor(sequence, index) {
488
- let offset = 1;
489
- const colorMode = sequence[index + offset++];
490
- let color;
508
+ function parseColor(sequence) {
509
+ const colorMode = sequence.shift();
491
510
  if (colorMode === "2") {
492
- const rgb = [
493
- sequence[index + offset++],
494
- sequence[index + offset++],
495
- sequence[index + offset]
496
- ].map((x) => Number.parseInt(x));
497
- if (rgb.length === 3 && !rgb.some((x) => Number.isNaN(x))) {
498
- color = {
499
- type: "rgb",
500
- rgb
501
- };
502
- }
511
+ const rgb = sequence.splice(0, 3).map((x) => Number.parseInt(x));
512
+ if (rgb.length !== 3 || rgb.some((x) => Number.isNaN(x)))
513
+ return;
514
+ return {
515
+ type: "rgb",
516
+ rgb
517
+ };
503
518
  } else if (colorMode === "5") {
504
- const colorIndex = Number.parseInt(sequence[index + offset]);
505
- if (!Number.isNaN(colorIndex)) {
506
- color = { type: "table", index: Number(colorIndex) };
519
+ const index = sequence.shift();
520
+ if (index) {
521
+ return { type: "table", index: Number(index) };
507
522
  }
508
523
  }
509
- return [offset, color];
510
524
  }
511
525
  function parseSequence(sequence) {
512
526
  const commands = [];
513
- for (let i = 0; i < sequence.length; i++) {
514
- const code = sequence[i];
527
+ while (sequence.length > 0) {
528
+ const code = sequence.shift();
529
+ if (!code)
530
+ continue;
515
531
  const codeInt = Number.parseInt(code);
516
532
  if (Number.isNaN(codeInt))
517
533
  continue;
@@ -532,6 +548,12 @@ function parseSequence(sequence) {
532
548
  type: "resetDecoration",
533
549
  value: decoration
534
550
  });
551
+ if (decoration === "dim") {
552
+ commands.push({
553
+ type: "resetDecoration",
554
+ value: "bold"
555
+ });
556
+ }
535
557
  }
536
558
  } else if (codeInt <= 37) {
537
559
  commands.push({
@@ -539,14 +561,13 @@ function parseSequence(sequence) {
539
561
  value: { type: "named", name: namedColors[codeInt - 30] }
540
562
  });
541
563
  } else if (codeInt === 38) {
542
- const [offset, color] = parseColor(sequence, i);
564
+ const color = parseColor(sequence);
543
565
  if (color) {
544
566
  commands.push({
545
567
  type: "setForegroundColor",
546
568
  value: color
547
569
  });
548
570
  }
549
- i += offset;
550
571
  } else if (codeInt === 39) {
551
572
  commands.push({
552
573
  type: "resetForegroundColor"
@@ -557,18 +578,27 @@ function parseSequence(sequence) {
557
578
  value: { type: "named", name: namedColors[codeInt - 40] }
558
579
  });
559
580
  } else if (codeInt === 48) {
560
- const [offset, color] = parseColor(sequence, i);
581
+ const color = parseColor(sequence);
561
582
  if (color) {
562
583
  commands.push({
563
584
  type: "setBackgroundColor",
564
585
  value: color
565
586
  });
566
587
  }
567
- i += offset;
568
588
  } else if (codeInt === 49) {
569
589
  commands.push({
570
590
  type: "resetBackgroundColor"
571
591
  });
592
+ } else if (codeInt === 53) {
593
+ commands.push({
594
+ type: "setDecoration",
595
+ value: "overline"
596
+ });
597
+ } else if (codeInt === 55) {
598
+ commands.push({
599
+ type: "resetDecoration",
600
+ value: "overline"
601
+ });
572
602
  } else if (codeInt >= 90 && codeInt <= 97) {
573
603
  commands.push({
574
604
  type: "setForegroundColor",
@@ -722,7 +752,7 @@ function tokenizeAnsiWithTheme(theme, fileContents, options) {
722
752
  bgColor = token.foreground ? colorPalette.value(token.foreground) : theme.fg;
723
753
  } else {
724
754
  color = token.foreground ? colorPalette.value(token.foreground) : theme.fg;
725
- bgColor = token.background ? colorPalette.value(token.background) : undefined;
755
+ bgColor = token.background ? colorPalette.value(token.background) : void 0;
726
756
  }
727
757
  color = applyColorReplacements(color, colorReplacements);
728
758
  bgColor = applyColorReplacements(bgColor, colorReplacements);
@@ -830,8 +860,8 @@ function _tokenizeWithTheme(code, grammar, theme, colorMap, options) {
830
860
  colorMap,
831
861
  {
832
862
  ...options,
833
- grammarState: undefined,
834
- grammarContextCode: undefined
863
+ grammarState: void 0,
864
+ grammarContextCode: void 0
835
865
  }
836
866
  ).stateStack : INITIAL;
837
867
  let actual = [];
@@ -857,7 +887,7 @@ function _tokenizeWithTheme(code, grammar, theme, colorMap, options) {
857
887
  let tokensWithScopes;
858
888
  let tokensWithScopesIndex;
859
889
  if (options.includeExplanation) {
860
- resultWithScopes = grammar.tokenizeLine(line, stateStack);
890
+ resultWithScopes = grammar.tokenizeLine(line, stateStack, tokenizeTimeLimit);
861
891
  tokensWithScopes = resultWithScopes.tokens;
862
892
  tokensWithScopesIndex = 0;
863
893
  }
@@ -1020,7 +1050,7 @@ function codeToTokensWithThemes(internal, code, options) {
1020
1050
  const mergedGrammarState = themedTokens[0].state ? new GrammarState(
1021
1051
  Object.fromEntries(themedTokens.map((s) => [s.theme, s.state?.getInternalStack(s.theme)])),
1022
1052
  themedTokens[0].state.lang
1023
- ) : undefined;
1053
+ ) : void 0;
1024
1054
  if (mergedGrammarState)
1025
1055
  setLastGrammarStateToMap(mergedTokens, mergedGrammarState);
1026
1056
  return mergedTokens;
@@ -1091,7 +1121,7 @@ function codeToTokens(internal, code, options) {
1091
1121
  fg = themes.map((t, idx) => (idx === 0 && defaultColor ? "" : `${cssVariablePrefix + t.color}:`) + (applyColorReplacements(themeRegs[idx].fg, themeColorReplacements[idx]) || "inherit")).join(";");
1092
1122
  bg = themes.map((t, idx) => (idx === 0 && defaultColor ? "" : `${cssVariablePrefix + t.color}-bg:`) + (applyColorReplacements(themeRegs[idx].bg, themeColorReplacements[idx]) || "inherit")).join(";");
1093
1123
  themeName = `shiki-themes ${themeRegs.map((t) => t.name).join(" ")}`;
1094
- rootStyle = defaultColor ? undefined : [fg, bg].join(";");
1124
+ rootStyle = defaultColor ? void 0 : [fg, bg].join(";");
1095
1125
  } else if ("theme" in options) {
1096
1126
  const colorReplacements = resolveColorReplacements(options.theme, options);
1097
1127
  tokens = codeToTokensBase(
@@ -1408,7 +1438,7 @@ function normalizeTheme(rawTheme) {
1408
1438
  theme.settings ||= [];
1409
1439
  let { bg, fg } = theme;
1410
1440
  if (!bg || !fg) {
1411
- const globalSetting = theme.settings ? theme.settings.find((s) => !s.name && !s.scope) : undefined;
1441
+ const globalSetting = theme.settings ? theme.settings.find((s) => !s.name && !s.scope) : void 0;
1412
1442
  if (globalSetting?.settings?.foreground)
1413
1443
  fg = globalSetting.settings.foreground;
1414
1444
  if (globalSetting?.settings?.background)
@@ -1808,6 +1838,8 @@ async function createHighlighterCore(options) {
1808
1838
  codeToTokens: (code, options2) => codeToTokens(internal, code, options2),
1809
1839
  codeToHast: (code, options2) => codeToHast(internal, code, options2),
1810
1840
  codeToHtml: (code, options2) => codeToHtml(internal, code, options2),
1841
+ getBundledLanguages: () => ({}),
1842
+ getBundledThemes: () => ({}),
1811
1843
  ...internal,
1812
1844
  getInternalContext: () => internal
1813
1845
  };
@@ -1821,6 +1853,8 @@ function createHighlighterCoreSync(options) {
1821
1853
  codeToTokens: (code, options2) => codeToTokens(internal, code, options2),
1822
1854
  codeToHast: (code, options2) => codeToHast(internal, code, options2),
1823
1855
  codeToHtml: (code, options2) => codeToHtml(internal, code, options2),
1856
+ getBundledLanguages: () => ({}),
1857
+ getBundledThemes: () => ({}),
1824
1858
  ...internal,
1825
1859
  getInternalContext: () => internal
1826
1860
  };
@@ -1905,6 +1939,12 @@ function createdBundledHighlighter(arg1, arg2, arg3) {
1905
1939
  },
1906
1940
  loadTheme(...themes) {
1907
1941
  return core.loadTheme(...themes.map(resolveTheme));
1942
+ },
1943
+ getBundledLanguages() {
1944
+ return bundledLanguages;
1945
+ },
1946
+ getBundledThemes() {
1947
+ return bundledThemes;
1908
1948
  }
1909
1949
  };
1910
1950
  }
@@ -1931,45 +1971,41 @@ function makeSingletonHighlighter(createHighlighter) {
1931
1971
  }
1932
1972
  return getSingletonHighlighter;
1933
1973
  }
1934
- function createSingletonShorthands(createHighlighter) {
1974
+ function createSingletonShorthands(createHighlighter, config) {
1935
1975
  const getSingletonHighlighter = makeSingletonHighlighter(createHighlighter);
1976
+ async function get(code, options) {
1977
+ const shiki = await getSingletonHighlighter({
1978
+ langs: [options.lang],
1979
+ themes: "theme" in options ? [options.theme] : Object.values(options.themes)
1980
+ });
1981
+ const langs = await config?.guessEmbeddedLanguages?.(code, options.lang, shiki);
1982
+ if (langs) {
1983
+ await shiki.loadLanguage(...langs);
1984
+ }
1985
+ return shiki;
1986
+ }
1936
1987
  return {
1937
1988
  getSingletonHighlighter(options) {
1938
1989
  return getSingletonHighlighter(options);
1939
1990
  },
1940
1991
  async codeToHtml(code, options) {
1941
- const shiki = await getSingletonHighlighter({
1942
- langs: [options.lang],
1943
- themes: "theme" in options ? [options.theme] : Object.values(options.themes)
1944
- });
1992
+ const shiki = await get(code, options);
1945
1993
  return shiki.codeToHtml(code, options);
1946
1994
  },
1947
1995
  async codeToHast(code, options) {
1948
- const shiki = await getSingletonHighlighter({
1949
- langs: [options.lang],
1950
- themes: "theme" in options ? [options.theme] : Object.values(options.themes)
1951
- });
1996
+ const shiki = await get(code, options);
1952
1997
  return shiki.codeToHast(code, options);
1953
1998
  },
1954
1999
  async codeToTokens(code, options) {
1955
- const shiki = await getSingletonHighlighter({
1956
- langs: [options.lang],
1957
- themes: "theme" in options ? [options.theme] : Object.values(options.themes)
1958
- });
2000
+ const shiki = await get(code, options);
1959
2001
  return shiki.codeToTokens(code, options);
1960
2002
  },
1961
2003
  async codeToTokensBase(code, options) {
1962
- const shiki = await getSingletonHighlighter({
1963
- langs: [options.lang],
1964
- themes: [options.theme]
1965
- });
2004
+ const shiki = await get(code, options);
1966
2005
  return shiki.codeToTokensBase(code, options);
1967
2006
  },
1968
2007
  async codeToTokensWithThemes(code, options) {
1969
- const shiki = await getSingletonHighlighter({
1970
- langs: [options.lang],
1971
- themes: Object.values(options.themes).filter(Boolean)
1972
- });
2008
+ const shiki = await get(code, options);
1973
2009
  return shiki.codeToTokensWithThemes(code, options);
1974
2010
  },
1975
2011
  async getLastGrammarState(code, options) {
@@ -2226,4 +2262,4 @@ function createCssVariablesTheme(options = {}) {
2226
2262
  return theme;
2227
2263
  }
2228
2264
 
2229
- export { addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createJavaScriptRegexEngine, createOnigurumaEngine, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createWasmOnigEngine, createdBundledHighlighter, defaultJavaScriptRegexConstructor, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations, warnDeprecated };
2265
+ export { addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createJavaScriptRegexEngine, createOnigurumaEngine, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createWasmOnigEngine, createdBundledHighlighter, defaultJavaScriptRegexConstructor, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, guessEmbeddedLanguages, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations, warnDeprecated };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/core",
3
3
  "type": "module",
4
- "version": "2.3.2",
4
+ "version": "2.4.1",
5
5
  "description": "Core of Shiki",
6
6
  "author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -60,12 +60,12 @@
60
60
  "dist"
61
61
  ],
62
62
  "dependencies": {
63
- "@shikijs/vscode-textmate": "^10.0.1",
63
+ "@shikijs/vscode-textmate": "^10.0.2",
64
64
  "@types/hast": "^3.0.4",
65
65
  "hast-util-to-html": "^9.0.4",
66
- "@shikijs/engine-javascript": "2.3.2",
67
- "@shikijs/types": "2.3.2",
68
- "@shikijs/engine-oniguruma": "2.3.2"
66
+ "@shikijs/engine-javascript": "2.4.1",
67
+ "@shikijs/engine-oniguruma": "2.4.1",
68
+ "@shikijs/types": "2.4.1"
69
69
  },
70
70
  "scripts": {
71
71
  "build": "unbuild",