Skip to main content

Overview

Mermaid provides advanced rendering capabilities that allow you to customize SVG output, manipulate rendering behavior, and optimize diagram generation. This guide covers the internal rendering API and advanced techniques for working with Mermaid’s output.

The render function

The mermaid.render() function is the core API for programmatic diagram rendering. It returns both the SVG code and optional binding functions for interactivity.

Basic usage

Return value

The render function returns a RenderResult object:

SVG manipulation

Cleaning up SVG code

Mermaid automatically cleans up the generated SVG using the cleanUpSvgCode() function from mermaidAPI.ts:182:

Customizing SVG attributes

Mermaid automatically sets standard SVG attributes. The SVG structure created in mermaidAPI.ts:235:
You can modify these attributes after rendering:

Security levels and sandbox mode

Mermaid provides different security levels that affect rendering behavior:

Security level options

  • strict (default): HTML tags are encoded, click functionality disabled
  • antiscript: HTML tags allowed (script elements removed), clicks enabled
  • loose: HTML tags and clicks enabled
  • sandbox: Rendering in sandboxed iframe, JavaScript disabled

Configuring security level

Sandbox mode rendering

When securityLevel is set to 'sandbox', Mermaid wraps the SVG in an iframe (mermaidAPI.ts:212):
Sandbox mode prevents JavaScript execution, which may disable interactive features like tooltips, clicks, and links in diagrams.

Style customization

Creating custom CSS styles

Mermaid provides the createCssStyles() function for generating custom styles (mermaidAPI.ts:109):

Using themeCSS

Rendering queue and concurrency

Mermaid uses an execution queue to handle multiple render calls serially (mermaid.ts:308):
This ensures diagrams are rendered sequentially, preventing race conditions:

DOMPurify sanitization

When not in sandbox or loose mode, Mermaid sanitizes SVG output using DOMPurify (mermaidAPI.ts:454):

Text size limits

Mermaid enforces a maximum text size to prevent performance issues (mermaidAPI.ts:29):
You can override this limit in configuration:
Increasing maxTextSize significantly may cause browser performance issues with large diagrams.

Arrow marker URLs

Control whether arrow markers use full URLs or anchors with the arrowMarkerAbsolute configuration:
When false, Mermaid converts full URLs to anchors:

Advanced example: Custom rendering pipeline