d2

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 19 Imported by: 12 modules, 10 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

Goldmark D2

Go Reference

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var KindBlock = ast.NewNodeKind("Block")

Functions

This section is empty.

Types

type Block

type Block struct {
	ast.BaseBlock
}

func (*Block) Dump

func (n *Block) Dump(source []byte, level int)

func (*Block) IsBlank

func (n *Block) IsBlank(source []byte) bool

func (*Block) Kind

func (n *Block) Kind() ast.NodeKind

type Extender

type Extender struct {
	Layout  d2graph.LayoutGraph
	ThemeID *int64
	Sketch  bool
	// FS resolves the files that diagrams import. Nil disables imports.
	FS fs.FS `json:"-"`
}

func (*Extender) Extend

func (e *Extender) Extend(m goldmark.Markdown)

type HTMLRenderer

type HTMLRenderer struct {
	Layout  d2graph.LayoutGraph
	ThemeID *int64
	Sketch  bool
	// FS resolves the files that diagrams import. Nil disables imports.
	FS fs.FS
}

func (*HTMLRenderer) RegisterFuncs

func (r *HTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer)

func (*HTMLRenderer) Render

func (r *HTMLRenderer) Render(w util.BufWriter, src []byte, node ast.Node, entering bool) (ast.WalkStatus, error)

type Transformer

type Transformer struct {
}

func (*Transformer) Transform

func (s *Transformer) Transform(doc *ast.Document, reader text.Reader, pctx parser.Context)

Jump to

Keyboard shortcuts

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