d2

package module
v0.0.0-...-97c73e7 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 16 Imported by: 10

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 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

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
}

func (*Extender) Extend

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

type HTMLRenderer

type HTMLRenderer struct {
	Layout  d2graph.LayoutGraph
	ThemeID *int64
	Sketch  bool
}

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