Skip to content

fix: render LaTeX math in chat messages - #46

Closed
xiuguangli wants to merge 1 commit into
koltyakov:mainfrom
xiuguangli:fix/render-latex-math
Closed

xiuguangli wants to merge 1 commit into
koltyakov:mainfrom
xiuguangli:fix/render-latex-math

Conversation

@xiuguangli

@xiuguangli xiuguangli commented Oct 22, 2026 •

Copy link
Copy Markdown

Closes #45

Summary

Chat currently renders $...$ and $$...$$ as raw text. This adds KaTeX rendering for inline and display math, including the display form models commonly emit where the delimiters share lines with the formula:

$$\mu_t=...
...\Sigma_t=...$$

Changes

  • Add marked-katex-extension and katex, and render math in MarkdownRenderer.
  • Add a block rule for $$...$$ when the delimiters share lines with the formula. The extension only accepts delimiters on their own lines by default.
  • Re-render each formula from its sanitized TeX annotation after DOMPurify. This keeps KaTeX positioning styles without allowing arbitrary style attributes through the general Markdown sanitizer.
  • Bundle KaTeX CSS and fonts in the webview and allow font-src from the webview resource source.
  • Keep math out of path and session-reference linkification.

Before / After

Before:

Chat showing raw LaTeX delimiters and commands

After:

Chat showing the same formulas rendered with KaTeX

Verification

  • npm run lint
  • npm run fmt
  • npm run typecheck
  • npm run typecheck:suggestions
  • npx vitest run src/webview/components/MarkdownRenderer.test.ts src/webview/components/MarkdownRenderer.perf.test.ts src/webview/components/MarkdownRenderer.lazy-highlight.test.tsx src/webview/perf/markdown-renderer.perf.test.ts src/extension/webview-html.test.ts (231 tests)
  • npm test (10,460 passed, 51 skipped). The only failure is src/extension/open-code-process.test.ts > rejects public and symlinked default ownership records without altering them, which also fails on unmodified main in this environment.
  • Manual check at a 760 px chat width with the formulas from Chat shows LaTeX math as raw text ($...$ / $$...$$ not rendered) #45.

Notes

  • $...$ keeps the extension's boundary rules, so currency such as $5 is not treated as math.
  • Math inside code spans and fenced code blocks stays literal.
  • During streaming, a formula renders after its closing delimiter arrives.
  • katex is pinned to 0.18.10 because marked-katex-extension supports <0.19; mermaid keeps its existing 0.19.0 override, so npm nests that copy.

- Render inline and display math with KaTeX
- Accept display delimiters that share lines with the formula
- Restore KaTeX layout after DOMPurify sanitization
- Bundle KaTeX assets and allow them in the webview CSP
@koltyakov

Copy link
Copy Markdown
Owner

Thank you @xiuguangli for the contribution! I merged the feature along with fixing a few small bugs it contained; performance optimizations and streaming requirements. More details: #45 (comment)

@koltyakov koltyakov closed this Oct 22, 2026
koltyakov added a commit that referenced this pull request Oct 22, 2026
Bundle KaTeX and local fonts for inline and display math, with horizontal scrolling for wide equations. Keep code and currency literal and render TeX with trust disabled.

Show a fixed-size streaming indicator while formulas and other Markdown syntax are incomplete, preserving settled content as more text arrives.

Reuse finalized Markdown across virtual remounts, batch scroll-mounted row measurements before saving anchors, and use exact content heights during text entrance animations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat shows LaTeX math as raw text ($...$ / $$...$$ not rendered)

2 participants