Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rehype-mermaidjs

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rehype-mermaidjs

A rehype plugin to render mermaid diagrams

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

rehype-mermaidjs

github actions codecov npm prettier

A rehype plugin to render mermaid diagrams.

Table of Contents

Installation

npm install rehype-mermaidjs

In Node.js this package uses playwright under the hood. To use it, you may need to install additional dependencies. These can be installed with:

npx playwright install --with-deps chromium

See the Playwright Browsers documentation for more information.

Usage

This plugin takes all <pre class="mermaid"> and <code class="lang-mermaid"> elements, and replaces them with a rendered version of the diagram. If the <code> element is wrapped in a <pre> element, the <pre> element is replaced as well. This is compatible with what Mermaid would render client side, as well as the output of mermaid code blocks processed by remark-rehype.

The plugin has several rendering strategies described below.

Given a file named index.html:

<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    <pre><code class="lang-mermaid">
      graph TD;
          A-->B;
          A-->C;
          B-->D;
          C-->D;
    </code></pre>
    <pre class="mermaid">
      graph TD;
          A-->B;
          A-->C;
          B-->D;
          C-->D;
    </pre>
  </body>
</html>

The following script:

import { readFile } from 'node:fs/promises'

import { rehype } from 'rehype'
import rehypeMermaid from 'rehype-mermaidjs'

const { value } = await rehype()
  .use(rehypeMermaid, {
    // The default strategy is 'inline-svg'
    // strategy: 'img-png'
    // strategy: 'img-svg'
    // strategy: 'inline-svg'
    // strategy: 'pre-mermaid'
  })
  .process(await readFile('index.html'))

console.log(value)

Yields the following results, depending on the stragey used.

'img-png'

This strategy renders a diagram as an <img> element with an inline base64 PNG image. Given the example, this yields:

<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    <img alt="" height="215" id="mermaid-0" src="data:image/png;base64,iVBORw0KGgoA…" width="118" />
    <img alt="" height="215" id="mermaid-1" src="data:image/png;base64,iVBORw0KGgoA…" width="118" />
  </body>
</html>

'img-svg'

This strategy renders a diagram as an <img> element with an inline SVG image. Given the example, this yields:

<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    <img alt="" height="215" id="mermaid-0" src="data:image/xml+svg,%3csvg…" width="118" />
    <img alt="" height="215" id="mermaid-1" src="data:image/xml+svg,%3csvg…" width="118" />
  </body>
</html>

'inline-svg'

This strategy renders a diagram as an inline <svg> element. Given the example, this yields:

<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    <svg id="mermaid-0" …></svg>
    <svg id="mermaid-1" …></svg>
  </body>
</html>

'pre-mermaid'

This strategy replaces the element with a <pre class="mermaid"> element with only the diagram as its child. Given the example, this yields:

<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    <pre class="mermaid">
      graph TD;
          A-->B;
          A-->C;
          B-->D;
          C-->D;
    </pre>
    <pre class="mermaid">
      graph TD;
          A-->B;
          A-->C;
          B-->D;
          C-->D;
    </pre>
  </body>
</html>

This allows Mermaid to render the diagram on the client side, for example using:

import mermaid from 'mermaid'

mermaid.initialize({ startOnLoad: true })

API

This package has a default export rehypeMermaid.

unified().use(rehypeMermaid, options?)

options
browser

The Playwright browser to use. (object, default: chromium)

css

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. (string | URL)

errorFallback

Create a fallback node if processing of a mermaid diagram fails. If nothing is returned, the code block is removed. The function receives the following arguments:

  • element The hast element that could not be rendered.
  • diagram The Mermaid diagram that could not be rendered as a string.
  • error: The error that was thrown.
  • file: The file on which the error occurred.
launchOptions

The options used to launch the browser. (object)

mermaidOptions

A custom Mermaid configuration. By default fontFamily is set to arial,sans-serif. This option is ignored in the browser. You need to call mermaid.initialize() manually. (object)

prefix

A custom prefix to use for Mermaid IDs. (string, default: mermaid)

strategy

The render strategy to use. One of 'img-png', 'img-svg', 'inline-svg', or 'pre-mermaid'. (default: 'inline-svg')

Compatibility

This project is compatible with Node.js 16 or greater. It’s compatible with mermaid code blocks processed by remark-rehype. This means it’s also compatible with MDX.

  • mermaid is the library that’s used to render the diagrams.
  • mermaid-isomorphic allows this package to render Mermaid diagrams in both Node.js and the browser.
  • rehype provides HTML processing using a unified pipeline.

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.

License

MIT © Remco Haszing

Keywords

FAQs

Package last updated on 04 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc