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

class

Mermaid

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

NameDescription
MermaidInitializes a new instance of the Mermaid class.
Constructor
Mermaid
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

NameDescription
SourceGets or sets the Mermaid source. Setting it redraws the diagram.
IsSanitizedGets whether the diagram's SVG is sanitized before it is inserted.
ErrorGets why the current source could not be drawn, or null when it was (or has not been tried yet).
Property
Mermaid.Source
public string Source { get ; set ; }

Gets or sets the Mermaid source. Setting it redraws the diagram.

Property
Mermaid.IsSanitized
public bool IsSanitized

Gets whether the diagram's SVG is sanitized before it is inserted.

Property
Mermaid.Error
public string Error

Gets why the current source could not be drawn, or null when it was (or has not been tried yet).

Methods

NameDescription
SanitizeSets whether the SVG is run through DOMPurify before it is inserted (on by default) and redraws.
LinksSets 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.
ImagesSets 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.
ZoomSets whether a diagram that has been shrunk to fit its container can be zoomed and panned (on by default) and redraws.
OnErrorRegisters 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.
RenderRenders the component.
Method
Mermaid.Sanitize
public Mermaid Sanitize(bool sanitize = true)

Sets whether the SVG is run through DOMPurify before it is inserted (on by default) and redraws.

Method
Mermaid.Images
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.

Method
Mermaid.Zoom
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.

Method
Mermaid.OnError
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.

Method
Mermaid.Render
public override HTMLElement Render()

Renders the component.

See also