From dbfb450e23285cad458d25655e3df7347e8e18d9 Mon Sep 17 00:00:00 2001 From: Zaidhaan Date: Thu, 2 Mar 2023 22:27:32 +0800 Subject: [PATCH] fix: allow parsing codeblocks with multiple text nodes in children (#1842) --- composables/content-parse.ts | 7 +++++++ composables/content-render.ts | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/composables/content-parse.ts b/composables/content-parse.ts index 3bab8947..06caa50b 100644 --- a/composables/content-parse.ts +++ b/composables/content-parse.ts @@ -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 = '' diff --git a/composables/content-render.ts b/composables/content-render.ts index 0bdd294f..eec93848 100644 --- a/composables/content-render.ts +++ b/composables/content-render.ts @@ -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) }) } }