Mermaid #

A diagram made in a drawing tool has to be exported as an image, and fixing one word means starting over. Mermaid lets you describe a diagram as text, and @kawapress/plugin-mermaid draws it.

The drawing is done by Pele. It does not need a browser, so diagrams are drawn at build time. Readers see them as soon as the page opens, without waiting for a script.

Write a diagram #

Use mermaid as the language of a code block:

Markdown
```mermaid
flowchart LR
  A[Write Markdown] --> B[Build] --> C[Publish]
```

This is the result:

Write MarkdownBuildPublish

Pele supports every diagram type in Mermaid 12.1.0. Here is a sequence diagram:

Markdown
```mermaid
sequenceDiagram
  Reader->>Site: Open a page
  Site-->>Reader: Return the finished diagram
```
Open a pageReturn the finished diagramReaderSiteReaderSite

Three views #

Every diagram has three buttons at its top right:

ButtonWhat it shows
DiagramOnly the drawn diagram
CodeOnly the Mermaid source
Side by sideThe source on the left and the diagram on the right, with a divider in the middle that you can drag. Stacked on narrow screens, with the diagram on top

Use "Side by side" when readers should see how the diagram is written. Keep the default "Diagram" when you only want the picture.

Zoom #

When a diagram is wider than the text, or you want to see details, zoom in:

  • Hold Ctrl (⌘ on a Mac) and scroll, or pinch on a touch screen, to zoom around the pointer.
  • When zoomed in, drag to move the diagram.
  • Double-click to zoom in, and double-click again to reset.
  • Two buttons at the top right zoom in and out. A reset button appears once the diagram is zoomed.

Scrolling without Ctrl scrolls the page. The diagram does not take the wheel from the reader.

Choose the starting view #

Add one word after mermaid to start this diagram in another view:

Markdown
```mermaid split
flowchart LR
  A --> B
```

Use diagram, code or split. The diagram below starts side by side:

mermaid
flowchart LR
  A --> B
AB

To change the default for the whole site, use defaultView below.

Colors follow the theme #

Diagram colors read the theme's shared variables, so switching between light and dark mode updates them at once without a redraw. To change one kind of color, override the matching Pele variable:

CSS
.kawa-mermaid__stage {
  --pele-accent: oklch(0.6 0.2 30);
}

See Pele's theming guide for the available variables.

When a diagram cannot be drawn #

A syntax error in a diagram does not fail the build. Pele's error message, with the line and column, shows in place of that diagram. Other pages are not affected, and the "Code" view lets you compare it with the source.

Options #

TypeScript
import mermaidPlugin from '@kawapress/plugin-mermaid'
import { nagi } from 'kawapress/nagi'

export default nagi({
  plugins: [
    mermaidPlugin({
      defaultView: 'diagram',
      labels: {
        code: 'Source',
      },
      localeLabels: {
        zh: { split: '对照' },
      },
    }),
  ],
})
OptionDescription
defaultViewThe view a diagram starts in: diagram (default), code or split. A view written in a code block wins.
labelsOverrides the button texts for every language.
localeLabelsOverrides the button texts for one language. The key is a name from locales.

You can override views, diagram, code, split, zoomIn, zoomOut and reset. English and Chinese are built in, and other languages fall back to English.

Styles #

The plugin's styles live in the kawa.plugin layer and read only the shared --kawa-* variables. To adjust them, override these class names:

ClassElement
.kawa-mermaidThe whole diagram block
.kawa-mermaid__barThe top bar with the view buttons
.kawa-mermaid__viewOne view button
.kawa-mermaid__stageThe area with the diagram; the Pele color variables are set here
.kawa-mermaid__codeThe area with the source

Install #

The nagi preset does not include this plugin, so install it yourself:

Shell
npm install --save-dev @kawapress/plugin-mermaid
TypeScript
import mermaidPlugin from '@kawapress/plugin-mermaid'
import { nagi } from 'kawapress/nagi'

export default nagi({
  plugins: [
    mermaidPlugin(),
  ],
})

Adding it to plugins is enough. KawaPress loads the component and styles by itself, and pages without a diagram download nothing.

Limitations #

  • Pele does not include a math typesetter. $$…$$ in a diagram shows its TeX source as written.
  • Only mermaid code blocks are handled. Diagrams written directly in HTML are not.

Comments