Skip to content

Diagram cells, and Mermaid drawn on par with D2 - #16

Open
ewiger wants to merge 5 commits into
mainfrom
feat/mermaid-diagrams
Open

ewiger wants to merge 5 commits into
mainfrom
feat/mermaid-diagrams

Conversation

@ewiger

@ewiger ewiger commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Mermaid becomes a diagram language with the same standing as D2, and both are drawn as diagram cells.

What changes for a user

  • A fence that opens mermaid#show or d2#show is a diagram cell. The preview draws it like an HQL cell: the drawing first, the source behind Code, Edit beside it, and Show in place of Run.
  • Both languages are drawn as the source is typed. When an edit leaves source that does not draw, the cell keeps its last drawing, marked stale, with the reason under it. Show retries.
  • Every drawing has three small zoom buttons at its top right edge: smaller, the current size (which resets it), and larger. A zoomed drawing scrolls inside its own box.
  • A plain mermaid fence is a listing, shown as code. A plain d2 fence still draws, so nothing written before this stops drawing.
  • The site build draws the same cells with the same renderers.

The renderer

Mermaid is drawn by merman-cli 0.11.0, a native renderer with no browser and no Node runtime. It is run the way d2 is: source on stdin, SVG on stdout, delivered as a data: image.

merman-cli render - --format svg --svg-pipeline resvg-safe \
    --resource-profile constrained --operation-timeout-ms 2000 --quiet

Measured on macOS arm64 with six diagram types:

Result
Time for one render about 10 ms, against a 2 s bound
Labels SVG text with resvg-safe; the default uses foreignObject, which an image does not show
Determinism the same bytes on every run
Syntax error exit 1 and a one-line reason

It is pinned through toolchain 0.7.0, which carries it for all five platforms. The toolchain's release check ran it on each platform. Every platform build of the extension bundles it, and each build is about 26 MiB larger. On Linux the bundled renderer needs glibc 2.35 or later.

By tool

  • @hypermarkdown/core: DIAGRAM_CELL_KINDS, diagramLanguage, diagramEngine, isDiagramCell, scanDiagramCells. DiagramBlock gains drawnFrom and current, a cell is keyed by position, and IR_VERSION is 5.
  • VS Code extension: one engine runs both renderers; the setting hyperMarkdown.diagram.mermaidPath; the command and code lens Show Diagram; the showDiagram message, validated like every inbound message; zoom; and examples/diagrams, opened with npm run example:diagrams.
  • hmd: MermaidEngine beside D2Engine on one base class; the MkDocs plugin draws mermaid#show and d2#show.
  • CI and release: CI and the Pages deploy check merman-cli --version beside d2 --version; CI sets MERMAN_BIN; the release workflow checks each VSIX for the binary.
  • Records: HMD-0034 is new and accepted. HMD-0030 is amended, because its first draft forbade drawing while the author types on the assumption that Mermaid needed a headless browser. The rejection of Mermaid is reversed in the source sketch, HMD-0001, and HMD-0022. The Features page draws the same picture in both languages.

Still open

  • Turning a plain d2 fence into a listing. It waits on HQL writing d2#show for a graph.
  • HQL's mermaid format step writes a plain mermaid fence, so the output of that step is a listing until HQL writes mermaid#show.

Both are rows in the trackers.

Tests

  • npm test with HQL_BIN and MERMAN_BIN set: 306 tests in the core and 197 in the extension, none skipped.
  • uv run python -m pytest with the pinned toolchain on PATH: 933 pass, none skipped.
  • uv run hmd lint --root doc --strict is clean, and uv run mkdocs build --strict builds and draws the Mermaid example.

A fence that opens `d2#show` or `mermaid#show` is a diagram cell. The
preview draws it the way it draws an HQL cell: the drawing first, the
source behind Code, Edit beside it, and Show in place of Run. A plain
`mermaid` fence is a listing. A plain `d2` fence still draws.

Mermaid is drawn by merman-cli, a native renderer that needs no browser.
It is run the way d2 is: source on stdin, SVG on stdout, within the same
64 KiB, 2 s and 64-entry bounds, delivered as a data: image. It is asked
for the resvg-safe pipeline, because Mermaid's default HTML labels do not
show inside an image.

Both languages are drawn as the source is typed. When the source stops
drawing, the cell keeps its last drawing, marked stale, with the reason
under it, and Show retries.

- ts-core: DIAGRAM_CELL_KINDS, diagramLanguage, diagramEngine,
  scanDiagramCells; DiagramBlock gains drawnFrom and current, a cell is
  keyed by position, and IR_VERSION is 5.
- vsc-ext: one engine for both renderers, the
  hyperMarkdown.diagram.mermaidPath setting, the Show Diagram command and
  code lens, and the showDiagram message.
- hmd: MermaidEngine beside D2Engine, and the site build draws
  mermaid#show and d2#show cells.
HMD-0034 records the renderer choice with its measurements: merman-cli,
run with the resvg-safe pipeline under the bounds D2 has.

HMD-0030 is amended: a diagram cell is drawn as its source is edited, in
both languages, and stale means the edited source does not draw. The
rejection of Mermaid as superseded by D2 is reversed in the source
sketch, in HMD-0001, and in HMD-0022.

The trackers move the rows this branch closes to Done. What remains is
recorded as blocked on a toolchain release that pins merman-cli: the
extension build, CI and the Pages build, and the public pages.
Every drawing in the preview, in D2 and in Mermaid, has three small
buttons at its top right edge: smaller, the current size, which resets
it, and larger. A zoomed drawing scrolls inside its own box, and a cell
keeps its zoom when its source is edited.

examples/diagrams is a vault to test diagram cells by hand: the same
picture in both languages, a listing, a Mermaid gallery, and a cell in
each state. Open it with npm run example:diagrams.

HMD-0034 is accepted, and the Mermaid card moves to doing.
Every platform build of the extension now holds merman-cli 0.11.0, so a
Mermaid cell draws on a fresh install. CI and the Pages deploy check
merman-cli beside d2, and CI sets MERMAN_BIN so the engine's tests run
against the real renderer. The release workflow checks each VSIX for
the binary.

The Features page draws the same picture in D2 and in Mermaid, and the
READMEs and the tools page name Mermaid and its renderer. The three
tracker rows that waited on the toolchain release are done.

This branch has not been deployed

No deployments
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.

1 participant