Skip to content

Python API

markdown_d2 exposes the two functions pymdownx.superfences needs for a custom fence.

formatter

formatter(*, root: str | Path = '.', cache_dir: str | Path | None = '.cache/markdown-d2', light_theme: int = 0, dark_theme: int = 200, dark_selector: str = '[data-md-color-scheme="slate"]', errors: Literal['raise', 'show'] = 'raise', timeout: float = 60, node: str | Path | None = None, transition: Literal['none', 'fade', 'morph'] = 'fade') -> Formatter

Return the function that turns a d2 block into a figure.

Parameters:

Name Type Description Default
root str | Path

Folder that imports and icon paths are relative to.

'.'
cache_dir str | Path | None

Folder that keeps rendered SVGs between builds; None keeps nothing.

'.cache/markdown-d2'
light_theme int

D2 theme ID of the light picture; a diagram's own theme-id wins.

0
dark_theme int

D2 theme ID of the dark picture; a diagram's own dark-theme-id wins.

200
dark_selector str

CSS selector of the page element that marks the dark theme.

'[data-md-color-scheme="slate"]'
errors Literal['raise', 'show']

"raise" stops the build on a broken block; "show" draws the error in place of the diagram.

'raise'
timeout float

Seconds to wait for one diagram, every board in both themes.

60
node str | Path | None

Node program to run; by default the one nodejs-wheel-binaries installed.

None
transition Literal['none', 'fade', 'morph']

How a diagram with several steps changes from one to the next: "none" switches at once, "fade" fades the new step in, and "morph" moves each shape the two steps share to its new place and fades in the rest. A block's own transition option wins.

'fade'

Raises:

Type Description
ValueError

If transition is not one of those three.

validator

validator(language: str, inputs: dict[str, str], options: dict[str, Any], attrs: dict[str, Any], md: Markdown) -> bool

Accept every option of a d2 block; the formatter checks them.