@shikijs/core 1.5.1 → 1.6.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/chunk-tokens.d.mts +2 -2
- package/dist/index.d.mts +5 -2
- package/dist/index.mjs +20 -13
- package/dist/types.d.mts +2 -2
- package/package.json +1 -1
package/dist/chunk-tokens.d.mts
CHANGED
|
@@ -1272,7 +1272,7 @@ interface TokenizeWithThemeOptions {
|
|
|
1272
1272
|
*
|
|
1273
1273
|
* This will be merged with theme's `colorReplacements` if any.
|
|
1274
1274
|
*/
|
|
1275
|
-
colorReplacements?: Record<string, string
|
|
1275
|
+
colorReplacements?: Record<string, string | Record<string, string>>;
|
|
1276
1276
|
/**
|
|
1277
1277
|
* Lines above this length will not be tokenized for performance reasons.
|
|
1278
1278
|
*
|
|
@@ -1320,4 +1320,4 @@ declare enum FontStyle {
|
|
|
1320
1320
|
Underline = 4
|
|
1321
1321
|
}
|
|
1322
1322
|
|
|
1323
|
-
export { type ThemeRegistration as $, type
|
|
1323
|
+
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 };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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
|
|
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
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';
|
|
4
4
|
import { L as LoadWasmOptions } from './chunk-index.mjs';
|
|
5
5
|
export { W as WebAssemblyInstantiator, l as loadWasm } from './chunk-index.mjs';
|
|
@@ -111,6 +111,9 @@ declare function splitToken<T extends Pick<ThemedToken, 'content' | 'offset'>>(t
|
|
|
111
111
|
* Split 2D tokens array by given breakpoints.
|
|
112
112
|
*/
|
|
113
113
|
declare function splitTokens<T extends Pick<ThemedToken, 'content' | 'offset'>>(tokens: T[][], breakpoints: number[] | Set<number>): T[][];
|
|
114
|
+
declare function resolveColorReplacements(theme: ThemeRegistrationAny | string, options?: TokenizeWithThemeOptions): {
|
|
115
|
+
[x: string]: string;
|
|
116
|
+
};
|
|
114
117
|
declare function applyColorReplacements(color: string, replacements?: Record<string, string>): string;
|
|
115
118
|
declare function applyColorReplacements(color?: string | undefined, replacements?: Record<string, string>): string | undefined;
|
|
116
119
|
declare function getTokenStyleObject(token: TokenStyles): Record<string, string>;
|
|
@@ -398,4 +401,4 @@ declare class ShikiError extends Error {
|
|
|
398
401
|
constructor(message: string);
|
|
399
402
|
}
|
|
400
403
|
|
|
401
|
-
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, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
|
|
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 };
|
package/dist/index.mjs
CHANGED
|
@@ -121,6 +121,17 @@ function splitTokens(tokens, breakpoints) {
|
|
|
121
121
|
});
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
|
+
function resolveColorReplacements(theme, options) {
|
|
125
|
+
const replacements = typeof theme === 'string' ? {} : { ...theme.colorReplacements };
|
|
126
|
+
const themeName = typeof theme === 'string' ? theme : theme.name;
|
|
127
|
+
for (const [key, value] of Object.entries(options?.colorReplacements || {})) {
|
|
128
|
+
if (typeof value === 'string')
|
|
129
|
+
replacements[key] = value;
|
|
130
|
+
else if (key === themeName)
|
|
131
|
+
Object.assign(replacements, value);
|
|
132
|
+
}
|
|
133
|
+
return replacements;
|
|
134
|
+
}
|
|
124
135
|
function applyColorReplacements(color, replacements) {
|
|
125
136
|
if (!color)
|
|
126
137
|
return color;
|
|
@@ -438,10 +449,7 @@ function createColorPalette(namedColorsMap = defaultNamedColorsMap) {
|
|
|
438
449
|
}
|
|
439
450
|
|
|
440
451
|
function tokenizeAnsiWithTheme(theme, fileContents, options) {
|
|
441
|
-
const colorReplacements =
|
|
442
|
-
...theme.colorReplacements,
|
|
443
|
-
...options?.colorReplacements,
|
|
444
|
-
};
|
|
452
|
+
const colorReplacements = resolveColorReplacements(theme, options);
|
|
445
453
|
const lines = splitLines(fileContents);
|
|
446
454
|
const colorPalette = createColorPalette(Object.fromEntries(namedColors.map(name => [
|
|
447
455
|
name,
|
|
@@ -523,10 +531,7 @@ function codeToTokensBase(internal, code, options = {}) {
|
|
|
523
531
|
return tokenizeWithTheme(code, _grammar, theme, colorMap, options);
|
|
524
532
|
}
|
|
525
533
|
function tokenizeWithTheme(code, grammar, theme, colorMap, options) {
|
|
526
|
-
const colorReplacements =
|
|
527
|
-
...theme.colorReplacements,
|
|
528
|
-
...options?.colorReplacements,
|
|
529
|
-
};
|
|
534
|
+
const colorReplacements = resolveColorReplacements(theme, options);
|
|
530
535
|
const { tokenizeMaxLineLength = 0, tokenizeTimeLimit = 500, } = options;
|
|
531
536
|
const lines = splitLines(code);
|
|
532
537
|
let ruleStack = INITIAL;
|
|
@@ -750,7 +755,7 @@ function codeToTokens(internal, code, options) {
|
|
|
750
755
|
let themeName;
|
|
751
756
|
let rootStyle;
|
|
752
757
|
if ('themes' in options) {
|
|
753
|
-
const { defaultColor = 'light', cssVariablePrefix = '--shiki-',
|
|
758
|
+
const { defaultColor = 'light', cssVariablePrefix = '--shiki-', } = options;
|
|
754
759
|
const themes = Object.entries(options.themes)
|
|
755
760
|
.filter(i => i[1])
|
|
756
761
|
.map(i => ({ color: i[0], theme: i[1] }))
|
|
@@ -764,17 +769,18 @@ function codeToTokens(internal, code, options) {
|
|
|
764
769
|
const themesOrder = themes.map(t => t.color);
|
|
765
770
|
tokens = themeTokens
|
|
766
771
|
.map(line => line.map(token => mergeToken(token, themesOrder, cssVariablePrefix, defaultColor)));
|
|
772
|
+
const themeColorReplacements = themes.map(t => resolveColorReplacements(t.theme, options));
|
|
767
773
|
fg = themes.map((t, idx) => (idx === 0 && defaultColor
|
|
768
774
|
? ''
|
|
769
|
-
: `${cssVariablePrefix + t.color}:`) + (applyColorReplacements(themeRegs[idx].fg,
|
|
775
|
+
: `${cssVariablePrefix + t.color}:`) + (applyColorReplacements(themeRegs[idx].fg, themeColorReplacements[idx]) || 'inherit')).join(';');
|
|
770
776
|
bg = themes.map((t, idx) => (idx === 0 && defaultColor
|
|
771
777
|
? ''
|
|
772
|
-
: `${cssVariablePrefix + t.color}-bg:`) + (applyColorReplacements(themeRegs[idx].bg,
|
|
778
|
+
: `${cssVariablePrefix + t.color}-bg:`) + (applyColorReplacements(themeRegs[idx].bg, themeColorReplacements[idx]) || 'inherit')).join(';');
|
|
773
779
|
themeName = `shiki-themes ${themeRegs.map(t => t.name).join(' ')}`;
|
|
774
780
|
rootStyle = defaultColor ? undefined : [fg, bg].join(';');
|
|
775
781
|
}
|
|
776
782
|
else if ('theme' in options) {
|
|
777
|
-
const
|
|
783
|
+
const colorReplacements = resolveColorReplacements(options.theme, options.colorReplacements);
|
|
778
784
|
tokens = codeToTokensBase(internal, code, options);
|
|
779
785
|
const _theme = internal.getTheme(options.theme);
|
|
780
786
|
bg = applyColorReplacements(_theme.bg, colorReplacements);
|
|
@@ -1178,6 +1184,7 @@ function splitWhitespaceTokens(tokens) {
|
|
|
1178
1184
|
return line.flatMap((token) => {
|
|
1179
1185
|
if (token.content.match(/^\s+$/))
|
|
1180
1186
|
return token;
|
|
1187
|
+
// eslint-disable-next-line regexp/no-super-linear-backtracking
|
|
1181
1188
|
const match = token.content.match(/^(\s*)(.*?)(\s*)$/);
|
|
1182
1189
|
if (!match)
|
|
1183
1190
|
return token;
|
|
@@ -5535,4 +5542,4 @@ function createSingletonShorthands(getHighlighter) {
|
|
|
5535
5542
|
};
|
|
5536
5543
|
}
|
|
5537
5544
|
|
|
5538
|
-
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, setDefaultWasmLoader, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations };
|
|
5545
|
+
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 };
|
package/dist/types.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { h as ThemeInput, r as SpecialTheme, L as LanguageInput, q as SpecialLanguage,
|
|
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,
|
|
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';
|
|
3
3
|
export { W as WebAssemblyInstantiator } from './chunk-index.mjs';
|
|
4
4
|
|
|
5
5
|
/**
|