From fddc34ac82356338118faf4c8da373030d5bb1fc Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Thu, 14 Nov 2024 09:37:31 +0800 Subject: [PATCH] refactor: optimize mdUtil --- src/util/mdUtil.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/util/mdUtil.ts b/src/util/mdUtil.ts index 9105f24..ab8b31a 100644 --- a/src/util/mdUtil.ts +++ b/src/util/mdUtil.ts @@ -27,9 +27,18 @@ const turndownService = new TurndownService({ export const mdToHtml = (markdown: string) => { if (!markdown) return markdown; - const html = md.render(markdown).trim(); - if (html.startsWith("

") && html.endsWith("

")) { - return html.substring(3, html.length - 4); + const renderHtml = md.render(markdown).trim(); + if (!renderHtml) return markdown; + const parser = new DOMParser(); + const doc = parser.parseFromString(renderHtml, 'text/html'); + let html = ''; + for (let i = 0; i < doc.body.children.length; i++) { + const child = doc.body.children[i]; + if (i == 0 && child.tagName === "P") { + html += child.innerHTML; + } else { + html += child.outerHTML; + } } return html; }