Skip to content

Render Mermaid diagrams in chat with Pele #8045

Open

Description

@vibegui

Proposal

Render fenced ```mermaid blocks as diagrams in chat and other Markdown surfaces, using Pele (repository).

Why

  • Agents already write Mermaid. Plans, architecture explanations, and flows come back as Mermaid. Today CodeBlock in apps/web/src/components/chat/markdown.tsx shows them as syntax-highlighted text, so the reader has to paste them somewhere else to see the diagram.
  • Pele is a better fit than Mermaid.js, according to its own comparison: about 230 kB gzip against 1.6 MB, render times under 1 ms against tens of ms, zero dependencies, MIT license, and support for every diagram type built into Mermaid 12.1.0.
  • Theming uses CSS variables (--pele-bg, --pele-fg, --pele-line, --pele-surface). They can map to our design tokens, so light and dark mode work without re-rendering.
  • Rendering is synchronous and cheap. That fits streamed messages: re-render on each update and keep the last good SVG while the fence is still incomplete.

Sketch

  1. In MemoizedMarkdown, send block.lang === "mermaid" to a lazy-loaded MermaidBlock, using the same split-chunk pattern as LazyHighlighter.
  2. MermaidBlock calls Pele's render() or mount() and inserts the SVG. If parsing fails, it falls back to the current CodeBlock.
  3. Keep the copy button, and add a toggle between the diagram and its source.
  4. Apply the same renderer to the shared packages/ui Markdown component, so documents and other surfaces get it too.

Security: required

Diagram source comes from model output, which can repeat untrusted content (issues, web pages, files). Pele's security page says what it already filters and what the app must handle:

  • Sanitize every SVG before it is inserted. Pass mount() our SVG sanitizer, or sanitize render() output ourselves. Either way, nothing goes into the page through raw innerHTML. No sanitizer is installed in this repository yet; DOMPurify's SVG profile is the obvious candidate.
  • Disable links and images. Images make network requests when the diagram is displayed, which would let a diagram send data out of the page.
  • Do not pass icons. Pele inserts icon markup without filtering it.
  • Show errors with textContent. Error messages can contain text from the diagram.
  • Never let diagram source set render options or options.config.
  • Keep Pele's default size limits (50k characters of source, 5k edges) or lower them.

Acceptance

  • Mermaid fences render as diagrams in chat, in light and dark mode.
  • Invalid or partial source falls back to the code block, with no console errors.
  • A test feeds a hostile diagram (script in a label, a javascript: link, an image URL, a click callback) and asserts the inserted DOM has no scripts, no event handlers, and no external URLs.
  • The new chunk loads only when a message contains a Mermaid block.

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions