Skip to content

Latest commit

 

History

History
160 lines (104 loc) · 3.67 KB

d2.md

File metadata and controls

160 lines (104 loc) · 3.67 KB

D2 language - Declarative Diagrams

https://d2lang.com/

Excellent declarative language good for creating Cloud & Architecture diagrams.

Key Points

  • less code than Python diagrams
  • CLI watch mode - interactive local web server auto-updates from code file for fast interactive development
  • output formats - SVG, PNG, PDF
  • good for Cloud Architecture diagrams with icons
  • multiple layout engines - use --layout dagre or --layout elk
  • nice dark theme

Real World Architecture Diagrams

HariSekhon/Diagrams-as-Code - ready made architecture diagrams

See thse D2 Code files.

Readme Card

D2 Template

HariSekhon/Templates - diagram.d2

Readme Card

Tutorial

https://d2lang.com/tour/hello-world

Interactive Online Sandbox

https://play.d2lang.com/

Install

In DevOps-Bash-tools install/ directory:

install_d2.sh

Auto-format the .d2 file:

d2 fmt "$filename.d2"

Every compile auto-formats your $filename.d2 file so you don't even need to run fmt manually.

d2 input.d2 output.svg

Gives a hand drawn appearance:

d2 --sketch ...

Local Development

Launch local webserver - auto-updates page upon write to diagram.d2:

d2 --watch --theme 200 "$templates/diagram.d2" /tmp/template.png
d2 --watch --dark-theme 200 "$templates/diagram.d2" /tmp/template.svg

Interactive development sandbox - doesn't export png files right though, see github issue.

d2 -w -t 200 ...

Bug - animate-interval requires something in the base to work, see:

d2 --animate-interval=1200 ...

Themes

https://d2lang.com/tour/themes

Show list of available themes:

d2 themes

Set theme for both light and dark mode

d2 --theme <number>

Set only when when user's browser is set in dark mode - only works for SVGs, not PNGs:

d2 --dark-theme

200 is Dark Mauve color theme - my favourite

Define in environment variables:

export D2_THEME=200
export D2_DARK_THEME=200

Layouts

https://d2lang.com/tour/layouts

show layout engines
d2 layout

Issues

Some things I'd like to see improved:

Libraries

https://terrastruct.com/blog/post/generate-diagrams-programmatically/

Ported from private Knowledge Base page 2023+