@shikijs/core 3.16.0 → 3.17.1

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, CodeOptionsMultipleThemes, TokenStyles } from '@shikijs/types';
1
+ import { CreateBundledHighlighterOptions, 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';
@@ -8,7 +8,7 @@ import { toHtml } from 'hast-util-to-html';
8
8
  *
9
9
  * @example
10
10
  * ```ts
11
- * const createHighlighter = createdBundledHighlighter({
11
+ * const createHighlighter = createBundledHighlighter({
12
12
  * langs: {
13
13
  * typescript: () => import('@shikijs/langs/typescript'),
14
14
  * // ...
@@ -23,7 +23,7 @@ import { toHtml } from 'hast-util-to-html';
23
23
  *
24
24
  * @param options
25
25
  */
26
- declare function createdBundledHighlighter<BundledLangs extends string, BundledThemes extends string>(options: CreatedBundledHighlighterOptions<BundledLangs, BundledThemes>): CreateHighlighterFactory<BundledLangs, BundledThemes>;
26
+ declare function createBundledHighlighter<BundledLangs extends string, BundledThemes extends string>(options: CreateBundledHighlighterOptions<BundledLangs, BundledThemes>): CreateHighlighterFactory<BundledLangs, BundledThemes>;
27
27
  interface ShorthandsBundle<L extends string, T extends string> {
28
28
  /**
29
29
  * Shorthand for `codeToHtml` with auto-loaded theme and language.
@@ -78,6 +78,10 @@ interface CreateSingletonShorthandsOptions<L extends string, T extends string> {
78
78
  guessEmbeddedLanguages?: (code: string, lang: string | undefined, highlighter: HighlighterGeneric<L, T>) => Awaitable<string[] | undefined>;
79
79
  }
80
80
  declare function createSingletonShorthands<L extends string, T extends string>(createHighlighter: CreateHighlighterFactory<L, T>, config?: CreateSingletonShorthandsOptions<L, T>): ShorthandsBundle<L, T>;
81
+ /**
82
+ * @deprecated Use `createBundledHighlighter` instead.
83
+ */
84
+ declare const createdBundledHighlighter: typeof createBundledHighlighter;
81
85
 
82
86
  /**
83
87
  * Create a Shiki core highlighter instance, with no languages or themes bundled.
@@ -225,6 +229,19 @@ declare function addClassToHast(node: Element, className: string | string[]): El
225
229
 
226
230
  /**
227
231
  * Split a string into lines, each line preserves the line ending.
232
+ *
233
+ * @param code - The code string to split into lines
234
+ * @param preserveEnding - Whether to preserve line endings in the result
235
+ * @returns Array of tuples containing [line content, offset index]
236
+ *
237
+ * @example
238
+ * ```ts
239
+ * splitLines('hello\nworld', false)
240
+ * // => [['hello', 0], ['world', 6]]
241
+ *
242
+ * splitLines('hello\nworld', true)
243
+ * // => [['hello\n', 0], ['world', 6]]
244
+ * ```
228
245
  */
229
246
  declare function splitLines(code: string, preserveEnding?: boolean): [string, number][];
230
247
  /**
@@ -241,6 +258,20 @@ declare function createPositionConverter(code: string): {
241
258
  * Guess embedded languages from given code and highlighter.
242
259
  *
243
260
  * When highlighter is provided, only bundled languages will be included.
261
+ *
262
+ * @param code - The code string to analyze
263
+ * @param _lang - The primary language of the code (currently unused)
264
+ * @param highlighter - Optional highlighter instance to validate languages
265
+ * @returns Array of detected language identifiers
266
+ *
267
+ * @example
268
+ * ```ts
269
+ * // Detects 'javascript' from Vue SFC
270
+ * guessEmbeddedLanguages('<script lang="javascript">')
271
+ *
272
+ * // Detects 'python' from markdown code block
273
+ * guessEmbeddedLanguages('```python\nprint("hi")\n```')
274
+ * ```
244
275
  */
245
276
  declare function guessEmbeddedLanguages(code: string, _lang: string | undefined, highlighter?: HighlighterGeneric<any, any>): string[];
246
277
 
@@ -272,5 +303,5 @@ declare function enableDeprecationWarnings(emitDeprecation?: DeprecationTarget |
272
303
  */
273
304
  declare function warnDeprecated(message: string, version?: DeprecationTarget): void;
274
305
 
275
- export { addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createdBundledHighlighter, enableDeprecationWarnings, flatTokenVariants, getSingletonHighlighterCore, getTokenStyleObject, guessEmbeddedLanguages, hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations, warnDeprecated };
306
+ export { addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createBundledHighlighter, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createdBundledHighlighter, enableDeprecationWarnings, flatTokenVariants, getSingletonHighlighterCore, getTokenStyleObject, guessEmbeddedLanguages, hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations, warnDeprecated };
276
307
  export type { CreateSingletonShorthandsOptions, CssVariablesThemeOptions, ShorthandsBundle };
package/dist/index.mjs CHANGED
@@ -57,6 +57,9 @@ function addClassToHast(node, className) {
57
57
  }
58
58
 
59
59
  function splitLines(code, preserveEnding = false) {
60
+ if (code.length === 0) {
61
+ return [["", 0]];
62
+ }
60
63
  const parts = code.split(/(\r?\n)/g);
61
64
  let index = 0;
62
65
  const lines = [];
@@ -102,14 +105,26 @@ function createPositionConverter(code) {
102
105
  }
103
106
  function guessEmbeddedLanguages(code, _lang, highlighter) {
104
107
  const langs = /* @__PURE__ */ new Set();
105
- for (const match of code.matchAll(/lang=["']([\w-]+)["']/g)) {
106
- langs.add(match[1]);
108
+ for (const match of code.matchAll(/:?lang=["']([^"']+)["']/g)) {
109
+ const lang = match[1].toLowerCase().trim();
110
+ if (lang)
111
+ langs.add(lang);
107
112
  }
108
113
  for (const match of code.matchAll(/(?:```|~~~)([\w-]+)/g)) {
109
- langs.add(match[1]);
114
+ const lang = match[1].toLowerCase().trim();
115
+ if (lang)
116
+ langs.add(lang);
110
117
  }
