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.

fix: preserve dollar sign followed by numbers in markdown rendering (#7027)

Akarshan Biswas32816bdd13bc
diff --git a/web-app/src/containers/RenderMarkdown.tsx b/web-app/src/containers/RenderMarkdown.tsx
index 5d680d385..0e85450fe 100644
--- a/web-app/src/containers/RenderMarkdown.tsx
+++ b/web-app/src/containers/RenderMarkdown.tsx
@@ -64,7 +64,7 @@ const normalizeLatex = (input: string): string => {
// --- Escape $<number> to prevent Markdown from treating it as LaTeX
// Example: "$1" → "\$1"
- s = s.replace(/\$(\d+)/g, '\\$$1')
+ s = s.replace(/\$(\d+)/g, (_, num) => '\\$' + num)
return s
})
diff --git a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
index 074e7d634..9e1b2c8d2 100644
--- a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
+++ b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
@@ -1,18 +1,18 @@
-import { render, screen } from '@testing-library/react';
-import { describe, it, expect, vi } from 'vitest';
-import { RenderMarkdown } from '../RenderMarkdown';
+import { render } from '@testing-library/react'
+import { describe, it, expect, vi } from 'vitest'
+import { RenderMarkdown } from '../RenderMarkdown'
vi.mock('@i18n/react-i18next-compat', () => ({
useTranslation: () => ({
- t: (key: string) => key
- })
+ t: (key: string) => key,
+ }),
}))
// Mock clipboard API
Object.assign(navigator, {
clipboard: {
- writeText: vi.fn()
- }
+ writeText: vi.fn(),
+ },
})
describe('RenderMarkdown', () => {
@@ -20,60 +20,148 @@ describe('RenderMarkdown', () => {
const modelResponseWithNewLines = `This is line 1
This is line 2
This is line 3`
- render (
- <RenderMarkdown
- content={modelResponseWithNewLines}
- />
- )
+ render(<RenderMarkdown content={modelResponseWithNewLines} />)
const markdownContainer = document.querySelector('.markdown')
expect(markdownContainer?.innerHTML).toContain('<br>')
// Match either <br> or <br/>
- const brCount = (markdownContainer?.innerHTML.match(/<br\s*\/?>/g) || []).length
+ const brCount = (markdownContainer?.innerHTML.match(/<br\s*\/?>/g) || [])
+ .length
expect(brCount).toBe(2)
})
-
+
it('preserves line breaks in user message (when isUser == true)', () => {
const userMessageWithNewlines = `User question line 1
User question line 2
User question line 3`
- render(
- <RenderMarkdown
- content={userMessageWithNewlines}
- isUser={true}
- />
- )
+ render(<RenderMarkdown content={userMessageWithNewlines} isUser={true} />)
const markdownContainer = document.querySelector('.markdown')
expect(markdownContainer).toBeTruthy()
expect(markdownContainer?.innerHTML).toContain('<br>')
- const brCount = (markdownContainer?.innerHTML.match(/<br\s*\/?>/g) || []).length
+ const brCount = (markdownContainer?.innerHTML.match(/<br\s*\/?>/g) || [])
+ .length
expect(brCount).toBe(2)
})
it('preserves line breaks with different line ending types', () => {
- const contentWithDifferentLineEndings = "Line1\nLine2\r\nLine3\rLine4"
- render(
- <RenderMarkdown
- content={contentWithDifferentLineEndings}
- />
- )
+ const contentWithDifferentLineEndings = 'Line1\nLine2\r\nLine3\rLine4'
+ render(<RenderMarkdown content={contentWithDifferentLineEndings} />)
const markdownContainer = document.querySelector('.markdown')
expect(markdownContainer?.innerHTML).toContain('<br>')
- const brCount = (markdownContainer?.innerHTML.match(/<br\s*\/?>/g) || []).length
+ const brCount = (markdownContainer?.innerHTML.match(/<br\s*\/?>/g) || [])
+ .length
expect(brCount).toBe(3)
})
it('handles empty lines correctly', () => {
- const contentWithEmptyLines = 'Line 1\n\nLine 3 (after empty line)\n\nLine 5 (after two empty lines)'
- render(
- <RenderMarkdown
- content={contentWithEmptyLines}
- />
- )
+ const contentWithEmptyLines =
+ 'Line 1\n\nLine 3 (after empty line)\n\nLine 5 (after two empty lines)'
+ render(<RenderMarkdown content={contentWithEmptyLines} />)
const markdownContainer = document.querySelector('.markdown')
const html = markdownContainer?.innerHTML || ''
// Double new lines (`\n\n`) creates paragraph breaks, not line breaks
expect(html).not.toContain('<br>')
const paragraphCount = (html.match(/<p>/g) || []).length
- expect(paragraphCount).toBe(3) // Expect 3 paragraphs for 2 empty lines
+ expect(paragraphCount).toBe(3) // Expect 3 paragraphs for 2 empty lines
+ })
+
+ describe('LaTeX normalization - dollar sign escaping', () => {
+ it('escapes dollar signs followed by numbers to prevent LaTeX interpretation', () => {
+ const content = 'The price is $200 for the item'
+ render(<RenderMarkdown content={content} />)
+ const markdownContainer = document.querySelector('.markdown')
+ const text = markdownContainer?.textContent || ''
+ expect(text).toContain('$200')
+ })
+
+ it('handles multiple dollar amounts in the same content', () => {
+ const content = 'Items cost $100, $200, and $350 respectively'
+ render(<RenderMarkdown content={content} />)
+ const markdownContainer = document.querySelector('.markdown')
+ const text = markdownContainer?.textContent || ''
+ expect(text).toContain('$100')
+ expect(text).toContain('$200')
+ expect(text).toContain('$350')
+ })
+
+ it('handles single digit dollar amounts', () => {
+ const content = 'Only $5 or $9 available'
+ render(<RenderMarkdown content={content} />)
+ const markdownContainer = document.querySelector('.markdown')
+ const text = markdownContainer?.textContent || ''
+ expect(text).toContain('$5')
+ expect(text).toContain('$9')
+ })
+
+ it('handles large dollar amounts', () => {
+ const content = 'The total is $1000000 for the project'
+ render(<RenderMarkdown content={content} />)
+ const markdownContainer = document.querySelector('.markdown')
+ const text = markdownContainer?.textContent || ''
+ expect(text).toContain('$1000000')
+ })
+
+ it('preserves dollar amounts in sentences with mixed content', () => {
+ const content = 'I paid $50 yesterday and will pay $75 tomorrow.'
+ render(<RenderMarkdown content={content} />)
+ const markdownContainer = document.querySelector('.markdown')
+ const text = markdownContainer?.textContent || ''
+ expect(text).toContain('$50')
+ expect(text).toContain('$75')
+ })
+
+ 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')
+ // LaTeX should be rendered (KaTeX will process it)
+ expect(markdownContainer).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')
+ // Display math should be rendered
+ expect(markdownContainer).toBeTruthy()
+ })
})
-})
\ No newline at end of file
+
+ describe('LaTeX normalization - display math', () => {
+ 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')
+ // Display math should be rendered by KaTeX
+ expect(markdownContainer).toBeTruthy()
+ })
+ })
+
+ describe('LaTeX normalization - inline math', () => {
+ it('converts \\(...\\) to $ inline math', () => {
+ const content = 'The formula \\(E = mc^2\\) is famous'
+ render(<RenderMarkdown content={content} />)
+ const markdownContainer = document.querySelector('.markdown')
+ // Inline math should be rendered
+ expect(markdownContainer).toBeTruthy()
+ })
+ })
+
+ describe('LaTeX normalization - code block preservation', () => {
+ it('does not process dollar amounts inside code blocks', () => {
+ const content = '```\nconst price = $100\n```'
+ render(<RenderMarkdown content={content} />)
+ const markdownContainer = document.querySelector('.markdown')
+ // Code blocks should preserve original content
+ expect(markdownContainer).toBeTruthy()
+ })
+
+ it('does not process dollar amounts inside inline code', () => {
+ const content = 'Use `$100` in the variable'
+ render(<RenderMarkdown content={content} />)
+ const markdownContainer = document.querySelector('.markdown')
+ const text = markdownContainer?.textContent || ''
+ expect(text).toContain('$100')
+ })
+ })
+})
+