npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rehype-mermaid

v3.0.0

Published

A rehype plugin to render mermaid diagrams

Downloads

25,714

Readme

rehype-mermaid

github actions codecov npm version npm downloads

A rehype plugin to render mermaid diagrams.

Table of Contents

Installation

npm install rehype-mermaid

Outside of browsers rehype-mermaid uses Playwright. If you use this outside of a browser, you need to install Playwright and a Playwright browser.

npm install playwright
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="language-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="language-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-mermaid'

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>

This strategy is asynchronous.

This strategy supports the dark option.

'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>

This strategy is asynchronous.

This strategy supports the dark option.

'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>

This strategy is asynchronous.

This strategy does not support the dark option.

'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 })

This strategy is synchronous.

This strategy does not support the dark option.

API

This package has a default export rehypeMermaid.

unified().use(rehypeMermaid, options?)

options

browser

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

colorScheme

The default color scheme.

If not specified, rehype-mermaid will determine the color scheme based on the color-scheme meta tag. If this doesn’t exist, the default color scheme is light. (string)

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)

dark

If specified, add responsive dark mode using a <picture> element.

This option is only supported by the img-png and img-svg strategies.

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)

mermaidConfig

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')

Examples

remark

This package works with Mermaid codeblocks in markdown using remark-parse, remark-rehype, and rehype-stringify.

import rehypeMermaid from 'rehype-mermaid'
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark'
import remarkRehype from 'remark-rehype'
import { unified } from 'unified'

const processor = unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeMermaid)
  .use(rehypeStringify)

const markdown = `
# Mermaid Diagram

\`\`\`mermaid
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;
\`\`\`
`

const { value } = await processor.process(markdown)

console.log(value)

MDX

This package works with Mermaid codeblocks in MDX.

import { compile } from '@mdx-js/mdx'
import rehypeMermaid from 'rehype-mermaid'

const mdx = `
# Mermaid Diagram

\`\`\`mermaid
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;
\`\`\`
`

const { value } = await compile(mdx, {
  rehypePlugins: [rehypeMermaid]
})

console.log(value)

Compatibility

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

Related Projects

  • 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.

Acknowledgements

Thanks to @bitekong for giving me the npm package name.

License

MIT © Remco Haszing