@diplodoc/mermaid-extension

中文文档
2.2.3发布于 2026/08/20MITTypeScriptCommonJS构建来源已验证近 30 天下载 2.1万

Mermaid plugin for Diplodoc transformer and builder

原始使用文档
文档目录5 节
  1. Quickstart
  2. Prepared Mermaid runtime
  3. Custom initialize options
  4. MarkdownIt transform plugin
  5. React hook and component for smart control of Mermaid

english | русский


NPM version Quality Gate Status Coverage Maintainability Rating Reliability Rating Security Rating

Diplodoc Mermaid extension

This is extension for Diplodoc platform which adds support for Mermaid diagrams.

Extension contains some parts:

Quickstart

Attach plugin to transformer

import mermaid from '@diplodoc/mermaid-extension';
import transform from '@diplodoc/transform';

const {result} = await transform(
  `
\`\`\`mermaid
graph TD
    A[Christmas] -->|Get money| B(Go shopping)
\`\`\`
`,
  {
    plugins: [mermaid.transform({bundle: false})],
  },
);

Add mermaid runtime to your final page

<html>
  <head>
    <!-- Read more about '_assets/mermaid-extension.js' in 'MarkdownIt transform plugin' section -->
    <script src="_assets/mermaid-extension.js" async />
  </head>
  <body style="background: #000">
    ${result.html}
    <script>
      // Read more about 'mermaidJsonp' in 'Prepared Mermaid runtime' section
      window.mermaidJsonp = window.mermaidJsonp || [];
      window.mermaidJsonp.push((mermaid) => {
        mermaid.initialize({theme: 'dark'});
        mermaid.run();
      });
    </script>
  </body>
</html>

Prepared Mermaid runtime

The problem with Mermaid is that it has big bundle size. The most expected behavior is loading it asynchronously. But if we want to disable Mermaid's startOnLoad option, then we don't know when the Mermaid will be initialized.

Prepared Mermaid runtime designed to solve this problem. We disable Mermaid's startOnLoad option to precise control render step. Then we add mermaidJsonp global callback to handle Mermaid's loading.

Also, we limit exposed Mermaid API by two methods:

  • initialize - configure mermaid before next render
  • run - start diagrams rendering

Usage example:

window.mermaidJsonp = window.mermaidJsonp || [];

// This callback will be called when runtime is loaded
window.mermaidJsonp.push((mermaid) => {
  mermaid.initialize({theme: 'dark'});
  mermaid.run();
});

// You can configure more than one callback
window.mermaidJsonp.push((mermaid) => {
  console.log('Render diagrams');
});

Custom initialize options

Exposed mermaid.initialize method has extra configuration options:

  • zoom - Enable diagram zoom and explore feature. Can be boolean or object with inner props.
    (Default: false)
    • showMenu - Show navigation menu.
      (Default: false)
    • bindKeys - Enable wasd controls.
      Use w/a/s/d to explore diagram, e/q to zoom in/out and r to reset diagram position.
      (Default: false)
    • maximumScale - Maximum zoom scale.
      (Default: 5)
    • resetOnBlur - Reset diagram position on outer click.
      (Default: false)

MarkdownIt transform plugin

Plugin for @diplodoc/transform package.

Configuration:

  • runtime - name of runtime script which will be exposed in results script section.
    (Default: _assets/mermaid-extension.js)

  • bundle - boolean flag to enable/disable copying of bundled runtime to target directory.
    Where target directory is <transformer output option>/<plugin runtime option>
    Default: true

  • classes - additional classes which will be added to Mermaid's diagrams.
    Example: my-own-class and-other-class

React hook and component for smart control of Mermaid

Simplifies Mermaid control with react

import React from 'react';
import {transform} from '@diplodoc/transform';
import mermaid from '@diplodoc/mermaid-extension/plugin';
import {MermaidRuntime} from '@diplodoc/mermaid-extension/react';

const MERMAID_RUNTIME = 'extension:mermaid';

const Doc: React.FC = ({content}) => {
  const result = transform(content, {
    plugins: [
      // Initialize plugin for client/server rendering
      mermaid.transform({
        // Do not touch file system
        bundle: false,
        // Set custom runtime name for searching in result scripts
        runtime: MERMAID_RUNTIME,
      }),
    ],
  });

  // Load mermaid only if one or more diagram should be rendered
  if (result.script.includes(MERMAID_RUNTIME)) {
    // Load oversized mermaid runtime asynchronously
    import('@diplodoc/mermaid-extension/runtime');
  }

  return <div dangerouslySetInnerHTML={{__html: result.html}} />;
};

export const App: React.FC = ({theme}) => {
  return (
    <>
      <Doc
        content={`
            \`\`\`mermaid
            graph TD
                A[Christmas] -->|Get money| B(Go shopping)
                B --> C{Let me think}
            \`\`\`
        `}
      />
      <MermaidRuntime
        zoom={{
          showMenu: true,
          bindKeys: true,
          resetOnBlur: true,
        }}
      />
    </>
  );
};

For detailed information about architecture, development, and contributing, see AGENTS.md.

版本与发布记录

@diplodoc/mermaid-extension 共发布 37 个版本,首次发布于 2023/04/18;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.2.3latest2026/08/202 个月前
2.2.22026/08/063 个月前
2.2.12026/07/213 个月前
2.2.02026/07/143 个月前
2.1.32026/06/174 个月前
2.1.22026/06/095 个月前

关于 @diplodoc/mermaid-extension 的常见问题

@diplodoc/mermaid-extension 是什么?

Mermaid plugin for Diplodoc transformer and builder

如何安装 @diplodoc/mermaid-extension?

在项目目录运行 npm install @diplodoc/mermaid-extension,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:markdown-it(^13.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@diplodoc/mermaid-extension 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@diplodoc/mermaid-extension 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@diplodoc/mermaid-extension 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@diplodoc/mermaid-extension 可以商用吗?

@diplodoc/mermaid-extension 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@diplodoc/mermaid-extension 的下载量有多少?

根据 npm 官方下载统计,@diplodoc/mermaid-extension 在 2026/10/13 – 2026/11/11 这 30 天内被下载 21,050 次,日均约 702 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@diplodoc/mermaid-extension 还在维护吗?

最新版本 2.2.3 发布于 2026/08/20(2 个月前);至今共发布 37 个版本,其中过去 12 个月发布了 11 个;首次发布于 2023/04/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。