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 or later, inherited from D2 v0.8.
Usage
import (
d2 "github.com/FurqanSoftware/goldmark-d2"
"github.com/d2lang/d2/d2layouts/d2dagrelayout"
"github.com/d2lang/d2/d2themes/d2themescatalog"
"github.com/yuin/goldmark"
)
goldmark.New(
goldmark.WithExtensions(&d2.Extender{
// Defaults when omitted
Layout: d2dagrelayout.DefaultLayout,
ThemeID: &d2themescatalog.CoolClassics.ID,
Sketch: false,
}),
).Convert(src, dst)
Every field is optional:
Layout selects the layout engine. Defaults to d2dagrelayout.DefaultLayout.
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.
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.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