@stesura/mermaid
Framework-free Mermaid diagrams: the node spec, commands, rendering, markdown and DOCX support.
The editor UI is in @stesura/mermaid-react. See the
diagrams guide for setup and behaviour.
Mermaid itself is only import()ed by the render functions, and mermaid and dompurify are
optional peers (installed with @stesura/mermaid-react). A server imports the node spec from
@stesura/mermaid and needs neither.
Schema
mermaidNodes / mermaidNodeSpec
The mermaid node: a block atom whose text content is the source (content: "text*"), with
measureAs: "atomic" for pagination. toDOM writes the source in a pre; parseDOM also reads
a GitHub-style pre > code.language-mermaid.
import { stesuraSchema } from '@stesura/core/schema'
import { mermaidGlobalAttributes, mermaidNodes } from '@stesura/mermaid'
const schema = stesuraSchema({
nodes: mermaidNodes,
globalAttributes: { nodes: mermaidGlobalAttributes },
})mermaidGlobalAttributes
The globalAttributes.nodes delta granting the node spacing, spanColumns and
trackChanges.
The global-attribute bindings a diagram needs, for
stesuraSchema({ globalAttributes: { nodes: mermaidGlobalAttributes } }):
spacing, spanning columns, whole-node track changes and whole-node redaction.
| Name | Type | Default |
|---|---|---|
spacing | { mermaid: true; } | |
spanColumns | { mermaid: true; } | |
trackChanges | { mermaid: true; } | |
redaction | { mermaid: true; } |
mermaidSource / isMermaidNode / MERMAID_NODE_NAME
mermaidSource(node) is the current source: the text without tracked deletions. isMermaidNode
narrows a node; MERMAID_NODE_NAME is "mermaid".
MERMAID_THEMES
export const MERMAID_THEMES = ["default", "neutral", "dark", "forest", "base"] as const;Mermaid’s built-in themes; default renders with no theme directive.
MermaidTheme
export type MermaidTheme = (typeof MERMAID_THEMES)[number];One of MERMAID_THEMES.
Commands
insertMermaid
Inserts a diagram at the selection and selects it.
| Name | Type | Default |
|---|---|---|
source | string |
setMermaidSource
Replaces the source of the diagram at pos; a no-op when unchanged.
| Name | Type | Default |
|---|---|---|
pos | number | |
source | string |
DEFAULT_MERMAID_SOURCE is the starter flowchart insertMermaid uses when given no source.
setMermaidTheme
Sets the theme of the diagram at pos, or the selected one. Applies, without
a transaction, when the diagram already has it.
| Name | Type | Default |
|---|---|---|
theme | "default" | "dark" | "base" | "neutral" | "forest" | |
pos | number |
deleteMermaid
Deletes the diagram at pos, or the selected one. Like Word, the only
block of its container leaves an empty paragraph behind rather than taking
the container (a callout, a cell) with it.
| Name | Type | Default |
|---|---|---|
pos | number |
selectedMermaidPos
Position of the node-selected diagram, or null.
| Name | Type | Default |
|---|---|---|
state | EditorState |
Rendering
Browser only. Every caller (the node view, its preview, the DOCX export) goes through these, so the security limits hold everywhere. See Diagrams → Security.
renderMermaidSvg
Renders a source to a sanitized SVG string. Rejects with a MermaidRenderError whose reason is
"too-large" (over MERMAID_SOURCE_MAX), "timeout" (over MERMAID_RENDER_TIMEOUT_MS) or
"invalid"; for "invalid", detail holds Mermaid’s own message (English, quotes the input:
log it, don’t show it).
Renders a diagram to a sanitized SVG string. Throws MermaidRenderError.
| Name | Type | Default |
|---|---|---|
source | string | |
theme | "default" | "dark" | "base" | "neutral" | "forest" |
MermaidRenderErrorReason
export type MermaidRenderErrorReason = "too-large" | "timeout" | "invalid";Why renderMermaidSvg rejected: over MERMAID_SOURCE_MAX, over MERMAID_RENDER_TIMEOUT_MS, or Mermaid’s own parse error.
rasterizeSvg
Draws an SVG string to a PNG through a canvas, at scale× its viewBox size. Returns the PNG
bytes and the CSS-px size to display it at.
loadMermaid
The cached import() of Mermaid and DOMPurify, initialized with the locked-down config. A failed
load is retried on the next call.
| Constant | Value |
|---|---|
MERMAID_SOURCE_MAX | 20,000 characters |
MERMAID_RENDER_TIMEOUT_MS | 5,000 |
Markdown
mermaidMarkdown
A MarkdownExtension mapping the node to a mermaid fence, both ways. Spread it into
parseMarkdown / serializeBlocks options, or register it for a schema with
registerMarkdownExtensions from @stesura/markdown.
DOCX
mermaidDocx
DocxExportOptions plus a nodeTransform for import:
import { downloadDocx, importDocx } from '@stesura/docx'
import { mermaidDocx } from '@stesura/mermaid'
await downloadDocx(state, comments, 'report.docx', mermaidDocx)
await importDocx(file, schema, { nodeTransform: mermaidDocx.nodeTransform })| Field | Role |
|---|---|
preloadExtra | Renders and rasterizes every diagram in the browser before export. No-op without a DOM. |
nodes.mermaid | Writes the raster as a picture described mermaid:<JSON source>, or the source as a code block (with one warning) when there is no raster. |
nodeTransform | On import, turns a picture with that description back into the diagram. |
MERMAID_DESCRIPTION_PREFIX is "mermaid:".