Skip to content

feat(core): render mermaid diagrams with zoom, pan, and copy controls - #192

Merged
cpojer merged 3 commits into
nkzw-tech:mainfrom
bkrem:feat/mermaid-diagrams
Oct 30, 2026
Merged

cpojer merged 3 commits into
nkzw-tech:mainfrom
bkrem:feat/mermaid-diagrams

Conversation

@bkrem

@bkrem bkrem commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Why

GitHub renders ```mermaid fences as diagrams, so PR descriptions and comments often include them. Codiff shows them as raw code.

What changed

This PR renders them as diagrams in PR/MR descriptions, comments, and shared plans. Markdown editors keep showing the code.

flowchart LR
    A[mermaid fence] --> B{Read-only view?}
    B -- No --> C[Code block]
    B -- Yes --> D[Lazy-load Mermaid and render]
    D -- SVG --> E[Diagram with controls]
    D -- Parse error --> F[Code block plus error note]
Loading
  • A code-block descriptor outranks CodeMirror's for the mermaid language. Only ReadOnlyMarkdownView turns it on, through a renderDiagrams prop.
  • Hovering a diagram shows GitHub's zoom, reset, and pan buttons, plus Copy code and Show source. ⌘/Ctrl+scroll zooms around the cursor, dragging pans, and double-click resets. Plain scrolling still scrolls the page.
  • Diagrams follow the light and dark themes. Renders go through one queue, because Mermaid's config is global, and results land in an LRU cache of 50.
  • If Mermaid can't parse a fence, the code block stays, with "Unable to render Mermaid diagram" under it.

Security

Each diagram is an <img> with an SVG data URL, so nothing inside the SVG can run. Comments come from any GitHub user and the renderer can reach the preload bridge, so this sits on top of Mermaid's securityLevel: 'strict' instead of relying on it.

Bundle size

Nothing loads until a page has a Mermaid fence. Incremental cost, measured on dist/assets from vpr build:

Loads on Raw Gzip
First diagram (Mermaid core) 668 kB 166 kB
First flowchart (diagram plus dagre) 149 kB 52 kB
First sequence diagram 118 kB 31 kB

Mermaid 12 lays diagrams out with ELK by default, which adds 1,468 kB (451 kB gzip). The renderer sets layout: 'dagre', so ELK loads only for diagrams that ask for it, such as flowchart-elk.

Verification

  • vp test: 1,314 passed, 4 skipped. The new tests in core/__tests__/mermaid.test.ts and core/__tests__/ReadOnlyMarkdownView.test.tsx mock only mermaid.
  • vp check and vpr build pass.

Preview

Screen.Recording.2026-10-30.at.14.09.31.mov

bkrem added 3 commits October 30, 2026 14:15
Render ```mermaid fences as diagrams in every read-only Markdown view
(PR bodies, PR and review comments, Markdown previews, shared plans).
Editors keep showing the fenced code.

- Register a code-block descriptor that outranks CodeMirror for the
  `mermaid` language, only when `renderDiagrams` is set.
- Load `mermaid` lazily on the first diagram, render one diagram at a
  time, and cache the last 50 results per theme.
- Render with `securityLevel: 'strict'` and the `dagre` layout, so the
  1.5 MB ELK chunk only loads when a diagram asks for it.
- Show the SVG through an `<img>` data URL, so no script inside it can
  run; fall back to the code block plus an error note on failure.

Claude-Session: https://claude.ai/code/session_014TASJcnCpJt1rDvM2zAE71
Rendered diagrams gain GitHub's viewer controls: zoom in, zoom out,
reset, and four pan arrows in a 3x3 grid at the bottom right. Drag pans,
Cmd/Ctrl+scroll or a trackpad pinch zooms around the cursor, and
double-click resets. Plain scrolling still scrolls the page.

A top-right cluster adds Copy code and a Show source toggle that swaps
the diagram for its read-only code block. Both clusters appear on hover
or keyboard focus.

The view is a CSS transform on the existing img data URL, so zooming
never changes layout height. CopyCodeButton moves into its own module
so collapsibles and diagrams share it.

Claude-Session: https://claude.ai/code/session_014TASJcnCpJt1rDvM2zAE71
Ctrl+wheel gets a 10x boost so trackpad pinches feel responsive, but a
Ctrl+mouse-wheel notch on Windows or Linux sends a delta of about 100,
which zoomed 4x per notch. Each wheel event now zooms at most 2x in or
out.

Claude-Session: https://claude.ai/code/session_014TASJcnCpJt1rDvM2zAE71

@cpojer cpojer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@cpojer
cpojer merged commit 6f4a1ed into nkzw-tech:main Oct 30, 2026
1 check passed
@bkrem
bkrem deleted the feat/mermaid-diagrams branch October 30, 2026 07:54
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.

2 participants