@shikijs/core 1.8.0 → 1.9.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.
@@ -1017,7 +1017,7 @@ interface BundledHighlighterOptions<L extends string, T extends string> {
1017
1017
  *
1018
1018
  * @default []
1019
1019
  */
1020
- themes: (ThemeInput | StringLiteralUnion<T>)[];
1020
+ themes: (ThemeInput | StringLiteralUnion<T> | SpecialTheme)[];
1021
1021
  /**
1022
1022
  * Language registation
1023
1023
  *
package/dist/index.d.mts CHANGED
@@ -10,17 +10,23 @@ export { W as WebAssemblyInstantiator, l as loadWasm } from './chunk-index.mjs';
10
10
  *
11
11
  * @see http://shiki.style/guide/install#fine-grained-bundle
12
12
  */
13
+ declare function createHighlighterCore(options?: HighlighterCoreOptions): Promise<HighlighterCore>;
14
+ declare function makeSingletonHighlighterCore(createHighlighter: typeof createHighlighterCore): (options?: Partial<HighlighterCoreOptions>) => Promise<HighlighterCore>;
15
+ declare const getSingletonHighlighterCore: (options?: Partial<HighlighterCoreOptions>) => Promise<HighlighterCore>;
16
+ /**
17
+ * @deprecated Use `createHighlighterCore` or `getSingletonHighlighterCore` instead.
18
+ */
13
19
  declare function getHighlighterCore(options?: HighlighterCoreOptions): Promise<HighlighterCore>;
14
20
 
15
- type GetHighlighterFactory<L extends string, T extends string> = (options: BundledHighlighterOptions<L, T>) => Promise<HighlighterGeneric<L, T>>;
21
+ type CreateHighlighterFactory<L extends string, T extends string> = (options: BundledHighlighterOptions<L, T>) => Promise<HighlighterGeneric<L, T>>;
16
22
  /**
17
- * Create a `getHighlighter` function with bundled themes and languages.
23
+ * Create a `createHighlighter` function with bundled themes and languages.
18
24
  *
19
25
  * @param bundledLanguages
20
26
  * @param bundledThemes
21
27
  * @param loadWasm
22
28
  */
23
- declare function createdBundledHighlighter<BundledLangs extends string, BundledThemes extends string>(bundledLanguages: Record<BundledLangs, LanguageInput>, bundledThemes: Record<BundledThemes, ThemeInput>, loadWasm: HighlighterCoreOptions['loadWasm']): GetHighlighterFactory<BundledLangs, BundledThemes>;
29
+ declare function createdBundledHighlighter<BundledLangs extends string, BundledThemes extends string>(bundledLanguages: Record<BundledLangs, LanguageInput>, bundledThemes: Record<BundledThemes, ThemeInput>, loadWasm: HighlighterCoreOptions['loadWasm']): CreateHighlighterFactory<BundledLangs, BundledThemes>;
24
30
  interface ShorthandsBundle<L extends string, T extends string> {
25
31
  /**
26
32
  * Shorthand for `codeToHtml` with auto-loaded theme and language.
@@ -59,12 +65,11 @@ interface ShorthandsBundle<L extends string, T extends string> {
59
65
  codeToTokensWithThemes: (code: string, options: RequireKeys<CodeToTokensWithThemesOptions<L, T>, 'themes' | 'lang'>) => Promise<ThemedTokenWithVariants[][]>;
60
66
  /**
61
67
  * Get internal singleton highlighter.
62
- *
63
- * @internal
64
68
  */
65
- getSingletonHighlighter: () => Promise<HighlighterGeneric<L, T>>;
69
+ getSingletonHighlighter: (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
66
70
  }
67
- declare function createSingletonShorthands<L extends string, T extends string>(getHighlighter: GetHighlighterFactory<L, T>): ShorthandsBundle<L, T>;
71
+ declare function makeSingletonHighlighter<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
72
+ declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): ShorthandsBundle<L, T>;
68
73
 
69
74
  declare function toArray<T>(x: MaybeArray<T>): T[];
70
75
  /**
@@ -135,6 +140,10 @@ declare function setDefaultWasmLoader(_loader: LoadWasmOptions): void;
135
140
  /**
136
141
  * Get the minimal shiki context for rendering.
137
142
  */
143
+ declare function createShikiInternal(options?: HighlighterCoreOptions): Promise<ShikiInternal>;
144
+ /**
145
+ * @deprecated Use `createShikiInternal` instead.
146
+ */
138
147
  declare function getShikiInternal(options?: HighlighterCoreOptions): Promise<ShikiInternal>;
139
148
 
140
149
  /**
@@ -401,4 +410,4 @@ declare class ShikiError extends Error {
401
410
  constructor(message: string);
402
411
  }
403
412
 
404
- export { BundledHighlighterOptions, CodeToHastOptions, CodeToHastRenderOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, type GetHighlighterFactory, IGrammar as Grammar, HighlighterCore, HighlighterCoreOptions, HighlighterGeneric, LanguageInput, MaybeArray, PlainTextLanguage, Position, RequireKeys, ShikiError, ShikiInternal, ShikiTransformer, ShikiTransformerContextCommon, ShikiTransformerContextSource, type ShorthandsBundle, SpecialLanguage, SpecialTheme, ThemeInput, ThemeRegistrationAny, ThemeRegistrationResolved, ThemedToken, ThemedTokenWithVariants, TokenStyles, TokenizeWithThemeOptions, TokensResult, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createPositionConverter, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
413
+ export { BundledHighlighterOptions, CodeToHastOptions, CodeToHastRenderOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, type CreateHighlighterFactory, IGrammar as Grammar, HighlighterCore, HighlighterCoreOptions, HighlighterGeneric, LanguageInput, MaybeArray, PlainTextLanguage, Position, RequireKeys, ShikiError, ShikiInternal, ShikiTransformer, ShikiTransformerContextCommon, ShikiTransformerContextSource, type ShorthandsBundle, SpecialLanguage, SpecialTheme, ThemeInput, ThemeRegistrationAny, ThemeRegistrationResolved, ThemedToken, ThemedTokenWithVariants, TokenStyles, TokenizeWithThemeOptions, TokensResult, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createHighlighterCore, createPositionConverter, createShikiInternal, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
package/dist/index.mjs CHANGED
@@ -5399,7 +5399,7 @@ let instancesCount = 0;
5399
5399
  /**
5400
5400
  * Get the minimal shiki context for rendering.
5401
5401
  */
5402
- async function getShikiInternal(options = {}) {
5402
+ async function createShikiInternal(options = {}) {
5403
5403
  instancesCount += 1;
5404
5404
  if (options.warnings !== false && instancesCount >= 10 && instancesCount % 10 === 0)
5405
5405
  console.warn(`[Shiki] ${instancesCount} instances have been created. Shiki is supposed to be used as a singleton, consider refactoring your code to cache your highlighter instance; Or call \`highlighter.dispose()\` to release unused instances.`);
@@ -5499,6 +5499,13 @@ async function getShikiInternal(options = {}) {
5499
5499
  [Symbol.dispose]: dispose,
5500
5500
  };
5501
5501
  }
5502
+ /**
5503
+ * @deprecated Use `createShikiInternal` instead.
5504
+ */
5505
+ function getShikiInternal(options = {}) {
5506
+ // TODO: next: console.warn('`getShikiInternal` is deprecated. Use `createShikiInternal` instead.')
5507
+ return createShikiInternal(options);
5508
+ }
5502
5509
 
5503
5510
  /**
5504
5511
  * Create a Shiki core highlighter instance, with no languages or themes bundled.
@@ -5506,8 +5513,8 @@ async function getShikiInternal(options = {}) {
5506
5513
  *
5507
5514
  * @see http://shiki.style/guide/install#fine-grained-bundle
5508
5515
  */
5509
- async function getHighlighterCore(options = {}) {
5510
- const internal = await getShikiInternal(options);
5516
+ async function createHighlighterCore(options = {}) {
5517
+ const internal = await createShikiInternal(options);
5511
5518
  return {
5512
5519
  codeToTokensBase: (code, options) => codeToTokensBase(internal, code, options),
5513
5520
  codeToTokensWithThemes: (code, options) => codeToTokensWithThemes(internal, code, options),
@@ -5518,16 +5525,47 @@ async function getHighlighterCore(options = {}) {
5518
5525
  getInternalContext: () => internal,
5519
5526
  };
5520
5527
  }
5528
+ function makeSingletonHighlighterCore(createHighlighter) {
5529
+ let _shiki;
5530
+ async function getSingletonHighlighterCore(options = {}) {
5531
+ if (!_shiki) {
5532
+ _shiki = createHighlighter({
5533
+ ...options,
5534
+ themes: options.themes || [],
5535
+ langs: options.langs || [],
5536
+ });
5537
+ return _shiki;
5538
+ }
5539
+ else {
5540
+ const s = await _shiki;
5541
+ await Promise.all([
5542
+ s.loadTheme(...(options.themes || [])),
5543
+ s.loadLanguage(...(options.langs || [])),
5544
+ ]);
5545
+ return s;
5546
+ }
5547
+ }
5548
+ return getSingletonHighlighterCore;
5549
+ }
5550
+ const getSingletonHighlighterCore = /* @__PURE__ */ makeSingletonHighlighterCore(createHighlighterCore);
5551
+ /**
5552
+ * @deprecated Use `createHighlighterCore` or `getSingletonHighlighterCore` instead.
5553
+ */
5554
+ /* v8 ignore next 5 */
5555
+ function getHighlighterCore(options = {}) {
5556
+ // TODO: next: console.warn('`getHighlighterCore` is deprecated. Use `createHighlighterCore` or `getSingletonHighlighterCore` instead.')
5557
+ return createHighlighterCore(options);
5558
+ }
5521
5559
 
5522
5560
  /**
5523
- * Create a `getHighlighter` function with bundled themes and languages.
5561
+ * Create a `createHighlighter` function with bundled themes and languages.
5524
5562
  *
5525
5563
  * @param bundledLanguages
5526
5564
  * @param bundledThemes
5527
5565
  * @param loadWasm
5528
5566
  */
5529
5567
  function createdBundledHighlighter(bundledLanguages, bundledThemes, loadWasm) {
5530
- async function getHighlighter(options) {
5568
+ async function createHighlighter(options) {
5531
5569
  function resolveLang(lang) {
5532
5570
  if (typeof lang === 'string') {
5533
5571
  if (isSpecialLang(lang))
@@ -5553,7 +5591,7 @@ function createdBundledHighlighter(bundledLanguages, bundledThemes, loadWasm) {
5553
5591
  const _themes = (options.themes ?? []).map(i => resolveTheme(i));
5554
5592
  const langs = (options.langs ?? [])
5555
5593
  .map(i => resolveLang(i));
5556
- const core = await getHighlighterCore({
5594
+ const core = await createHighlighterCore({
5557
5595
  ...options,
5558
5596
  themes: _themes,
5559
5597
  langs,
@@ -5569,62 +5607,72 @@ function createdBundledHighlighter(bundledLanguages, bundledThemes, loadWasm) {
5569
5607
  },
5570
5608
  };
5571
5609
  }
5572
- return getHighlighter;
5610
+ return createHighlighter;
5573
5611
  }
5574
- function createSingletonShorthands(getHighlighter) {
5612
+ function makeSingletonHighlighter(createHighlighter) {
5575
5613
  let _shiki;
5576
- async function _getHighlighter(options = {}) {
5614
+ async function getSingletonHighlighter(options = {}) {
5577
5615
  if (!_shiki) {
5578
- _shiki = getHighlighter({
5579
- themes: toArray(options.theme || []),
5580
- langs: toArray(options.lang || []),
5616
+ _shiki = createHighlighter({
5617
+ ...options,
5618
+ themes: options.themes || [],
5619
+ langs: options.langs || [],
5581
5620
  });
5582
5621
  return _shiki;
5583
5622
  }
5584
5623
  else {
5585
5624
  const s = await _shiki;
5586
5625
  await Promise.all([
5587
- s.loadTheme(...toArray(options.theme || [])),
5588
- s.loadLanguage(...toArray(options.lang || [])),
5626
+ s.loadTheme(...(options.themes || [])),
5627
+ s.loadLanguage(...(options.langs || [])),
5589
5628
  ]);
5590
5629
  return s;
5591
5630
  }
5592
5631
  }
5632
+ return getSingletonHighlighter;
5633
+ }
5634
+ function createSingletonShorthands(createHighlighter) {
5635
+ const getSingletonHighlighter = makeSingletonHighlighter(createHighlighter);
5593
5636
  return {
5594
- getSingletonHighlighter: () => _getHighlighter(),
5637
+ getSingletonHighlighter(options) {
5638
+ return getSingletonHighlighter(options);
5639
+ },
5595
5640
  async codeToHtml(code, options) {
5596
- const shiki = await _getHighlighter({
5597
- lang: options.lang,
5598
- theme: ('theme' in options ? [options.theme] : Object.values(options.themes)),
5641
+ const shiki = await getSingletonHighlighter({
5642
+ langs: [options.lang],
5643
+ themes: ('theme' in options ? [options.theme] : Object.values(options.themes)),
5599
5644
  });
5600
5645
  return shiki.codeToHtml(code, options);
5601
5646
  },
5602
5647
  async codeToHast(code, options) {
5603
- const shiki = await _getHighlighter({
5604
- lang: options.lang,
5605
- theme: ('theme' in options ? [options.theme] : Object.values(options.themes)),
5648
+ const shiki = await getSingletonHighlighter({
5649
+ langs: [options.lang],
5650
+ themes: ('theme' in options ? [options.theme] : Object.values(options.themes)),
5606
5651
  });
5607
5652
  return shiki.codeToHast(code, options);
5608
5653
  },
5609
5654
  async codeToTokens(code, options) {
5610
- const shiki = await _getHighlighter({
5611
- lang: options.lang,
5612
- theme: ('theme' in options ? [options.theme] : Object.values(options.themes)),
5655
+ const shiki = await getSingletonHighlighter({
5656
+ langs: [options.lang],
5657
+ themes: ('theme' in options ? [options.theme] : Object.values(options.themes)),
5613
5658
  });
5614
5659
  return shiki.codeToTokens(code, options);
5615
5660
  },
5616
5661
  async codeToTokensBase(code, options) {
5617
- const shiki = await _getHighlighter(options);
5662
+ const shiki = await getSingletonHighlighter({
5663
+ langs: [options.lang],
5664
+ themes: [options.theme],
5665
+ });
5618
5666
  return shiki.codeToTokensBase(code, options);
5619
5667
  },
5620
5668
  async codeToTokensWithThemes(code, options) {
5621
- const shiki = await _getHighlighter({
5622
- lang: options.lang,
5623
- theme: Object.values(options.themes).filter(Boolean),
5669
+ const shiki = await getSingletonHighlighter({
5670
+ langs: [options.lang],
5671
+ themes: Object.values(options.themes).filter(Boolean),
5624
5672
  });
5625
5673
  return shiki.codeToTokensWithThemes(code, options);
5626
5674
  },
5627
5675
  };
5628
5676
  }
5629
5677
 
5630
- export { FontStyle, ShikiError, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createPositionConverter, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
5678
+ export { FontStyle, ShikiError, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createHighlighterCore, createPositionConverter, createShikiInternal, createSingletonShorthands, createdBundledHighlighter, getHighlighterCore, getShikiInternal, getSingletonHighlighterCore, getTokenStyleObject, toHtml as hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, loadWasm, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeTheme, resolveColorReplacements, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/core",
3
3
  "type": "module",
4
- "version": "1.8.0",
4
+ "version": "1.9.1",
5
5
  "description": "Core of Shiki",
6
6
  "author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",