Mermaid

How it works

The conversion pipeline from a Mermaid definition to Excalidraw element skeletons.

parseMermaidToExcalidraw doesn't lay diagrams out itself. Pele parses the definition and draws it as an SVG. The package then reads the shapes, routes and labels from that drawing and rebuilds them as Excalidraw elements in the same places.

Pele's SVG (left) and the converted Excalidraw diagram (right)

The pipeline

  1. Load the diagram type. parsePele detects the diagram type and loads Pele's code for it from pele/lazy, so each type is downloaded on first use.
  2. Parse. Pele parses the definition into a model of the diagram, with the maxTextSize and maxEdges limits. <br> doesn't parse everywhere \n does (e.g. in bare flowchart ids or sequence participants), so if parsing fails, withLineBreakFallback retries once with <br> replaced by \n.
  3. Lay out and draw. Pele renders the model to an SVG, at its natural size and direction. Labels are measured with Excalifont, the font Excalidraw draws them with, and the spacing between nodes is scaled up by 1.5, because Excalidraw's hand-drawn strokes and arrowheads are heavier than Pele's.
  4. Read the drawing. A parser per diagram type in src/pele/ combines Pele's model (ids, labels, shape types, styles) with the SVG's geometry (positions, sizes, edge routes, label positions). It returns a plain graph: a Flowchart, Sequence, Class, ERD or State from src/interfaces.ts.
  5. Convert. graphToExcalidraw hands the graph to its type's converter in src/converter/types/. The converter builds element skeletons: containers with bound labels, arrows bound to their nodes, lines and text. Duplicate consecutive points are then removed from lines and arrows.

Diagram types that aren't converted skip steps 4 and 5. Pele's SVG becomes an image element, with its data in files. A diagram whose conversion throws falls back to the image too.

Source layout

PathWhat's in it
src/index.tsparseMermaidToExcalidraw, the public API
src/parseDiagram.tsMaps the Mermaid config to Pele's options
src/pele/parsePele.tsLoads, parses and renders with Pele, and picks the parser for the diagram type
src/pele/<type>.tsReads a diagram type's graph from Pele's model and SVG
src/pele/svg.ts, src/pele/elements.tsHelpers for SVG geometry: shape bounds, edge routes, text lines
src/interfaces.tsThe graph types passed from parsers to converters, and MermaidConfig
src/graphToExcalidraw.tsPicks the converter for a graph
src/converter/types/<type>.tsConverts a graph to Excalidraw element skeletons
src/converter/transformToExcalidrawSkeleton.ts, src/converter/helpers.tsShared builders for containers, lines, arrows and text, and style and arrowhead helpers
src/constants.tsDefaults, such as the font size and the Mermaid config defaults
testcases/Mermaid test cases, see Development
playground/The playground app

Want to support another diagram type? See Adding a diagram type.

Last updated on

On this page