1
0

fix: allow parsing codeblocks with multiple text nodes in children (#1842)

This commit is contained in:
Zaidhaan 2023-03-02 22:27:32 +08:00 committed by GitHub
parent f3ad179c69
commit dbfb450e23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -162,6 +162,13 @@ export function htmlToText(html: string) {
} }
} }
export function recursiveTreeToText(input: Node): string {
if (input && input.children && input.children.length > 0)
return input.children.map((n: Node) => recursiveTreeToText(n)).join('')
else
return treeToText(input)
}
export function treeToText(input: Node): string { export function treeToText(input: Node): string {
let pre = '' let pre = ''
let body = '' let body = ''

View File

@ -107,7 +107,7 @@ function handleCodeBlock(el: Node) {
const codeEl = el.children[0] as Node const codeEl = el.children[0] as Node
const classes = codeEl.attributes.class as string const classes = codeEl.attributes.class as string
const lang = classes?.split(/\s/g).find(i => i.startsWith('language-'))?.replace('language-', '') const lang = classes?.split(/\s/g).find(i => i.startsWith('language-'))?.replace('language-', '')
const code = codeEl.children[0] ? treeToText(codeEl.children[0]) : '' const code = codeEl.children && codeEl.children.length > 0 ? recursiveTreeToText(codeEl) : ''
return h(ContentCode, { lang, code: encodeURIComponent(code) }) return h(ContentCode, { lang, code: encodeURIComponent(code) })
} }
} }