Skip to content

Add LaTeX math rendering to MarkdownPreview (#441)#442

Merged
SawyerHood merged 1 commit into
mainfrom
bb/add-latex-support-thr_gfmrjs7vjp
Jun 29, 2026
Merged

Add LaTeX math rendering to MarkdownPreview (#441)#442
SawyerHood merged 1 commit into
mainfrom
bb/add-latex-support-thr_gfmrjs7vjp

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

Closes #441.

What

Adds LaTeX math rendering to the shared MarkdownPreview component, which backs both surfaces in the issue — the timeline message renderer (assistant + user messages) and the markdown file preview.

  • remark-math parses $…$ / $$…$$; rehype-katex renders with KaTeX (stylesheet imported, fonts bundled by Vite).
  • For the allowHtml file-preview path, rehype-katex runs after rehype-sanitize: the default schema already keeps the language-math class on <code>, so the math wrappers survive sanitization and KaTeX (which runs with trust: false and self-escapes its TeX input) emits its output without being re-sanitized.
  • KaTeX text inherits currentColor, so math tracks --foreground across custom palettes.

Delimiters

GitHub-style (per design decision): $x$ inline and $$x$$ block both render. Known trade-off — a line with two unescaped $ (e.g. $5 to $10, $HOME and $PATH) parses the span between them as math; authors escape a literal dollar with \$. Documented in a code comment; flipping to $$-only later is a one-line change (singleDollarTextMath: false).

Also

Widened isGutterUtilityPath to accept the [EventTarget?] shape that @types/node's web-globals give Event.composedPath(), clearing a pre-existing typecheck failure in PierreLineSelectionActions.tsx (unrelated to math, but it was blocking a clean tsc).

Tests run

  • turbo run typecheck --filter=@bb/app — 0 errors.
  • turbo run test --filter=@bb/app — 1115 passing (5 new math tests: inline, display, escaped-dollar literal, allowHtml math+sanitize, invalid-TeX containment).
  • turbo run build --filter=@bb/app — green; KaTeX CSS + fonts bundled.
  • Added a "math (LaTeX)" Ladle story row for visual QA.

Risks

  • Single-dollar false-positives in prose (above) — mitigated by \$ and documented.
  • KaTeX adds ~60 emitted font files to the build (only WOFF2 loads at runtime), consistent with how the app already bundles fonts.

A 10-agent adversarial review verified no XSS through the math path and no missed render paths.

🤖 Generated with Claude Code

Render LaTeX in the shared MarkdownPreview component, which backs both the
timeline message renderer and the markdown file preview. Math is parsed with
remark-math and rendered with rehype-katex (KaTeX CSS imported); for the
allowHtml file-preview path, rehype-katex runs after rehype-sanitize so the
language-math wrappers the default schema preserves are rendered without
re-sanitizing KaTeX's own (trust:false) output.

GitHub-style delimiters: $x$ inline and $$x$$ block. Single-dollar math is on,
so a literal $ in prose is escaped with \$.

Also widen isGutterUtilityPath to accept the [EventTarget?] shape that
@types/node's web-globals give Event.composedPath(), clearing a pre-existing
typecheck failure.

Tests: inline/display/escaped-dollar/invalid-TeX and the allowHtml
math+sanitize path; added a "math (LaTeX)" story row. Full @bb/app suite and
build green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@SawyerHood SawyerHood merged commit f5efa19 into main Jun 29, 2026
6 checks passed
@SawyerHood SawyerHood deleted the bb/add-latex-support-thr_gfmrjs7vjp branch June 29, 2026 19:57
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.

Please add LaTeX support!

1 participant