From 524c9beee4fa02323e4dde295d64c2d224fe3186 Mon Sep 17 00:00:00 2001 From: "l.tingting" Date: Sat, 2 Mar 2024 11:08:34 +0800 Subject: [PATCH] support \(...\) and \[...\] style math formula --- app/components/markdown.tsx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index f3a916cc535..7c70fe1a5ac 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -116,9 +116,27 @@ 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}$`; + } + return match; + }, + ); +} + function _MarkDownContent(props: { content: string }) { const escapedContent = useMemo( - () => escapeDollarNumber(props.content), + () => escapeBrackets(escapeDollarNumber(props.content)), [props.content], );