mermaid2d2

package module
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

mermaid2d2

Convert between D2 and Mermaid diagram syntax, as a Go library and a CLI (m2d2).

Coverage

✅ supported · 🚧 planned · ❌ no equivalent in the other format

Mermaid → D2
Diagram Maps to
flowchart / graph ✅ shapes · edge styles · subgraphs · colors
sequenceDiagram ✅ messages · loop/alt/opt groups · notes
stateDiagram-v2 ✅ states · transitions · composites · start/end
classDiagram ✅ members · UML arrowheads
erDiagram ✅ sql_table · keys · cardinalities
mindmap ✅ node tree · shapes
C4Context/C4Container/C4Component/C4Dynamic/C4Deployment ✅ elements → nodes · boundaries → containers · Rel → connections
pie · gantt · journey · xychart · gitGraph ❌ no D2 graph analog
D2 → Mermaid
Feature Maps to
nodes · containers · connections · direction ✅ flowchart
sql_table ✅ erDiagram · keys · cardinalities
class ✅ classDiagram · members · UML arrowheads
classes:/class: styling ✅ flowchart classDef/class
mixed sql_table/class/plain shapes ❌ no single Mermaid diagram type
grids ❌ no Mermaid equivalent
Features
Label quoting (special chars) ✅ auto-quoted
Flowchart classDef/class colors ✅ ↔ D2 classes
State / class diagram notes ✅ tooltip on the target; standalone class notes as floating nodes
Inline style · :::class ✅ ↔ style.* on the node · :::x → class: x (Mermaid → D2)
Sequence note side · spans · mindmap icons ❌ no D2 counterpart
C4 diagram title · sprites · tags · links · UpdateElementStyle/UpdateRelStyle ❌ no D2 counterpart; D2 has no native C4 notation

Examples

Each example is the Mermaid source — rendered inline by GitHub — followed by the D2 that m2d2 produces from it, rendered to SVG. Click a diagram to open it full size. Regenerate the renders with docs/examples/generate.sh.

The renders use d2 --layout=elk. Layout is the renderer's choice rather than anything this tool emits, but ELK places a composite state's terminal node below its container, where the default dagre layout puts it off to one side.

Flowchart
flowchart LR
    Start[Start] --> Parse{Valid?}
    Parse -->|yes| Build[Build graph]
    Parse -->|no| Fail[Report error]
    subgraph pipeline[Conversion pipeline]
        Build --> Emit[Emit D2]
    end
    Emit --> Done[Done]
    classDef error fill:#fdd,stroke:#c00,stroke-width:2px,color:#333
    class Fail error
m2d2 -to d2 output
direction: right
classes: {
  error: {
    style: {
      fill: "#fdd"
      stroke: "#c00"
      stroke-width: 2
      font-color: "#333"
    }
  }
}
Parse: Valid? {shape: diamond}
Fail: Report error {class: error}
pipeline: Conversion pipeline {
  Build: Build graph
  Emit: Emit D2
  Build -> Emit
}
Start -> Parse
Parse -> pipeline.Build: yes
Parse -> Fail: no
pipeline.Emit -> Done

D2 flowchart

Sequence
sequenceDiagram
    participant C as Client
    participant S as Server
    C->>S: GET /diagram
    Note over S: cache miss
    loop retry on failure
        S->>S: render
    end
    alt success
        S-->>C: 200 OK
    else error
        S-->>C: 500 Internal
    end
m2d2 -to d2 output
shape: sequence_diagram
C: Client
S: Server
C -> S: GET /diagram
S.note_1: cache miss
loop_1: {
  label: retry on failure
  S -> S: render
}
alt_1: {
  label: alt
  case_1: {
    label: success
    S -> C: 200 OK
  }
  case_2: {
    label: error
    S -> C: 500 Internal
  }
}

D2 sequence diagram

