Help us shape the future of Go! Take the 2026 Go Developer Survey

d2render

package
v1.152.6 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package d2render renders a D2 diagram source to SVG in the platform (#2302), with D2's own Go library and its dagre layout.

It renders on the server because D2's browser build loads its layout engine with new Function in a blob: worker, which the share viewer's policy does not allow, and the same SVG then serves the asset page, both kinds of share link, the tile and the PDF alike. A source is held to the save-time check (internal/sourcecheck) before it is stored, so the render sees no import and no URL other than a managed resource's.

A render runs dagre's JavaScript inside goja, the Go JavaScript runtime D2 uses; the diagram source is never script. Each render is bounded by the source size, a timeout and how many run at once, and a rendered diagram is kept by its source's hash so a page viewed again does not render again.

Index

Constants

View Source
const (
	// MaxSourceBytes is the largest source rendered.
	MaxSourceBytes = 256 << 10
	// MaxSVGBytes is the largest drawing returned.
	MaxSVGBytes = 16 << 20
	// Timeout bounds one render.
	Timeout = 10 * time.Second
)

Variables

View Source
var ErrTimeout = errors.New("d2render: the diagram did not finish rendering in time")

ErrTimeout is returned for a render that did not finish in time.

View Source
var ErrTooLarge = errors.New("d2render: the diagram is too large to render")

ErrTooLarge is returned for a source or drawing past its bound.

Functions

func Render

func Render(ctx context.Context, src string, dark bool) ([]byte, error)

Render returns the SVG drawing of src, in D2's default light theme or its dark one.

func Serve

func Serve(w http.ResponseWriter, r *http.Request, name, contentType string, source []byte) bool

Serve answers ?render=svg on a content route for a D2 source with the SVG rendered from it (&theme=dark for the dark drawing), and reports whether it answered. The source is the one the route already read and authorized, its references rewritten, so an icon names the reference route. The drawing is served through blobserve like any stored SVG: an attachment under the sandbox policy, which the portal reads as text and cleans before drawing.

Types

This section is empty.

Jump to

Keyboard shortcuts

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