fix(ui): load shiki languages from marked tokens
This commit is contained in:
@@ -127,17 +127,23 @@ async function ensureLanguages(content: string) {
|
|||||||
if (highlightSuppressed) {
|
if (highlightSuppressed) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Parse code fences to extract language tokens
|
|
||||||
// Updated regex to capture optional language tokens and handle trailing annotations
|
|
||||||
const codeBlockRegex = /```[ \t]*([A-Za-z0-9_.+#-]+)?[^`]*?```/g
|
|
||||||
const foundLanguages = new Set<string>()
|
|
||||||
let match
|
|
||||||
|
|
||||||
while ((match = codeBlockRegex.exec(content)) !== null) {
|
// Extract code-fence language tokens via `marked` so we correctly handle code blocks
|
||||||
const langToken = match[1]
|
// that contain backticks (e.g. JS template literals). Regex-based fence scans tend
|
||||||
if (langToken && langToken.trim()) {
|
// to miss these and prevent languages from loading.
|
||||||
foundLanguages.add(langToken.trim())
|
const foundLanguages = new Set<string>()
|
||||||
}
|
try {
|
||||||
|
const tokens = marked.lexer(content) as any
|
||||||
|
marked.walkTokens(tokens, (token: any) => {
|
||||||
|
if (token?.type !== "code") return
|
||||||
|
const langToken = typeof token.lang === "string" ? token.lang : ""
|
||||||
|
if (langToken.trim()) {
|
||||||
|
foundLanguages.add(langToken.trim())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch {
|
||||||
|
// If tokenization fails for any reason, skip language preloading.
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Queue language loading tasks
|
// Queue language loading tasks
|
||||||
|
|||||||
Reference in New Issue
Block a user