From 5a94c8c61443c01400711dc2c956649f1db68765 Mon Sep 17 00:00:00 2001 From: Arthals <36695271+zhuozhiyongde@users.noreply.github.com> Date: Thu, 25 Jul 2024 14:38:33 +0800 Subject: [PATCH] :bug: fix: Incorrect math parser (#186) --- src/Markdown/demos/data.ts | 7 +++++++ src/Markdown/index.tsx | 4 ++-- src/Markdown/utils.ts | 40 -------------------------------------- 3 files changed, 9 insertions(+), 42 deletions(-) diff --git a/src/Markdown/demos/data.ts b/src/Markdown/demos/data.ts index bf5b3af8..b4ba9084 100644 --- a/src/Markdown/demos/data.ts +++ b/src/Markdown/demos/data.ts @@ -95,6 +95,13 @@ $$ $$ \\int_{a}^{b} f(x) \\, dx $$ + +--- + +我是一个嵌套测试: +\`\`\` +$1 +\`\`\` `; export const content2 = `# Customize Markdown Components diff --git a/src/Markdown/index.tsx b/src/Markdown/index.tsx index 1edf5261..24a7bb3e 100644 --- a/src/Markdown/index.tsx +++ b/src/Markdown/index.tsx @@ -21,7 +21,7 @@ import { CodeFullFeatured, CodeLite } from './CodeBlock'; import type { TypographyProps } from './Typography'; import { useStyles as useMarkdownStyles } from './markdown.style'; import { useStyles } from './style'; -import { escapeBrackets, escapeDollarNumber, escapeMhchem } from './utils'; +import { escapeBrackets, escapeMhchem } from './utils'; export interface MarkdownProps extends TypographyProps { allowHtml?: boolean; @@ -65,7 +65,7 @@ const Markdown = memo( const escapedContent = useMemo(() => { if (!enableLatex) return children; - return escapeMhchem(escapeBrackets(escapeDollarNumber(children))); + return escapeMhchem(escapeBrackets(children)); }, [children, enableLatex]); const components: Components = useMemo( diff --git a/src/Markdown/utils.ts b/src/Markdown/utils.ts index 0f820b79..0dc7479c 100644 --- a/src/Markdown/utils.ts +++ b/src/Markdown/utils.ts @@ -1,43 +1,3 @@ -export function escapeDollarNumber(text: string): string { - let escapedText = ''; - let inCodeBlock = false; - let inSingleLineCodeBlock = false; - let i = 0; - while (i < text.length) { - let char = text[i]; - if (char === '`') { - let tickCount = 1; - while (text[i + tickCount] === '`') { - tickCount++; - } - if (tickCount === 3) { - inCodeBlock = !inCodeBlock; - escapedText += '```'; - i += tickCount; - continue; - } else if (tickCount === 1) { - inSingleLineCodeBlock = !inSingleLineCodeBlock; - escapedText += '`'; - i += tickCount; - continue; - } - } - if ( - !inCodeBlock && - !inSingleLineCodeBlock && - char === '$' && - i + 1 < text.length && - text[i + 1] >= '0' && - text[i + 1] <= '9' - ) { - char = '\\$'; - } - escapedText += char; - i++; - } - return escapedText; -} - export function escapeBrackets(text: string) { const pattern = /(```[\S\s]*?```|`.*?`)|\\\[([\S\s]*?[^\\])\\]|\\\((.*?)\\\)/g; return text.replaceAll(pattern, (match, codeBlock, squareBracket, roundBracket) => {