fix(desktop): shield math spans from the visible-prose rewrites

`$\sqrt[3]{8}$` renders as a plain square root — the index is gone. It is
not a KaTeX layout problem: the index never reaches KaTeX. CITATION_MARKER_RE
strips `[3]` as a citation marker, because its lookbehind accepts any letter
and the `t` of `\sqrt` qualifies. That runs inside normalizeVisibleProse,
which splits out inline code spans but not math, so TeX is fed to rewrites
written for prose.

Numeric-only, which is why `\sqrt[n]{8}` survives and made this look like a
layout edge case rather than a preprocessing one.

Shield math the same way inline code is already shielded: split each prose
part on math spans and rewrite only the segments between them. That also
takes math out of the reach of the other rewrites in that pass
(autoLinkRawUrls, LOCAL_PREVIEW_URL_RE, the ``` stripper, linkifySessionRefs),
any of which can corrupt TeX the same way with different input.

The split is capturing, and math segments are identified by index parity
rather than a leading `$`, so a prose run that merely opens with a stray
dollar cannot be mistaken for math. Escaped `\$` delimiters stay prose, which
is what keeps `$5 and $10` escaping intact.

Verified in the real renderer through the desktop mock-backend E2E harness:
`.katex .root` (the span KaTeX emits for a radical index) goes from 1 to 4 on
the same four-radical reply, and the MathML annotations show KaTeX receiving
`\sqrt[3]{8}` intact rather than `\sqrt{8}`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SPGjEQ2yrS4nWiooUEdYti
This commit is contained in:
whisky0809
2026-08-08 12:52:20 +02:00
committed by Teknium
parent 47066f5ea0
commit 21fc7c5a14
2 changed files with 56 additions and 7 deletions
@@ -360,4 +360,28 @@ describe('preprocessMarkdown', () => {
expect(preprocessMarkdown(input)).toBe(input)
})
it('keeps a radical index inside inline math', () => {
expect(preprocessMarkdown('$\\sqrt[3]{8}$')).toBe('$\\sqrt[3]{8}$')
})
it('keeps a radical index inside display math', () => {
expect(preprocessMarkdown('$$\\sqrt[3]{8}$$')).toBe('$$\\sqrt[3]{8}$$')
})
it('keeps a radical index inside a multiline display block', () => {
const input = ['$$', '\\sqrt[3]{8} + \\sqrt[4]{16}', '$$'].join('\n')
expect(preprocessMarkdown(input)).toBe(input)
})
it('keeps a radical index in math that arrived as bracket delimiters', () => {
expect(preprocessMarkdown('\\(\\sqrt[3]{8}\\)')).toContain('$\\sqrt[3]{8}$')
})
it('still strips a citation marker in prose that also contains math', () => {
const output = preprocessMarkdown('Per the paper[2], $\\sqrt[3]{8}$ is 2.')
expect(output).toBe('Per the paper, $\\sqrt[3]{8}$ is 2.')
})
})
+32 -7
View File
@@ -14,6 +14,11 @@ const FENCE_LINE_RE = /^([ \t]*)(`{3,}|~{3,})([^\n]*)$/
const EMPTY_FENCE_BLOCK_RE = /(^|\n)[ \t]*(?:`{3,}|~{3,})[^\n]*\n[ \t]*(?:`{3,}|~{3,})[ \t]*(?=\n|$)/g
const CODE_FENCE_SPLIT_RE = /((?:```|~~~)[\s\S]*?(?:```|~~~))/g
const INLINE_CODE_SPLIT_RE = /(`[^`\n]+`)/g
// Math spans as remark-math will see them: a `$$…$$` block, which may span
// lines, or a same-line `$…$`. A delimiter escaped as `\$` is prose — that is
// exactly how escapeCurrencyDollarsPreservingMath marks a price, so the
// lookbehinds keep `\$5 and \$10` out of the math branch.
const MATH_SPAN_SPLIT_RE = /((?<!\\)\$\$[\s\S]*?(?<!\\)\$\$|(?<!\\)\$[^\n$]+?(?<!\\)\$)/g
const LATEX_DISPLAY_OPEN_LINE_RE = /^([ \t]*(?:>[ \t]*)*(?:(?:[-+*]|\d+[.)])[ \t]+)?[ \t]*)\\{1,2}\[[ \t]*\r?$/
const LATEX_DISPLAY_CLOSE_LINE_RE = /^([ \t]*(?:>[ \t]*)*(?:(?:[-+*]|\d+[.)])[ \t]+)?[ \t]*)\\{1,2}\][ \t]*\r?$/
const CUSTOM_DISPLAY_MATH_LINE_RE = /^([ \t]*(?:>[ \t]*)*(?:(?:[-+*]|\d+[.)])[ \t]+)?[ \t]*)\[\/math\][ \t]*\r?$/
@@ -182,19 +187,39 @@ function routeFileLinksToPreview(text: string): string {
})
}
function rewriteProseSegment(segment: string): string {
return linkifySessionRefs(
autoLinkRawUrls(
routeFileLinksToPreview(
segment.replace(/`{3,}/g, '').replace(LOCAL_PREVIEW_URL_RE, '$1').replace(CITATION_MARKER_RE, '')
)
)
)
}
/**
* Apply the prose rewrites to visible prose only.
*
* Inline code has always been shielded here. Math has to be shielded for the
* same reason: these rewrites read TeX as prose. `CITATION_MARKER_RE` is the
* one that bites — its lookbehind accepts any letter, so the `t` of `\sqrt`
* qualifies and `$\sqrt[3]{8}$` loses its index to what looks like a citation
* marker, long before KaTeX sees it.
*
* Split on math spans is odd-index-is-a-delimiter (capturing split), not a
* `startsWith('$')` test, so a prose segment that merely opens with a stray
* dollar can't be mistaken for math.
*/
function normalizeVisibleProse(text: string): string {
return text
.split(INLINE_CODE_SPLIT_RE)
.map(part =>
part.startsWith('`')
? part
: linkifySessionRefs(
autoLinkRawUrls(
routeFileLinksToPreview(
part.replace(/`{3,}/g, '').replace(LOCAL_PREVIEW_URL_RE, '$1').replace(CITATION_MARKER_RE, '')
)
)
)
: part
.split(MATH_SPAN_SPLIT_RE)
.map((segment, index) => (index % 2 === 1 ? segment : rewriteProseSegment(segment)))
.join('')
)
.join('')
}