@shikijs/core 1.9.1 → 1.10.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.
@@ -852,6 +852,24 @@ interface BundledThemeInfo {
852
852
  import: DynamicImportThemeRegistration;
853
853
  }
854
854
 
855
+ /**
856
+ * GrammarState is a special reference object that holds the state of a grammar.
857
+ *
858
+ * It's used to highlight code snippets that are part of the target language.
859
+ */
860
+ declare class GrammarState {
861
+ private _stack;
862
+ lang: string;
863
+ theme: string;
864
+ constructor(_stack: StateStack, lang: string, theme: string);
865
+ get scopes(): string[];
866
+ toJSON(): {
867
+ lang: string;
868
+ theme: string;
869
+ scopes: string[];
870
+ };
871
+ }
872
+
855
873
  interface TransformerOptions {
856
874
  /**
857
875
  * Transformers for the Shiki pipeline.
@@ -1087,7 +1105,7 @@ interface CodeOptionsMultipleThemes<Themes extends string = string> {
1087
1105
  }
1088
1106
  type CodeOptionsThemes<Themes extends string = string> = CodeOptionsSingleTheme<Themes> | CodeOptionsMultipleThemes<Themes>;
1089
1107
  type CodeToHastOptions<Languages extends string = string, Themes extends string = string> = CodeToHastOptionsCommon<Languages> & CodeOptionsThemes<Themes> & CodeOptionsMeta;
1090
- interface CodeToHastOptionsCommon<Languages extends string = string> extends TransformerOptions, DecorationOptions, Pick<TokenizeWithThemeOptions, 'colorReplacements' | 'tokenizeMaxLineLength' | 'tokenizeTimeLimit'> {
1108
+ interface CodeToHastOptionsCommon<Languages extends string = string> extends TransformerOptions, DecorationOptions, Pick<TokenizeWithThemeOptions, 'colorReplacements' | 'tokenizeMaxLineLength' | 'tokenizeTimeLimit' | 'grammarState' | 'grammarContextCode'> {
1091
1109
  lang: StringLiteralUnion<Languages | SpecialLanguage>;
1092
1110
  /**
1093
1111
  * Merge whitespace tokens to saving extra `<span>`.
@@ -1290,6 +1308,19 @@ interface TokenizeWithThemeOptions {
1290
1308
  * @default 500 (0.5s)
1291
1309
  */
1292
1310
  tokenizeTimeLimit?: number;
1311
+ /**
1312
+ * Represent the state of the grammar, allowing to continue tokenizing from a intermediate grammar state.
1313
+ *
1314
+ * You can get the grammar state from `getLastGrammarState`.
1315
+ */
1316
+ grammarState?: GrammarState;
1317
+ /**
1318
+ * The code context of the grammar.
1319
+ * Consider it a prepended code to the input code, that only participate the grammar inference but not presented in the final output.
1320
+ *
1321
+ * This will be ignored if `grammarState` is provided.
1322
+ */
1323
+ grammarContextCode?: string;
1293
1324
  }
