@shikijs/core 3.5.0 → 3.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/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: string | boolean | 'light-dark()'): ThemedToken;
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
@@ -154,7 +154,7 @@ function splitTokens(tokens, breakpoints) {
154
154
  });
155
155
  });
156
156
  }
157
- function flatTokenVariants(merged, variantsOrder, cssVariablePrefix, defaultColor) {
157
+ function flatTokenVariants(merged, variantsOrder, cssVariablePrefix, defaultColor, colorsRendering = "css-vars") {
158
158
  const token = {
159
159
  content: merged.content,
160
160
  explanation: merged.explanation,
@@ -163,6 +163,10 @@ function flatTokenVariants(merged, variantsOrder, cssVariablePrefix, defaultColo
163
163
  const styles = variantsOrder.map((t) => getTokenStyleObject(merged.variants[t]));
164
164
  const styleKeys = new Set(styles.flatMap((t) => Object.keys(t)));
165
165
  const mergedStyles = {};
166
+ const varKey = (idx, key) => {
167
+ const keyName = key === "color" ? "" : key === "background-color" ? "-bg" : `-${key}`;
168
+ return cssVariablePrefix + variantsOrder[idx] + (key === "color" ? "" : keyName);
169
+ };
166
170
  styles.forEach((cur, idx) => {
167
171
  for (const key of styleKeys) {
168
172
  const value = cur[key] || "inherit";
@@ -175,16 +179,14 @@ function flatTokenVariants(merged, variantsOrder, cssVariablePrefix, defaultColo
175
179
  const lightValue = styles[lightIndex][key] || "inherit";
176
180
  const darkValue = styles[darkIndex][key] || "inherit";
177
181
  mergedStyles[key] = `light-dark(${lightValue}, ${darkValue})`;
178
- const keyName = key === "color" ? "" : key === "background-color" ? "-bg" : `-${key}`;
179
- const varKey = cssVariablePrefix + variantsOrder[idx] + (key === "color" ? "" : keyName);
180
- mergedStyles[varKey] = value;
182
+ if (colorsRendering === "css-vars")
183
+ mergedStyles[varKey(idx, key)] = value;
181
184
  } else {
182
185
  mergedStyles[key] = value;
183
186
  }
184
187
  } else {
185
- const keyName = key === "color" ? "" : key === "background-color" ? "-bg" : `-${key}`;
186
- const varKey = cssVariablePrefix + variantsOrder[idx] + (key === "color" ? "" : keyName);
187
- mergedStyles[varKey] = value;
188
+ if (colorsRendering === "css-vars")
189
+ mergedStyles[varKey(idx, key)] = value;
188
190
  }
189
191
  }
190
192
  });
@@ -1116,7 +1118,8 @@ function codeToTokens(internal, code, options) {
1116
1118
  if ("themes" in options) {
1117
1119
  const {
1118
1120
  defaultColor = "light",
1119
- cssVariablePrefix = "--shiki-"
1121
+ cssVariablePrefix = "--shiki-",
1122
+ colorsRendering = "css-vars"
1120
1123
  } = options;
1121
1124
  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
1125
  if (themes.length === 0)
@@ -1131,12 +1134,12 @@ function codeToTokens(internal, code, options) {
1131
1134
  throw new ShikiError$1(`\`themes\` option must contain the defaultColor key \`${defaultColor}\``);
1132
1135
  const themeRegs = themes.map((t) => internal.getTheme(t.theme));
1133
1136
  const themesOrder = themes.map((t) => t.color);
1134
- tokens = themeTokens.map((line) => line.map((token) => flatTokenVariants(token, themesOrder, cssVariablePrefix, defaultColor)));
1137
+ tokens = themeTokens.map((line) => line.map((token) => flatTokenVariants(token, themesOrder, cssVariablePrefix, defaultColor, colorsRendering)));
1135
1138
  if (grammarState)
1136
1139
  setLastGrammarStateToMap(tokens, grammarState);
1137
1140
  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");
1141
+ fg = mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, "fg", colorsRendering);
1142
+ bg = mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, "bg", colorsRendering);
1140
1143
  themeName = `shiki-themes ${themeRegs.map((t) => t.name).join(" ")}`;
1141
1144
  rootStyle = defaultColor ? void 0 : [fg, bg].join(";");
1142
1145
  } else if ("theme" in options) {
@@ -1163,7 +1166,7 @@ function codeToTokens(internal, code, options) {
1163
1166
  grammarState
1164
1167
  };
1165
1168
  }
1166
- function mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, property) {
1169
+ function mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, property, colorsRendering) {
1167
1170
  return themes.map((t, idx) => {
1168
1171
  const value = applyColorReplacements(themeRegs[idx][property], themeColorReplacements[idx]) || "inherit";
1169
1172
  const cssVar = `${cssVariablePrefix + t.color}${property === "bg" ? "-bg" : ""}:${value}`;
@@ -1179,8 +1182,11 @@ function mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePr
1179
1182
  }
1180
1183
  return value;
1181
1184
  }
1182
- return cssVar;
1183
- }).join(";");
1185
+ if (colorsRendering === "css-vars") {
1186
+ return cssVar;
1187
+ }
1188
+ return null;
1189
+ }).filter((i) => !!i).join(";");
1184
1190
  }
1185
1191
 
1186
1192
  function codeToHast(internal, code, options, transformerContext = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/core",
3
3
  "type": "module",
4
- "version": "3.5.0",
4
+ "version": "3.6.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.5.0"
42
+ "@shikijs/types": "3.6.0"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "unbuild",