From cc759bef5601c419d89e527cad69a08373b09599 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 15 Oct 2022 06:11:17 +0800 Subject: [PATCH] fix: handle highlight unknown language error --- web/src/labs/marked/parser/CodeBlock.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/web/src/labs/marked/parser/CodeBlock.ts b/web/src/labs/marked/parser/CodeBlock.ts index ca5afb3a..52e94540 100644 --- a/web/src/labs/marked/parser/CodeBlock.ts +++ b/web/src/labs/marked/parser/CodeBlock.ts @@ -10,11 +10,18 @@ const renderer = (rawStr: string): string => { } const language = escape(matchResult[1]) || "plaintext"; - const highlightedCodes = hljs.highlight(matchResult[2], { - language, - }).value; + let highlightedCode = hljs.highlightAuto(matchResult[2]).value; - return `
${highlightedCodes}
${matchResult[3]}`; + try { + const temp = hljs.highlight(matchResult[2], { + language, + }).value; + highlightedCode = temp; + } catch (error) { + // do nth + } + + return `
${highlightedCode}
${matchResult[3]}`; }; export default {