How terrastruct/d2 works
D2 is a diagram scripting language: authors provide text, which the CLI or Go library turns into a graph, lays out, and renders or exports. The README documents multiple layout engines, themes, language tooling, and SVG, PNG, GIF, PDF, and PPTX output. The tree supports these subsystem paths, but most implementation excerpts are absent, so the diagram shows the documented workflow without asserting uncertain internal call details.
Main entry points: d2.go Go library · d2layouts.go Layout selection · main.go CLI · parse.go Parser
Components 18
Entry points
- CLI
d2cli/main.go - Go library
d2lib/d2.go
Language processing
- Parser
d2parser/parse.go - AST
d2ast/d2ast.go - Compiler and IR
d2compiler/compile.go - Diagram graph
d2graph/d2graph.go
Graph layout
- Layout selection
d2layouts/d2layouts.go - Dagre engine
d2layouts/d2dagrelayout/layout.go - ELK engine
d2layouts/d2elklayout/layout.go - TALA engine
d2layouts/d2talalayout/layout.go - Sequence and grid
d2layouts/d2sequence/layout.go
Rendering and export
- Scene builder
d2renderers/d2scenebuild/build.go - SVG renderer
d2renderers/d2svg/d2svg.go - File exporters
d2exporter/export.go
Language tooling
- Formatter
d2format/format.go - Language server
d2lsp/d2lsp.go
Other components
- Diagram author
- Diagram files
Connections 18
- Diagram author → CLI: provides source
- Diagram author → Go library: provides source
- CLI → Go library: invokes
- Go library → Parser: parses source
- Parser → AST: produces AST
- Go library → Compiler and IR: compiles diagram
- Compiler and IR → Diagram graph: produces graph
- Go library → Layout selection: selects layout
- Layout selection → Dagre engine: dispatches
- Layout selection → ELK engine: dispatches
- Layout selection → TALA engine: dispatches
- Layout selection → Sequence and grid: supports specialized layouts
- Go library → Scene builder: builds render scene
- Scene builder → SVG renderer: provides render data
- Go library → File exporters: exports formats
- File exporters → Diagram files: writes SVG PNG GIF PDF PPTX
- Diagram author → Formatter: formats source
- Diagram author → Language server: uses language tooling