1294
1325
  /**
1295
1326
  * Result of `codeToTokens`, an object with 2D array of tokens and meta info like background and foreground color.
@@ -1325,4 +1356,4 @@ declare enum FontStyle {
1325
1356
  Underline = 4
1326
1357
  }
1327
1358
 
1328
- export { type ThemeRegistration as $, type RootContent as A, type BundledHighlighterOptions as B, type CodeToHastOptions as C, type ShikiTransformer as D, type Element as E, FontStyle as F, type AnsiLanguage as G, type HighlighterCoreOptions as H, INITIAL as I, type ResolveBundleKey as J, type LanguageRegistration as K, type LanguageInput as L, type MaybeArray as M, type Nodes as N, type BundledLanguageInfo as O, type PlainTextLanguage as P, type DynamicImportLanguageRegistration as Q, Registry as R, type StateStack as S, Theme as T, type CodeOptionsSingleTheme as U, type CodeOptionsMultipleThemes as V, type CodeOptionsThemes as W, type CodeToHastOptionsCommon as X, type CodeOptionsMeta as Y, type CodeToHastRenderOptionsCommon as Z, type ThemeRegistrationRaw as _, type IRawTheme as a, type DynamicImportThemeRegistration as a0, type BundledThemeInfo as a1, type ThemedTokenScopeExplanation as a2, type ThemedTokenExplanation as a3, type TokenBase as a4, type TransformerOptions as a5, type ShikiTransformerContextMeta as a6, type ShikiTransformerContext as a7, type Awaitable as a8, type MaybeGetter as a9, type MaybeModule as aa, type StringLiteralUnion as ab, type DecorationOptions as ac, type DecorationItem as ad, type ResolvedDecorationItem as ae, type DecorationTransformType as af, type Offset as ag, type OffsetOrPosition as ah, type ResolvedPosition as ai, type IRawGrammar as b, type IGrammar as c, type IGrammarConfiguration as d, type IOnigLib as e, type RegistryOptions as f, type IRawThemeSetting as g, type ThemeInput as h, type Root as i, type CodeToTokensOptions as j, type TokensResult as k, type RequireKeys as l, type CodeToTokensBaseOptions as m, type ThemedToken as n, type CodeToTokensWithThemesOptions as o, type ThemedTokenWithVariants as p, type SpecialLanguage as q, type SpecialTheme as r, type ThemeRegistrationAny as s, type TokenizeWithThemeOptions as t, type TokenStyles as u, type Position as v, type ThemeRegistrationResolved as w, type ShikiTransformerContextCommon as x, type CodeToHastRenderOptions as y, type ShikiTransformerContextSource as z };
1359
+ export { type ThemeRegistrationRaw as $, type RootContent as A, type BundledHighlighterOptions as B, type CodeToHastOptions as C, type ShikiTransformer as D, type Element as E, FontStyle as F, GrammarState as G, type HighlighterCoreOptions as H, INITIAL as I, type AnsiLanguage as J, type ResolveBundleKey as K, type LanguageInput as L, type MaybeArray as M, type Nodes as N, type LanguageRegistration as O, type PlainTextLanguage as P, type BundledLanguageInfo as Q, Registry as R, type StateStack as S, Theme as T, type DynamicImportLanguageRegistration as U, type CodeOptionsSingleTheme as V, type CodeOptionsMultipleThemes as W, type CodeOptionsThemes as X, type CodeToHastOptionsCommon as Y, type CodeOptionsMeta as Z, type CodeToHastRenderOptionsCommon as _, type IRawTheme as a, type ThemeRegistration as a0, type DynamicImportThemeRegistration as a1, type BundledThemeInfo as a2, type ThemedTokenScopeExplanation as a3, type ThemedTokenExplanation as a4, type TokenBase as a5, type TransformerOptions as a6, type ShikiTransformerContextMeta as a7, type ShikiTransformerContext as a8, type Awaitable as a9, type MaybeGetter as aa, type MaybeModule as ab, type StringLiteralUnion as ac, type DecorationOptions as ad, type DecorationItem as ae, type ResolvedDecorationItem as af, type DecorationTransformType as ag, type Offset as ah, type OffsetOrPosition as ai, type ResolvedPosition as aj, type IRawGrammar as b, type IGrammar as c, type IGrammarConfiguration as d, type IOnigLib as e, type RegistryOptions as f, type IRawThemeSetting as g, type ThemeInput as h, type Root as i, type CodeToTokensOptions as j, type TokensResult as k, type RequireKeys as l, type CodeToTokensBaseOptions as m, type ThemedToken as n, type CodeToTokensWithThemesOptions as o, type ThemedTokenWithVariants as p, type SpecialLanguage as q, type SpecialTheme as r, type ThemeRegistrationAny as s, type TokenizeWithThemeOptions as t, type TokenStyles as u, type Position as v, type ThemeRegistrationResolved as w, type ShikiTransformerContextCommon as x, type CodeToHastRenderOptions as y, type ShikiTransformerContextSource as z };
package/dist/index.d.mts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { HighlighterCore, HighlighterGeneric, ShikiInternal } from './types.mjs';
2
- import { H as HighlighterCoreOptions, B as BundledHighlighterOptions, L as LanguageInput, h as ThemeInput, C as CodeToHastOptions, i as Root, j as CodeToTokensOptions, k as TokensResult, l as RequireKeys, m as CodeToTokensBaseOptions, n as ThemedToken, o as CodeToTokensWithThemesOptions, p as ThemedTokenWithVariants, M as MaybeArray, P as PlainTextLanguage, q as SpecialLanguage, r as SpecialTheme, E as Element, s as ThemeRegistrationAny, t as TokenizeWithThemeOptions, u as TokenStyles, v as Position, c as IGrammar, w as ThemeRegistrationResolved, x as ShikiTransformerContextCommon, y as CodeToHastRenderOptions, z as ShikiTransformerContextSource, N as Nodes$1, A as RootContent$1, D as ShikiTransformer } from './chunk-tokens.mjs';
3
- export { G as AnsiLanguage, a8 as Awaitable, O as BundledLanguageInfo, a1 as BundledThemeInfo, Y as CodeOptionsMeta, V as CodeOptionsMultipleThemes, U as CodeOptionsSingleTheme, W as CodeOptionsThemes, X as CodeToHastOptionsCommon, Z as CodeToHastRenderOptionsCommon, ad as DecorationItem, ac as DecorationOptions, af as DecorationTransformType, Q as DynamicImportLanguageRegistration, a0 as DynamicImportThemeRegistration, F as FontStyle, K as LanguageRegistration, a9 as MaybeGetter, aa as MaybeModule, ag as Offset, ah as OffsetOrPosition, b as RawGrammar, a as RawTheme, g as RawThemeSetting, J as ResolveBundleKey, ae as ResolvedDecorationItem, ai as ResolvedPosition, a7 as ShikiTransformerContext, a6 as ShikiTransformerContextMeta, ab as StringLiteralUnion, $ as ThemeRegistration, _ as ThemeRegistrationRaw, a3 as ThemedTokenExplanation, a2 as ThemedTokenScopeExplanation, a4 as TokenBase, a5 as TransformerOptions } from './chunk-tokens.mjs';
2
+ export { Grammar } from './types.mjs';
3
+ import { H as HighlighterCoreOptions, B as BundledHighlighterOptions, L as LanguageInput, h as ThemeInput, C as CodeToHastOptions, i as Root, j as CodeToTokensOptions, k as TokensResult, l as RequireKeys, m as CodeToTokensBaseOptions, n as ThemedToken, o as CodeToTokensWithThemesOptions, p as ThemedTokenWithVariants, G as GrammarState, M as MaybeArray, P as PlainTextLanguage, q as SpecialLanguage, r as SpecialTheme, E as Element, s as ThemeRegistrationAny, t as TokenizeWithThemeOptions, u as TokenStyles, v as Position, c as IGrammar, w as ThemeRegistrationResolved, x as ShikiTransformerContextCommon, y as CodeToHastRenderOptions, z as ShikiTransformerContextSource, N as Nodes$1, A as RootContent$1, D as ShikiTransformer } from './chunk-tokens.mjs';
4
+ export { J as AnsiLanguage, a9 as Awaitable, Q as BundledLanguageInfo, a2 as BundledThemeInfo, Z as CodeOptionsMeta, W as CodeOptionsMultipleThemes, V as CodeOptionsSingleTheme, X as CodeOptionsThemes, Y as CodeToHastOptionsCommon, _ as CodeToHastRenderOptionsCommon, ae as DecorationItem, ad as DecorationOptions, ag as DecorationTransformType, U as DynamicImportLanguageRegistration, a1 as DynamicImportThemeRegistration, F as FontStyle, O as LanguageRegistration, aa as MaybeGetter, ab as MaybeModule, ah as Offset, ai as OffsetOrPosition, b as RawGrammar, a as RawTheme, g as RawThemeSetting, K as ResolveBundleKey, af as ResolvedDecorationItem, aj as ResolvedPosition, a8 as ShikiTransformerContext, a7 as ShikiTransformerContextMeta, ac as StringLiteralUnion, a0 as ThemeRegistration, $ as ThemeRegistrationRaw, a4 as ThemedTokenExplanation, a3 as ThemedTokenScopeExplanation, a5 as TokenBase, a6 as TransformerOptions } from './chunk-tokens.mjs';
4
5
  import { L as LoadWasmOptions } from './chunk-index.mjs';
5
6
  export { W as WebAssemblyInstantiator, l as loadWasm } from './chunk-index.mjs';
6
7
 
@@ -64,9 +65,14 @@ interface ShorthandsBundle<L extends string, T extends string> {
64
65
  */
