The MermaidJS embed plugin allows you to insert diagrams, such as flowcharts and sequence diagrams, written in Mermaid code.

Demo

graph LR;
Write-->Edit;
Edit-->Build;
Build-->Publish;

Installation

Add the package to your project:

npm install --save-dev @lvl99dotcom/edaha-plugin-mermaidjs
yarn add --dev @lvl99dotcom/edaha-plugin-mermaidjs
pnpm add --save-dev @lvl99dotcom/edaha-plugin-mermaidjs

Add the plugin’s package name to your configuration file:

plugins:
  - "@lvl99dotcom/edaha-plugin-mermaidjs"

If you use your own layout template, it needs {% embedFoot %} before </body> to load Mermaid (the default theme’s layouts/html.njk has it). See Templating.

How to use

When using the WYSIWYG or Markdown editor, press the MermaidJS diagram button in the toolbar to add a diagram, then write its Mermaid code. To change a diagram, select it in the body and press its Edit button.

You can also write the diagram in an article’s Markdown yourself, with its code between the tags on their own lines:

<!-- embed:mermaidjs -->

graph LR;
Write-->Edit;
Edit-->Build;
Build-->Publish;
<!-- /embed:mermaidjs -->

The diagram has no options. See Mermaid’s documentation for the kinds of diagrams and how to write them; its live editor is handy for trying out code.

How it works

The diagram’s code is kept in the page, in <pre class="mermaid">, so it can still be read as text without scripts. Pages with diagrams load Mermaid 12.3.0 from https://cdn.jsdelivr.net once, however many diagrams they have, and Mermaid draws them in the visitor’s browser when the page has loaded. Building doesn’t need internet access.

Diagrams are drawn in Mermaid’s dark theme when the page is dark: when its <html> element’s data-theme is dark (as the default theme’s switcher sets it), or, without data-theme, when the visitor’s device is set to dark mode.

A diagram with no code gives a warning, and is left out of the website; the rest of the article is still built. Mermaid itself shows an error in place of a diagram whose code it can’t read.

Diagrams are wrapped in <div class="embed embed-mermaidjs">. See Embeds for more about embeds.