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(markdown): rework LaTeX preprocessing to placeholder protection

Akarshan Biswas0635a7153f35
diff --git a/web-app/src/containers/RenderMarkdown.tsx b/web-app/src/containers/RenderMarkdown.tsx
index 5d21e11bb..115e5aa59 100644
--- a/web-app/src/containers/RenderMarkdown.tsx
+++ b/web-app/src/containers/RenderMarkdown.tsx
@@ -75,73 +75,110 @@ function StreamingCode({ node, className, children, ...props }: CodeProps) {
}
const STREAMING_COMPONENTS: Components = { code: StreamingCode }
+const ZWSP = '​'
+
+// "word**,**" is neither left- nor right-flanking per CommonMark, so the markers
+// render literally; a ZWSP just inside restores flanking (U+200B is treated as
+// non-punctuation). Runs after math/code are masked, so '_'/'*' subscripts never
+// get a ZWSP — that made KaTeX warn "Unrecognized Unicode character 8203".
+const fixEmphasisFlanking = (s: string): string =>
+ s.includes('*') || s.includes('_')
+ ? s
+ .replace(/(?<=[\p{L}\p{N}])(\*\*?|__?)(?=[^\s\p{L}\p{N}*_])/gu, `$1${ZWSP}`)
+ .replace(/(?<=[^\s\p{L}\p{N}*_])(\*\*?|__?)(?=[\p{L}\p{N}])/gu, `${ZWSP}$1`)
+ : s
+
+// Placeholder-protection pipeline (adapted from llama.cpp's webui / LibreChat):
+// remark-math only parses $…$/$$…$$, so brackets need converting — but converting,
+// escaping currency, and fixing emphasis would corrupt each other unless code and
+// math are first lifted out as opaque tokens. PUA-delimited tokens can't occur in
+// model output and are inert to every transform here.
+const CODE_BLOCK = /(```[\s\S]*?```|`[^`\n]+`)/g
+const BRACKET_MATH =
+ /(\$\$[\s\S]*?\$\$|(?<!\\)\\\[[\s\S]*?\\\]|(?<!\\)\\\(.*?\\\))/g
+const tok = (kind: 'C' | 'L', n: number) => `\uE000${kind}${n}\uE000`
+
+// Mask genuine inline $…$ math; leave currency/identifiers ($5, a$b) in place.
+// Per-line so a stray $ can't swallow the rest.
+const maskInlineMath = (content: string, store: string[]): string => {
+ if (!content.includes('$')) return content
+ return content
+ .split('\n')
+ .map((line) => {
+ if (!line.includes('$')) return line
+ let out = ''
+ let pos = 0
+ while (pos < line.length) {
+ const open = line.indexOf('$', pos)
+ if (open === -1) {
+ out += line.slice(pos)
+ break
+ }
+ const close = line.indexOf('$', open + 1)
+ if (close === -1) {
+ out += line.slice(pos)
+ break
+ }
+
+ const before = open > 0 ? line[open - 1] : ''
+ const afterOpen = line[open + 1]
+ const beforeClose = open + 1 < close ? line[close - 1] : ''
+ const afterClose = close + 1 < line.length ? line[close + 1] : ''
+
+ const empty = close === open + 1
+ const gluedLeft = /[A-Za-z0-9_$-]/.test(before)
+ const looksMoney =
+ /[0-9]/.test(afterOpen) &&
+ (/[A-Za-z0-9_$-]/.test(afterClose) || beforeClose === ' ')
+
+ if (empty || gluedLeft || looksMoney) {
+ out += line.slice(pos, open + 1)
+ pos = open + 1
+ continue
+ }
+
+ out += line.slice(pos, open)
+ store.push(line.slice(open, close + 1))
+ out += tok('L', store.length - 1)
+ pos = close + 1
+ }
+ return out
+ })
+ .join('\n')
+}
+
// Cache for normalized LaTeX content
const latexCache = new Map<string, string>()
-/**
- * Optimized preprocessor: normalize LaTeX fragments into $ / $$.
- * Uses caching to avoid reprocessing the same content.
- */
const normalizeLatex = (input: string): string => {
- // Check cache first
- if (latexCache.has(input)) {
- return latexCache.get(input)!
- }
+ if (latexCache.has(input)) return latexCache.get(input)!
- const segments = input.split(/(```[\s\S]*?```|`[^`]*`|<[a-zA-Z/_!][^>]*>)/g)
+ const code: string[] = []
+ const math: string[] = []
- let result = '';
+ let s = input
+ .replace(CODE_BLOCK, (m) => tok('C', code.push(m) - 1))
+ .replace(BRACKET_MATH, (m) => tok('L', math.push(m) - 1))
- for (let i = 0; i < segments.length; i++) {
- const segment = segments[i];
- if (!segment) continue;
+ s = maskInlineMath(s, math)
+ s = s.replace(/\$(?=\d)/g, '\\$') // leftover currency renders literally
+ s = fixEmphasisFlanking(s)
- // Captured code blocks, inline code, html tags
- if (i % 2 === 1) {
- result += segment;
- continue;
- }
+ // Restore math, converting bracket delimiters to $… / $$… for remark-math.
+ s = s.replace(/\uE000L(\d+)\uE000/g, (_, n) => {
+ const expr = math[Number(n)]
+ if (expr.startsWith('\\[')) return `$$\n${expr.slice(2, -2).trim()}\n$$`
+ if (expr.startsWith('\\(')) return `$${expr.slice(2, -2).trim()}$`
+ return expr
+ })
+ s = s.replace(/\uE000C(\d+)\uE000/g, (_, n) => code[Number(n)])
- 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
- 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 \( ... \)
- if (s.includes('\\('))
- s = s.replace(
- /(^|[^$\\])\\\((.+?)\\\)(?=[^$\\]|$)/g,
- (_, pre, inner) => `${pre}$${inner.trim()}$`
- )
-
- // Emphasis glued between a word char and punctuation (e.g. "word**,**")
- // is neither left- nor right-flanking per CommonMark, so the markers render
- // literally. Inject a zero-width space just inside the delimiter to restore
- // flanking — invisible, and CommonMark treats U+200B as non-punctuation.
- if (s.includes('*') || s.includes('_'))
- s = s
- .replace(/(?<=[\p{L}\p{N}])(\*\*?|__?)(?=[^\s\p{L}\p{N}*_])/gu, '$1​')
- .replace(/(?<=[^\s\p{L}\p{N}*_])(\*\*?|__?)(?=[\p{L}\p{N}])/gu, '​$1')
-
- result += s;
- }
-
- // Cache the result (with size limit to prevent memory leaks)
if (latexCache.size > 100) {
const firstKey = latexCache.keys().next().value || ''
latexCache.delete(firstKey)
}
- latexCache.set(input, result)
-
- return result
+ latexCache.set(input, s)
+ return s
}
function RenderMarkdownComponent({
diff --git a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
index d73bf1d15..9ac979e9b 100644
--- a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
+++ b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
@@ -146,6 +146,20 @@ describe('RenderMarkdown', () => {
expect(katexContainer).toBeTruthy()
expect(katexError).toBeNull()
})
+
+ it('does not inject a ZWSP into subscripts (emphasis fix must skip math)', () => {
+ // '_' is a LaTeX subscript; the emphasis-flanking fix must not touch it,
+ // else KaTeX warns "Unrecognized Unicode character 8203" (U+200B).
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ const content = 'The term $x_{i}$ and $a_1 + b_2$ are indexed'
+ render(<RenderMarkdown content={content} />)
+ expect(document.querySelector('.katex')).toBeTruthy()
+ const warnedAboutZwsp = warn.mock.calls
+ .flat()
+ .some((arg) => typeof arg === 'string' && arg.includes('8203'))
+ expect(warnedAboutZwsp).toBe(false)
+ warn.mockRestore()
+ })
})
it('does not format 4-space indented text as code blocks', () => {
@@ -336,6 +350,13 @@ describe('RenderMarkdown', () => {
// Inline math should be rendered
expect(katexContainer).toBeTruthy()
})
+
+ it('converts \\[...\\] mid-sentence (no surrounding newlines)', () => {
+ const content = 'Inline display \\[a^2 + b^2\\] right here'
+ render(<RenderMarkdown content={content} />)
+ expect(document.querySelector('.katex')).toBeTruthy()
+ expect(document.querySelector('.katex-error')).toBeNull()
+ })
})
describe('LaTeX normalization - code block preservation', () => {
@@ -356,6 +377,15 @@ describe('RenderMarkdown', () => {
const text = markdownContainer?.textContent || ''
expect(text).toContain('$100')
})
+
+ it('does not convert bracket math inside inline code', () => {
+ // Code is masked first, so \(x\) stays literal instead of becoming $x$.
+ const content = 'Type `\\(x\\)` to write inline math'
+ render(<RenderMarkdown content={content} />)
+ const text = document.querySelector('.markdown')?.textContent || ''
+ expect(text).toContain('\\(x\\)')
+ expect(document.querySelector('.katex')).toBeNull()
+ })
})
describe('emphasis glued to punctuation (CommonMark flanking)', () => {