d2compiler

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MPL-2.0 Imports: 28 Imported by: 8 modules, 0 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.

Documentation

Overview

Package d2compiler implements a parser, compiler and autoformatter for the Terrastruct d2 diagramming language.

Index

Constants

This section is empty.

Variables

View Source
var FullToShortLanguageAliases map[string]string
View Source
var ShortToFullLanguageAliases = map[string]string{
	"md":  "markdown",
	"tex": "latex",
	"js":  "javascript",
	"go":  "golang",
	"py":  "python",
	"rb":  "ruby",
	"ts":  "typescript",
}

TODO add more, e.g. C, bash

Functions

func Compile

func Compile(p string, r io.Reader, opts *CompileOptions) (*d2graph.Graph, *d2target.Config, error)

func WithWarningHandler added in v0.10.1

func WithWarningHandler(ctx context.Context, handler func(d2ast.Error)) context.Context

WithWarningHandler attaches a destination for non-fatal source diagnostics. Compilations without a handler remain silent.

Types

type CompileOptions

type CompileOptions struct {
	// Context stops parsing and compilation when canceled. A nil Context is treated as
	// context.Background().
	Context  context.Context
	UTF16Pos bool
	// CheckSourceWarnings checks directly authored declarations for likely mistakes.
	// Diagnostics go to the handler installed by WithWarningHandler.
	CheckSourceWarnings bool
	// WarnUnmatchedGlob and WarnOverwrittenStyle enable additional source checks.
	WarnUnmatchedGlob    bool
	WarnOverwrittenStyle bool
	// WarningLayout is an explicit caller override of the in-source layout engine.
	WarningLayout string
	// AnimateDuration overrides the root animate-duration, in milliseconds.
	AnimateDuration *int64
	// AnimateTweens controls sampling of step and scenario transitions. Nil follows
	// the root animate-interval. False compiles only the authored endpoints.
	AnimateTweens *bool
	// MaxVariableExpansion bounds work added by variable substitutions and the
	// automatic copies they induce. Zero uses d2ir.DefaultMaxVariableExpansion.
	MaxVariableExpansion int64
	// MaxGlobExpansion bounds work performed by glob matching and
	// materialization. Zero uses d2ir.DefaultMaxGlobExpansion. Explicit source
	// fields are not counted as materialization work.
	MaxGlobExpansion int64
	// MaxEdgeExpansion bounds distinct edge-segment and endpoint combinations
	// considered by edge globs. Zero uses d2ir.DefaultMaxEdgeExpansion. Explicit
	// edges do not consume this budget.
	MaxEdgeExpansion int64
	// MaxEdgeExpansionWork bounds all endpoint-pair examinations performed by
	// edge globs, including lazy replays. Zero uses the secure compiler default.
	MaxEdgeExpansionWork int64
	// FS is the file system used for resolving imports in the D2 text. Nil
	// disables imports. Callers that accept untrusted input should prefer a
	// filesystem constrained to the intended import root; lib/localfile provides
	// rooted and explicit unrestricted host-filesystem policies.
	FS fs.FS
}

func (*CompileOptions) EnableWarning added in v0.10.1

func (opts *CompileOptions) EnableWarning(name string) error

EnableWarning enables a named optional rule. Validate also runs the default rules.

type ValidationDiagnostic added in v0.10.1

type ValidationDiagnostic struct {
	Severity  string `json:"severity"`
	Code      string `json:"code,omitempty"`
	File      string `json:"file,omitempty"`
	Line      int    `json:"line,omitempty"`
	Column    int    `json:"column,omitempty"`
	EndLine   int    `json:"end_line,omitempty"`
	EndColumn int    `json:"end_column,omitempty"`
	Message   string `json:"message"`
}

ValidationDiagnostic locates a finding in the input or an import. Lines and columns start at one, and the end is exclusive. Columns use UTF-8 bytes by default, or UTF-16 code units when CompileOptions.UTF16Pos is set.

type ValidationReport added in v0.10.1

type ValidationReport struct {
	Version      string                 `json:"format_version"`
	Valid        bool                   `json:"valid"`
	ErrorCount   int                    `json:"error_count"`
	WarningCount int                    `json:"warning_count"`
	Diagnostics  []ValidationDiagnostic `json:"diagnostics"`
}

ValidationReport is the versioned result returned by Validate.

func Validate added in v0.10.1

func Validate(path string, r io.Reader, opts *CompileOptions) (ValidationReport, error)

Validate checks a source file without layout or rendering. Source errors are returned in a report with Valid false. Cancellation and failures without a source diagnostic return an error. Warnings are collected in the report.

Jump to

Keyboard shortcuts

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