Repository navigation
Close display math whose $$ delimiters are glued to the TeX - #2151
Merged
Merged
Conversation
micromark-extension-math parses `$$` display math like a fenced code
block: `$$T_{a}` opens a block whose TeX is dropped as fence meta, and
only a `$$` alone on its own line closes it. Models routinely write
`$$T_{a}\n\approx 73$$`, so the block never closed and the rest of the
message rendered as one `.katex-error` (heading, list, and links lost).
Normalize those spans into the canonical `$$\n…\n$$` shape before the
markdown reaches remark-math. Fenced code is skipped and a span without
a closing delimiter is left alone.
Fixes #1778
Co-Authored-By: Claude <[email protected]>
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.
What was wrong
micromark-extension-math(behindremark-mathinMarkdownPreview) parses$$display math like a fenced code block.$$T_{a}at line start opens a block whose TeX is stored as fence meta and never rendered, and the block only closes on a$$that sits alone on its own line. A trailing…$$is ordinary content, so the block runs to the end of the message andrehype-katexrenders the heading, list, and links after it as one.katex-error. bb did no normalization before handing text to the parser. Issue: #1778. Report: https://get-bb.github.io/reports/issues/1778.htmlWhat changed
apps/app/src/components/ui/markdown-math-fences.ts(new):normalizeMathFencesrewrites LaTeX-style display spans into the canonical$$\n…\n$$shape before parsing. It handles an opener with glued TeX ($$x), a trailing closer (y$$), and both together, including the$$x\n$$shape whose first line used to vanish as meta. Fenced code blocks are skipped, a span with no closing delimiter is left alone (same unclosed-fence behavior as before, which also covers a block still streaming in), and canonical blocks and inline$$x$$come out unchanged.apps/app/src/components/ui/markdown-preview.tsx: apply the normalizer to the body after the frontmatter split, so parse positions still line up with the string react-markdown receives.apps/app/src/components/ui/markdown-prompt-blockquote-boundaries.ts: export the existing code-fence helpers so the normalizer reuses them.Not covered: math blocks inside blockquotes or list items keep their container prefix and are not normalized (status quo). The mobile renderer (
apps/mobile/src/markdown/parse.ts) has the same parser semantics but renders math as source and slices by source offsets, so it is left for a separate change.How you verified
apps/app/src/components/ui/markdown-preview.test.tsx: new case renders the issue body throughMarkdownPreview. On origin/main it fails withAssertionError: expected null not to be nullatexpect(container.querySelector(".katex-display")).not.toBeNull()(the DOM contains a single.katex-errorholding the heading, list, and link). With the fix it passes: one.katex-displaywhose annotation contains both TeX lines, no.katex-error,h2, twoli, and the link.apps/app/src/components/ui/markdown-math-fences.test.ts: shapes A/B/C, two blocks in one document, unchanged canonical/inline/prose input, unclosed span, fenced and indented code, CRLF.pnpm exec turbo run typecheck --filter=@bb/app:Tasks: 3 successful, 3 totalpnpm exec turbo run test --filter=@bb/app:Tasks: 4 successful, 4 total(410 files, 3166 tests passed)## Content after the formulaheading, two list items, and the link;document.querySelectorAll(".katex-error").length === 0.Fixes #1778
Independent verification
Verified at head
6df9819a9(origin/main is an ancestor;mergeable: MERGEABLE).Commands:
git checkout origin/main -- apps/app/src/components/ui/markdown-preview.tsx apps/app/src/components/ui/markdown-prompt-blockquote-boundaries.ts, movedmarkdown-math-fences.tsaside, thenpnpm exec vitest run src/components/ui/markdown-preview.test.tsx -t 1778 src/components/ui/markdown-math-fences.test.tsinapps/app.Fail-before:
FAIL markdown-preview.test.tsx > closes a display math block whose $$ delimiters are glued to the TeX (#1778)—AssertionError: expected null not to be nullatexpect(container.querySelector(".katex-display")).not.toBeNull(); DOM dump shows onespan.katex-errorcontaining## Content after the formulaand the list/link.markdown-math-fences.test.tsfails to import the module.Test Files 2 passed (2),Tests 27 passed (27).pnpm exec turbo run typecheck --filter=@bb/app --force->Tasks: 3 successful, 3 total.pnpm exec turbo run test --filter=@bb/app --force->Tasks: 4 successful, 4 total(Test Files 410 passed,Tests 3166 passed | 3 skipped).remark-parse + remark-gfm + remark-mathon 19 inputs before/afternormalizeMathFences. Canonical blocks, 2/3-space-indented blocks inside ordered lists, inline$$x$$(line start, mid-paragraph, table cell),$5 to $10,\$\$escapes,~~~/backtick fences, blockquote-prefixed spans, and prose ending in$$all parse identically. Only the glued shapes A/B/C from the report change, each to a closed math node with the meta line restored as TeX.katexDisplay=1,katexError=0, annotation contains both TeX lines,h2"Content after the formula", 2li, link "This should remain a link". Before the fix the same DOM is a single.katex-error(report screenshot).CI on head: all required checks pass (Checks, Tests app-1/2/3, packages, server, integration, Package Smoke ubuntu/macos, version check).
Residual risks (non-blocking): spans with a container prefix (
> $$x/- $$x) are not normalized and keep the old behavior;apps/mobile/src/markdown/parse.tsshares the parser semantics and still swallows the suffix into one source-rendered math block; a bare$$opener followed by a$$x$$line now closes at that line (invalid TeX either way).