@shikijs/core 1.21.0 → 1.22.0
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 +4 -5
- package/dist/index.d.ts +4 -5
- package/dist/index.mjs +149 -62
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
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, 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, 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';
|
|
5
|
-
import { IGrammar } from '@shikijs/vscode-textmate';
|
|
6
5
|
export { FontStyle, EncodedTokenMetadata as StackElementMetadata } from '@shikijs/vscode-textmate';
|
|
7
6
|
export { toHtml as hastToHtml } from 'hast-util-to-html';
|
|
8
7
|
|
|
@@ -77,7 +76,7 @@ interface ShorthandsBundle<L extends string, T extends string> {
|
|
|
77
76
|
* Shorthand for `getLastGrammarState` with auto-loaded theme and language.
|
|
78
77
|
* A singleton highlighter it maintained internally.
|
|
79
78
|
*/
|
|
80
|
-
getLastGrammarState: (code: string, options: CodeToTokensBaseOptions<L, T>) => Promise<GrammarState
|
|
79
|
+
getLastGrammarState: ((element: ThemedToken[][] | Root) => GrammarState) | ((code: string, options: CodeToTokensBaseOptions<L, T>) => Promise<GrammarState>);
|
|
81
80
|
}
|
|
82
81
|
declare function makeSingletonHighlighter<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
|
|
83
82
|
declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): ShorthandsBundle<L, T>;
|
|
@@ -144,7 +143,7 @@ declare function createWasmOnigEngine(options?: LoadWasmOptions | null): Promise
|
|
|
144
143
|
declare function loadWasm(options: LoadWasmOptions): Promise<void>;
|
|
145
144
|
|
|
146
145
|
declare function codeToHast(internal: ShikiInternal, code: string, options: CodeToHastOptions, transformerContext?: ShikiTransformerContextCommon): Root;
|
|
147
|
-
declare function tokensToHast(tokens: ThemedToken[][], options: CodeToHastRenderOptions, transformerContext: ShikiTransformerContextSource): Root;
|
|
146
|
+
declare function tokensToHast(tokens: ThemedToken[][], options: CodeToHastRenderOptions, transformerContext: ShikiTransformerContextSource, grammarState?: GrammarState | undefined): Root;
|
|
148
147
|
|
|
149
148
|
/**
|
|
150
149
|
* Get highlighted code in HTML.
|
|
@@ -164,7 +163,7 @@ declare function tokenizeAnsiWithTheme(theme: ThemeRegistrationResolved, fileCon
|
|
|
164
163
|
* Code to tokens, with a simple theme.
|
|
165
164
|
*/
|
|
166
165
|
declare function codeToTokensBase(internal: ShikiInternal, code: string, options?: CodeToTokensBaseOptions): ThemedToken[][];
|
|
167
|
-
declare function tokenizeWithTheme(code: string, grammar:
|
|
166
|
+
declare function tokenizeWithTheme(code: string, grammar: Grammar, theme: ThemeRegistrationResolved, colorMap: string[], options: TokenizeWithThemeOptions): ThemedToken[][];
|
|
168
167
|
|
|
169
168
|
/**
|
|
170
169
|
* Get tokens with multiple themes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
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, 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, 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';
|
|
5
|
-
import { IGrammar } from '@shikijs/vscode-textmate';
|
|
6
5
|
export { FontStyle, EncodedTokenMetadata as StackElementMetadata } from '@shikijs/vscode-textmate';
|
|
7
6
|
export { toHtml as hastToHtml } from 'hast-util-to-html';
|
|
8
7
|
|
|
@@ -77,7 +76,7 @@ interface ShorthandsBundle<L extends string, T extends string> {
|
|
|
77
76
|
* Shorthand for `getLastGrammarState` with auto-loaded theme and language.
|
|
78
77
|
* A singleton highlighter it maintained internally.
|
|
79
78
|
*/
|
|
80
|
-
getLastGrammarState: (code: string, options: CodeToTokensBaseOptions<L, T>) => Promise<GrammarState
|
|
79
|
+
getLastGrammarState: ((element: ThemedToken[][] | Root) => GrammarState) | ((code: string, options: CodeToTokensBaseOptions<L, T>) => Promise<GrammarState>);
|
|
81
80
|
}
|
|
82
81
|
declare function makeSingletonHighlighter<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): (options?: Partial<BundledHighlighterOptions<L, T>>) => Promise<HighlighterGeneric<L, T>>;
|
|
83
82
|
declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>): ShorthandsBundle<L, T>;
|
|
@@ -144,7 +143,7 @@ declare function createWasmOnigEngine(options?: LoadWasmOptions | null): Promise
|
|
|
144
143
|
declare function loadWasm(options: LoadWasmOptions): Promise<void>;
|
|
145
144
|
|
|
146
145
|
declare function codeToHast(internal: ShikiInternal, code: string, options: CodeToHastOptions, transformerContext?: ShikiTransformerContextCommon): Root;
|
|
147
|
-
declare function tokensToHast(tokens: ThemedToken[][], options: CodeToHastRenderOptions, transformerContext: ShikiTransformerContextSource): Root;
|
|
146
|
+
declare function tokensToHast(tokens: ThemedToken[][], options: CodeToHastRenderOptions, transformerContext: ShikiTransformerContextSource, grammarState?: GrammarState | undefined): Root;
|
|
148
147
|
|
|
149
148
|
/**
|
|
150
149
|
* Get highlighted code in HTML.
|
|
@@ -164,7 +163,7 @@ declare function tokenizeAnsiWithTheme(theme: ThemeRegistrationResolved, fileCon
|
|
|
164
163
|
* Code to tokens, with a simple theme.
|
|
165
164
|
*/
|
|
166
165
|
declare function codeToTokensBase(internal: ShikiInternal, code: string, options?: CodeToTokensBaseOptions): ThemedToken[][];
|
|
167
|
-
declare function tokenizeWithTheme(code: string, grammar:
|
|
166
|
+
declare function tokenizeWithTheme(code: string, grammar: Grammar, theme: ThemeRegistrationResolved, colorMap: string[], options: TokenizeWithThemeOptions): ThemedToken[][];
|
|
168
167
|
|
|
169
168
|
/**
|
|
170
169
|
* Get tokens with multiple themes
|
package/dist/index.mjs
CHANGED
|
@@ -181,6 +181,101 @@ class ShikiError extends Error {
|
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
+
var __defProp$2 = Object.defineProperty;
|
|
185
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
186
|
+
var __publicField$2 = (obj, key, value) => {
|
|
187
|
+
__defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
188
|
+
return value;
|
|
189
|
+
};
|
|
190
|
+
const _grammarStateMap = /* @__PURE__ */ new WeakMap();
|
|
191
|
+
function setLastGrammarStateToMap(keys, state) {
|
|
192
|
+
_grammarStateMap.set(keys, state);
|
|
193
|
+
}
|
|
194
|
+
function getLastGrammarStateFromMap(keys) {
|
|
195
|
+
return _grammarStateMap.get(keys);
|
|
196
|
+
}
|
|
197
|
+
class GrammarState {
|
|
198
|
+
constructor(...args) {
|
|
199
|
+
/**
|
|
200
|
+
* Theme to Stack mapping
|
|
201
|
+
*/
|
|
202
|
+
__publicField$2(this, "_stacks", {});
|
|
203
|
+
__publicField$2(this, "lang");
|
|
204
|
+
if (args.length === 2) {
|
|
205
|
+
const [stacksMap, lang] = args;
|
|
206
|
+
this.lang = lang;
|
|
207
|
+
this._stacks = stacksMap;
|
|
208
|
+
} else {
|
|
209
|
+
const [stack, lang, theme] = args;
|
|
210
|
+
this.lang = lang;
|
|
211
|
+
this._stacks = { [theme]: stack };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
get themes() {
|
|
215
|
+
return Object.keys(this._stacks);
|
|
216
|
+
}
|
|
217
|
+
get theme() {
|
|
218
|
+
return this.themes[0];
|
|
219
|
+
}
|
|
220
|
+
get _stack() {
|
|
221
|
+
return this._stacks[this.theme];
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Static method to create a initial grammar state.
|
|
225
|
+
*/
|
|
226
|
+
static initial(lang, themes) {
|
|
227
|
+
return new GrammarState(
|
|
228
|
+
Object.fromEntries(toArray(themes).map((theme) => [theme, INITIAL])),
|
|
229
|
+
lang
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Get the internal stack object.
|
|
234
|
+
* @internal
|
|
235
|
+
*/
|
|
236
|
+
getInternalStack(theme = this.theme) {
|
|
237
|
+
return this._stacks[theme];
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* @deprecated use `getScopes` instead
|
|
241
|
+
*/
|
|
242
|
+
get scopes() {
|
|
243
|
+
return getScopes(this._stacks[this.theme]);
|
|
244
|
+
}
|
|
245
|
+
getScopes(theme = this.theme) {
|
|
246
|
+
return getScopes(this._stacks[theme]);
|
|
247
|
+
}
|
|
248
|
+
toJSON() {
|
|
249
|
+
return {
|
|
250
|
+
lang: this.lang,
|
|
251
|
+
theme: this.theme,
|
|
252
|
+
themes: this.themes,
|
|
253
|
+
scopes: this.scopes
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function getScopes(stack) {
|
|
258
|
+
const scopes = [];
|
|
259
|
+
const visited = /* @__PURE__ */ new Set();
|
|
260
|
+
function pushScope(stack2) {
|
|
261
|
+
if (visited.has(stack2))
|
|
262
|
+
return;
|
|
263
|
+
visited.add(stack2);
|
|
264
|
+
const name = stack2?.nameScopesList?.scopeName;
|
|
265
|
+
if (name)
|
|
266
|
+
scopes.push(name);
|
|
267
|
+
if (stack2.parent)
|
|
268
|
+
pushScope(stack2.parent);
|
|
269
|
+
}
|
|
270
|
+
pushScope(stack);
|
|
271
|
+
return scopes;
|
|
272
|
+
}
|
|
273
|
+
function getGrammarStack(state, theme) {
|
|
274
|
+
if (!(state instanceof GrammarState))
|
|
275
|
+
throw new ShikiError("Invalid grammar state");
|
|
276
|
+
return state.getInternalStack(theme);
|
|
277
|
+
}
|
|
278
|
+
|
|
184
279
|
function transformerDecorations() {
|
|
185
280
|
const map = /* @__PURE__ */ new WeakMap();
|
|
186
281
|
function getContext(shiki) {
|
|
@@ -350,51 +445,6 @@ function getTransformers(options) {
|
|
|
350
445
|
];
|
|
351
446
|
}
|
|
352
447
|
|
|
353
|
-
class GrammarState {
|
|
354
|
-
constructor(_stack, lang, theme) {
|
|
355
|
-
this._stack = _stack;
|
|
356
|
-
this.lang = lang;
|
|
357
|
-
this.theme = theme;
|
|
358
|
-
}
|
|
359
|
-
/**
|
|
360
|
-
* Static method to create a initial grammar state.
|
|
361
|
-
*/
|
|
362
|
-
static initial(lang, theme) {
|
|
363
|
-
return new GrammarState(INITIAL, lang, theme);
|
|
364
|
-
}
|
|
365
|
-
get scopes() {
|
|
366
|
-
return getScopes(this._stack);
|
|
367
|
-
}
|
|
368
|
-
toJSON() {
|
|
369
|
-
return {
|
|
370
|
-
lang: this.lang,
|
|
371
|
-
theme: this.theme,
|
|
372
|
-
scopes: this.scopes
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
function getScopes(stack) {
|
|
377
|
-
const scopes = [];
|
|
378
|
-
const visited = /* @__PURE__ */ new Set();
|
|
379
|
-
function pushScope(stack2) {
|
|
380
|
-
if (visited.has(stack2))
|
|
381
|
-
return;
|
|
382
|
-
visited.add(stack2);
|
|
383
|
-
const name = stack2?.nameScopesList?.scopeName;
|
|
384
|
-
if (name)
|
|
385
|
-
scopes.push(name);
|
|
386
|
-
if (stack2.parent)
|
|
387
|
-
pushScope(stack2.parent);
|
|
388
|
-
}
|
|
389
|
-
pushScope(stack);
|
|
390
|
-
return scopes;
|
|
391
|
-
}
|
|
392
|
-
function getGrammarStack(state) {
|
|
393
|
-
if (!(state instanceof GrammarState))
|
|
394
|
-
throw new ShikiError("Invalid grammar state");
|
|
395
|
-
return state._stack;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
448
|
// src/colors.ts
|
|
399
449
|
var namedColors = [
|
|
400
450
|
"black",
|
|
@@ -735,13 +785,17 @@ function codeToTokensBase(internal, code, options = {}) {
|
|
|
735
785
|
if (options.grammarState.lang !== _grammar.name) {
|
|
736
786
|
throw new ShikiError$1(`Grammar state language "${options.grammarState.lang}" does not match highlight language "${_grammar.name}"`);
|
|
737
787
|
}
|
|
738
|
-
if (options.grammarState.theme
|
|
739
|
-
throw new ShikiError$1(`Grammar state
|
|
788
|
+
if (!options.grammarState.themes.includes(theme.name)) {
|
|
789
|
+
throw new ShikiError$1(`Grammar state themes "${options.grammarState.themes}" do not contain highlight theme "${theme.name}"`);
|
|
740
790
|
}
|
|
741
791
|
}
|
|
742
792
|
return tokenizeWithTheme(code, _grammar, theme, colorMap, options);
|
|
743
793
|
}
|
|
744
|
-
function getLastGrammarState(
|
|
794
|
+
function getLastGrammarState(...args) {
|
|
795
|
+
if (args.length === 2) {
|
|
796
|
+
return getLastGrammarStateFromMap(args[1]);
|
|
797
|
+
}
|
|
798
|
+
const [internal, code, options = {}] = args;
|
|
745
799
|
const {
|
|
746
800
|
lang = "text",
|
|
747
801
|
theme: themeName = internal.getLoadedThemes()[0]
|
|
@@ -759,7 +813,14 @@ function getLastGrammarState(internal, code, options = {}) {
|
|
|
759
813
|
);
|
|
760
814
|
}
|
|
761
815
|
function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
|
|
762
|
-
|
|
816
|
+
const result = _tokenizeWithTheme(code, grammar, theme, colorMap, options);
|
|
817
|
+
const grammarState = new GrammarState(
|
|
818
|
+
_tokenizeWithTheme(code, grammar, theme, colorMap, options).stateStack,
|
|
819
|
+
grammar.name,
|
|
820
|
+
theme.name
|
|
821
|
+
);
|
|
822
|
+
setLastGrammarStateToMap(result.tokens, grammarState);
|
|
823
|
+
return result.tokens;
|
|
763
824
|
}
|
|
764
825
|
function _tokenizeWithTheme(code, grammar, theme, colorMap, options) {
|
|
765
826
|
const colorReplacements = resolveColorReplacements(theme, options);
|
|
@@ -768,7 +829,7 @@ function _tokenizeWithTheme(code, grammar, theme, colorMap, options) {
|
|
|
768
829
|
tokenizeTimeLimit = 500
|
|
769
830
|
} = options;
|
|
770
831
|
const lines = splitLines(code);
|
|
771
|
-
let stateStack = options.grammarState ? getGrammarStack(options.grammarState) : options.grammarContextCode != null ? _tokenizeWithTheme(
|
|
832
|
+
let stateStack = options.grammarState ? getGrammarStack(options.grammarState, theme.name) ?? INITIAL : options.grammarContextCode != null ? _tokenizeWithTheme(
|
|
772
833
|
options.grammarContextCode,
|
|
773
834
|
grammar,
|
|
774
835
|
theme,
|
|
@@ -924,11 +985,21 @@ function explainThemeScope(themeSettingsSelectors, scope, parentScopes) {
|
|
|
924
985
|
|
|
925
986
|
function codeToTokensWithThemes(internal, code, options) {
|
|
926
987
|
const themes = Object.entries(options.themes).filter((i) => i[1]).map((i) => ({ color: i[0], theme: i[1] }));
|
|
927
|
-
const
|
|
928
|
-
|
|
988
|
+
const themedTokens = themes.map((t) => {
|
|
989
|
+
const tokens2 = codeToTokensBase(internal, code, {
|
|
929
990
|
...options,
|
|
930
991
|
theme: t.theme
|
|
931
|
-
})
|
|
992
|
+
});
|
|
993
|
+
const state = getLastGrammarStateFromMap(tokens2);
|
|
994
|
+
const theme = typeof t.theme === "string" ? t.theme : t.theme.name;
|
|
995
|
+
return {
|
|
996
|
+
tokens: tokens2,
|
|
997
|
+
state,
|
|
998
|
+
theme
|
|
999
|
+
};
|
|
1000
|
+
});
|
|
1001
|
+
const tokens = syncThemesTokenization(
|
|
1002
|
+
...themedTokens.map((i) => i.tokens)
|
|
932
1003
|
);
|
|
933
1004
|
const mergedTokens = tokens[0].map(
|
|
934
1005
|
(line, lineIdx) => line.map((_token, tokenIdx) => {
|
|
@@ -952,6 +1023,12 @@ function codeToTokensWithThemes(internal, code, options) {
|
|
|
952
1023
|
return mergedToken;
|
|
953
1024
|
})
|
|
954
1025
|
);
|
|
1026
|
+
const mergedGrammarState = themedTokens[0].state ? new GrammarState(
|
|
1027
|
+
Object.fromEntries(themedTokens.map((s) => [s.theme, s.state?.getInternalStack(s.theme)])),
|
|
1028
|
+
themedTokens[0].state.lang
|
|
1029
|
+
) : void 0;
|
|
1030
|
+
if (mergedGrammarState)
|
|
1031
|
+
setLastGrammarStateToMap(mergedTokens, mergedGrammarState);
|
|
955
1032
|
return mergedTokens;
|
|
956
1033
|
}
|
|
957
1034
|
function syncThemesTokenization(...themes) {
|
|
@@ -994,6 +1071,7 @@ function codeToTokens(internal, code, options) {
|
|
|
994
1071
|
let tokens;
|
|
995
1072
|
let themeName;
|
|
996
1073
|
let rootStyle;
|
|
1074
|
+
let grammarState;
|
|
997
1075
|
if ("themes" in options) {
|
|
998
1076
|
const {
|
|
999
1077
|
defaultColor = "light",
|
|
@@ -1001,17 +1079,20 @@ function codeToTokens(internal, code, options) {
|
|
|
1001
1079
|
} = options;
|
|
1002
1080
|
const themes = Object.entries(options.themes).filter((i) => i[1]).map((i) => ({ color: i[0], theme: i[1] })).sort((a, b) => a.color === defaultColor ? -1 : b.color === defaultColor ? 1 : 0);
|
|
1003
1081
|
if (themes.length === 0)
|
|
1004
|
-
throw new ShikiError("`themes` option must not be empty");
|
|
1082
|
+
throw new ShikiError$1("`themes` option must not be empty");
|
|
1005
1083
|
const themeTokens = codeToTokensWithThemes(
|
|
1006
1084
|
internal,
|
|
1007
1085
|
code,
|
|
1008
1086
|
options
|
|
1009
1087
|
);
|
|
1088
|
+
grammarState = getLastGrammarStateFromMap(themeTokens);
|
|
1010
1089
|
if (defaultColor && !themes.find((t) => t.color === defaultColor))
|
|
1011
|
-
throw new ShikiError(`\`themes\` option must contain the defaultColor key \`${defaultColor}\``);
|
|
1090
|
+
throw new ShikiError$1(`\`themes\` option must contain the defaultColor key \`${defaultColor}\``);
|
|
1012
1091
|
const themeRegs = themes.map((t) => internal.getTheme(t.theme));
|
|
1013
1092
|
const themesOrder = themes.map((t) => t.color);
|
|
1014
1093
|
tokens = themeTokens.map((line) => line.map((token) => mergeToken(token, themesOrder, cssVariablePrefix, defaultColor)));
|
|
1094
|
+
if (grammarState)
|
|
1095
|
+
setLastGrammarStateToMap(tokens, grammarState);
|
|
1015
1096
|
const themeColorReplacements = themes.map((t) => resolveColorReplacements(t.theme, options));
|
|
1016
1097
|
fg = themes.map((t, idx) => (idx === 0 && defaultColor ? "" : `${cssVariablePrefix + t.color}:`) + (applyColorReplacements(themeRegs[idx].fg, themeColorReplacements[idx]) || "inherit")).join(";");
|
|
1017
1098
|
bg = themes.map((t, idx) => (idx === 0 && defaultColor ? "" : `${cssVariablePrefix + t.color}-bg:`) + (applyColorReplacements(themeRegs[idx].bg, themeColorReplacements[idx]) || "inherit")).join(";");
|
|
@@ -1028,15 +1109,17 @@ function codeToTokens(internal, code, options) {
|
|
|
1028
1109
|
bg = applyColorReplacements(_theme.bg, colorReplacements);
|
|
1029
1110
|
fg = applyColorReplacements(_theme.fg, colorReplacements);
|
|
1030
1111
|
themeName = _theme.name;
|
|
1112
|
+
grammarState = getLastGrammarStateFromMap(tokens);
|
|
1031
1113
|
} else {
|
|
1032
|
-
throw new ShikiError("Invalid options, either `theme` or `themes` must be provided");
|
|
1114
|
+
throw new ShikiError$1("Invalid options, either `theme` or `themes` must be provided");
|
|
1033
1115
|
}
|
|
1034
1116
|
return {
|
|
1035
1117
|
tokens,
|
|
1036
1118
|
fg,
|
|
1037
1119
|
bg,
|
|
1038
1120
|
themeName,
|
|
1039
|
-
rootStyle
|
|
1121
|
+
rootStyle,
|
|
1122
|
+
grammarState
|
|
1040
1123
|
};
|
|
1041
1124
|
}
|
|
1042
1125
|
function mergeToken(merged, variantsOrder, cssVariablePrefix, defaultColor) {
|
|
@@ -1078,7 +1161,8 @@ function codeToHast(internal, code, options, transformerContext = {
|
|
|
1078
1161
|
fg,
|
|
1079
1162
|
bg,
|
|
1080
1163
|
themeName,
|
|
1081
|
-
rootStyle
|
|
1164
|
+
rootStyle,
|
|
1165
|
+
grammarState
|
|
1082
1166
|
} = codeToTokens(internal, input, options);
|
|
1083
1167
|
const {
|
|
1084
1168
|
mergeWhitespaces = true
|
|
@@ -1104,10 +1188,11 @@ function codeToHast(internal, code, options, transformerContext = {
|
|
|
1104
1188
|
themeName,
|
|
1105
1189
|
rootStyle
|
|
1106
1190
|
},
|
|
1107
|
-
contextSource
|
|
1191
|
+
contextSource,
|
|
1192
|
+
grammarState
|
|
1108
1193
|
);
|
|
1109
1194
|
}
|
|
1110
|
-
function tokensToHast(tokens, options, transformerContext) {
|
|
1195
|
+
function tokensToHast(tokens, options, transformerContext, grammarState = getLastGrammarStateFromMap(tokens)) {
|
|
1111
1196
|
const transformers = getTransformers(options);
|
|
1112
1197
|
const lines = [];
|
|
1113
1198
|
const root = {
|
|
@@ -1222,6 +1307,8 @@ function tokensToHast(tokens, options, transformerContext) {
|
|
|
1222
1307
|
let result = root;
|
|
1223
1308
|
for (const transformer of transformers)
|
|
1224
1309
|
result = transformer?.root?.call(context, result) || result;
|
|
1310
|
+
if (grammarState)
|
|
1311
|
+
setLastGrammarStateToMap(result, grammarState);
|
|
1225
1312
|
return result;
|
|
1226
1313
|
}
|
|
1227
1314
|
function mergeWhitespaceTokens(tokens) {
|
|
@@ -1731,7 +1818,7 @@ function getShikiInternal(options = {}) {
|
|
|
1731
1818
|
async function createHighlighterCore(options = {}) {
|
|
1732
1819
|
const internal = await createShikiInternal(options);
|
|
1733
1820
|
return {
|
|
1734
|
-
getLastGrammarState: (
|
|
1821
|
+
getLastGrammarState: (...args) => getLastGrammarState(internal, ...args),
|
|
1735
1822
|
codeToTokensBase: (code, options2) => codeToTokensBase(internal, code, options2),
|
|
1736
1823
|
codeToTokensWithThemes: (code, options2) => codeToTokensWithThemes(internal, code, options2),
|
|
1737
1824
|
codeToTokens: (code, options2) => codeToTokens(internal, code, options2),
|
|
@@ -1744,7 +1831,7 @@ async function createHighlighterCore(options = {}) {
|
|
|
1744
1831
|
function createHighlighterCoreSync(options = {}) {
|
|
1745
1832
|
const internal = createShikiInternalSync(options);
|
|
1746
1833
|
return {
|
|
1747
|
-
getLastGrammarState: (
|
|
1834
|
+
getLastGrammarState: (...args) => getLastGrammarState(internal, ...args),
|
|
1748
1835
|
codeToTokensBase: (code, options2) => codeToTokensBase(internal, code, options2),
|
|
1749
1836
|
codeToTokensWithThemes: (code, options2) => codeToTokensWithThemes(internal, code, options2),
|
|
1750
1837
|
codeToTokens: (code, options2) => codeToTokens(internal, code, options2),
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shikijs/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.22.0",
|
|
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": "^9.
|
|
63
|
+
"@shikijs/vscode-textmate": "^9.3.0",
|
|
64
64
|
"@types/hast": "^3.0.4",
|
|
65
65
|
"hast-util-to-html": "^9.0.3",
|
|
66
|
-
"@shikijs/engine-
|
|
67
|
-
"@shikijs/
|
|
68
|
-
"@shikijs/
|
|
66
|
+
"@shikijs/engine-oniguruma": "1.22.0",
|
|
67
|
+
"@shikijs/types": "1.22.0",
|
|
68
|
+
"@shikijs/engine-javascript": "1.22.0"
|
|
69
69
|
},
|
|
70
70
|
"scripts": {
|
|
71
71
|
"build": "unbuild",
|