Entity relationship
erDiagram
    CUSTOMER {
        int id PK
        string name
        string email UK
    }
    ORDER {
        int id PK
        int customer_id FK
    }
    CUSTOMER ||--o{ ORDER : places
m2d2 -to d2 output
CUSTOMER: {
  shape: sql_table
  id: int {constraint: primary_key}
  name: string
  email: string {constraint: unique}
}
ORDER: {
  shape: sql_table
  id: int {constraint: primary_key}
  customer_id: int {constraint: foreign_key}
}
CUSTOMER -> ORDER: places {source-arrowhead: {label: 1}; target-arrowhead: {label: 0..N}}

D2 ER diagram

Class
classDiagram
    class Order {
        +String id
        +submit(String coupon) bool
    }
    Entity <|-- Order
    Order *-- LineItem
    Order --> Customer : placed by
m2d2 -to d2 output
Order: {
  shape: class
  +id: String
  +submit(String coupon): bool
}
Entity <-> Order: {source-arrowhead: {shape: triangle; style.filled: false}; target-arrowhead: {shape: none}}
Order <-> LineItem: {source-arrowhead: {shape: diamond; style.filled: true}; target-arrowhead: {shape: none}}
Order -> Customer: placed by

D2 class diagram

State
stateDiagram-v2
    [*] --> Active
    Active --> [*]
    state Active {
        [*] --> Idle
        Idle --> Running: start
        Running --> Idle: stop
    }
m2d2 -to d2 output
start: "" {shape: circle; width: 20; height: 20}
start -> Active
end: "" {shape: circle; style.double-border: true; width: 24; height: 24}
Active -> end
Active: {
  start: "" {shape: circle; width: 20; height: 20}
  start -> Idle
  Idle -> Running: start
  Running -> Idle: stop
}

D2 state diagram

CLI

go install github.com/noamsto/mermaid2d2/cmd/m2d2@latest

m2d2 diagram.d2                 # -> Mermaid on stdout (target inferred from .d2)
m2d2 diagram.d2 -o diagram.mmd  # write to a file
cat diagram.d2 | m2d2 -to mermaid -   # read from stdin

The target format is inferred from the input extension (.d2 → mermaid, .mmd/.mermaid → d2) unless -to is given.

Library

import "github.com/noamsto/mermaid2d2"

out, err := mermaid2d2.D2ToMermaid(src)
d2, err := mermaid2d2.MermaidToD2(src)

Parsing uses sammcj/mermaid-check.

Development

The repo ships a Nix flake with a Go devShell, formatting, and pre-commit hooks:

direnv allow   # or: nix develop
go test ./...

Documentation

Overview

Package mermaid2d2 converts between Mermaid and D2 diagram syntax.

Conversion is source-to-source in both directions and driven by two functions: MermaidToD2 and D2ToMermaid. Neither renders anything; the output is diagram source for the other tool to lay out.

Mermaid flowchart, sequenceDiagram, stateDiagram-v2, classDiagram, erDiagram, mindmap, and the C4 family convert to D2. Diagram types with no D2 graph analog (gantt, pie, journey, xychart, gitGraph) return an error rather than a mangled approximation.

The reverse direction infers the Mermaid diagram type from the D2 shapes present: sql_table shapes become an erDiagram, class shapes a classDiagram, and anything else a flowchart.

Both directions are lossy where the formats disagree, and each function documents what it drops.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func D2ToMermaid

func D2ToMermaid(src string) (string, error)

D2ToMermaid parses D2 source and emits the Mermaid diagram it maps to.

The Mermaid diagram type is inferred from the shapes present: sql_table shapes emit an erDiagram, class shapes emit a classDiagram, and anything else emits a flowchart (containers become subgraphs, connections become edges, classes:/class: styling becomes classDef/class, and the board direction sets orientation). A graph that mixes sql_table or class shapes with each other or with plain nodes/containers has no single Mermaid diagram type and returns an error. D2 features with no Mermaid equivalent in the chosen diagram type (grids, and — outside flowcharts — arrows, styling, containers) are dropped.

func MermaidToD2

func MermaidToD2(src string) (string, error)

MermaidToD2 parses Mermaid source and emits equivalent D2.

Flowchart nodes, edges, and subgraphs map onto D2 shapes, connections, and containers; sequence diagrams map onto a D2 sequence_diagram, with loop/alt/ opt blocks becoming labeled groups; state diagrams map onto nodes and connections, with composite states becoming containers; class diagrams map onto D2 class shapes, with relationships becoming connections whose arrowheads encode the UML relation type; ER diagrams map onto D2 sql_table shapes, with attributes as typed columns and relationships as connections; mindmaps map onto a D2 tree of nodes connected to their parents; C4 diagrams (Context, Container, Component, Dynamic, Deployment) map onto D2 nodes and containers, with Rel(...) relationships becoming connections. Diagram types with no D2 equivalent (gantt, pie, journey, ...) return an error rather than being mangled. Sequence notes become D2 notes; state and class notes render as a tooltip attribute on their target node.

Types

This section is empty.

Directories

Path Synopsis
cmd
m2d2 command
Command m2d2 converts between D2 and Mermaid diagram syntax.
Command m2d2 converts between D2 and Mermaid diagram syntax.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址