rehype-pretty-code
Advanced tools
Comparing version 0.13.2 to 0.14.0
@@ -21,2 +21,3 @@ import { Element, Properties, Text, Root } from 'hast'; | ||
keepBackground?: boolean; | ||
bypassInlineCode?: boolean; | ||
defaultLang?: string | { | ||
@@ -23,0 +24,0 @@ block?: string; |
@@ -18,3 +18,6 @@ import { getHighlighter } from 'shiki'; | ||
} | ||
function isInlineCode(element, parent) { | ||
function isInlineCode(element, parent, bypass = false) { | ||
if (bypass) { | ||
return false; | ||
} | ||
return element.tagName === "code" && isElement(parent) && parent.tagName !== "pre" || element.tagName === "inlineCode"; | ||
@@ -34,3 +37,2 @@ } | ||
let meta = filter( | ||
// @ts-expect-error: TODO handle this | ||
element.data?.meta ?? element.properties?.metastring ?? "" | ||
@@ -75,8 +77,6 @@ ); | ||
const segments = meta.match(/\{[^}]+\}#[a-zA-Z0-9]+/g); | ||
if (!segments) | ||
return null; | ||
if (!segments) return null; | ||
for (const segment of segments) { | ||
const [range, id] = segment.split("#"); | ||
if (!(range && id)) | ||
continue; | ||
if (!(range && id)) continue; | ||
const match = range.match(/\{(.*?)\}/); | ||
@@ -161,4 +161,3 @@ const capture = match?.[1]; | ||
function getContent(node) { | ||
if (!node) | ||
return; | ||
if (!node) return; | ||
return toString(node); | ||
@@ -351,4 +350,3 @@ } | ||
); | ||
if (elementsToWrap.length === 0) | ||
break; | ||
if (elementsToWrap.length === 0) break; | ||
wrapHighlightedChars( | ||
@@ -375,4 +373,3 @@ element, | ||
element.children.forEach((childNode) => { | ||
if (!isElement(childNode)) | ||
return; | ||
if (!isElement(childNode)) return; | ||
if (Object.hasOwn(childNode.properties, "rehype-pretty-code-visited")) { | ||
@@ -401,3 +398,3 @@ childNode.properties["rehype-pretty-code-visited"] = void 0; | ||
const codeData = element.children[0]?.data; | ||
element.children = [tree].map((tree2) => { | ||
element.children = [tree].flatMap((tree2) => { | ||
const pre = tree2.children[0]; | ||
@@ -474,3 +471,3 @@ const themeNames = getThemeNames(theme); | ||
return fragments; | ||
}).flatMap((c) => c); | ||
}); | ||
} | ||
@@ -485,2 +482,3 @@ var globalHighlighterCache = /* @__PURE__ */ new Map(); | ||
keepBackground = true, | ||
bypassInlineCode = false, | ||
defaultLang = "", | ||
@@ -522,12 +520,9 @@ tokensMap = {}, | ||
const highlighter = await cachedHighlighter; | ||
if (!highlighter) | ||
return; | ||
if (!highlighter) return; | ||
visit(tree, "element", (element, _, parent) => { | ||
if (isInlineCode(element, parent)) { | ||
if (isInlineCode(element, parent, bypassInlineCode)) { | ||
const textElement = element.children[0]; | ||
if (!isText(textElement)) | ||
return; | ||
if (!isText(textElement)) return; | ||
const value = textElement.value; | ||
if (!value) | ||
return; | ||
if (!value) return; | ||
const lang = getInlineCodeLang(value, defaultInlineCodeLang); | ||
@@ -540,4 +535,3 @@ if (lang && lang[0] !== ".") { | ||
const codeElement = element.children[0]; | ||
if (!isElement(codeElement)) | ||
return; | ||
if (!isElement(codeElement)) return; | ||
const { lang } = parseBlockMetaString( | ||
@@ -569,9 +563,7 @@ codeElement, | ||
visit(tree, "element", (element, _, parent) => { | ||
if (isInlineCode(element, parent)) { | ||
if (isInlineCode(element, parent, bypassInlineCode)) { | ||
const textElement = element.children[0]; | ||
if (!isText(textElement)) | ||
return; | ||
if (!isText(textElement)) return; | ||
const value = textElement.value; | ||
if (!value) | ||
return; | ||
if (!value) return; | ||
const keepLangPart = /\\{:[a-zA-Z.-]+}$/.test(value); | ||
@@ -582,4 +574,3 @@ const strippedValue = keepLangPart ? value.replace(/\\({:[a-zA-Z.-]+})$/, "$1") : value.replace(/{:[a-zA-Z.-]+}$/, ""); | ||
const isLang = lang[0] !== "."; | ||
if (!lang) | ||
return; | ||
if (!lang) return; | ||
let codeTree; | ||
@@ -626,4 +617,3 @@ if (isLang) { | ||
const codeElement = element.children[0]; | ||
if (!isElement(codeElement)) | ||
return; | ||
if (!isElement(codeElement)) return; | ||
const textElement = codeElement.children[0]; | ||
@@ -635,4 +625,3 @@ const { title, caption, meta, lang } = parseBlockMetaString( | ||
); | ||
if (!lang || lang === "math") | ||
return; | ||
if (!lang || lang === "math") return; | ||
const lineNumbers = []; | ||
@@ -674,4 +663,3 @@ if (meta) { | ||
} | ||
if (!isText(textElement)) | ||
return; | ||
if (!isText(textElement)) return; | ||
const strippedValue = textElement.value.replace(/\n$/, ""); | ||
@@ -678,0 +666,0 @@ let codeTree; |
{ | ||
"name": "rehype-pretty-code", | ||
"version": "0.13.2", | ||
"version": "0.14.0", | ||
"description": "Beautiful code for your MD/MDX docs.", | ||
@@ -30,3 +30,3 @@ "homepage": "https://rehype-pretty.pages.dev", | ||
"rehype-parse": "^9.0.0", | ||
"unified": "^11.0.4", | ||
"unified": "^11.0.5", | ||
"unist-util-visit": "^5.0.0" | ||
@@ -36,13 +36,13 @@ }, | ||
"@arethetypeswrong/cli": "^0.15.3", | ||
"@shikijs/transformers": "^1.5.1", | ||
"@types/node": "^20.12.11", | ||
"@shikijs/transformers": "^1.9.1", | ||
"@types/node": "^20.14.9", | ||
"hast-util-to-html": "^9.0.1", | ||
"mdast-util-to-hast": "^13.1.0", | ||
"prettier": "^3.2.5", | ||
"mdast-util-to-hast": "^13.2.0", | ||
"prettier": "^3.3.2", | ||
"remark": "^15.0.1", | ||
"shiki": "^1.5.1", | ||
"tsup": "^8.0.2", | ||
"tsx": "^4.10.2", | ||
"typescript": "^5.4.5", | ||
"vite": "^5.2.11", | ||
"shiki": "^1.9.1", | ||
"tsup": "^8.1.0", | ||
"tsx": "^4.15.7", | ||
"typescript": "^5.5.2", | ||
"vite": "^5.3.1", | ||
"vitest": "^1.6.0" | ||
@@ -49,0 +49,0 @@ }, |
29267
757
Updatedunified@^11.0.5