mermaid-isomorphic
Render Mermaid diagrams in the browser or Node.js.
This is useful if you wish to render Mermaid diagrams in a Node.js or an isomorphic environment. If you want to render Mermaid diagrams in the browser directly, use the mermaid
package directly.
Table of Contents
Installation
npm install mermaid-isomorphic
This library uses Playwright under the hood in Node.js. You may need to install the Playwright browser before using this package.
npx playwright install --with-deps chromium
Usage
First, create a Mermaid renderer. Then use this renderer to render your diagrams.
import { createMermaidRenderer } from 'mermaid-isomorphic'
const renderer = createMermaidRenderer()
const diagram = `
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
`
const results = await renderer([diagram])
console.log(results)
Fonts
FontAwesome
Mermaid has support for FontAwesome. This is also supported by isomorphic-mermaid
, but you need to load the FontAwesome CSS yourself when serving the SVG.
Custom Fonts
By default mermaid-isomorphic
uses the arial,sans-serif
font family. This font family is mostly compatible across all browsers and devices. If you wish to use a custom font, you need to specify both the mermaidConfig.fontFamily
and css
options.
Browser
mermaid-isomorphic
is intended for use in Node.js, but also provides a browser export. This means it can be used in the browser, but it’s recommended to use the mermaid package directly.
API
createMermaidRenderer(options?: CreateMermaidRendererOptions)
Create a Mermaid renderer.
The Mermaid renderer manages a browser instance. If multiple diagrams are being rendered simultaneously, the internal browser instance will be re-used. If no diagrams are being rendered, the browser will be closed.
Options
browser
(BrowserType
): The Playwright browser to use. (default: chromium)launchOptions
: (LaunchOptions
): The options used to launch the browser.
Returns
A function that renders Mermaid diagrams in the browser. This function has arguments:
diagrams
(string[]
): An array of mermaid diagrams to render.options
:css
(string
|URL
) A URL that points to a custom CSS file to load. Use this to load custom fonts. This option is ignored in the browser. You need to include the CSS in your build manually.screenshot
(boolean
): If true, a PNG screenshot of the diagram is included as a buffer. This is only supported in Node.js.prefix
(string
): A custom prefix to use for Mermaid IDs (default:mermaid
).mermaidOptions
(MermaidConfig
): A custom Mermaid configuration. By defaultfontFamily
is set toarial,sans-serif
. This option is ignored in the browser. You need to callmermaid.initialize()
manually.
It returns a promise settled result with the render results. The render results have the following properties:
description
(string
): The aria description of the diagram, if it has one.height
(number
): The height of the resulting SVG.id
(string
): The DOM id of the SVG node.screenshot
(Buffer
): The diagram SVG rendered as a PNG buffer. This is only added if thescreenshot
option is true.svg
(string
): The diagram rendered as an SVG.title
(string
): The title of the rendered diagram, if it has one.width
(number
): The width of the resulting SVG.
Contributing
Test fixtures are generated and verified using Linux. Rendering on other platforms may yield slightly different results. Don’t worry about adding new fixtures, but don’t update existing ones that cause CI to fail. Furthermore see my global contributing guidelines.