111
118
  for (const match of code.matchAll(/\\begin\{([\w-]+)\}/g)) {
112
- langs.add(match[1]);
119
+ const lang = match[1].toLowerCase().trim();
120
+ if (lang)
121
+ langs.add(lang);
122
+ }
123
+ for (const match of code.matchAll(/<script\s+(?:type|lang)=["']([^"']+)["']/gi)) {
124
+ const fullType = match[1].toLowerCase().trim();
125
+ const lang = fullType.includes("/") ? fullType.split("/").pop() : fullType;
126
+ if (lang)
127
+ langs.add(lang);
113
128
  }
114
129
  if (!highlighter)
115
130
  return Array.from(langs);
@@ -844,15 +859,26 @@ function tokenizeAnsiWithTheme(theme, fileContents, options) {
844
859
  );
845
860
  }
846
861
  function dimColor(color) {
847
- const hexMatch = color.match(/#([0-9a-f]{3})([0-9a-f]{3})?([0-9a-f]{2})?/i);
862
+ const hexMatch = color.match(/#([0-9a-f]{3,8})/i);
848
863
  if (hexMatch) {
849
- if (hexMatch[3]) {
850
- const alpha = Math.round(Number.parseInt(hexMatch[3], 16) / 2).toString(16).padStart(2, "0");
851
- return `#${hexMatch[1]}${hexMatch[2]}${alpha}`;
852
- } else if (hexMatch[2]) {
853
- return `#${hexMatch[1]}${hexMatch[2]}80`;
854
- } else {
855
- return `#${Array.from(hexMatch[1]).map((x) => `${x}${x}`).join("")}80`;
864
+ const hex = hexMatch[1];
865
+ if (hex.length === 8) {
866
+ const alpha = Math.round(Number.parseInt(hex.slice(6, 8), 16) / 2).toString(16).padStart(2, "0");
867
+ return `#${hex.slice(0, 6)}${alpha}`;
868
+ } else if (hex.length === 6) {
869
+ return `#${hex}80`;
870
+ } else if (hex.length === 4) {
871
+ const r = hex[0];
872
+ const g = hex[1];
873
+ const b = hex[2];
874
+ const a = hex[3];
875
+ const alpha = Math.round(Number.parseInt(`${a}${a}`, 16) / 2).toString(16).padStart(2, "0");
876
+ return `#${r}${r}${g}${g}${b}${b}${alpha}`;
877
+ } else if (hex.length === 3) {
878
+ const r = hex[0];
879
+ const g = hex[1];
880
+ const b = hex[2];
881
+ return `#${r}${r}${g}${g}${b}${b}80`;
856
882
  }
857
883
  }
858
884
  const cssVarMatch = color.match(/var\((--[\w-]+-ansi-[\w-]+)\)/);
@@ -1810,8 +1836,9 @@ class Registry extends Registry$1 {
1810
1836
  resolveEmbeddedLanguages(lang) {
1811
1837
  this._langMap.set(lang.name, lang);
1812
1838
  this._langGraph.set(lang.name, lang);
1813
- if (lang.embeddedLangs) {
1814
- for (const embeddedLang of lang.embeddedLangs)
1839
+ const embedded = lang.embeddedLanguages ?? lang.embeddedLangs;
1840
+ if (embedded) {
1841
+ for (const embeddedLang of embedded)
1815
1842
  this._langGraph.set(embeddedLang, this._langMap.get(embeddedLang));
1816
1843
  }
1817
1844
  }
@@ -2032,7 +2059,7 @@ function makeSingletonHighlighterCore(createHighlighter) {
2032
2059
  }
2033
2060
  const getSingletonHighlighterCore = /* @__PURE__ */ makeSingletonHighlighterCore(createHighlighterCore);
2034
2061
 
2035
- function createdBundledHighlighter(options) {
2062
+ function createBundledHighlighter(options) {
2036
2063
  const bundledLanguages = options.langs;
2037
2064
  const bundledThemes = options.themes;
2038
2065
  const engine = options.engine;
@@ -2158,6 +2185,7 @@ function createSingletonShorthands(createHighlighter, config) {
2158
2185
  }
2159
2186
  };
2160
2187
  }
2188
+ const createdBundledHighlighter = createBundledHighlighter;
2161
2189
 
2162
2190
  function createCssVariablesTheme(options = {}) {
2163
2191
  const {
@@ -2394,4 +2422,4 @@ function createCssVariablesTheme(options = {}) {
2394
2422
  return theme;
2395
2423
  }
2396
2424
 
2397
- export { addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createdBundledHighlighter, enableDeprecationWarnings, flatTokenVariants, getSingletonHighlighterCore, getTokenStyleObject, guessEmbeddedLanguages, hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations, warnDeprecated };
2425
+ export { addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createBundledHighlighter, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createPositionConverter, createShikiInternal, createShikiInternalSync, createSingletonShorthands, createdBundledHighlighter, enableDeprecationWarnings, flatTokenVariants, getSingletonHighlighterCore, getTokenStyleObject, guessEmbeddedLanguages, hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations, warnDeprecated };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/core",
3
3
  "type": "module",
4
- "version": "3.16.0",
4
+ "version": "3.17.1",
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.16.0"
42
+ "@shikijs/types": "3.17.1"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "unbuild",