@shikijs/core 3.4.1 → 3.5.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
@@ -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): ThemedToken;
257
+ declare function flatTokenVariants(merged: ThemedTokenWithVariants, variantsOrder: string[], cssVariablePrefix: string, defaultColor: string | boolean | 'light-dark()'): 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
@@ -117,6 +117,8 @@ function guessEmbeddedLanguages(code, _lang, highlighter) {
117
117
  return Array.from(langs).filter((l) => l && bundle[l]);
118
118
  }
119
119
 
120
+ const DEFAULT_COLOR_LIGHT_DARK = "light-dark()";
121
+
120
122
  function splitToken(token, offsets) {
121
123
  let lastOffset = 0;
122
124
  const tokens = [];
@@ -165,7 +167,20 @@ function flatTokenVariants(merged, variantsOrder, cssVariablePrefix, defaultColo
165
167
  for (const key of styleKeys) {
166
168
  const value = cur[key] || "inherit";
167
169
  if (idx === 0 && defaultColor) {
168
- mergedStyles[key] = value;
170
+ if (defaultColor === DEFAULT_COLOR_LIGHT_DARK && styles.length > 1) {
171
+ const lightIndex = variantsOrder.findIndex((t) => t === "light");
172
+ const darkIndex = variantsOrder.findIndex((t) => t === "dark");
173
+ if (lightIndex === -1 || darkIndex === -1)
174
+ throw new ShikiError$1('When using `defaultColor: "light-dark()"`, you must provide both `light` and `dark` themes');
175
+ const lightValue = styles[lightIndex][key] || "inherit";
176
+ const darkValue = styles[darkIndex][key] || "inherit";
177
+ 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;
181
+ } else {
182
+ mergedStyles[key] = value;
183
+ }
169
184
  } else {
170
185
  const keyName = key === "color" ? "" : key === "background-color" ? "-bg" : `-${key}`;
171
186
  const varKey = cssVariablePrefix + variantsOrder[idx] + (key === "color" ? "" : keyName);
@@ -1112,7 +1127,7 @@ function codeToTokens(internal, code, options) {
1112
1127
  options
1113
1128
  );
1114
1129
  grammarState = getLastGrammarStateFromMap(themeTokens);
1115
- if (defaultColor && !themes.find((t) => t.color === defaultColor))
1130
+ if (defaultColor && DEFAULT_COLOR_LIGHT_DARK !== defaultColor && !themes.find((t) => t.color === defaultColor))
1116
1131
  throw new ShikiError$1(`\`themes\` option must contain the defaultColor key \`${defaultColor}\``);
1117
1132
  const themeRegs = themes.map((t) => internal.getTheme(t.theme));
1118
1133
  const themesOrder = themes.map((t) => t.color);
@@ -1120,8 +1135,8 @@ function codeToTokens(internal, code, options) {
1120
1135
  if (grammarState)
1121
1136
  setLastGrammarStateToMap(tokens, grammarState);
1122
1137
  const themeColorReplacements = themes.map((t) => resolveColorReplacements(t.theme, options));
1123
- fg = themes.map((t, idx) => (idx === 0 && defaultColor ? "" : `${cssVariablePrefix + t.color}:`) + (applyColorReplacements(themeRegs[idx].fg, themeColorReplacements[idx]) || "inherit")).join(";");
1124
- bg = themes.map((t, idx) => (idx === 0 && defaultColor ? "" : `${cssVariablePrefix + t.color}-bg:`) + (applyColorReplacements(themeRegs[idx].bg, themeColorReplacements[idx]) || "inherit")).join(";");
1138
+ fg = mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, "fg");
1139
+ bg = mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, "bg");
1125
1140
  themeName = `shiki-themes ${themeRegs.map((t) => t.name).join(" ")}`;
1126
1141
  rootStyle = defaultColor ? void 0 : [fg, bg].join(";");
1127
1142
  } else if ("theme" in options) {
@@ -1148,6 +1163,25 @@ function codeToTokens(internal, code, options) {
1148
1163
  grammarState
1149
1164
  };
1150
1165
  }
1166
+ function mapThemeColors(themes, themeRegs, themeColorReplacements, cssVariablePrefix, defaultColor, property) {
1167
+ return themes.map((t, idx) => {
1168
+ const value = applyColorReplacements(themeRegs[idx][property], themeColorReplacements[idx]) || "inherit";
1169
+ const cssVar = `${cssVariablePrefix + t.color}${property === "bg" ? "-bg" : ""}:${value}`;
1170
+ if (idx === 0 && defaultColor) {
1171
+ if (defaultColor === DEFAULT_COLOR_LIGHT_DARK && themes.length > 1) {
1172
+ const lightIndex = themes.findIndex((t2) => t2.color === "light");
1173
+ const darkIndex = themes.findIndex((t2) => t2.color === "dark");
1174
+ if (lightIndex === -1 || darkIndex === -1)
1175
+ throw new ShikiError$1('When using `defaultColor: "light-dark()"`, you must provide both `light` and `dark` themes');
1176
+ const lightValue = applyColorReplacements(themeRegs[lightIndex][property], themeColorReplacements[lightIndex]) || "inherit";
1177
+ const darkValue = applyColorReplacements(themeRegs[darkIndex][property], themeColorReplacements[darkIndex]) || "inherit";
1178
+ return `light-dark(${lightValue}, ${darkValue});${cssVar}`;
1179
+ }
1180
+ return value;
1181
+ }
1182
+ return cssVar;
1183
+ }).join(";");
1184
+ }
1151
1185
 
1152
1186
  function codeToHast(internal, code, options, transformerContext = {
1153
1187
  meta: {},
@@ -1395,8 +1429,8 @@ function mergeAdjacentStyledTokens(tokens) {
1395
1429
  continue;
1396
1430
  }
1397
1431
  const prevToken = newLine[newLine.length - 1];
1398
- const prevStyle = prevToken.htmlStyle || stringifyTokenStyle(getTokenStyleObject(prevToken));
1399
- const currentStyle = token.htmlStyle || stringifyTokenStyle(getTokenStyleObject(token));
1432
+ const prevStyle = stringifyTokenStyle(prevToken.htmlStyle || getTokenStyleObject(prevToken));
1433
+ const currentStyle = stringifyTokenStyle(token.htmlStyle || getTokenStyleObject(token));
1400
1434
  const isPrevDecorated = prevToken.fontStyle && (prevToken.fontStyle & FontStyle.Underline || prevToken.fontStyle & FontStyle.Strikethrough);
1401
1435
  const isDecorated = token.fontStyle && (token.fontStyle & FontStyle.Underline || token.fontStyle & FontStyle.Strikethrough);
1402
1436
  if (!isPrevDecorated && !isDecorated && prevStyle === currentStyle) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/core",
3
3
  "type": "module",
4
- "version": "3.4.1",
4
+ "version": "3.5.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.4.1"
42
+ "@shikijs/types": "3.5.0"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "unbuild",