@aparte/plugin-mermaid

中文文档
1.0.0发布于 2026/11/07MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2410

Diagrams for aparté via Mermaid — drawn as the fence streams, in a sandboxed image.

原始使用文档
文档目录5 节
  1. An image, so nothing in it can act on the page
  2. While a reply streams
  3. The chat's colours
  4. Options
  5. Two entry points

@aparte/plugin-mermaid

Diagrams for aparté via Mermaid. Every ```mermaid fence in a reply is drawn as a diagram — as it streams, whichever Markdown provider is installed, or none.

A Mermaid flowchart drawn from a fence, in an aparté chat
npm install @aparte/plugin-mermaid @aparte/core mermaid
import { setupMermaid } from '@aparte/plugin-mermaid';

setupMermaid();

@aparte/core and mermaid (11.16.1 or later) are peer dependencies. Mermaid is loaded on the first diagram, not before, and a diagram is drawn only once it comes near the screen, so reopening a long conversation stays fast.

An image, so nothing in it can act on the page

Each diagram is an <img> of its SVG. A browser draws an image's SVG with no scripts and no access to the page, so neither a label a model wrote nor a style inside the diagram can act on the chat around it. Mermaid also runs with securityLevel: 'strict' and HTML labels off, and a diagram's own %%{init}%% cannot turn either back on.

Mermaid draws before that image exists, in the page, and what a diagram names there is fetched at that moment. Three things name a picture. A node's or an actor's own: A@{ shape: imageSquare, img: "https://…" } or a sequence diagram's properties A: {"icon": "https://…"} would send a request to that address, so the plugin asks Mermaid what it parsed, and a diagram holding a picture that is not inline (data:image/…) is not drawn, however the source spelled it. A label Mermaid writes as HTML even with HTML labels off, such as an architecture service's icon text: it goes through Mermaid's own sanitizer with pictures, players, frames and styles forbidden, a rule a diagram's %%{init}%% cannot lift. And a style: classDef c mask-image:url(https://…) in a state or block diagram is fetched as soon as the diagram is measured, so a source holding a CSS url(, image-set( or cross-fade( (or an @import) is not drawn, nor is one that styles something and holds a backslash escape, which could spell one. A diagram that is not drawn shows its source and the note that it could not be drawn. A Mermaid object that cannot be asked what it parsed (setupMermaidWith with your own) falls back to refusing on the text alone, which also refuses a label saying "img" in a diagram with shape data and a sequence diagram using the word properties or details.

What that costs: the text in a diagram cannot be selected, and it is set in the system fonts, since an image cannot load the page's. A drawn diagram keeps its code card, so the copy button copies its source.

While a reply streams

The diagram is redrawn as its fence arrives — at most every 300 ms, or as long as the last drawing took — and keeps the last version that parsed. Once the fence closes, a diagram that does not parse shows a note and its source.

The chat's colours

The diagram takes its colours from the chat's theme (--aparte-bg, --aparte-text, --aparte-primary…) and is drawn again when the theme changes. Its alt text is the diagram's own accTitle or accDescr, else "Diagram"; set diagramAlt and diagramError with extendLocale() to translate the fallback and the error note.

Options

setupMermaid(options?, config?) takes MermaidOptions:

optiondefault
progressivetruefalse draws a diagram once its fence has closed
mermaid{}Mermaid's own configuration: a flowchart curve, themeVariables…

Two entry points

@aparte/plugin-mermaid imports Mermaid on the first diagram. To hand it over yourself, use the entry that imports nothing from mermaid:

import mermaid from 'mermaid';
import { setupMermaidWith } from '@aparte/plugin-mermaid/core';

setupMermaidWith(mermaid);

setupMermaidWith takes the instance, or a function returning a promise of it, and any object shaped like MermaidLike (initialize, parse, render). Both functions return a disposer.

Mermaid keeps one configuration for the whole page, and the plugin calls initialize with its own before every diagram it draws. If your page also draws diagrams of its own with the same Mermaid, call mermaid.initialize(yourConfig) again before your own render() or run().

ESM only.

版本与发布记录

@aparte/plugin-mermaid 共发布 15 个版本,首次发布于 2026/10/29;过去 12 个月发布了 15 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0latest2026/11/07今天
0.32.12026/11/025 天前
0.32.02026/11/025 天前
0.31.02026/11/026 天前
0.30.12026/11/016 天前
0.30.02026/11/016 天前

关于 @aparte/plugin-mermaid 的常见问题

@aparte/plugin-mermaid 是什么?

Diagrams for aparté via Mermaid — drawn as the fence streams, in a sandboxed image.

如何安装 @aparte/plugin-mermaid?

在项目目录运行 npm install @aparte/plugin-mermaid,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:mermaid(^11.16.1)、@aparte/core(>=1.0.0 <2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aparte/plugin-mermaid 支持 TypeScript 吗?

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

@aparte/plugin-mermaid 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aparte/plugin-mermaid 需要什么版本的 Node.js?

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

@aparte/plugin-mermaid 可以商用吗?

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

@aparte/plugin-mermaid 的下载量有多少?

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

@aparte/plugin-mermaid 还在维护吗?

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