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

mermaid-isomorphic

v2.1.2

Published

Transform mermaid diagrams in the browser or Node.js

Downloads

14,900

Readme

mermaid-isomorphic

github actions codecov npm version npm downloads

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-core 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 default fontFamily is set to arial,sans-serif. This option is ignored in the browser. You need to call mermaid.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 the screenshot 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.

Compatibility

This project is compatible with Node.js 16 or greater.

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