From a457c9d128fe3a82edee9c5a23a58782ac652ceb Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 9 May 2022 12:20:17 +0800 Subject: [PATCH] Update article content only when code blocks exist --- packages/api/src/utils/parser.ts | 35 +++++++++++++++++--------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/packages/api/src/utils/parser.ts b/packages/api/src/utils/parser.ts index 2472988b2..f39f11794 100644 --- a/packages/api/src/utils/parser.ts +++ b/packages/api/src/utils/parser.ts @@ -255,22 +255,25 @@ export const parsePreparedContent = async ( // TODO: we probably want to move this type of thing // to the handlers, and have some concept of postHandle if (article?.dom) { - article.dom.querySelectorAll('code').forEach((e) => { - console.log(e.textContent) - if (e.textContent) { - const att = hljs.highlightAuto(e.textContent) - const code = window.document.createElement('code') - const langClass = - `hljs language-${att.language}` + - (att.second_best?.language - ? ` language-${att.second_best?.language}` - : '') - code.setAttribute('class', langClass) - code.innerHTML = att.value - e.replaceWith(code) - } - }) - article.content = article.dom.outerHTML + const codeBlocks = article.dom.querySelectorAll('code') + if (codeBlocks.length > 0) { + codeBlocks.forEach((e) => { + console.log(e.textContent) + if (e.textContent) { + const att = hljs.highlightAuto(e.textContent) + const code = window.document.createElement('code') + const langClass = + `hljs language-${att.language}` + + (att.second_best?.language + ? ` language-${att.second_best?.language}` + : '') + code.setAttribute('class', langClass) + code.innerHTML = att.value + e.replaceWith(code) + } + }) + article.content = article.dom.outerHTML + } } const newWindow = new JSDOM('').window