Goldmark D2

Goldmark D2 is a Goldmark extension providing diagram support through D2.
Install
go get github.com/FurqanSoftware/goldmark-d2
Requires Go 1.27.4 or later, inherited from D2 v0.10.
Usage
import (
d2 "github.com/FurqanSoftware/goldmark-d2"
"github.com/d2lang/d2/d2themes/d2themescatalog"
"github.com/yuin/goldmark"
)
goldmark.New(
goldmark.WithExtensions(&d2.Extender{
// Defaults when omitted
Layout: nil, // per-diagram, Dagre unless the block says otherwise
ThemeID: &d2themescatalog.CoolClassics.ID,
Sketch: false,
FS: nil, // imports disabled
}),
).Convert(src, dst)
Every field is optional:
Layout forces a layout engine on every diagram, and accepts any d2graph.LayoutGraph, including a configured one such as d2elklayout.Layout bound to its ConfigurableOpts. When omitted, each block is laid out by the engine it names itself (see below), which is Dagre unless stated otherwise.
ThemeID selects a theme; see the d2themescatalog package for the catalog. Defaults to CoolClassics.
Sketch renders diagrams in a hand-drawn style. Defaults to false.
FS is the fs.FS that D2 imports (...@component, x: @component) are resolved against. Defaults to nil, which disables imports, so a block that imports another file falls back to its source.
Imports
To let diagrams import other D2 files, give the extender a filesystem holding only those files, such as an embed.FS, or D2's localfile.Rooted for a directory on disk:
import "github.com/d2lang/d2/lib/localfile"
policy, err := localfile.Rooted("diagrams")
if err != nil {
// ...
}
defer policy.Close()
goldmark.New(
goldmark.WithExtensions(&d2.Extender{
FS: policy.FilesWithExtension(".d2"),
}),
)
Imports are resolved relative to the root of FS. Don't hand untrusted Markdown a filesystem that holds anything you wouldn't publish.
Layout engine
A block picks its own engine with D2's layout-engine config. dagre, elk, and tala are all built in:
```d2
vars: {
d2-config: {
layout-engine: elk
}
}
a -> x
b -> x
c -> x
```
Setting Layout on the extender overrides this for every block.
Output
Each d2 block is replaced by an inline SVG wrapped in a <div class="d2">, which you can target for styling. The SVG carries its own <style>, so no additional CSS is needed.
A block that D2 cannot compile falls back to its source, HTML-escaped, in a <pre><code> inside the same wrapper. Convert still succeeds, so one bad diagram does not take down the rest of the document.
Example
The following diagram shows the important link between the letters X and Y:
```d2
x -> y
```
|

|
|
{Sketch: true}
```d2
dogs -> cats -> mice: chase
replica 1 <-> replica 2
a -> b: To err is human, to moo bovine {
source-arrowhead: 1
target-arrowhead: * {
shape: diamond
}
}
```
|

|
Upgrading
As of D2 v0.10, D2 disables imports unless it is given a filesystem. If your diagrams import other D2 files, set FS on the extender.
As of D2 v0.8, D2 moved from oss.terrastruct.com/d2 to github.com/d2lang/d2. If you set Layout or ThemeID, update your own imports to match.
More Goldmark Extensions