render

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package render provides D2 diagram rendering using the d2 library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compile

func Compile(ctx context.Context, d2Code string) (*d2target.Diagram, error)

Compile parses D2 code and returns the compiled diagram (for validation/inspection).

func MustRender

func MustRender(d2Code string, format Format) []byte

MustRender renders D2 code and panics on error. Useful for tests.

func Quick

func Quick(d2Code string) ([]byte, error)

Quick renders D2 code to SVG with default options. Returns error on failure.

func QuickToString

func QuickToString(d2Code string) (string, error)

QuickToString renders D2 code to SVG string with default options.

func SVGDimensions

func SVGDimensions(svg []byte) (width, height int, err error)

SVGDimensions extracts width and height from rendered SVG.

func ThemeID

func ThemeID(name string) (int64, error)

ThemeID returns the theme ID for a theme name.

func ThemeNames

func ThemeNames() []string

ThemeNames returns a list of available theme names.

func Validate

func Validate(ctx context.Context, d2Code string) error

Validate checks if D2 code is valid without rendering.

Types

type Buffer

type Buffer struct {
	bytes.Buffer
}

Buffer is a convenience wrapper for rendering to a bytes.Buffer.

type Format

type Format string

Format represents the output format for rendering.

const (
	FormatSVG Format = "svg"
	FormatPNG Format = "png"
	FormatPDF Format = "pdf"
)

func FormatFromPath

func FormatFromPath(path string) (Format, error)

FormatFromPath infers the format from a file path extension.

func ParseFormat

func ParseFormat(s string) (Format, error)

ParseFormat parses a format string.

type Options

type Options struct {
	// ThemeID is the D2 theme to use (0 = default).
	ThemeID int64

	// Pad is the padding around the diagram in pixels.
	Pad int64

	// Sketch enables sketch/hand-drawn mode.
	Sketch bool

	// Center centers the diagram in the output.
	Center bool

	// Scale is the output scale factor (default 1.0).
	Scale float64
}

Options configures rendering behavior.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns default rendering options.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer renders D2 code to various output formats.

func New

func New() (*Renderer, error)

New creates a new Renderer.

func (*Renderer) Render

func (r *Renderer) Render(ctx context.Context, d2Code string, format Format, opts *Options) ([]byte, error)

Render compiles and renders D2 code to the specified format.

func (*Renderer) RenderPDF

func (r *Renderer) RenderPDF(ctx context.Context, d2Code string, opts *Options) ([]byte, error)

RenderPDF is a convenience method for rendering to PDF.

func (*Renderer) RenderPNG

func (r *Renderer) RenderPNG(ctx context.Context, d2Code string, opts *Options) ([]byte, error)

RenderPNG is a convenience method for rendering to PNG.

func (*Renderer) RenderSVG

func (r *Renderer) RenderSVG(ctx context.Context, d2Code string, opts *Options) ([]byte, error)

RenderSVG is a convenience method for rendering to SVG.

Jump to

Keyboard shortcuts

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