Port StaticDiagramRenderable's left-drag scrolling and horizontal-wheel capture, record the layout dead ends that show the code as a deviation, and bring the design note up to date.
6 KiB
P.3 Mermaid plugin (rs-plugin-mermaid)
Rows F12a (core and flowcharts) and F12b (sequence, state, gantt, gitgraph, timeline). The plugin
opencode.merman renders mermaid fences in the transcript, as packages/merman/src/plugin.ts does.
Library choice
The run notes ask for a small plugin on a Rust library, and for a port of only the TS layout and drawing when no library draws close to TS. I looked at the Rust crates that turn Mermaid into terminal text (crates.io, 2026-11):
| Crate | What it is | Why not |
|---|---|---|
merman-core with merman-ascii or mermansi |
Mermaid parser (a port of mermaid.js) plus terminal adapters | Its own layout; merman-core is 0.10.0-alpha.1, a 1.1 MB crate that returns its model as serde_json::Value |
mmdflux |
Flowchart, class, state, sequence to text or SVG | Its own layered layout; no gantt, gitgraph, or timeline |
mermaid-text, termiflow, graphs-tui |
Flowchart-first text renderers | Their own layouts; the documented examples draw square ┌─┐ boxes and <Dec> diamonds |
All of them lay diagrams out their own way, so none can draw cells equal to TS: TS has its own terminal
layout (rounded node frames, source connectors with color fades, label lanes, responsive folding at
layoutMaxWidth), and P-COMP compares cells. So the layout and drawing are a port of
packages/merman/src.
The parser is ported too, not taken from merman-core, for three reasons:
- The streaming fallback depends on TS's parser errors. TS accepts a subset of Mermaid and throws
MermaidSyntaxErrorwith the line number of the first statement it does not support; the renderer then keeps the last good diagram or renders the lines before that line (markdown.ts:242-272). A full Mermaid parser accepts statements the layout cannot draw and has no such line. - The TS parsers are small (about 1,500 lines for all six kinds) next to the layout and routing
(about 9,000), and their output types are what the layout takes. Mapping
merman-core's JSON model to them would be about as much code as the parsers, plus the gaps between the two grammars. merman-coreis an alpha with a large dependency set. The port needs onlyfancy-regex(the TS flowchart edge pattern uses a lookahead),unicode-segmentation(TSIntl.Segmenter), andhtml-escape(TSentities.decodeHTMLStrict).
So the "library" is the port itself, kept as one crate with the TS module layout (core, flowchart,
sequence, state, gantt, gitgraph, timeline) so a TS change maps to one Rust file.
Port notes
- Text width follows TS
string-width7, notunicode-width:core/wide.rslists the code points it counts as two cells, generated fromstring-widthitself. It includes emoji such as▶and◀, which decide whether a wide state diagram folds. Math.roundisfloor(x + 0.5), and colors blend and round in bytes, as OpenTUI'sRGBA.fromIntsdoes.- TS compares edges, transitions, and bounds by object identity. Edges carry their parsed index, state transitions their index in the visible model, and the state layout keeps one arena of boxes that both its state and composite maps index, so a move shows through either map.
- Gantt uses the plugin's options only (track style, thin line, labels on the left, spaced sections, faint
track); the other
GanttDiagramRenderOptionsare not reachable from the plugin.
API
opencode_mermaid::plugin::<H: Host>() -> Definition<H>: registers themermaidcode-block renderer (P.1'sSetup::code_block, the U.4c hook). The TUI'shost::builtins()uses it foropencode.merman.opencode_mermaid::render(source, width, &Palette) -> Rendered: the plan'srender(lang, source, width, theme) -> cells, as styled lines.Rendered::Codeis TS'sundefined;Rendered::Syntax { kind, prefix }is a syntax error with the drawable prefix, if any.Rendereradds the streaming fallback;widget(app, &Diagram)is theTextit returns.Palette::from_theme(&Theme):resolveOpenCodeDiagramPalette. TS takes the theme mode from the context; the plugin API passes only the theme, so a page background above half luminance means light.
Widget
TS's StaticDiagramRenderable is a TextRenderable: full width, the diagram's height, no wrap, not
selectable, one row of top margin, and a left drag that scrolls it sideways. The Rust widget is a Text
with the same options and drag scrolling, with two differences that keep the cells equal:
- No top margin: Core's Markdown uses that margin as the gap before the block, while the Rust Markdown widget puts the gap between blocks itself.
- A white default fg, TS
TextRenderable's, which unstyled cells keep.
Streaming
TS keeps the last good diagram per fence (keyed by the block's id) and shows it while a later edit does
not parse. The U.4c hook gives the widget it returned before as CodeBlock::previous; the renderer
remembers each widget's diagram kind and returns the previous widget when the new source fails with a
syntax error of the same kind. opentui-rust's Markdown re-runs renderers when streaming ends, so a kept
preview does not outlive a broken fence.
Proof
- P-ORACLE
parity/oracle/P.3-mermaid.tscollects every Mermaid source inpackages/merman/src/**/*.test.tsand the layout-audit fixtures (template literals whose first meaningful line is a diagram header: 248 sources), renders each through TS'screateMermaidCodeBlockRendererat 120 and 60 columns with theopencodedark palette, and writes the styled lines (text, fg, bg, attributes per run) ornulltoparity/fixtures/P.3/corpus.json.crates/mermaid/tests/oracle.rsrenders the same sources in Rust and compares every run: 496 of 496 equal. - P-COMP
parity/component/P.3-markdown.tsx:TextPart's Markdown with a flowchart and a sequence fence through the real TS renderer, compared cell by cell incrates/mermaid/tests/markdown.rs. - Deviation:
parity/deviations/F12b-diagram-layout-errors.md(a layout dead end shows the code; TS throws).