mdast2docx logo Md2Docx
Version 0.1.0

Overview

Updated 5 min ago

CIcodecovnpm versionnpm downloadsnpm bundle sizelicense

@m2d/mermaid-node: Server-side Node.js Mermaid diagram plugin for @m2d/core using beautiful-mermaid with zero DOM dependencies.

✨ Features

  • ⚑ Zero DOM / Headless Browser Dependencies: Powered by beautiful-mermaid and ELK.jsβ€”renders vector SVGs in milliseconds without Chromium, Puppeteer, or JSDOM.
  • 🎨 Sharp & librsvg Color Inlining: Automatically resolves CSS variables and color-mix() in sRGB to explicit hex colors, ensuring diagrams render in full theme colors rather than solid black in sharp.
  • πŸͺŸ Transparent Canvas Background: Defaults to transparent: true matching client-side output for seamless blending onto document pages.
  • πŸ“ Sequence Diagram Auto-Normalization: Automatically fixes multi-word actor names in sequence diagrams to ensure they render reliably.
  • 🌈 15+ Built-In Themes: Includes zinc-light, github-light, tokyo-night, dracula, nord, and custom two-color/enriched palettes.
  • πŸ’Ύ Persistent Caching: Caches diagram SVGs using @m2d/core/cache to eliminate repeated computation across requests.

πŸ’‘ Looking for browser / client-side Mermaid rendering? Use @m2d/mermaid powered by official mermaid.js.

πŸ“¦ Installation

$ pnpm add @m2d/mermaid-node @m2d/image-node @m2d/core docx

or

$ npm install @m2d/mermaid-node @m2d/image-node @m2d/core docx

πŸš€ Quick Start

import { toDocx } from "@m2d/core";
import { mermaidNodePlugin } from "@m2d/mermaid-node";
import { nodeImagePlugin } from "@m2d/image-node";
import remarkParse from "remark-parse";
import { unified } from "unified";

const markdown = `
# Backend Architecture

\`\`\`mermaid
graph TD
  Client[Client Request] --> API[Node.js API]
  API --> Database[(PostgreSQL)]
\`\`\`
`;

const mdast = unified().use(remarkParse).parse(markdown);

const docxBuffer = await toDocx(
  mdast,
  { title: "Architecture Report" },
  {
    plugins: [
      mermaidNodePlugin({ theme: "github-light" }), // 1. Generates SVG AST nodes
      nodeImagePlugin(),                             // 2. Rasterizes SVGs into DOCX images
    ],
  },
  "buffer",
);

⚠️ Plugin Ordering: Place mermaidNodePlugin() before nodeImagePlugin() in the plugins array.

βš™οΈ Plugin Options

export interface IMermaidNodePluginOptions {
  /**
   * Built-in theme name (e.g. 'zinc-light', 'github-light', 'dracula') or custom theme object.
   * @default 'zinc-light'
   */
  theme?: MermaidTheme;

  /** Additional render options forwarded to beautiful-mermaid */
  renderOptions?: Record<string, unknown>;

  /** Optional custom fixer function to repair invalid Mermaid code */
  fixMermaid?: (code: string, error: Error) => string;

  /** In-memory cache object for sharing across instances */
  cache?: Record<string, Promise<unknown>>;

  /** Persistent cache configuration */
  cacheConfig?: CacheConfigType;
}

πŸ“š Deep Dive Guides

License

This library is licensed under the MIT open-source license.


with πŸ’– by Mayank Kumar Chaudhari

On this page