Mermaid

API

parseMermaidToExcalidraw: its config options, what it returns and the errors it throws.

parseMermaidToExcalidraw

parseMermaidToExcalidraw(
  definition: string,
  config?: MermaidConfig,
): Promise<MermaidToExcalidrawResult>

Parses and lays out a Mermaid definition, and resolves to Excalidraw element skeletons. Pass them to convertToExcalidrawElements to get elements you can add to a scene (see Getting started).

import { parseMermaidToExcalidraw } from "@excalidraw/mermaid-to-excalidraw";

const { elements, files } = await parseMermaidToExcalidraw(definition, {
  elbow: true,
  themeVariables: { fontSize: "25px" },
});

Config

config takes a subset of Mermaid's config. Other Mermaid options are ignored.

OptionTypeDefaultDescription
elbowboolean | { flowchart?: boolean; state?: boolean }falseDraws flowchart edges and state diagram transitions as elbow arrows: true for both, or per diagram type, e.g. { flowchart: true }. Swimlane edges are always elbow arrows. A curve set in the definition itself (front matter or a %%{init}%% directive) wins.
themeVariables.fontSizestring"20px"The font size of labels, e.g. "25px". Nodes are sized for it.
maxEdgesnumber500The most edges Pele parses in a diagram.
maxTextSizenumber50000The most characters Pele parses in a definition.

The type is exported as MermaidConfig:

import type { MermaidConfig } from "@excalidraw/mermaid-to-excalidraw";

With elbow: true, edges become elbow arrows:

A flowchart with elbow arrows, converted

Result

interface MermaidToExcalidrawResult {
  elements: ExcalidrawElementSkeleton[];
  files?: BinaryFiles;
}
FieldDescription
elementsThe diagram as ExcalidrawElementSkeletons. Labels are bound to their shapes, and flowchart edges and state transitions to the shapes they connect.
filesSet when the diagram is inserted as an image (diagram types that aren't converted to elements). Add them with excalidrawAPI.addFiles(Object.values(files)).

Errors

The promise rejects when the definition isn't valid Mermaid syntax, or Pele can't parse it. The error message follows Mermaid's format, so you can show it to users as is.

Last updated on

On this page