fix(desktop): inline-math classifier gaps + display-math/blockquote rendering bugs#4543
fix(desktop): inline-math classifier gaps + display-math/blockquote rendering bugs#4543lightfront wants to merge 7 commits into
Conversation
…ne math The inline-math classifier's function-call rule only accepted a single letter before the parentheses (f(x), g(x)), so multi-letter group notation — SO(3,1), SU(2), SL(2), GL(n), Sp(2n), Spin(n), Diff(M) — fell through to the prose fallback and rendered as literal dollar signs instead of going through remark-math/rehype-katex. Broaden the identifier from one letter to 1-6 letters. The cap plus the requirement that the whole token sits inside one $...$ span keep prose parentheticals out. Adds 9 regression cases to the math-golden suite. 177/177 pass.
The classifier's backslash-command rule used \b after the command name:
if (/\\[A-Za-z]+\b/.test(math)) return true;
\b is a word boundary, but \tfrac12 / \frac12 / \sqrt2 / \log3 /
\overline3 have no boundary between the name and a trailing digit
('c' and '1' are both word chars), so the regex rejected them and
these common LaTeX forms rendered as literal dollar signs instead of
going through remark-math/rehype-katex.
Drop the \b — a backslash command is a backslash command regardless of
what follows. \alpha, \frac{x}{y}, \cdot 3 (all already passing) are
unaffected; the currency / env-var guards below catch any new
false-positives.
+5 regression cases. 182/182 pass.
The inline-math classifier accepted comma-separated tuples like (A, B) or (1, 2, 3), but not permutation cycle notation — (12), (123), (12)(34) — where digits are packed together without commas. These fell through every accept rule and returned false, so normalizeMath wrapped them in &esengine#36; entities and they rendered as literal dollar signs instead of going through remark-math/rehype-katex. Add a rule that accepts one or more parenthesised digit groups: if (/^(?:\(\d+\))+$/.test(math)) return true; This is specific enough to avoid false positives: a parenthesised all-digit group is currency-shaped in prose only when it looks like (5), which is rare and never written inside $...$. Comma tuples continue through the existing rule. +4 regression cases (transposition, 3-cycle, product-of-transpositions, and an end-to-end normalizeMath check). 207/207 pass.
…ing delimiters
Two related bugs in the display-math pipeline (mathNormalize.ts):
1. Repair-before-extraction ordering: the repair regex (which inserts
\n\n before glued $$$) ran BEFORE the display-pair extraction,
so it split the closing $$ of well-formed pairs like
``\end{pmatrix}$$` or `> $$x$$`, breaking display math.
Fix: extract $$…$$ pairs as a unit FIRST, then repair remaining
orphaned $$.
2. Blockquote display-math: when $$...$$ appears inside a Markdown
blockquote (>), remark-math requires the closing $$ to also carry
the > prefix. Without it, the closing fence isn't recognised and
remark-math swallows the following paragraph as math content
(KaTeX error). Fix: the DM-restore step now detects whether the
opening $$ was inside a blockquote and prefixes the closing $$
with > accordingly.
Also restores the d450aec newline-delimited DM restore ($$\n…\n$$)
which remark-math requires for block-math recognition, and updates 7
test expectations to match the corrected behavior.
203/203 pass.
Added: fix for display math inside blockquotes + closing-delimiter splittingWhile testing, a user reported that a blockquote ( 1. Repair-before-extraction orderingThe repair regex (which inserts Fix: extract 2. Blockquote display-math fenceWhen Fix: the DM-restore step now detects whether the opening Test status
|
…th bug remark-math has a known limitation: multi-line $$...$$ display math inside a Markdown blockquote (>) breaks inline math parsing on subsequent blockquote lines. The closing fence isn't recognised and inline $...$ after the display block gets swallowed. Workaround: when display math is detected inside a blockquote, close the blockquote before the display math (\n\n$$\n...\n$$\n\n> ) and reopen it after. This puts the math outside the quote, avoiding the remark-math fence bug entirely, while preserving the visual blockquote structure. The Mackey theorem blockquote with inline math after display math now renders with 0 errors (was: cascading KaTeX failure). 203/203 pass.
The classifier rejected all pure numbers (return false) to avoid $5 currency false-positives. But $0$ (mass eigenvalue), $1$, $42$ are extremely common in math/physics and were rendering as literal dollar signs. Currency in prose is almost always written without a closing $ (costs $5, not costs $5$), so the $N$ form almost always means math. Flip return false → return true for pure numbers. Update 10 test expectations to match the corrected behavior. 203/203 pass.
…s math Two classifier gaps in the binary-operator rule: 1. 'K = -iJ' rejected: the regex required an operand ([A-Za-z0-9...]) immediately after the operator, but '-' (unary sign) was not in that class. Fix: allow an optional [+\-]? after the operator so 'K = -iJ', 'p = +\alpha', 'a = -b' are recognised. 2. '$+$', '$-$', '$=$' rejected: lone operators had no matching rule. Fix: add /^[+\-=<>±∓]$/ for single-character operator tokens. +8 regression tests. 211/211 pass.
Fix: inline-math classifier gaps + display-math/blockquote rendering bugs
Overview
This PR fixes eight rendering bugs in the math pipeline — six in the inline-math classifier (
mathClassify.ts) and two in the display-math pre-pass (mathNormalize.ts). All eight produce visible KaTeX failures (red error blocks or raw$...$text) in LLM chat output.Classifier fixes (
mathClassify.ts)Three common LaTeX math forms rendered as literal dollar signs because the inline-math classifier rejected them:
1. LaTeX command immediately followed by a digit —
$\tfrac12$,$\sqrt2$,$\log3$2. Multi-letter group notation —
$SO(3,1)$,$SU(2)$,$SL(2)$,$GL(n)$3. Permutation cycle notation —
$(12)$,$(123)$,$(12)(34)$4. Pure numbers rejected —
$0$,$1$,$42$The classifier rejected all pure numbers (
return false) to avoid$5currencyfalse-positives. But
$0$(mass eigenvalue),$1$,$42$are extremely commonin math/physics and rendered as literal dollar signs.
Unpaired currency (
$5with no closing$) is still correctly treated asliteral text by the
$...$pairing logic, so "costs $5 and $6" is unaffected.5. Binary operator with signed RHS —
$K = -iJ$,$p = +\alpha$The binary-operator regex required an operand character (
[A-Za-z0-9...])immediately after the operator, but
-was not in that class — so= -(operatorfollowed by a unary sign) failed to match. This rejected common physics
expressions like
$K = -iJ$,$a = -b$,$p = +\alpha$.6. Lone operators —
$+$,$-$,$=$,$<$Single-character operator tokens (
+,-,=,<,>,±,∓) matched noaccept rule and were rendered as literal text. These are common in physics prose
("the sign of
$+$", "the$<$relation").Display-math fixes (
mathNormalize.ts)Two bugs in the
$$...$$display-math pipeline caused cascading KaTeX errors — particularly inside Markdown blockquotes.7. Repair-before-extraction ordering — closing
$$split offThe repair regex (which inserts
\n\nbefore glued$$) ran before the display-pair extraction. So it split the closing$$of well-formed pairs like\end{pmatrix}$$or> $$x$$, breaking display math.Fix: extract
$$…$$pairs as a unit first, then repair only remaining orphaned$$. This is thed450aec1ordering fix applied to current main-v2.8. Blockquote display-math fence —
remark-mathupstream limitationremark-mathhas a known limitation: multi-line$$...$$display math inside a Markdown blockquote (>) breaks inline math parsing on subsequent blockquote lines. The closing fence isn't recognised and inline$...$after the display block gets swallowed.Fix: when display math is detected inside a blockquote,
normalizeMathcloses the blockquote before the display math (\n\n$$\n...\n$$\n\n>) and reopens it after. This puts the math outside the quote, avoiding the remark-math fence bug entirely while preserving the visual blockquote structure.Verification
$\tfrac12$,$\sqrt2$,$\log3$$SO(3,1)$,$SU(2)$,$SL(2)$,$GL(n)$$(12)$,$(123)$,$(12)(34)$$0$,$1$,$42$$K = -iJ$,$p = +\alpha$$+$,$-$,$=$,$<$the apples cost $5 and $6.(currency)$$\end{pmatrix}$$(closing brace)$$preserved$PATH$,costs $5(unpaired)costs $5$(closed pair)Files changed
desktop/frontend/src/components/mathClassify.ts\b; broaden function-call rule to 1–6 letters; add cycle-notation rule; accept pure numbers; signed RHS in binary-op rule; lone operatorsdesktop/frontend/src/components/mathNormalize.ts$$…$$before repair regex; newline-delimited DM restore; blockquote-aware closing (break quote around display math)desktop/frontend/src/__tests__/math-golden.test.tsCommits
792df63c— multi-letter group notation (SO(3,1),SU(2), …) classified as inline math6c64bc91— classify LaTeX commands followed by a digit (\tfrac12,\sqrt2) as math2b050155— classify pure numbers ($0$,$1$,$42$) as inline math7ceeebfa— classify operators with signed RHS ($K = -iJ$) and lone operators ($+$)6f7ef086— repair display-math extraction ordering + blockquote$$closing84e6ac81— break blockquote around display math to avoid remark-math fence bug