65
66
  codeToTokensWithThemes: (code: string, options: RequireKeys<CodeToTokensWithThemesOptions<L, T>, 'themes' | 'lang'>) => Promise<ThemedTokenWithVariants[][]>;
66
67
  /**
67
- * Get internal singleton highlighter.
68
+ * Get the singleton highlighter.
68
69
  */
69
70
  getSingletonHighlighter: (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
71
+ /**
72
+ * Shorthand for `getLastGrammarState` with auto-loaded theme and language.
73
+ * A singleton highlighter it maintained internally.
74
+ */
75
+ getLastGrammarState: (code: string, options: CodeToTokensBaseOptions<L, T>) => Promise<GrammarState>;
70
76
  }
71
77
  declare function makeSingletonHighlighter<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
72
78
  declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): ShorthandsBundle<L, T>;
@@ -410,4 +416,4 @@ declare class ShikiError extends Error {
410
416
  constructor(message: string);
411
417
  }
412
418
 
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 };
419
+ export { BundledHighlighterOptions, CodeToHastOptions, CodeToHastRenderOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, type CreateHighlighterFactory, GrammarState, 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
@@ -1,4 +1,4 @@
1
- import { StackElementMetadata, INITIAL, Registry as Registry$1, Theme } from './textmate.mjs';
1
+ import { INITIAL, StackElementMetadata, Registry as Registry$1, Theme } from './textmate.mjs';
2
2
  import { FontStyle } from './types.mjs';
3
3
 
4
4
  function toArray(x) {
@@ -517,6 +517,61 @@ function dimColor(color) {
517
517
  return color;
518
518
  }
519
519
 
520
+ class ShikiError extends Error {
521
+ constructor(message) {
522
+ super(message);
523
+ this.name = 'ShikiError';
524
+ }
525
+ }
526
+
527
+ /**
528
+ * GrammarState is a special reference object that holds the state of a grammar.
529
+ *
530
+ * It's used to highlight code snippets that are part of the target language.
531
+ */
532
+ class GrammarState {
533
+ _stack;
534
+ lang;
535
+ theme;
536
+ constructor(_stack, lang, theme) {
537
+ this._stack = _stack;
538
+ this.lang = lang;
539
+ this.theme = theme;
540
+ }
541
+ get scopes() {
542
+ return getScopes(this._stack);
543
+ }
544
+ toJSON() {
545
+ return {
546
+ lang: this.lang,
547
+ theme: this.theme,
548
+ scopes: this.scopes,
549
+ };
550
+ }
551
+ }
552
+ function getScopes(stack) {
553
+ const scopes = [];
554
+ const visited = new Set();
555
+ function pushScope(stack) {
556
+ if (visited.has(stack))
557
+ return;
558
+ visited.add(stack);
559
+ const name = stack?.nameScopesList?.scopeName;
560
+ if (name)
561
+ scopes.push(name);
562
+ if (stack.parent)
563
+ pushScope(stack.parent);
564
+ }
565
+ pushScope(stack);
566
+ return scopes;
567
+ }
568
+ function getGrammarStack(state) {
569
+ if (!(state instanceof GrammarState))
570
+ throw new ShikiError('Invalid grammar state');
571
+ // @ts-expect-error _stack is private
572
+ return state._stack;
573
+ }
574
+
520
575
  /**
521
576
  * Code to tokens, with a simple theme.
522
577
  */
@@ -528,13 +583,42 @@ function codeToTokensBase(internal, code, options = {}) {
528
583
  if (lang === 'ansi')
529
584
  return tokenizeAnsiWithTheme(theme, code, options);
530
585
  const _grammar = internal.getLanguage(lang);
586
+ if (options.grammarState) {
587
+ if (options.grammarState.lang !== _grammar.name) {
588
+ throw new ShikiError(`Grammar state language "${options.grammarState.lang}" does not match highlight language "${_grammar.name}"`);
589
+ }
590
+ if (options.grammarState.theme !== themeName) {
591
+ throw new ShikiError(`Grammar state theme "${options.grammarState.theme}" does not match highlight theme "${themeName}"`);
592
+ }
593
+ }
531
594
  return tokenizeWithTheme(code, _grammar, theme, colorMap, options);
532
595
  }
596
+ function getLastGrammarState(internal, code, options = {}) {
597
+ const { lang = 'text', theme: themeName = internal.getLoadedThemes()[0], } = options;
598
+ if (isPlainLang(lang) || isNoneTheme(themeName))
599
+ throw new ShikiError('Plain language does not have grammar state');
600
+ if (lang === 'ansi')
601
+ throw new ShikiError('ANSI language does not have grammar state');
602
+ const { theme, colorMap } = internal.setTheme(themeName);
603
+ const _grammar = internal.getLanguage(lang);
604
+ return new GrammarState(_tokenizeWithTheme(code, _grammar, theme, colorMap, options).stateStack, _grammar.name, theme.name);
605
+ }
533
606
  function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
607
+ return _tokenizeWithTheme(code, grammar, theme, colorMap, options).tokens;
608
+ }
609
+ function _tokenizeWithTheme(code, grammar, theme, colorMap, options) {
534
610
  const colorReplacements = resolveColorReplacements(theme, options);
535
611
  const { tokenizeMaxLineLength = 0, tokenizeTimeLimit = 500, } = options;
536
612
  const lines = splitLines(code);
537
- let ruleStack = INITIAL;
613
+ let stateStack = options.grammarState
614
+ ? getGrammarStack(options.grammarState)
615
+ : options.grammarContextCode != null
616
+ ? _tokenizeWithTheme(options.grammarContextCode, grammar, theme, colorMap, {
617
+ ...options,
618
+ grammarState: undefined,
619
+ grammarContextCode: undefined,
620
+ }).stateStack
621
+ : INITIAL;
538
622
  let actual = [];
539
623
  const final = [];
540
624
  const themeSettingsSelectors = [];
@@ -579,11 +663,11 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
579
663
  let tokensWithScopes;
580
664
  let tokensWithScopesIndex;
581
665
  if (options.includeExplanation) {
582
- resultWithScopes = grammar.tokenizeLine(line, ruleStack);
666
+ resultWithScopes = grammar.tokenizeLine(line, stateStack);
583
667
  tokensWithScopes = resultWithScopes.tokens;
584
668
  tokensWithScopesIndex = 0;
585
669
  }
586
- const result = grammar.tokenizeLine2(line, ruleStack, tokenizeTimeLimit);
670
+ const result = grammar.tokenizeLine2(line, stateStack, tokenizeTimeLimit);
587
671
  const tokensLength = result.tokens.length / 2;
588
672
  for (let j = 0; j < tokensLength; j++) {
589
673
  const startIndex = result.tokens[2 * j];
@@ -617,9 +701,12 @@ function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
617
701
  }
618
702
  final.push(actual);
619
703
  actual = [];
620
- ruleStack = result.ruleStack;
704
+ stateStack = result.ruleStack;
621
705
  }
622
- return final;
706
+ return {
707
+ tokens: final,
708
+ stateStack,
709
+ };
623
710
  }
