Skip to content

Close display math whose $$ delimiters are glued to the TeX - #2151

Merged
SawyerHood merged 1 commit into
mainfrom
bb/fix-1778-katex-multiline-error
Aug 21, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
bb/fix-1778-katex-multiline-error

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

micromark-extension-math (behind remark-math in MarkdownPreview) 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 and rehype-katex renders 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.html

What changed

  • apps/app/src/components/ui/markdown-math-fences.ts (new): normalizeMathFences rewrites 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 through MarkdownPreview. On origin/main it fails with AssertionError: expected null not to be null at expect(container.querySelector(".katex-display")).not.toBeNull() (the DOM contains a single .katex-error holding the heading, list, and link). With the fix it passes: one .katex-display whose annotation contains both TeX lines, no .katex-error, h2, two li, 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 total
  • pnpm exec turbo run test --filter=@bb/app: Tasks: 4 successful, 4 total (410 files, 3166 tests passed)
  • Manual: spawned a codex thread on a dev instance of this branch with the issue body as the prompt. The user bubble renders the KaTeX display formula (both lines), the ## Content after the formula heading, two list items, and the link; document.querySelectorAll(".katex-error").length === 0.

Fixes #1778

AGENT GENERATED: by Claude Opus 5

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, moved markdown-math-fences.ts aside, then pnpm exec vitest run src/components/ui/markdown-preview.test.tsx -t 1778 src/components/ui/markdown-math-fences.test.ts in apps/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 null at expect(container.querySelector(".katex-display")).not.toBeNull(); DOM dump shows one span.katex-error containing ## Content after the formula and the list/link. markdown-math-fences.test.ts fails to import the module.
  • Restored the PR files and re-ran both test files: 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).
  • Ad-hoc parse probe (not committed): ran remark-parse + remark-gfm + remark-math on 19 inputs before/after normalizeMathFences. 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.
  • Live repro on my own dev instance (PR branch): spawned a codex thread whose prompt is the issue body. User bubble DOM: katexDisplay=1, katexError=0, annotation contains both TeX lines, h2 "Content after the formula", 2 li, 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.ts shares 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).

AGENT GENERATED: by Claude Opus 5

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]>
@SawyerHood
SawyerHood marked this pull request as ready for review August 21, 2026 05:39
@SawyerHood
SawyerHood merged commit 1aafe4d into main Aug 21, 2026
13 checks passed
@SawyerHood
SawyerHood deleted the bb/fix-1778-katex-multiline-error branch August 21, 2026 14:40
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.

Malformed multiline $$ math is not closed by trailing $$: the parser consumes the rest of the message into one .katex-error

1 participant