From ca7dc02d88f43498ef0d2af3723e236639d98f90 Mon Sep 17 00:00:00 2001 From: jialin Date: Fri, 20 Jun 2025 11:17:24 +0800 Subject: [PATCH] revert: markdown katex parse --- src/components/markdown-viewer/utils.ts | 68 ++++++------------------- 1 file changed, 16 insertions(+), 52 deletions(-) diff --git a/src/components/markdown-viewer/utils.ts b/src/components/markdown-viewer/utils.ts index d21d8d99..a9b367a6 100644 --- a/src/components/markdown-viewer/utils.ts +++ b/src/components/markdown-viewer/utils.ts @@ -32,60 +32,24 @@ export function escapeDollarNumber(text: string) { return escapedText; } -export function escapeBrackets(text: string): string { - const codeRegex = /```[\s\S]*?```|`[^`\n]+`/g; - - const codeBlocks: string[] = []; - const placeholder = '%%CODE_BLOCK%%'; - - // 1. replace code blocks with a placeholder - // and store them in an array - const temp = text.replace(codeRegex, (match) => { - codeBlocks.push(match); - return placeholder; - }); - - // 2. repace \[...\] with $$...$$,and replace \(...\) with $...$ - const replaced = temp - .replace(/\\\[([\s\S]*?)\\\]/g, (_, inner) => `$$${inner}$$`) - .replace(/\\\(([\s\S]*?)\\\)/g, (_, inner) => `$${inner}$`); - - // 3. restore code blocks from the placeholder - let i = 0; - return replaced.replace( - new RegExp(placeholder, 'g'), - () => codeBlocks[i++] || '' +export function escapeBrackets(text: string) { + const pattern = + /(```[\S\s]*?```|`.*?`)|\\\[([\S\s]*?[^\\])\\]|\\\((.*?)\\\)/g; + return text.replaceAll( + pattern, + (match, codeBlock, squareBracket, roundBracket) => { + if (codeBlock) { + return codeBlock; + } else if (squareBracket) { + return `$$${squareBracket}$$`; + } else if (roundBracket) { + return `$${roundBracket}$`; + } + return match; + } ); } -export function escapeMhchem_2(text: string) { +export function escapeMhchem(text: string) { return text.replaceAll('$\\ce{', '$\\\\ce{').replaceAll('$\\pu{', '$\\\\pu{'); } - -export function escapeMhchem(text: string): string { - const codeRegex = /```[\s\S]*?```|`[^`\n]+`/g; - const codeBlocks: string[] = []; - const placeholder = '%%CODE_BLOCK%%'; - - // 1. Replace code blocks with a placeholder - const temp = text.replace(codeRegex, (match) => { - codeBlocks.push(match); - return placeholder; - }); - - // 2. Only perform \ce{} / \pu{} escaping inside $...$ (to avoid incorrect escaping outside math environments) - const mathRegex = /\$(.+?)\$/gs; - const escaped = temp.replace(mathRegex, (_, content) => { - const fixed = content - .replace(/(? codeBlocks[i++] || '' - ); -}