Skip to content

fix(ui): render $..$ and same-line $$..$$ math - #52100

Open
wulart wants to merge 1 commit into
anomalyco:v2from
wulart:feat/markdown-dollar-math
Open

wulart wants to merge 1 commit into
anomalyco:v2from
wulart:feat/markdown-dollar-math

Conversation

@wulart

@wulart wulart commented Sep 29, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #51725

Type of change

  • Bug fix

What does this PR do?

The katex extension only recognized \(...\) inline math and block math requiring newlines right after the opening $$ and before the closing $$. Most LLM chat output uses $..$ for inline math and single-line $$..$$ for display math, so those formulas render as raw source (#51725).

This extends the existing extension rather than adding a new one:

  • inline: try $..$ after \(...\) fails, with display mode for $$..$$
  • block: accept single-line and same-line $$..$$ alongside the existing newline-delimited form
  • start hints: report the earliest $ for inline math and \n$$ for block math so marked can split paragraphs around glued display math

$..$ was dropped in #34850 because the old matcher had no currency guards; this reopens that ruling with the guards it lacked. The opening $ must not be followed by whitespace, the closing $ must not be preceded by whitespace nor followed by a digit, and the content cannot contain $ or newlines — "costs $5 and $10" and "$1,000 to $2,000" stay plain text. Unclosed delimiters are left as text; invalid latex renders as an inline error via the existing throwOnError: false path.

Supersedes #52087 (same branch and commits; auto-closed for missing the PR template) and the earlier dev-based #51989.

How did you verify your code works?

On v2: full suite 34/34 green, typecheck (tsgo) clean. marked-parser.test.ts grew from 4 to 15 cases: single-$ inline (symbol-heavy, CJK inside \text{}), single-line and glued $$ blocks, display math inside a paragraph, money amounts staying literal, unclosed delimiters, invalid latex as inline error, dollar signs inside code spans, and plain markdown unaffected.

Screenshots / recordings

Textual before/after for the repro from #51725:

  • Before: The distance is $d_i = \sqrt{(x_i-\bar{x})^\top S^{-1}(x_i-\bar{x})}$. shows the dollar signs and LaTeX source literally.
  • After: renders as inline KaTeX math; a single-line $$E = mc^2$$ renders as display math.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

The katex extension only recognized \(...\) inline math and block math
requiring newlines right after the opening $$ and before the closing $$.
Most LLM chat output uses $..$ for inline math and single-line $$..$$
for display math, so those formulas currently render as raw source.

Extend the existing extension rather than adding a new one:

- inline: try $..$ after \(...\) fails, with display mode for $$..$$
- block: accept single-line and same-line $$..$$ alongside the existing
  newline-delimited form
- start hints: report the earliest $ for inline math and \n$$ for block
  math so marked can split paragraphs around glued display math

Currency guards keep money amounts as plain text: the opening $ must
not be followed by whitespace, the closing $ must not be preceded by
whitespace nor followed by a digit, and the content cannot contain $
or newlines ("costs $5 and $10", "$1,000 to $2,000" stay literal).
Unclosed delimiters are left as text; invalid latex renders as an
inline error via the existing throwOnError:false path.

Co-Authored-By: Claude Code <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant