Skip to main content

Overview

Mermaid provides an External Diagram API that allows you to create and register custom diagram types. This enables you to extend Mermaid with your own diagram syntax and rendering logic without modifying the core library.

External Diagram API

The External Diagram API is defined in diagram-api/types.ts:96:

DiagramDefinition structure

The loaded diagram must conform to the DiagramDefinition interface (diagram-api/types.ts:78):

Registering external diagrams

Use mermaid.registerExternalDiagrams() to register custom diagram types (mermaid.ts:254):

Lazy loading vs. immediate loading

  • Lazy loading (default): Diagram code is loaded only when detected
  • Immediate loading: All diagram code is loaded upfront
The order of diagram registration is important. The first detector to return true will be used, so register more specific detectors first.

Creating a custom diagram

Step 1: Create the detector

The detector identifies your diagram type from the text (detectType.ts:36):

Step 2: Create the parser

Define how your diagram syntax is parsed:

Step 3: Create the database

The database manages diagram state (diagram-api/types.ts:27):

Step 4: Create the renderer

The renderer draws the diagram (diagram-api/types.ts:70):

Step 5: Create the loader

The loader returns the complete diagram definition:

Step 6: Register the diagram

Complete example: Simple network diagram

1

Create the custom diagram module

2

Register and use the diagram

3

Use in HTML

Advanced features

Theming support

Provide theme-aware styles:

Configuration support

Interactive features

Add click handlers and tooltips:

Detector best practices

From detectType.ts:36, detectors should:
  1. Remove frontmatter and directives before checking:
  1. Be specific to avoid false positives:
  1. Consider configuration if needed:

Accessibility requirements

All custom diagrams must implement accessibility methods:
These are used by Mermaid to add ARIA attributes to the SVG.

Testing custom diagrams