fix(ui): fallback math rendering for multiline $$ blocks#34642
Open
8Nothing8 wants to merge 10 commits into
Open
fix(ui): fallback math rendering for multiline $$ blocks#346428Nothing8 wants to merge 10 commits into
8Nothing8 wants to merge 10 commits into
Conversation
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().
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #34656
Type of change
What does this PR do?
Display math blocks ($$...$$ ) don't render when the content spans multiple lines.
For example:
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/sflag), so multiline$$blocksare 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 theexisting
renderMathExpressions()function. This function already has amultiline-aware regex (
/\$\$([\s\S]*?)\$\$/g) that catches any$$blocksthe 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 directreturn jsParserwith a wrapper that callsrenderMathExpressions()on theparsed 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) → renderedAll three produce valid KaTeX HTML with no residual
$$characters.Screenshots / recordings
N/A — regex parsing change, no UI change.
Checklist