Mermaid
Description
Mermaid diagram source drawn as themed SVG
Mermaid takes diagram source (flowcharts, sequence, class and state diagrams, pie and gantt charts, and more) and draws it as SVG. Use it when the picture describes something, such as a process, a protocol or a schedule, rather than a canvas people arrange by hand; for that, use Diagram.
Drawing is done by Pele, a renderer for Mermaid syntax. It is not the Mermaid library. Colors, fonts and radius come from Tesserae's tokens, so a diagram follows light and dark mode and custom themes without being redrawn.
Pele is a separate file, fetched the first time a diagram is drawn. Until it loads, the component shows the source as text. A mermaid code fence in MarkdownBlock is drawn by the same renderer.
The diagram redraws to fit the width of its container, so keep it in a container whose width does not depend on the diagram. A diagram shrunk to fit can be zoomed and panned with Zoom().
Source is sanitized with DOMPurify before it is inserted. This is on by default; Sanitize(false) turns it off and should be used only for source you wrote. Links(false) and Images(false) remove the diagram's own click hyperlinks and pictures, which is the setting for source you do not trust.
A source that cannot be drawn does not throw. The diagram shows the reason, Error holds it, and OnError(...) is called with it. An empty source draws nothing.
Samples
Flowchart and sequence diagram
Live editing
Assign Source to redraw in place. The error line is filled from OnError(...); the toggle switches Sanitize(...).
Untrusted source
The first diagram keeps its click link. The second has Links(false) and Images(false), so the link is dropped.
API reference
public class Mermaid : ComponentBase<Mermaid, HTMLElement>Draws Mermaid diagram source as SVG, with Pele (not the Mermaid library), themed from Tesserae's tokens so it follows light/dark mode and custom themes without being redrawn. The same renderer draws a ```mermaid fence in MarkdownBlock.
Remarks
Pele is fetched the first time a diagram is drawn; until then the source is shown as text. Changing Source redraws in place, and a source that cannot be drawn is reported through Error and OnError (and shown in the diagram's place) rather than thrown.
- Namespace
- Tesserae
- Inheritance
- ComponentBase<Mermaid, HTMLElement> → Mermaid
Constructors
| Name | Description |
|---|---|
| Mermaid | Initializes a new instance of the Mermaid class. |
public Mermaid(string source = "", bool sanitize = true)Initializes a new instance of the Mermaid class.
Parameters
- source
- The Mermaid source, e.g.
flowchart LR\n A --> B. - sanitize
- Whether the SVG is run through DOMPurify before it is inserted, on top of Pele's own escaping of diagram text. On by default; turn it off only for source you wrote yourself.
Properties
| Name | Description |
|---|---|
| Source | Gets or sets the Mermaid source. Setting it redraws the diagram. |
| IsSanitized | Gets whether the diagram's SVG is sanitized before it is inserted. |
| Error | Gets why the current source could not be drawn, or null when it was (or has not been tried yet). |
public string Source { get ; set ; }Gets or sets the Mermaid source. Setting it redraws the diagram.
public bool IsSanitizedGets whether the diagram's SVG is sanitized before it is inserted.
Methods
| Name | Description |
|---|---|
| Sanitize | Sets whether the SVG is run through DOMPurify before it is inserted (on by default) and redraws. |
| Links | Sets whether the diagram's own hyperlinks (click statements) are kept (on by default) and redraws. Turn them off for source you do not trust to link anywhere. |
| Images | Sets whether the diagram's own images are kept (on by default) and redraws. An image is a request to whatever server hosts it, so turn them off for source you do not trust. |
| Zoom | Sets whether a diagram that has been shrunk to fit its container can be zoomed and panned (on by default) and redraws. |
| OnError | Registers a callback for when the source cannot be drawn (a syntax error, the library failing to load). It is called with the reason, once per failed attempt. |
| Render | Renders the component. |
public Mermaid Sanitize(bool sanitize = true)Sets whether the SVG is run through DOMPurify before it is inserted (on by default) and redraws.
public Mermaid Links(bool allow = true)Sets whether the diagram's own hyperlinks (click statements) are kept (on by default) and redraws. Turn them off for source you do not trust to link anywhere.
public Mermaid Images(bool allow = true)Sets whether the diagram's own images are kept (on by default) and redraws. An image is a request to whatever server hosts it, so turn them off for source you do not trust.
public Mermaid Zoom(bool enabled = true)Sets whether a diagram that has been shrunk to fit its container can be zoomed and panned (on by default) and redraws.
public Mermaid OnError(Action<string> callback)Registers a callback for when the source cannot be drawn (a syntax error, the library failing to load). It is called with the reason, once per failed attempt.