Skip to content

fix(ui): fallback math rendering for multiline $$ blocks#34642

Open
8Nothing8 wants to merge 10 commits into
anomalyco:devfrom
8Nothing8:fix-multiline-math
Open

fix(ui): fallback math rendering for multiline $$ blocks#34642
8Nothing8 wants to merge 10 commits into
anomalyco:devfrom
8Nothing8:fix-multiline-math

Conversation

@8Nothing8

@8Nothing8 8Nothing8 commented Jun 30, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #34656

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Display math blocks ($$...$$) don't render when the content spans multiple lines.
For example:

$$\begin{aligned} \end{aligned}$$

appears as plain text instead of rendered math.

The root cause is in marked-katex-extension v5.1.6: its inline tokenizer regex
does not match across newlines (. without /s flag), so multiline $$ blocks
are treated as regular paragraphs and the delimiters pass through unprocessed.

The fix adds a post-processing step in the primary (non-nativeParser) rendering
path. After marked.parse() produces HTML, we run the output through the
existing renderMathExpressions() function. This function already has a
multiline-aware regex (/\$\$([\s\S]*?)\$\$/g) that catches any $$ blocks
the extension missed. The function also skips <pre>, <code>, and <kbd>
blocks, so it doesn't interfere with syntax-highlighted code.

Change is one file (packages/ui/src/context/marked.tsx), replacing a direct
return jsParser with a wrapper that calls renderMathExpressions() on the
parsed output.

How did you verify your code works?

Tested renderMathInText() directly with three inputs:

  • $$\begin{aligned}\end{aligned}$$ (single line) → rendered
  • $$\begin{aligned}\n\end{aligned}$$ (multiline) → rendered
  • $$\begin{aligned}\nx &= 1 \\\ny &= 2\n\end{aligned}$$ (real math) → rendered

All three produce valid KaTeX HTML with no residual $$ characters.

Screenshots / recordings

N/A — regex parsing change, no UI change.

Checklist

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

marked-katex-extension v5.1.6 fails to match display math ($$...$$) that
spans multiple lines because its inline tokenizer regex likely does not
use the dotAll (/s) flag, so a newline between \begin{aligned} and
\end{aligned} causes the block to be treated as plain text.

The fix: wrap the non-nativeParser path to post-process the HTML output
with the existing renderMathExpressions() function, which already uses a
multiline-aware regex (/\$\$([\s\S]*?)\$\$/g) to catch any $$ blocks
the extension missed. Successfully-rendered blocks contain no residual $$
and are safe from double-processing; code/pre/kbd blocks are protected by
the existing skip pattern in renderMathExpressions().
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jun 30, 2026
@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 github-actions Bot removed needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jun 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

Multiline display math ($$...$$) not rendered

1 participant