@shikijs/core 3.5.0 → 3.7.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 +2 -2
- package/dist/index.mjs +23 -15
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CreatedBundledHighlighterOptions, CreateHighlighterFactory, CodeToHastOptions, CodeToTokensOptions, TokensResult, RequireKeys, CodeToTokensBaseOptions, ThemedToken, CodeToTokensWithThemesOptions, ThemedTokenWithVariants, BundledHighlighterOptions, HighlighterGeneric, GrammarState, Awaitable, HighlighterCoreOptions, HighlighterCore, ShikiInternal, ShikiTransformerContextCommon, CodeToHastRenderOptions, ShikiTransformerContextSource, ThemeRegistrationResolved, TokenizeWithThemeOptions, Grammar, ThemeRegistrationAny, ThemeRegistration, ShikiTransformer, MaybeArray, MaybeGetter, PlainTextLanguage, SpecialLanguage, ThemeInput, SpecialTheme, Position, TokenStyles } from '@shikijs/types';
|
|
1
|
+
import { CreatedBundledHighlighterOptions, CreateHighlighterFactory, CodeToHastOptions, CodeToTokensOptions, TokensResult, RequireKeys, CodeToTokensBaseOptions, ThemedToken, CodeToTokensWithThemesOptions, ThemedTokenWithVariants, BundledHighlighterOptions, HighlighterGeneric, GrammarState, Awaitable, HighlighterCoreOptions, HighlighterCore, ShikiInternal, ShikiTransformerContextCommon, CodeToHastRenderOptions, ShikiTransformerContextSource, ThemeRegistrationResolved, TokenizeWithThemeOptions, Grammar, ThemeRegistrationAny, ThemeRegistration, ShikiTransformer, MaybeArray, MaybeGetter, PlainTextLanguage, SpecialLanguage, ThemeInput, SpecialTheme, Position, CodeOptionsMultipleThemes, TokenStyles } from '@shikijs/types';
|
|
2
2
|
export * from '@shikijs/types';
|
|
3
3
|
import { Root, Element } from 'hast';
|
|
4
4
|
import { toHtml } from 'hast-util-to-html';
|
|
@@ -254,7 +254,7 @@ declare function splitToken<T extends Pick<ThemedToken, 'content' | 'offset'>>(t
|
|
|
254
254
|
* Split 2D tokens array by given breakpoints.
|
|
255
255
|
*/
|
|
256
256
|
declare function splitTokens<T extends Pick<ThemedToken, 'content' | 'offset'>>(tokens: T[][], breakpoints: number[] | Set<number>): T[][];
|
|
257
|
-
declare function flatTokenVariants(merged: ThemedTokenWithVariants, variantsOrder: string[], cssVariablePrefix: string, defaultColor:
|
|
257
|
+
declare function flatTokenVariants(merged: ThemedTokenWithVariants, variantsOrder: string[], cssVariablePrefix: string, defaultColor: CodeOptionsMultipleThemes['defaultColor'], colorsRendering?: CodeOptionsMultipleThemes['colorsRendering']): ThemedToken;
|
|
258
258
|
declare function getTokenStyleObject(token: TokenStyles): Record<string, string>;
|
|
259
259
|
declare function stringifyTokenStyle(token: string | Record<string, string>): string;
|
|
260
260
|
|
package/dist/index.mjs
CHANGED
|
@@ -118,6 +118,7 @@ function guessEmbeddedLanguages(code, _lang, highlighter) {
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
const DEFAULT_COLOR_LIGHT_DARK = "light-dark()";
|
|
121
|
+
const COLOR_KEYS = ["color", "background-color"];
|
|
121
122
|
|
|
122
123
|
function splitToken(token, offsets) {
|
|
123
124
|
let lastOffset = 0;
|
|
@@ -154,7 +155,7 @@ function splitTokens(tokens, breakpoints) {
|
|
|
154
155
|
});
|
|
155
156
|
});
|
|
156
157
|
}
|
|
157
|
-
function flatTokenVariants(merged, variantsOrder, cssVariablePrefix, defaultColor) {
|
|
158
|
+
function flatTokenVariants(merged, variantsOrder, cssVariablePrefix, defaultColor, colorsRendering = "css-vars") {
|
|
158
159
|
const token = {
|
|
159
160
|
content: merged.content,
|
|
160
161
|
explanation: merged.explanation,
|
|
@@ -163,10 +164,14 @@ function flatTokenVariants(merged, variantsOrder, cssVariablePrefix, defaultColo
|
|
|
163
164
|
const styles = variantsOrder.map((t) => getTokenStyleObject(merged.variants[t]));
|
|
164
165
|
const styleKeys = new Set(styles.flatMap((t) => Object.keys(t)));
|
|
165
166
|
const mergedStyles = {};
|
|
167
|
+
const varKey = (idx, key) => {
|
|
168
|
+
const keyName = key === "color" ? "" : key === "background-color" ? "-bg" : `-${key}`;
|
|
169
|
+
return cssVariablePrefix + variantsOrder[idx] + (key === "color" ? "" : keyName);
|
|
170
|
+
};
|
|
166
171
|
styles.forEach((cur, idx) => {
|
|
167
172
|
for (const key of styleKeys) {
|
|
168
173
|
const value = cur[key] || "inherit";
|
|
169
|
-
if (idx === 0 && defaultColor) {
|
|
174
|
+
if (idx === 0 && defaultColor && COLOR_KEYS.includes(key)) {
|
|
170
175
|
if (defaultColor === DEFAULT_COLOR_LIGHT_DARK && styles.length > 1) {
|
|
171
176
|
const lightIndex = variantsOrder.findIndex((t) => t === "light");
|
|
172
177
|
const darkIndex = variantsOrder.findIndex((t) => t === "dark");
|
|
@@ -175,16 +180,14 @@ function flatTokenVariants(merged, variantsOrder, cssVariablePrefix, defaultColo
|
|
|
175
180
|
const lightValue = styles[lightIndex][key] || "inherit";
|
|
176
181
|
const darkValue = styles[darkIndex][key] || "inherit";
|
|
177
182
|
mergedStyles[key] = `light-dark(${lightValue}, ${darkValue})`;
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
mergedStyles[varKey] = value;
|
|
183
|
+
if (colorsRendering === "css-vars")
|
|
184
|
+
mergedStyles[varKey(idx, key)] = value;
|
|
181
185
|
} else {
|
|
182
186
|
mergedStyles[key] = value;
|
|
183
187
|
}
|
|
184
188
|
} else {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
mergedStyles[varKey] = value;
|
|
189
|
+
if (colorsRendering === "css-vars")
|
|
190
|
+
mergedStyles[varKey(idx, key)] = value;
|
|
188
191
|
}
|
|
189
192
|
}
|
|
190
193
|
});
|
|
@@ -1116,7 +1119,8 @@ function codeToTokens(internal, code, options) {
|
|
|
1116
1119
|
if ("themes" in options) {
|
|
1117
1120
|
const {
|
|
1118
1121
|
defaultColor = "light",
|
|
1119
|
-
cssVariablePrefix = "--shiki-"
|
|
1122
|
+
cssVariablePrefix = "--shiki-",
|
|
1123
|
+
colorsRendering = "css-vars"
|
|
1120
1124
|
} = options;
|
|
1121
1125
|
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);
|
|
1122
1126
|
if (themes.length === 0)
|
|
@@ -1131,12 +1135,12 @@ function codeToTokens(internal, code, options) {
|
|
|
1131
1135
|
throw new ShikiError$1(`\`themes\` option must contain the defaultColor key \`${defaultColor}\``);
|
|
1132
1136
|
const themeRegs = themes.map((t) => internal.getTheme(t.theme));
|
|
1133
1137
|
const themesOrder = themes.map((t) => t.color);
|
|
1134
|
-
tokens = themeTokens.map((line) => line.map((token) => flatTokenVariants(token, themesOrder, cssVariablePrefix, defaultColor)));
|
|
1138
|
+
tokens = themeTokens.map((line) => line.map((token) => flatTokenVariants(token, themesOrder, cssVariablePrefix, defaultColor, colorsRendering)));
|
|
1135
1139
|
if (grammarState)
|
|
1136
1140
|
setLastGrammarStateToMap(tokens, grammarState);
|
|
1137
1141
|
const themeColorReplacements = themes.map((t) => resolveColorReplacements(t.theme, options));
|
|
1138
|
-
fg = mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, "fg");
|
|
1139
|
-
bg = mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, "bg");
|
|
1142
|
+
fg = mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, "fg", colorsRendering);
|
|
1143
|
+
bg = mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, "bg", colorsRendering);
|
|
1140
1144
|
themeName = `shiki-themes ${themeRegs.map((t) => t.name).join(" ")}`;
|
|
1141
1145
|
rootStyle = defaultColor ? void 0 : [fg, bg].join(";");
|
|
1142
1146
|
} else if ("theme" in options) {
|
|
@@ -1163,7 +1167,7 @@ function codeToTokens(internal, code, options) {
|
|
|
1163
1167
|
grammarState
|
|
1164
1168
|
};
|
|
1165
1169
|
}
|
|
1166
|
-
function mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, property) {
|
|
1170
|
+
function mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, property, colorsRendering) {
|
|
1167
1171
|
return themes.map((t, idx) => {
|
|
1168
1172
|
const value = applyColorReplacements(themeRegs[idx][property], themeColorReplacements[idx]) || "inherit";
|
|
1169
1173
|
const cssVar = `${cssVariablePrefix + t.color}${property === "bg" ? "-bg" : ""}:${value}`;
|
|
@@ -1179,8 +1183,11 @@ function mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePr
|
|
|
1179
1183
|
}
|
|
1180
1184
|
return value;
|
|
1181
1185
|
}
|
|
1182
|
-
|
|
1183
|
-
|
|
1186
|
+
if (colorsRendering === "css-vars") {
|
|
1187
|
+
return cssVar;
|
|
1188
|
+
}
|
|
1189
|
+
return null;
|
|
1190
|
+
}).filter((i) => !!i).join(";");
|
|
1184
1191
|
}
|
|
1185
1192
|
|
|
1186
1193
|
function codeToHast(internal, code, options, transformerContext = {
|
|
@@ -1946,6 +1953,7 @@ function createdBundledHighlighter(options) {
|
|
|
1946
1953
|
if (typeof lang === "string") {
|
|
1947
1954
|
if (isSpecialLang(lang))
|
|
1948
1955
|
return [];
|
|
1956
|
+
lang = options2.langAlias?.[lang] || lang;
|
|
1949
1957
|
const bundle = bundledLanguages[lang];
|
|
1950
1958
|
if (!bundle)
|
|
1951
1959
|
throw new ShikiError$1(`Language \`${lang}\` is not included in this bundle. You may want to load it from external source.`);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shikijs/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.7.0",
|
|
5
5
|
"description": "Core of Shiki",
|
|
6
6
|
"author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"@shikijs/vscode-textmate": "^10.0.2",
|
|
40
40
|
"@types/hast": "^3.0.4",
|
|
41
41
|
"hast-util-to-html": "^9.0.5",
|
|
42
|
-
"@shikijs/types": "3.
|
|
42
|
+
"@shikijs/types": "3.7.0"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "unbuild",
|