Diagrams¶
The mermaid, plantuml, and uml directives render as live diagrams
in the Rendered preview.
Diagrams are drawn client-side by
Mermaid;
plantuml and uml sources are translated to Mermaid on the fly.
All three work in both reStructuredText and Markdown (MyST).
This reference describes the Rendered preview mode; in Structural mode every directive and role renders as a uniform placeholder (see Authoring).
Mermaid¶
A mermaid directive takes its diagram either as inline content
or as a file argument — one or the other.
If you supply both at once, that is reported as a diagnostic
and the inline content is used.
Inline content:
.. mermaid::
:caption: Firmware boot sequence
:align: center
sequenceDiagram
Bootloader->>Firmware: verify signature
Firmware-->>Bootloader: ok
A file argument instead:
.. mermaid:: diagrams/boot-sequence.mmd
:align: center
See the Mermaid documentation for the diagram syntax itself.
PlantUML and UML¶
The plantuml and uml directives accept
PlantUML source,
which is translated to Mermaid before rendering.
This translation is best-effort and covers a subset of PlantUML —
sequence, class, component, state, and activity diagrams.
Other PlantUML diagram families,
and individual lines the translator does not recognise,
fall back to showing the source as a placeholder
rather than a broken diagram.
The surrounding @startuml / @enduml markers are optional.
Preprocessing¶
PlantUML preprocessing
is applied to the directive body before it is translated,
so a diagram can be generated rather than written out in full:
variables (!$name = …), !define / !definelong,
!function and !procedure,
conditionals (!if / !ifdef / !else),
loops (!while / !foreach)
and the string, list and arithmetic % builtins
(%strlen, %substr, %splitstr, %size, %upper, %lower,
%intval, %dec2hex and the rest) all work.
This needs no configuration and cannot be turned off.
The C4 standard library is bundled,
so !include <C4/C4_Container.puml>
(and the other C4_* files) resolves offline
and the Person() / Container() / Rel() macros expand.
Not yet supported:
Including your own files, or a URL —
!include,!include_once,!include_manyand!includeurlresolve only against the bundled standard library. Local files are not read and URLs are never fetched; local-file includes are planned.Themes —
!themeis understood but no themes are bundled yet, so it is skipped.Sub-part includes —
!startsub/!endsub/!includesub,!includedefand!importare recognised but not executed.Structured data — a variable holding a JSON object or array cannot be navigated (
$obj.field,$arr[0]), and%load_jsonis not implemented. Lists produced by%splitstrdo work with!foreachand%size.Builtins that read the clock, the environment or the file being built —
%date,%now,%random,%getenv,%filename,%filename_no_extension,%dirpath,%filedateand%file_existsare recognised but return an empty string. A diagram whose content depended on any of them would not be reproducible from its own source, and%getenvwould additionally read the build environment.
Anything on that list is skipped, not fatal: the rest of the diagram still renders, exactly as it would have before the directive was written.
If a body preprocesses away to nothing —
every line inside an !if that turned out false, say —
the directive falls back to showing its source.
PlantUML itself would draw an empty diagram there;
showing the source is the more useful of the two.
Options¶
The following options are applied to the rendered diagram:
Option |
Effect |
|---|---|
|
Wraps the diagram as a figure with the caption below it. The caption text is shown as written — inline markup is not interpreted. |
|
Aligns the diagram: |
|
Gives the diagram a stable cross-reference target you can |
|
Sizes the diagram container,
in the preview and in |
|
Accepted so they do not raise errors, but not yet applied to the rendered diagram. |
Behaviour notes¶
Diagrams follow the editor theme, and re-render when you switch between light and dark.
A diagram whose source has a syntax error keeps its source visible with an error style, so you can see and fix it, rather than showing a blank space.
Note
Diagrams are designed to degrade gracefully. Whether the input is an unrecognised PlantUML family, a line the translator cannot handle, or Mermaid source with a syntax error, the preview shows the source (as a placeholder or with an error style) instead of a blank or broken block — so you always have something to read and correct.
See also
Directives — directives in the preview generally.
Markdown (MyST) — writing the same content in Markdown (MyST).
Differences from a Sphinx build — where the preview differs from a Sphinx build.