From 42ae85fe5239f4d19fd748d42bedaea4fd300147 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Tue, 2 Jul 2024 08:20:45 +0900 Subject: [PATCH] fix rendermarkdown --- src/ts/parser.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/ts/parser.ts b/src/ts/parser.ts index 72d83fb8..d28d3232 100644 --- a/src/ts/parser.ts +++ b/src/ts/parser.ts @@ -77,8 +77,9 @@ DOMPurify.addHook("uponSanitizeAttribute", (node, data) => { } }) -function renderMarkdown(data:string){ - return md.render(data) + +function renderMarkdown(md:markdownit, data:string){ + return md.render(data.replace(/“|”/g, '"').replace(/‘|’/g, "'")) .replace(/\uE9b0/gu, '“') .replace(/\uE9b1/gu, '”') .replace(/\uE9b2/gu, '‘') @@ -86,11 +87,7 @@ function renderMarkdown(data:string){ } async function renderHighlightableMarkdown(data:string) { - let rendered = mdHighlight.render(data) - .replace(/\uE9b0/gu, '“') - .replace(/\uE9b1/gu, '”') - .replace(/\uE9b2/gu, '‘') - .replace(/\uE9b3/gu, '’') + let rendered = renderMarkdown(mdHighlight, data) console.log(rendered) const highlightPlaceholders = rendered.match(/(.+?)<\/pre-hljs-placeholder>/gms) console.log(highlightPlaceholders) @@ -389,7 +386,7 @@ export async function postTranslationParse(data:string){ } export function parseMarkdownSafe(data:string) { - return DOMPurify.sanitize(renderMarkdown(data), { + return DOMPurify.sanitize(renderMarkdown(md, data), { FORBID_TAGS: ["a", "style"], FORBID_ATTR: ["style", "href", "class"] }) @@ -2470,7 +2467,7 @@ export function applyMarkdownToNode(node: Node) { if (node.nodeType === Node.TEXT_NODE) { const text = node.textContent; if (text) { - let markdown = renderMarkdown(text); + let markdown = renderMarkdown(md, text); if (markdown !== text) { const span = document.createElement('span'); span.innerHTML = markdown;