Format D2 Code
d2_formatFormat D2 diagram source code into canonical style, normalizing whitespace, indentation, and syntax while preserving semantics. Use to clean up hand-written diagrams and ensure consistent formatting.
Instructions
Format D2 diagram source code using the d2 binary formatter.
Normalizes whitespace, indentation, and syntax to D2's canonical style. The formatted output is semantically equivalent to the input.
NOTE: This tool requires the d2 binary to be installed (unlike d2_render and d2_validate which use WASM). Install from https://d2lang.com or set D2_PATH env var.
Args:
d2_code (string): D2 source code to format
Returns: Formatted D2 source code string.
Examples:
Clean up: format "a->b:label" to "a -> b: label"
Normalize after editing: run on any hand-written D2 code
Error Handling:
Returns error if d2 binary is not found (d2_render still works without it)
Returns error if code has syntax errors (format requires valid syntax)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| d2_code | Yes | The D2 diagram source code to format |