From 303a6df853c44672957e97958b80522a458ee42a Mon Sep 17 00:00:00 2001 From: canisminor1990 Date: Mon, 24 Jun 2024 13:43:34 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=9B=20fix:=20Fix=20LaTeX?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Markdown/index.tsx | 10 +++++++--- src/Markdown/preprocessLaTeX.ts | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 src/Markdown/preprocessLaTeX.ts diff --git a/src/Markdown/index.tsx b/src/Markdown/index.tsx index 9cbd6e73..52494091 100644 --- a/src/Markdown/index.tsx +++ b/src/Markdown/index.tsx @@ -11,6 +11,7 @@ import remarkGfm from 'remark-gfm'; import remarkMath from 'remark-math'; import ImageGallery from '@/Image/ImageGallery'; +import { preprocessLaTeX } from '@/Markdown/preprocessLaTeX'; import Image, { type ImageProps } from '@/mdx/Image'; import Link from '@/mdx/Link'; import type { PreProps } from '@/mdx/Pre'; @@ -33,6 +34,7 @@ export interface MarkdownProps extends TypographyProps { video?: Partial; }; enableImageGallery?: boolean; + enableLatex?: boolean; fullFeaturedCodeBlock?: boolean; onDoubleClick?: () => void; style?: CSSProperties; @@ -46,6 +48,7 @@ const Markdown = memo( style, fullFeaturedCodeBlock, onDoubleClick, + enableLatex = true, enableImageGallery = true, componentProps, allowHtml, @@ -88,11 +91,12 @@ const Markdown = memo( ); const rehypePlugins = useMemo( - () => [allowHtml && rehypeRaw, rehypeKatex].filter(Boolean) as any, + () => [allowHtml && rehypeRaw, enableLatex && rehypeKatex].filter(Boolean) as any, [allowHtml], ); const remarkPlugins = useMemo( - () => [remarkGfm, remarkMath, isChatMode && remarkBreaks].filter(Boolean) as any, + () => + [remarkGfm, enableLatex && remarkMath, isChatMode && remarkBreaks].filter(Boolean) as any, [isChatMode], ); @@ -128,7 +132,7 @@ const Markdown = memo( remarkPlugins={remarkPlugins} {...rest} > - {children} + {enableLatex ? preprocessLaTeX(children) : children} diff --git a/src/Markdown/preprocessLaTeX.ts b/src/Markdown/preprocessLaTeX.ts new file mode 100644 index 00000000..424530fb --- /dev/null +++ b/src/Markdown/preprocessLaTeX.ts @@ -0,0 +1,14 @@ +export const preprocessLaTeX = (content: string) => { + // Replace block-level LaTeX delimiters \[ \] with $$ $$ + + const blockProcessedContent = content.replaceAll( + /\\\[(.*?)\\]/gs, + (_, equation) => `$$${equation}$$`, + ); + // Replace inline LaTeX delimiters \( \) with $ $ + const inlineProcessedContent = blockProcessedContent.replaceAll( + /\\\((.*?)\\\)/gs, + (_, equation) => `$${equation}$`, + ); + return inlineProcessedContent; +}; From a46d3a85ad7cf89787f134501c9b45ff87ea8a1c Mon Sep 17 00:00:00 2001 From: canisminor1990 Date: Mon, 24 Jun 2024 14:00:36 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=90=9B=20fix:=20Fix=20escapeDollarNum?= =?UTF-8?q?ber?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Markdown/index.tsx | 9 +++++++-- src/Markdown/preprocessLaTeX.ts | 14 -------------- src/Markdown/utils.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 16 deletions(-) delete mode 100644 src/Markdown/preprocessLaTeX.ts create mode 100644 src/Markdown/utils.ts diff --git a/src/Markdown/index.tsx b/src/Markdown/index.tsx index 52494091..856d233c 100644 --- a/src/Markdown/index.tsx +++ b/src/Markdown/index.tsx @@ -11,7 +11,6 @@ import remarkGfm from 'remark-gfm'; import remarkMath from 'remark-math'; import ImageGallery from '@/Image/ImageGallery'; -import { preprocessLaTeX } from '@/Markdown/preprocessLaTeX'; import Image, { type ImageProps } from '@/mdx/Image'; import Link from '@/mdx/Link'; import type { PreProps } from '@/mdx/Pre'; @@ -22,6 +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 } from './utils'; export interface MarkdownProps extends TypographyProps { allowHtml?: boolean; @@ -63,6 +63,11 @@ const Markdown = memo( const { styles: mdStyles } = useMarkdownStyles({ fontSize, headerMultiple, marginMultiple }); const isChatMode = variant === 'chat'; + const escapedContent = useMemo(() => { + if (!enableLatex) return children; + return escapeBrackets(escapeDollarNumber(children)); + }, [children, enableLatex]); + const components: Components = useMemo( () => ({ a: (props: any) => , @@ -132,7 +137,7 @@ const Markdown = memo( remarkPlugins={remarkPlugins} {...rest} > - {enableLatex ? preprocessLaTeX(children) : children} + {escapedContent} diff --git a/src/Markdown/preprocessLaTeX.ts b/src/Markdown/preprocessLaTeX.ts deleted file mode 100644 index 424530fb..00000000 --- a/src/Markdown/preprocessLaTeX.ts +++ /dev/null @@ -1,14 +0,0 @@ -export const preprocessLaTeX = (content: string) => { - // Replace block-level LaTeX delimiters \[ \] with $$ $$ - - const blockProcessedContent = content.replaceAll( - /\\\[(.*?)\\]/gs, - (_, equation) => `$$${equation}$$`, - ); - // Replace inline LaTeX delimiters \( \) with $ $ - const inlineProcessedContent = blockProcessedContent.replaceAll( - /\\\((.*?)\\\)/gs, - (_, equation) => `$${equation}$`, - ); - return inlineProcessedContent; -}; diff --git a/src/Markdown/utils.ts b/src/Markdown/utils.ts new file mode 100644 index 00000000..72f1b5ee --- /dev/null +++ b/src/Markdown/utils.ts @@ -0,0 +1,30 @@ +export function escapeDollarNumber(text: string) { + let escapedText = ''; + + for (let i = 0; i < text.length; i += 1) { + let char = text[i]; + const nextChar = text[i + 1] || ' '; + + if (char === '$' && nextChar >= '0' && nextChar <= '9') { + char = '\\$'; + } + + escapedText += char; + } + + return escapedText; +} + +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; + }); +} From baf8e500c2907be0903da5bdd76b570a2291ad50 Mon Sep 17 00:00:00 2001 From: canisminor1990 Date: Mon, 24 Jun 2024 14:03:28 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=91=B7=20ci:=20Fix=20ci?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8a74d209..5d24effc 100644 --- a/package.json +++ b/package.json @@ -139,7 +139,7 @@ "@types/react": "18.2.40", "@types/react-dom": "^18.3.0", "@types/uuid": "^9.0.8", - "@vitest/coverage-v8": "^1.6.0", + "@vitest/coverage-v8": "~1.2.2", "antd-style": "^3.6.2", "babel-plugin-antd-style": "^1.0.4", "commitlint": "^19.3.0", From b2ca91ad043b82434e7f7e0d9b751bab49d149ba Mon Sep 17 00:00:00 2001 From: canisminor1990 Date: Mon, 24 Jun 2024 14:28:48 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=90=9B=20fix:=20Fix=20Mhchem=20in=20L?= =?UTF-8?q?aTeX?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Markdown/index.tsx | 4 ++-- src/Markdown/utils.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Markdown/index.tsx b/src/Markdown/index.tsx index 856d233c..1edf5261 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 } from './utils'; +import { escapeBrackets, escapeDollarNumber, 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 escapeBrackets(escapeDollarNumber(children)); + return escapeMhchem(escapeBrackets(escapeDollarNumber(children))); }, [children, enableLatex]); const components: Components = useMemo( diff --git a/src/Markdown/utils.ts b/src/Markdown/utils.ts index 72f1b5ee..e40ba463 100644 --- a/src/Markdown/utils.ts +++ b/src/Markdown/utils.ts @@ -28,3 +28,7 @@ export function escapeBrackets(text: string) { return match; }); } + +export function escapeMhchem(text: string) { + return text.replaceAll('$\\ce{', '$\\\\ce{').replaceAll('$\\pu{', '$\\\\pu{'); +}