Skip to content

Commit

Permalink
refactor: optimize mdUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfuhai committed Nov 14, 2024
1 parent d4568f6 commit fddc34a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/util/mdUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("<p>") && html.endsWith("</p>")) {
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;
}
Expand Down

0 comments on commit fddc34a

Please sign in to comment.