Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

refactor: fix `normalizeLatex` logic and improve performance (#7743)

cs-catcf509e7815b2
diff --git a/web-app/src/containers/RenderMarkdown.tsx b/web-app/src/containers/RenderMarkdown.tsx
index 6733947f1..2721ccfff 100644
--- a/web-app/src/containers/RenderMarkdown.tsx
+++ b/web-app/src/containers/RenderMarkdown.tsx
@@ -1,4 +1,4 @@
-
+
import { Components } from 'react-markdown'
import { memo, useMemo } from 'react'
import { cn, disableIndentedCodeBlockPlugin } from '@/lib/utils'
@@ -37,38 +37,42 @@ const normalizeLatex = (input: string): string => {
return latexCache.get(input)!
}
- const segments = input.split(/(```[\s\S]*?```|`[^`]*`|<[^>]+>)/g)
+ const segments = input.split(/(```[\s\S]*?```|`[^`]*`|<[a-zA-Z/_!][^>]*>)/g)
+
+ let result = '';
+
+ for (let i = 0; i < segments.length; i++) {
+ const segment = segments[i];
+ if (!segment) continue;
- const result = segments
- .map((segment) => {
- if (!segment) return ''
+ // Captured code blocks, inline code, html tags
+ if (i % 2 === 1) {
+ result += segment;
+ continue;
+ }
- // Skip code blocks, inline code, html tags
- if (/^```[\s\S]*```$/.test(segment)) return segment
- if (/^`[^`]*`$/.test(segment)) return segment
- if (/^<[^>]+>$/.test(segment)) return segment
+ let s = segment;
- let s = segment
+ // --- Escape suspicious $<number> to prevent Markdown from treating it as LaTeX
+ // Example: "$1" → "\$1"
+ s = s.replace(/\$(\d+)(?![^\n]*\$([^\d]|$))/g, (_, num) => '\\$' + num)
- // --- Display math: \[...\] surrounded by newlines
+ // --- Display math: \[...\] surrounded by newlines
+ if (s.includes('\\['))
s = s.replace(
/(^|\n)\\\[\s*\n([\s\S]*?)\n\s*\\\](?=\n|$)/g,
(_, pre, inner) => `${pre}$$\n${inner.trim()}\n$$`
)
- // --- Inline math: space \( ... \)
+ // --- Inline math: space \( ... \)
+ if (s.includes('\\('))
s = s.replace(
/(^|[^$\\])\\\((.+?)\\\)(?=[^$\\]|$)/g,
(_, pre, inner) => `${pre}$${inner.trim()}$`
)
- // --- Escape $<number> to prevent Markdown from treating it as LaTeX
- // Example: "$1" → "\$1"
- s = s.replace(/\$(\d+)/g, (_, num) => '\\$' + num)
-
- return s
- })
- .join('')
+ result += s;
+ }
// Cache the result (with size limit to prevent memory leaks)
if (latexCache.size > 100) {
diff --git a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
index d0dcd87ac..d205d80bd 100644
--- a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
+++ b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
@@ -115,17 +115,35 @@ describe('RenderMarkdown', () => {
it('does not interfere with actual LaTeX inline math', () => {
const content = 'The equation $x + y = z$ is simple'
render(<RenderMarkdown content={content} />)
- const markdownContainer = document.querySelector('.markdown')
+ const katexContainer = document.querySelector('.katex')
// LaTeX should be rendered (KaTeX will process it)
- expect(markdownContainer).toBeTruthy()
+ expect(katexContainer).toBeTruthy()
+ })
+
+ it('does not interfere with actual LaTeX inline math (numerical expression)', () => {
+ const content = 'The equation $12 \\times 12 = 144$ is simple'
+ render(<RenderMarkdown content={content} />)
+ const katexContainer = document.querySelector('.katex')
+ // LaTeX should be rendered (KaTeX will process it)
+ expect(katexContainer).toBeTruthy()
})
it('does not interfere with display math blocks', () => {
const content = '$$\nx^2 + y^2 = r^2\n$$'
render(<RenderMarkdown content={content} />)
- const markdownContainer = document.querySelector('.markdown')
+ const katexContainer = document.querySelector('.katex')
// Display math should be rendered
- expect(markdownContainer).toBeTruthy()
+ expect(katexContainer).toBeTruthy()
+ })
+
+ it('does not interfere with display math blocks (numerical expression)', () => {
+ const content = '$$12 \\times 12 = 144$$'
+ render(<RenderMarkdown content={content} />)
+ const katexContainer = document.querySelector('.katex')
+ const katexError = document.querySelector('.katex-error')
+ // Display math should be rendered
+ expect(katexContainer).toBeTruthy()
+ expect(katexError).toBeNull()
})
})
@@ -287,9 +305,17 @@ describe('RenderMarkdown', () => {
it('converts \\[...\\] to $$ display math', () => {
const content = 'Here is math:\n\\[\nx^2 + y^2\n\\]\nDone'
render(<RenderMarkdown content={content} />)
- const markdownContainer = document.querySelector('.markdown')
+ const katexContainer = document.querySelector('.katex')
// Display math should be rendered by KaTeX
- expect(markdownContainer).toBeTruthy()
+ expect(katexContainer).toBeTruthy()
+ })
+
+ it('converts \\[...\\] to $$ display math (numerical expression)', () => {
+ const content = 'Here is math:\n\\[\n3^2 + 4^2\n\\]\nDone'
+ render(<RenderMarkdown content={content} />)
+ const katexContainer = document.querySelector('.katex')
+ // Display math should be rendered by KaTeX
+ expect(katexContainer).toBeTruthy()
})
})
@@ -297,9 +323,17 @@ describe('RenderMarkdown', () => {
it('converts \\(...\\) to $ inline math', () => {
const content = 'The formula \\(E = mc^2\\) is famous'
render(<RenderMarkdown content={content} />)
- const markdownContainer = document.querySelector('.markdown')
+ const katexContainer = document.querySelector('.katex')
// Inline math should be rendered
- expect(markdownContainer).toBeTruthy()
+ expect(katexContainer).toBeTruthy()
+ })
+
+ it('converts \\(...\\) to $ inline math (numerical expression)', () => {
+ const content = 'The formula \\(3^2 + 4^2 = 5^2\\) is famous'
+ render(<RenderMarkdown content={content} />)
+ const katexContainer = document.querySelector('.katex')
+ // Inline math should be rendered
+ expect(katexContainer).toBeTruthy()
})
})
@@ -308,8 +342,10 @@ describe('RenderMarkdown', () => {
const content = '```\nconst price = $100\n```'
render(<RenderMarkdown content={content} />)
const markdownContainer = document.querySelector('.markdown')
+ const text = markdownContainer?.textContent || ''
// Code blocks should preserve original content
expect(markdownContainer).toBeTruthy()
+ expect(text).toContain('$100')
})
it('does not process dollar amounts inside inline code', () => {
@@ -320,5 +356,15 @@ describe('RenderMarkdown', () => {
expect(text).toContain('$100')
})
})
+
+ describe('LaTeX normalization - HTML tag recognition', () => {
+ it('does not treat invalid ("<",">") pairs as HTML tag', () => {
+ const content = '$1 < $2, So choose the $1 one.\n\n> quoted content'
+ render(<RenderMarkdown content={content} />)
+ const katexContainer = document.querySelector('.katex')
+ // Should not treat < $2, So choose the $1 one.\n\n> as HTML tag
+ expect(katexContainer).toBeNull()
+ })
+ })
})