Skip to content

fix: preserve escaped dollar signs in Markdown math - #4130

Open
hassamaslamkhan wants to merge 2 commits into
plotly:devfrom
hassamaslamkhan:fix/4123-markdown-escaped-dollar
Open

hassamaslamkhan wants to merge 2 commits into
plotly:devfrom
hassamaslamkhan:fix/4123-markdown-escaped-dollar

Conversation

@hassamaslamkhan

@hassamaslamkhan hassamaslamkhan commented Nov 8, 2026 •

Copy link
Copy Markdown

Description

Fixes #4123.

This PR fixes a bug in dcc.Markdown where 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

  • Added a small wrapper around the existing remark-math tokenizer to handle escaped dollar signs inside math expressions.
  • Reused the existing Markdown parser instead of introducing a separate Markdown scanner.
  • Preserved inline math rendering without converting $...$ into display math.
  • Added browser regression tests for inline MathJax rendering and preserving Markdown links and code blocks.

Example

Input:

a $x=\$3$ b

Expected: The complete expression renders as inline MathJax, with a and b remaining in the same paragraph.

The new browser test verifies this behavior in the rendered DOM.

Contributor Checklist

  • This PR targets dev (bug fix).
  • Identified and reproduced the escaped-dollar parsing bug.
  • Replaced the previous normalization utility with a smaller parser-level implementation.
  • Preserved existing Markdown behavior for links and code blocks.
  • Added browser regression tests.
  • Ran tests and code-quality checks locally.
  • Added tests covering the bug fixed in this PR.

Testing

  • Markdown integration tests: 30 passed.
  • Production build: Passed.
  • ESLint: Passed.
  • Prettier: Passed.
  • Pylint: Passed.
  • Flake8: Passed.
  • Black: Passed.
  • Additional parser-level checks: Passed for multiple escaped dollars, multiple math expressions, normal math, code, links, and other unchanged behavior.

Test Evidence

1. Original bug — before fix

The original incorrect rendering of escaped-dollar inline math.

2. Correct inline MathJax rendering — after fix

dash-4123-inline-math-fixed

The screenshot demonstrates correct rendering of escaped dollars, multiple expressions, and ordinary inline math.

3. Markdown preservation

dash-4123-markdown-preserved

The screenshot shows that inline code, Markdown links, and ordinary dollar signs remain correctly rendered.

4. Browser integration test results

dash-4123-browser-tests-passed

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-math tokenizer and preserves the original inline math structure.

Optionals

  • Changelog entry (not included in this PR).
  • Documentation follow-up (not included in this PR).

@AnnMarieW

Copy link
Copy Markdown
Collaborator

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

👉 Note: $math$ works similar to code. That means escapes don’t work inside math but you can use more dollars around the math instead: $$\raisebox{0.25em}{$\frac a b$}$$

Rather than creating our own markdown parser, it might be better to document this change.

@hassamaslamkhan

Copy link
Copy Markdown
Author

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 $$...$$ workaround would be simpler than maintaining custom parsing logic.

Would you prefer that I update this PR to focus on documentation, or close it and open a separate documentation PR?

@AnnMarieW

Copy link
Copy Markdown
Collaborator

@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 T4rk1n left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@hassamaslamkhan
hassamaslamkhan force-pushed the fix/4123-markdown-escaped-dollar branch from de0d98a to e265b8d Compare November 27, 2026 02:24
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] dcc.Markdown mishandles escaped dollar inside math expression

4 participants