rehype-pretty-code
Advanced tools
Comparing version 0.13.0 to 0.13.1
@@ -7,3 +7,3 @@ import { Element, Properties, Text, Root } from 'hast'; | ||
properties: Properties & { | ||
className?: string[]; | ||
className?: Array<string>; | ||
}; | ||
@@ -13,3 +13,3 @@ }; | ||
properties: Properties & { | ||
className?: string[]; | ||
className?: Array<string>; | ||
}; | ||
@@ -28,3 +28,3 @@ children: Array<Element | Text>; | ||
tokensMap?: Record<string, string>; | ||
transformers?: ShikiTransformer[]; | ||
transformers?: Array<ShikiTransformer>; | ||
filterMetaString?(str: string): string; | ||
@@ -31,0 +31,0 @@ getHighlighter?(options: BundledHighlighterOptions<any, any>): Promise<Highlighter>; |
@@ -77,3 +77,3 @@ import { getHighlighter } from 'shiki'; | ||
const [range, id] = segment.split("#"); | ||
if (!range || !id) | ||
if (!(range && id)) | ||
continue; | ||
@@ -400,3 +400,3 @@ const match = range.match(/\{(.*?)\}/); | ||
const themeNamesString = themeNames.join(" "); | ||
if (!isElement(pre) || !pre.properties) { | ||
if (!(isElement(pre) && pre.properties)) { | ||
return []; | ||
@@ -416,3 +416,3 @@ } | ||
pre.properties["data-theme"] = themeNamesString; | ||
if (!isElement(code) || !code.properties) { | ||
if (!(isElement(code) && code.properties)) { | ||
return []; | ||
@@ -472,3 +472,3 @@ } | ||
} | ||
var globalHighlighterCache = /* @__PURE__ */ new WeakMap(); | ||
var globalHighlighterCache = /* @__PURE__ */ new Map(); | ||
var hastParser = unified().use(rehypeParse, { fragment: true }); | ||
@@ -491,3 +491,4 @@ function rehypePrettyCode(options = {}) { | ||
} = options; | ||
let cachedHighlighter = globalHighlighterCache.get(options); | ||
const key = JSON.stringify(theme); | ||
let cachedHighlighter = globalHighlighterCache.get(key); | ||
if (!cachedHighlighter) { | ||
@@ -498,3 +499,3 @@ cachedHighlighter = getHighlighter$1({ | ||
}); | ||
globalHighlighterCache.set(options, cachedHighlighter); | ||
globalHighlighterCache.set(key, cachedHighlighter); | ||
} | ||
@@ -577,3 +578,13 @@ const defaultCodeBlockLang = typeof defaultLang === "string" ? defaultLang : defaultLang.block || ""; | ||
let codeTree; | ||
if (!isLang) { | ||
if (isLang) { | ||
try { | ||
codeTree = hastParser.parse( | ||
highlighter.codeToHtml(strippedValue, getOptions(lang)) | ||
); | ||
} catch { | ||
codeTree = hastParser.parse( | ||
highlighter.codeToHtml(strippedValue, getOptions("plaintext")) | ||
); | ||
} | ||
} else { | ||
const themeNames = getThemeNames(theme); | ||
@@ -589,3 +600,3 @@ const isMultiTheme = typeof theme === "object" && !isJSONTheme(theme); | ||
codeTree = hastParser.parse( | ||
`<pre><code><span style="${themeKeys.map((key, i) => `--shiki-${key}:${colorsByTheme[i]}`).join(";")}">${strippedValue}</span></code></pre>` | ||
`<pre><code><span style="${themeKeys.map((key2, i) => `--shiki-${key2}:${colorsByTheme[i]}`).join(";")}">${strippedValue}</span></code></pre>` | ||
); | ||
@@ -597,12 +608,2 @@ } else { | ||
} | ||
} else { | ||
try { | ||
codeTree = hastParser.parse( | ||
highlighter.codeToHtml(strippedValue, getOptions(lang)) | ||
); | ||
} catch (e) { | ||
codeTree = hastParser.parse( | ||
highlighter.codeToHtml(strippedValue, getOptions("plaintext")) | ||
); | ||
} | ||
} | ||
@@ -674,3 +675,3 @@ visit(codeTree, "element", replaceLineClass); | ||
); | ||
} catch (e) { | ||
} catch { | ||
codeTree = hastParser.parse( | ||
@@ -677,0 +678,0 @@ highlighter.codeToHtml( |
{ | ||
"name": "rehype-pretty-code", | ||
"version": "0.13.0", | ||
"version": "0.13.1", | ||
"description": "Beautiful code for your MD/MDX docs.", | ||
"homepage": "https://rehype-pretty-code.netlify.app", | ||
"homepage": "https://rehype-pretty.pages.dev", | ||
"type": "module", | ||
@@ -34,3 +34,3 @@ "main": "./dist/index.js", | ||
"prepublishOnly": "NODE_ENV='production' bun run build", | ||
"check-package": "bunx publint@latest --strict && bunx @arethetypeswrong/cli --pack --ignore-rules cjs-resolves-to-esm" | ||
"check-package": "bunx publint@latest --strict && bunx attw --pack --ignore-rules cjs-resolves-to-esm" | ||
}, | ||
@@ -46,18 +46,20 @@ "dependencies": { | ||
"devDependencies": { | ||
"@biomejs/biome": "^1.5.3", | ||
"@types/bun": "^1.0.5", | ||
"@types/node": "^20.11.16", | ||
"bumpp": "^9.3.0", | ||
"bun": "^1.0.26", | ||
"@arethetypeswrong/cli": "^0.15.2", | ||
"@biomejs/biome": "^1.6.3", | ||
"@shikijs/transformers": "^1.2.1", | ||
"@types/bun": "^1.0.11", | ||
"@types/node": "^20.11.30", | ||
"bumpp": "^9.4.0", | ||
"bun": "^1.0.35", | ||
"hast-util-to-html": "^9.0.0", | ||
"husky": "^9.0.10", | ||
"husky": "^9.0.11", | ||
"mdast-util-to-hast": "^13.1.0", | ||
"prettier": "^3.2.5", | ||
"remark": "^15.0.1", | ||
"tsup": "^8.0.1", | ||
"typescript": "^5.3.3", | ||
"vite": "^5.0.12", | ||
"vitest": "^1.2.2", | ||
"@shikijs/transformers": "^1.0.0", | ||
"shiki": "^1.0.0" | ||
"shiki": "^1.2.1", | ||
"tsup": "^8.0.2", | ||
"tsx": "^4.7.1", | ||
"typescript": "^5.4.3", | ||
"vite": "^5.2.6", | ||
"vitest": "^1.4.0" | ||
}, | ||
@@ -64,0 +66,0 @@ "peerDependencies": { |
@@ -5,2 +5,2 @@ # Rehype Pretty Code | ||
[View website for documentation](https://rehype-pretty-code.netlify.app) | ||
[View website for documentation](https://rehype-pretty.pages.dev) |
29451
767
18