forked from Mirrors/elk
fix: allow parsing codeblocks with multiple text nodes in children (#1842)
This commit is contained in:
parent
f3ad179c69
commit
dbfb450e23
2 changed files with 8 additions and 1 deletions
|
@ -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 {
|
||||
let pre = ''
|
||||
let body = ''
|
||||
|
|
|
@ -107,7 +107,7 @@ function handleCodeBlock(el: Node) {
|
|||
const codeEl = el.children[0] as Node
|
||||
const classes = codeEl.attributes.class as string
|
||||
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) })
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue