Repository navigation
fix: preserve escaped dollar signs in Markdown math - #4130
hassamaslamkhan wants to merge 2 commits into
Conversation
|
It looks like this regression is caused by upgrading remark-math from v3 to v5. In v5 escapes inside inline math expressions are not supported: https://www.npmjs.com/package/remark-math/v/5.1.1
Rather than creating our own markdown parser, it might be better to document this change. |
|
Thanks for pointing this out @AnnMarieW . I hadn't realized this was an intentional change in remark-math v5. I agree that documenting the behavior and the Would you prefer that I update this PR to focus on documentation, or close it and open a separate documentation PR? |
|
@hassamaslamkhan I think it would be better to document the change, but let's wait for the maintainers to chime in. Thanks for this PR and for offering to update the docs! The Dash docs are in a private repo, so that would have to be handled seperately. |
T4rk1n
left a comment
There was a problem hiding this comment.
Fix is needed (this regressed in #3960), but I don't want a ~100 line scanner that re-parses Markdown before remark runs. Every case it gets wrong silently rewrites normal documents for all mathjax users. 4.4.1 handled this with one regex that skipped escaped characters inside $...$: (\${1,2})((?:\\.|[^$])+)\1. Can you bring this down to something that small? Blocking on the size of the approach.
| // Work around remark-math treating `\$` inside `$...$` as the closing | ||
| // delimiter. Single-line `$$...$$` remains inline math in remark-math. | ||
| // Only rewrite Markdown text, not code, link destinations or raw HTML. | ||
| export function normalizeEscapedDollarMath(text: string): string { |
There was a problem hiding this comment.
This carries code, fence and HTML state line by line without the real block structure, so it will drift from remark. For example, one unmatched backtick turns off rewriting for the rest of the document. Can you replace it with a single targeted pattern like 4.4.1 had?
There was a problem hiding this comment.
Thanks @T4rk1n! I'll simplify the implementation using the approach from Dash 4.4.1 and remove the custom state tracking. I'll also add a test to make sure the math stays inline within a paragraph.
There was a problem hiding this comment.
Hi @T4rk1n , thanks for the feedback!
I first tried simplifying the fix using the regex from Dash 4.4.1, but during testing I found that it could also match dollar signs inside links, inline code, and HTML, which could change unrelated Markdown.
I then tried using the Markdown AST, but that needed extra parsing logic and was becoming complicated again.
Finally, I tried a smaller approach that reuses the existing remark-math tokenizer and handles escaped dollar signs directly during parsing. This avoids the custom scanner and doesn't rewrite the Markdown text.
I've updated the PR with this approach and added the inline rendering test you suggested. I also tested multiple escaped dollars, multiple equations, links, code blocks, and normal math. All 30 Markdown browser tests passed, along with the build and code-quality checks.
Would appreciate your thoughts on this approach. Thanks again for the guidance!
| // Only rewrite Markdown text, not code, link destinations or raw HTML. | ||
| export function normalizeEscapedDollarMath(text: string): string { | ||
| const mathOrHtml = | ||
| /<[^>\r\n]+>|(?<![\\$])\$(?!\$)((?:\\[^\r\n]|[^$\r\n])+?)\$(?!\$)/g; |
There was a problem hiding this comment.
Rewriting $x\$3$ to $$x\$3$$ changes inline math to display math as far as the parser sees. Can you add a test that checks the rendered DOM stays inline for a $x=\$3$ b mid-paragraph?
de0d98a to
e265b8d
Compare
|



Description
Fixes #4123.
This PR fixes a bug in
dcc.Markdownwhere escaped dollar signs (\$) inside inline math expressions are incorrectly interpreted as closing delimiters, causing MathJax rendering to fail.The previous implementation used a Markdown normalization utility. Following maintainer feedback, I replaced it with a smaller tokenizer-level fix.
Changes
remark-mathtokenizer to handle escaped dollar signs inside math expressions.$...$into display math.Example
Input:
a $x=\$3$ bExpected: The complete expression renders as inline MathJax, with
aandbremaining in the same paragraph.The new browser test verifies this behavior in the rendered DOM.
Contributor Checklist
dev(bug fix).Testing
Test Evidence
1. Original bug — before fix
The original incorrect rendering of escaped-dollar inline math.
2. Correct inline MathJax rendering — after fix
The screenshot demonstrates correct rendering of escaped dollars, multiple expressions, and ordinary inline math.
3. Markdown preservation
The screenshot shows that inline code, Markdown links, and ordinary dollar signs remain correctly rendered.
4. Browser integration test results
All 30 Markdown integration tests passed, including the two new regression tests.
Implementation Note
The fix operates during micromark tokenization rather than rewriting Markdown source. It reuses the existing
remark-mathtokenizer and preserves the original inline math structure.Optionals