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

mermaid-isomorphic

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mermaid-isomorphic

Transform mermaid diagrams in the browser or Node.js

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.1K
increased by6.51%
Maintainers
1
Weekly downloads
 
Created
Source

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

Outside of browsers mermaid-isomorphic 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

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

Keywords

FAQs

Package last updated on 08 Oct 2024

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