From 1847b6c7be66ea261a331e86712c64f8f9b2b402 Mon Sep 17 00:00:00 2001 From: Tony Giorgio Date: Thu, 10 Apr 2025 12:13:51 -0500 Subject: [PATCH] Simplify markdown without groups --- frontend/src/components/markdown.tsx | 38 ++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/frontend/src/components/markdown.tsx b/frontend/src/components/markdown.tsx index 0d23d387f..c659775ff 100644 --- a/frontend/src/components/markdown.tsx +++ b/frontend/src/components/markdown.tsx @@ -95,18 +95,36 @@ function escapeDollarNumber(text: string) { return escapedText; } -function escapeBrackets(text: string) { - const pattern = /(```[\s\S]*?```|`.*?`)|\\\[([\s\S]*?[^\\])\\\]|\\\((.*?)\\\)/g; - return text.replace(pattern, (match, codeBlock, squareBracket, roundBracket) => { - if (codeBlock) { - return codeBlock; - } else if (squareBracket) { - return `$$${squareBracket}$$`; - } else if (roundBracket) { - return `$${roundBracket}$`; +function escapeBrackets(text: string): string { + // First, handle code blocks to protect them from processing + const codeBlockPattern = /```[\s\S]*?```|`.*?`/g; + const codeBlocks: string[] = []; + const textWithoutCode = text.replace(codeBlockPattern, function (match: string) { + codeBlocks.push(match); + return `__CODE_BLOCK_${codeBlocks.length - 1}__`; + }); + + // Process escaped square brackets - LaTeX display mode + const squareBracketPattern = /\\\[([\s\S]*?[^\\])\\\]/g; + let result = textWithoutCode.replace( + squareBracketPattern, + function (_match: string, content: string) { + return `$$${content}$$`; } - return match; + ); + + // Process escaped parentheses - LaTeX inline mode + const roundBracketPattern = /\\\((.*?)\\\)/g; + result = result.replace(roundBracketPattern, function (_match: string, content: string) { + return `$${content}$`; }); + + // Restore code blocks + codeBlocks.forEach(function (block: string, i: number) { + result = result.replace(`__CODE_BLOCK_${i}__`, block); + }); + + return result; } function MarkDownContentToMemo(props: { content: string }) {