feat: add inline Mermaid diagram rendering#1158
feat: add inline Mermaid diagram rendering#1158chriswingler wants to merge 1 commit intoMoonshotAI:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds inline rendering of Mermaid diagrams from fenced code blocks (```mermaid), replacing plain syntax-highlighted text with interactive SVG visualizations. The implementation uses lazy-loading to avoid impacting the initial bundle size and integrates cleanly with the existing markdown rendering pipeline.
Changes:
- Added
mermaidpackage dependency (version 11.4.1) for diagram rendering - Created new
MermaidDiagramcomponent with fullscreen modal, copy functionality, and error handling - Modified
StreamdownCodeto interceptmermaidlanguage code blocks and route them to the diagram renderer via Suspense boundary
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 11 comments.
| File | Description |
|---|---|
web/package.json |
Adds mermaid library dependency (v11.4.1) for diagram rendering |
web/src/components/ai-elements/mermaid-diagram.tsx |
New component providing diagram rendering, modal view, error handling, and copy functionality with lazy-loaded mermaid library |
web/src/components/ai-elements/streamdown.tsx |
Intercepts mermaid code blocks in markdown and routes to MermaidDiagram component via lazy-loading with Suspense fallback |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3c28a89e68
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
3c28a89 to
bd4470b
Compare
Summary
```mermaidfenced code blocks as interactive SVG diagrams instead of syntax-highlighted textReact.lazy()+ dynamicimport()so the initial bundle size is unaffectedMotivation
When LLM assistants generate Mermaid diagrams in chat responses, they currently appear as plain syntax-highlighted code. This PR enables visual rendering of those diagrams inline, making flowcharts, sequence diagrams, class diagrams, and other Mermaid diagram types immediately useful.
Changes
web/package.jsonmermaiddependencyweb/src/components/ai-elements/mermaid-diagram.tsxMermaidDiagram+MermaidModalcomponentsweb/src/components/ai-elements/streamdown.tsxlanguage === "mermaid"inStreamdownCodeand route toMermaidDiagramviaSuspenseArchitecture
Key design decisions:
Lazy loading — Mermaid is ~2MB. Using
React.lazy()for the component and dynamicimport()for the library ensures zero impact on initial load.Intercept at StreamdownCode — Cleanest integration point. No changes to
CodeBlockorMessageResponse. Follows the same pattern as the existinghtmlpreview special-case inCodeBlock.Reuse existing UI primitives — Uses the project's
Dialog,Button,Tooltipcomponents andcn()utility. MatchesCodeBlock's styling patterns (border-term-border,bg-card, hover-reveal toolbar).Dark theme — Configured for readability on dark backgrounds, matching the existing code block aesthetic.
Supported diagram types
All Mermaid diagram types are supported: flowchart, sequence, class, state, ER, gantt, pie, git graph, mindmap, timeline, quadrant, requirement, C4, and more.
Test plan
```mermaidcode blocks render as SVG diagramsnpm run buildsucceedsnpm run typecheckpassesnpm run lintpasses🤖 Generated with Claude Code