@shikijs/core
Advanced tools
+31
-42
@@ -5,6 +5,5 @@ import { ShikiError as ShikiError$1 } from "@shikijs/types"; | ||
| import { toHtml } from "hast-util-to-html"; | ||
| export * from "@shikijs/types" | ||
| export * from "@shikijs/types"; | ||
| //#region src/utils/hast.ts | ||
| const RE_WHITESPACE = /\s+/g; | ||
| /** | ||
@@ -19,11 +18,14 @@ * Utility to append class to a hast node | ||
| node.properties.class ||= []; | ||
| if (typeof node.properties.class === "string") node.properties.class = node.properties.class.split(/\s+/g); | ||
| if (typeof node.properties.class === "string") node.properties.class = node.properties.class.split(RE_WHITESPACE); | ||
| if (!Array.isArray(node.properties.class)) node.properties.class = []; | ||
| const targets = Array.isArray(className) ? className : className.split(/\s+/g); | ||
| const targets = Array.isArray(className) ? className : className.split(RE_WHITESPACE); | ||
| for (const c of targets) if (c && !node.properties.class.includes(c)) node.properties.class.push(c); | ||
| return node; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/strings.ts | ||
| const RE_LANG_ATTR = /:?lang=["']([^"']+)["']/g; | ||
| const RE_CODE_FENCE = /(?:```|~~~)([\w-]+)/g; | ||
| const RE_LATEX_BEGIN = /\\begin\{([\w-]+)\}/g; | ||
| const RE_SCRIPT_LANG = /<script\s+(?:type|lang)=["']([^"']+)["']/gi; | ||
| /** | ||
@@ -39,3 +41,3 @@ * Creates a converter between index and position in a code block. | ||
| line: lines.length - 1, | ||
| character: lines[lines.length - 1].length | ||
| character: lines.at(-1).length | ||
| }; | ||
@@ -87,15 +89,15 @@ let character = index; | ||
| const langs = /* @__PURE__ */ new Set(); | ||
| for (const match of code.matchAll(/:?lang=["']([^"']+)["']/g)) { | ||
| for (const match of code.matchAll(RE_LANG_ATTR)) { | ||
| const lang = match[1].toLowerCase().trim(); | ||
| if (lang) langs.add(lang); | ||
| } | ||
| for (const match of code.matchAll(/(?:```|~~~)([\w-]+)/g)) { | ||
| for (const match of code.matchAll(RE_CODE_FENCE)) { | ||
| const lang = match[1].toLowerCase().trim(); | ||
| if (lang) langs.add(lang); | ||
| } | ||
| for (const match of code.matchAll(/\\begin\{([\w-]+)\}/g)) { | ||
| for (const match of code.matchAll(RE_LATEX_BEGIN)) { | ||
| const lang = match[1].toLowerCase().trim(); | ||
| if (lang) langs.add(lang); | ||
| } | ||
| for (const match of code.matchAll(/<script\s+(?:type|lang)=["']([^"']+)["']/gi)) { | ||
| for (const match of code.matchAll(RE_SCRIPT_LANG)) { | ||
| const fullType = match[1].toLowerCase().trim(); | ||
@@ -105,12 +107,7 @@ const lang = fullType.includes("/") ? fullType.split("/").pop() : fullType; | ||
| } | ||
| if (!highlighter) return Array.from(langs); | ||
| if (!highlighter) return [...langs]; | ||
| const bundle = highlighter.getBundledLanguages(); | ||
| return Array.from(langs).filter((l) => l && bundle[l]); | ||
| return [...langs].filter((l) => l && bundle[l]); | ||
| } | ||
| //#endregion | ||
| //#region src/utils/constants.ts | ||
| const DEFAULT_COLOR_LIGHT_DARK = "light-dark()"; | ||
| const COLOR_KEYS = ["color", "background-color"]; | ||
| //#endregion | ||
@@ -145,3 +142,3 @@ //#region src/utils/tokens.ts | ||
| function splitTokens(tokens, breakpoints) { | ||
| const sorted = Array.from(breakpoints instanceof Set ? breakpoints : new Set(breakpoints)).sort((a, b) => a - b); | ||
| const sorted = [...breakpoints instanceof Set ? breakpoints : new Set(breakpoints)].sort((a, b) => a - b); | ||
| if (!sorted.length) return tokens; | ||
@@ -172,3 +169,3 @@ return tokens.map((line) => { | ||
| const value = cur[key] || "inherit"; | ||
| if (idx === 0 && defaultColor && COLOR_KEYS.includes(key)) if (defaultColor === DEFAULT_COLOR_LIGHT_DARK && styles.length > 1) { | ||
| if (idx === 0 && defaultColor && COLOR_KEYS.includes(key)) if (defaultColor === "light-dark()" && styles.length > 1) { | ||
| const lightIndex = variantsOrder.findIndex((t) => t === "light"); | ||
@@ -204,3 +201,2 @@ const darkIndex = variantsOrder.findIndex((t) => t === "dark"); | ||
| } | ||
| //#endregion | ||
@@ -259,3 +255,3 @@ //#region src/transformer-decorations.ts | ||
| const ctx = getContext(this); | ||
| const lines = Array.from(codeEl.children).filter((i) => i.type === "element" && i.tagName === "span"); | ||
| const lines = [...codeEl.children].filter((i) => i.type === "element" && i.tagName === "span"); | ||
| if (lines.length !== ctx.converter.lines.length) throw new ShikiError$1(`Number of lines in code element (${lines.length}) does not match the number of lines in the source (${ctx.converter.lines.length}). Failed to apply decorations.`); | ||
@@ -347,3 +343,2 @@ function applyLineSection(line, start, end, decoration) { | ||
| } | ||
| //#endregion | ||
@@ -380,3 +375,2 @@ //#region src/highlight/_get-transformers.ts | ||
| } | ||
| //#endregion | ||
@@ -617,5 +611,6 @@ //#region ../../node_modules/.pnpm/ansi-sequence-parser@1.1.3/node_modules/ansi-sequence-parser/dist/index.js | ||
| } | ||
| //#endregion | ||
| //#region src/highlight/code-to-tokens-ansi.ts | ||
| const RE_HEX_COLOR = /#([0-9a-f]{3,8})/i; | ||
| const RE_CSS_VAR_ANSI = /var\((--[\w-]+-ansi-[\w-]+)\)/; | ||
| /** | ||
@@ -682,3 +677,3 @@ * Default ANSI palette (VSCode compatible fallbacks) | ||
| function dimColor(color) { | ||
| const hexMatch = color.match(/#([0-9a-f]{3,8})/i); | ||
| const hexMatch = color.match(RE_HEX_COLOR); | ||
| if (hexMatch) { | ||
@@ -703,7 +698,6 @@ const hex = hexMatch[1]; | ||
| } | ||
| const cssVarMatch = color.match(/var\((--[\w-]+-ansi-[\w-]+)\)/); | ||
| const cssVarMatch = color.match(RE_CSS_VAR_ANSI); | ||
| if (cssVarMatch) return `var(${cssVarMatch[1]}-dim)`; | ||
| return color; | ||
| } | ||
| //#endregion | ||
@@ -724,3 +718,2 @@ //#region src/highlight/code-to-tokens-base.ts | ||
| } | ||
| //#endregion | ||
@@ -749,3 +742,3 @@ //#region src/highlight/code-to-tokens.ts | ||
| grammarState = getLastGrammarStateFromMap(themeTokens); | ||
| if (defaultColor && DEFAULT_COLOR_LIGHT_DARK !== defaultColor && !themes.find((t) => t.color === defaultColor)) throw new ShikiError$1(`\`themes\` option must contain the defaultColor key \`${defaultColor}\``); | ||
| if (defaultColor && "light-dark()" !== defaultColor && !themes.some((t) => t.color === defaultColor)) throw new ShikiError$1(`\`themes\` option must contain the defaultColor key \`${defaultColor}\``); | ||
| const themeRegs = themes.map((t) => primitive.getTheme(t.theme)); | ||
@@ -783,3 +776,3 @@ const themesOrder = themes.map((t) => t.color); | ||
| if (idx === 0 && defaultColor) { | ||
| if (defaultColor === DEFAULT_COLOR_LIGHT_DARK && themes.length > 1) { | ||
| if (defaultColor === "light-dark()" && themes.length > 1) { | ||
| const lightIndex = themes.findIndex((t) => t.color === "light"); | ||
@@ -796,5 +789,6 @@ const darkIndex = themes.findIndex((t) => t.color === "dark"); | ||
| } | ||
| //#endregion | ||
| //#region src/highlight/code-to-hast.ts | ||
| const RE_WHITESPACE_ONLY = /^\s+$/; | ||
| const RE_LEADING_TRAILING_WHITESPACE = /^(\s*)(.*?)(\s*)$/; | ||
| function codeToHast(primitive, code, options, transformerContext = { | ||
@@ -978,3 +972,3 @@ meta: {}, | ||
| const couldMerge = !(token.fontStyle && (token.fontStyle & FontStyle.Underline || token.fontStyle & FontStyle.Strikethrough)); | ||
| if (couldMerge && token.content.match(/^\s+$/) && line[idx + 1]) { | ||
| if (couldMerge && RE_WHITESPACE_ONLY.test(token.content) && line[idx + 1]) { | ||
| if (firstOffset === void 0) firstOffset = token.offset; | ||
@@ -1002,4 +996,4 @@ carryOnContent += token.content; | ||
| return line.flatMap((token) => { | ||
| if (token.content.match(/^\s+$/)) return token; | ||
| const match = token.content.match(/^(\s*)(.*?)(\s*)$/); | ||
| if (RE_WHITESPACE_ONLY.test(token.content)) return token; | ||
| const match = token.content.match(RE_LEADING_TRAILING_WHITESPACE); | ||
| if (!match) return token; | ||
@@ -1033,3 +1027,3 @@ const [, leading, content, trailing] = match; | ||
| } | ||
| const prevToken = newLine[newLine.length - 1]; | ||
| const prevToken = newLine.at(-1); | ||
| const prevStyle = stringifyTokenStyle(prevToken.htmlStyle || getTokenStyleObject(prevToken)); | ||
@@ -1045,3 +1039,2 @@ const currentStyle = stringifyTokenStyle(token.htmlStyle || getTokenStyleObject(token)); | ||
| } | ||
| //#endregion | ||
@@ -1064,3 +1057,2 @@ //#region src/highlight/code-to-html.ts | ||
| } | ||
| //#endregion | ||
@@ -1131,3 +1123,2 @@ //#region src/constructors/highlighter.ts | ||
| const getSingletonHighlighterCore = /* @__PURE__ */ makeSingletonHighlighterCore(createHighlighterCore); | ||
| //#endregion | ||
@@ -1243,3 +1234,2 @@ //#region src/constructors/bundle-factory.ts | ||
| } | ||
| //#endregion | ||
@@ -1440,4 +1430,3 @@ //#region src/theme-css-variables.ts | ||
| } | ||
| //#endregion | ||
| export { ShikiError, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createBundledHighlighter, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createPositionConverter, createShikiInternal, createShikiInternalSync, createShikiPrimitive, createShikiPrimitiveAsync, createSingletonShorthands, flatTokenVariants, getLastGrammarState, getSingletonHighlighterCore, getTokenStyleObject, guessEmbeddedLanguages, hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations }; | ||
| export { ShikiError, addClassToHast, applyColorReplacements, codeToHast, codeToHtml, codeToTokens, codeToTokensBase, codeToTokensWithThemes, createBundledHighlighter, createCssVariablesTheme, createHighlighterCore, createHighlighterCoreSync, createPositionConverter, createShikiInternal, createShikiInternalSync, createShikiPrimitive, createShikiPrimitiveAsync, createSingletonShorthands, flatTokenVariants, getLastGrammarState, getSingletonHighlighterCore, getTokenStyleObject, guessEmbeddedLanguages, hastToHtml, isNoneTheme, isPlainLang, isSpecialLang, isSpecialTheme, makeSingletonHighlighter, makeSingletonHighlighterCore, normalizeGetter, normalizeTheme, resolveColorReplacements, splitLines, splitToken, splitTokens, stringifyTokenStyle, toArray, tokenizeAnsiWithTheme, tokenizeWithTheme, tokensToHast, transformerDecorations }; |
@@ -1,3 +0,2 @@ | ||
| export * from "@shikijs/vscode-textmate" | ||
| export { }; | ||
| export * from "@shikijs/vscode-textmate"; | ||
| export {}; |
+2
-3
@@ -1,3 +0,2 @@ | ||
| export * from "@shikijs/types" | ||
| export { }; | ||
| export * from "@shikijs/types"; | ||
| export {}; |
+3
-3
| { | ||
| "name": "@shikijs/core", | ||
| "type": "module", | ||
| "version": "4.0.2", | ||
| "version": "4.1.0", | ||
| "description": "Core of Shiki", | ||
@@ -52,4 +52,4 @@ "author": "Pine Wu <octref@gmail.com>; Anthony Fu <anthonyfu117@hotmail.com>", | ||
| "hast-util-to-html": "^9.0.5", | ||
| "@shikijs/primitive": "4.0.2", | ||
| "@shikijs/types": "4.0.2" | ||
| "@shikijs/primitive": "4.1.0", | ||
| "@shikijs/types": "4.1.0" | ||
| }, | ||
@@ -56,0 +56,0 @@ "scripts": { |
63841
0.32%1415
0.86%+ Added
+ Added
- Removed
- Removed
Updated
Updated