624
711
  function explainThemeScopes(themeSelectors, scopes) {
625
712
  const result = [];
@@ -739,13 +826,6 @@ function syncThemesTokenization(...themes) {
739
826
  return outThemes;
740
827
  }
741
828
 
742
- class ShikiError extends Error {
743
- constructor(message) {
744
- super(message);
745
- this.name = 'ShikiError';
746
- }
747
- }
748
-
749
829
  /**
750
830
  * High-level code-to-tokens API.
751
831
  *
@@ -5274,6 +5354,7 @@ class Registry extends Registry$1 {
5274
5354
  // @ts-expect-error Private members, set this to override the previous grammar (that can be a stub)
5275
5355
  this._syncRegistry._rawGrammars.set(lang.scopeName, lang);
5276
5356
  const g = await this.loadGrammarWithConfiguration(lang.scopeName, 1, grammarConfig);
5357
+ g.name = lang.name;
5277
5358
  this._resolvedGrammars.set(lang.name, g);
5278
5359
  if (lang.aliases) {
5279
5360
  lang.aliases.forEach((alias) => {
@@ -5516,6 +5597,7 @@ function getShikiInternal(options = {}) {
5516
5597
  async function createHighlighterCore(options = {}) {
5517
5598
  const internal = await createShikiInternal(options);
5518
5599
  return {
5600
+ getLastGrammarState: (code, options) => getLastGrammarState(internal, code, options),
5519
5601
  codeToTokensBase: (code, options) => codeToTokensBase(internal, code, options),
5520
5602
  codeToTokensWithThemes: (code, options) => codeToTokensWithThemes(internal, code, options),
5521
5603
  codeToTokens: (code, options) => codeToTokens(internal, code, options),
@@ -5672,6 +5754,13 @@ function createSingletonShorthands(createHighlighter) {
5672
5754
  });
5673
5755
  return shiki.codeToTokensWithThemes(code, options);
5674
5756
  },
5757
+ async getLastGrammarState(code, options) {
5758
+ const shiki = await getSingletonHighlighter({
5759
+ langs: [options.lang],
5760
+ themes: [options.theme],
5761
+ });
5762
+ return shiki.getLastGrammarState(code, options);
5763
+ },
5675
5764
  };
5676
5765
  }
5677
5766
 
package/dist/types.d.mts CHANGED
@@ -1,7 +1,11 @@
1
- import { h as ThemeInput, r as SpecialTheme, L as LanguageInput, q as SpecialLanguage, s as ThemeRegistrationAny, w as ThemeRegistrationResolved, K as LanguageRegistration, c as IGrammar, C as CodeToHastOptions, J as ResolveBundleKey, i as Root, j as CodeToTokensOptions, k as TokensResult, m as CodeToTokensBaseOptions, n as ThemedToken, o as CodeToTokensWithThemesOptions, p as ThemedTokenWithVariants } from './chunk-tokens.mjs';
2
- export { G as AnsiLanguage, a8 as Awaitable, B as BundledHighlighterOptions, O as BundledLanguageInfo, a1 as BundledThemeInfo, Y as CodeOptionsMeta, V as CodeOptionsMultipleThemes, U as CodeOptionsSingleTheme, W as CodeOptionsThemes, X as CodeToHastOptionsCommon, y as CodeToHastRenderOptions, Z as CodeToHastRenderOptionsCommon, ad as DecorationItem, ac as DecorationOptions, af as DecorationTransformType, Q as DynamicImportLanguageRegistration, a0 as DynamicImportThemeRegistration, F as FontStyle, H as HighlighterCoreOptions, M as MaybeArray, a9 as MaybeGetter, aa as MaybeModule, ag as Offset, ah as OffsetOrPosition, P as PlainTextLanguage, v as Position, b as RawGrammar, a as RawTheme, g as RawThemeSetting, l as RequireKeys, ae as ResolvedDecorationItem, ai as ResolvedPosition, D as ShikiTransformer, a7 as ShikiTransformerContext, x as ShikiTransformerContextCommon, a6 as ShikiTransformerContextMeta, z as ShikiTransformerContextSource, ab as StringLiteralUnion, $ as ThemeRegistration, _ as ThemeRegistrationRaw, a3 as ThemedTokenExplanation, a2 as ThemedTokenScopeExplanation, a4 as TokenBase, u as TokenStyles, t as TokenizeWithThemeOptions, a5 as TransformerOptions } from './chunk-tokens.mjs';
1
+ import { c as IGrammar, h as ThemeInput, r as SpecialTheme, L as LanguageInput, q as SpecialLanguage, s as ThemeRegistrationAny, w as ThemeRegistrationResolved, O as LanguageRegistration, C as CodeToHastOptions, K as ResolveBundleKey, i as Root, j as CodeToTokensOptions, k as TokensResult, m as CodeToTokensBaseOptions, n as ThemedToken, o as CodeToTokensWithThemesOptions, p as ThemedTokenWithVariants, G as GrammarState } from './chunk-tokens.mjs';
2
+ export { J as AnsiLanguage, a9 as Awaitable, B as BundledHighlighterOptions, Q as BundledLanguageInfo, a2 as BundledThemeInfo, Z as CodeOptionsMeta, W as CodeOptionsMultipleThemes, V as CodeOptionsSingleTheme, X as CodeOptionsThemes, Y as CodeToHastOptionsCommon, y as CodeToHastRenderOptions, _ as CodeToHastRenderOptionsCommon, ae as DecorationItem, ad as DecorationOptions, ag as DecorationTransformType, U as DynamicImportLanguageRegistration, a1 as DynamicImportThemeRegistration, F as FontStyle, H as HighlighterCoreOptions, M as MaybeArray, aa as MaybeGetter, ab as MaybeModule, ah as Offset, ai as OffsetOrPosition, P as PlainTextLanguage, v as Position, b as RawGrammar, a as RawTheme, g as RawThemeSetting, l as RequireKeys, af as ResolvedDecorationItem, aj as ResolvedPosition, D as ShikiTransformer, a8 as ShikiTransformerContext, x as ShikiTransformerContextCommon, a7 as ShikiTransformerContextMeta, z as ShikiTransformerContextSource, ac as StringLiteralUnion, a0 as ThemeRegistration, $ as ThemeRegistrationRaw, a4 as ThemedTokenExplanation, a3 as ThemedTokenScopeExplanation, a5 as TokenBase, u as TokenStyles, t as TokenizeWithThemeOptions, a6 as TransformerOptions } from './chunk-tokens.mjs';
3
3
  export { W as WebAssemblyInstantiator } from './chunk-index.mjs';
4
4
 
5
+ interface Grammar extends IGrammar {
6
+ name: string;
7
+ }
8
+
5
9
  /**
6
10
  * Internal context of Shiki, core textmate logic
7
11
  */
@@ -21,7 +25,7 @@ interface ShikiInternal<BundledLangKeys extends string = never, BundledThemeKeys
21
25
  /**
22
26
  * Get the registered language object
23
27
  */
24
- getLanguage: (name: string | LanguageRegistration) => IGrammar;
28
+ getLanguage: (name: string | LanguageRegistration) => Grammar;
25
29
  /**
26
30
  * Set the current theme and get the resolved theme object and color map.
27
31
  * @internal
@@ -81,6 +85,11 @@ interface HighlighterGeneric<BundledLangKeys extends string, BundledThemeKeys ex
81
85
  * @returns A 2D array of tokens, first dimension is lines, second dimension is tokens in a line.
82
86
  */
83
87
  codeToTokensWithThemes: (code: string, options: CodeToTokensWithThemesOptions<ResolveBundleKey<BundledLangKeys>, ResolveBundleKey<BundledThemeKeys>>) => ThemedTokenWithVariants[][];
88
+ /**
89
+ * Get the last grammar state of a code snippet.
90
+ * You can pass the grammar state to `codeToTokens` as `grammarState` to continue tokenizing from an intermediate state.
91
+ */
92
+ getLastGrammarState: (code: string, options: CodeToTokensBaseOptions<ResolveBundleKey<BundledLangKeys>, ResolveBundleKey<BundledThemeKeys>>) => GrammarState;
84
93
  /**
85
94
  * Get internal context object
86
95
  * @internal
@@ -93,4 +102,4 @@ interface HighlighterGeneric<BundledLangKeys extends string, BundledThemeKeys ex
93
102
  */
94
103
  type HighlighterCore = HighlighterGeneric<never, never>;
95
104
 
96
- export { CodeToHastOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, IGrammar as Grammar, type HighlighterCore, type HighlighterGeneric, LanguageInput, LanguageRegistration, ResolveBundleKey, type ShikiInternal, SpecialLanguage, SpecialTheme, ThemeInput, ThemeRegistrationAny, ThemeRegistrationResolved, ThemedToken, ThemedTokenWithVariants, TokensResult };
105
+ export { CodeToHastOptions, CodeToTokensBaseOptions, CodeToTokensOptions, CodeToTokensWithThemesOptions, type Grammar, GrammarState, type HighlighterCore, type HighlighterGeneric, LanguageInput, LanguageRegistration, ResolveBundleKey, type ShikiInternal, SpecialLanguage, SpecialTheme, ThemeInput, ThemeRegistrationAny, ThemeRegistrationResolved, ThemedToken, ThemedTokenWithVariants, TokensResult };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/core",
3
3
  "type": "module",
4
- "version": "1.9.1",
4
+ "version": "1.10.1",
5
5
  "description": "Core of Shiki",
6
6
  "author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",