feat(ui): add Mermaid diagram visualization in markdown#21497
Open
callmeYe wants to merge 8 commits intoanomalyco:devfrom
Open
feat(ui): add Mermaid diagram visualization in markdown#21497callmeYe wants to merge 8 commits intoanomalyco:devfrom
callmeYe wants to merge 8 commits intoanomalyco:devfrom
Conversation
Add interactive Mermaid diagram rendering for ```mermaid code blocks, inspired by Vercel's @streamdown/mermaid package. Features: - Lazy-load mermaid library (~3MB) on first use - Auto-detect dark/light theme for diagram rendering - Interactive controls: zoom (0.5x-3x), pan (drag), fullscreen, download (SVG/PNG/source), copy source - Fullscreen mode re-renders SVG to avoid DOM ID conflicts - Fix SVG width="100%" collapse in flex layouts via viewBox sizing - Streaming-compatible: incomplete code fences show raw code - Error handling with source fallback display Integration: - Intercept mermaid language in markedShiki highlight and highlightCodeBlocks (native parser path) - Skip mermaid blocks in ensureCodeWrapper (decorate) - Preserve rendered diagrams across morphdom updates - Async post-processing after morphdom Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
533e0c6 to
10e6c50
Compare
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Merged latest dev into feat/mermaid-upstream and regenerated bun.lock via `bun install` to resolve the lockfile conflict.
Author
|
/review |
The previous bun install runs on this branch picked up a local ~/.npmrc that points to the Alibaba intranet mirror (registry.anpm.alibaba-inc.com), which baked private registry URLs into ~3257 package entries in bun.lock. That makes the lockfile unreproducible for anyone outside the corp network and breaks upstream CI. Reset bun.lock to github/dev's baseline and re-ran `BUN_CONFIG_REGISTRY=https://registry.npmjs.org/ bun install` so the new mermaid dependency tree is added against the public registry. The only remaining diff vs dev is the mermaid packages plus the version bumps brought in by the merge from dev. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #
Type of change
What does this PR do?
Adds interactive Mermaid diagram visualization for
```mermaidcode blocks in the markdown renderer, inspired by Vercel's @streamdown/mermaid.Currently, mermaid code blocks are rendered as plain text (Shiki doesn't recognize the mermaid language and falls back to text). This PR intercepts mermaid code blocks in both
markedShikihighlight andhighlightCodeBlocks(native parser path), outputs a placeholder<div data-component="mermaid-diagram">, then asynchronously renders them as interactive SVG diagrams after morphdom DOM updates.The mermaid library (~3MB) is lazy-loaded on first use to avoid impacting initial bundle size.
Interactive features:
setPointerCapturefor reliable trackingdocument.body, re-renders SVG viamermaid.render()with a fresh ID to avoid DOM ID/CSS selector conflicts with the inline copy. Closes via Escape key or X button..mmdsource exportStreaming compatibility:
markdown-stream.tsalready splits incomplete code fences, so incomplete mermaid blocks display as raw code until the fence closes, then auto-render. Rendered diagrams are preserved acrossmorphdomupdates viaonBeforeElUpdatedreturning false fordata-mermaid-renderedelements.Key fix: Mermaid outputs
width="100%"on SVGs, which collapses to 0px in a pure flex layout (the fullscreen viewport).fixSvgSizing()replaces percentage width with the actual viewBox dimensions and constrains withmax-width: 90vw/max-height: 85vh.How did you verify your code works?
bun run typecheckinpackages/ui)Screenshots / recordings
N/A — this is best tested interactively. Send any message containing a
```mermaidcode block to see the rendered diagram with hover controls.Checklist
🤖 Generated with Claude Code