@edgetools/codemirror-lang-mermaid

中文文档
0.6.0发布于 2026/09/13MITTypeScriptESM + CJS近 30 天下载 2185

Mermaid language support for CodeMirror

原始使用文档
文档目录7 节
  1. Getting Started
  2. Supported Diagrams
  3. Supported Tags
  4. Syntax Highlighting with Custom Tags
  5. Syntax Highlighting with Parent Tags
  6. When to use Custom Tags vs Parent Tags
  7. Extensions

@edgetools/codemirror-lang-mermaid

This package implements Mermaid language support for the CodeMirror code editor. Get syntax highlighting for Mermaid diagrams!

Getting Started

Install the package:

npm install @edgetools/codemirror-lang-mermaid

Example usage:

import { basicSetup, EditorView } from 'codemirror';
import { mermaid, mindmapTags } from '@edgetools/codemirror-lang-mermaid';
import { HighlightStyle, syntaxHighlighting } from '@codemirror/language';

const myHighlightStyle = HighlightStyle.define([
  { tag: mindmapTags.diagramName, color: '#9650c8' },
  { tag: mindmapTags.lineText1, color: '#ce9178' },
  { tag: mindmapTags.lineText2, color: 'green' },
  { tag: mindmapTags.lineText3, color: 'red' },
  { tag: mindmapTags.lineText4, color: 'magenta' },
  { tag: mindmapTags.lineText5, color: '#569cd6' },
]);

new EditorView({
  doc: `mindmap
  root((mindmap))
    Origins
      Long history
      ::icon(fa fa-book)
      Popularisation
        British popular psychology author Tony Buzan
    Research
      On effectiveness<br/>and features
      On Automatic creation
        Uses
            Creative techniques
            Strategic planning
            Argument mapping
    Tools
      Pen and paper
      Mermaid
`,
  extensions: [basicSetup, mermaid(), syntaxHighlighting(myHighlightStyle)],
  parent: document.body,
});

If you run this code in the browser, you should get syntax highlighting!

Mermaid mindmap syntax highlighting

Supported Diagrams

Mermaid contains many different types of diagrams. Each diagram has its own Lezer grammar file. The following diagrams are supported:

Each diagram type is also exported individually — as a LanguageSupport extension (e.g. mindmap(), flowchart(), classDiagram()), a LRLanguage (e.g. mindmapLanguage), a LanguageDescription (e.g. mindmapLanguageDescription), and a set of custom highlighting tags (e.g. mindmapTags). The all-encompassing mermaid() extension automatically detects the diagram type from the first line and applies the matching grammar.

Supported Tags

CodeMirror supports a long list of tags that are suitable for handling syntax highlighting in a variety of languages. Some common tags include name, variableName, lineComment, string, and number.

CodeMirror also supports the ability to create custom tags. Custom tags are useful for styling tokens from a grammar that may not have a suitable tag available to us from CodeMirror.

You can choose to style the syntax highlighting based on either the custom tag I have defined or its corresponding parent tag (if it has one). Please see the tags file for a list of all custom tags I have defined and their corresponding parent tags.

Syntax Highlighting with Custom Tags

Below is an example of using custom tags to highlight tokens in a pie chart Mermaid diagram.

import { basicSetup, EditorView } from 'codemirror';
import { mermaid, pieTags } from '@edgetools/codemirror-lang-mermaid';
import { HighlightStyle, syntaxHighlighting } from '@codemirror/language';

const myHighlightStyle = HighlightStyle.define([
  { tag: pieTags.diagramName, color: '#9650c8' },
  { tag: pieTags.lineComment, color: '#ce9178' },
  { tag: pieTags.number, color: 'green' },
  { tag: pieTags.showData, color: 'blue' },
  { tag: pieTags.string, color: 'magenta' },
  { tag: pieTags.title, color: '#569cd6' },
  { tag: pieTags.titleText, color: 'red' },
]);

new EditorView({
  doc: `pie showData
    title Preferred Pizza Toppings
    "Cheese" : 300
    "Pepperoni" : 290
    "Mushroom" : 100
    "Pineapple" : 10`,
  extensions: [basicSetup, mermaid(), syntaxHighlighting(myHighlightStyle)],
  parent: document.body,
});

Running this code in the browser should result in the following syntax highlighting.

Mermaid pie chart syntax highlighting with custom tags

Syntax Highlighting with Parent Tags

Below is an example of using parent tags (defined by CodeMirror) to highlight tokens in a pie chart Mermaid diagram instead of using custom tags (defined by me).

import { basicSetup, EditorView } from 'codemirror';
import { mermaid } from '@edgetools/codemirror-lang-mermaid';
import { HighlightStyle, syntaxHighlighting } from '@codemirror/language';
import { tags as t } from '@lezer/highlight';

const myHighlightStyle = HighlightStyle.define([
  { tag: t.typeName, color: '#9650c8' }, // parent tag of pieTags.diagramName
  { tag: t.lineComment, color: '#ce9178' }, // parent tag of pieTags.lineComment
  { tag: t.number, color: 'green' }, // parent tag of pieTags.number
  { tag: t.keyword, color: '#569cd6' }, // parent tag of pieTags.showData and pieTags.title
  { tag: t.string, color: 'magenta' }, // parent tag of pieTags.string and pieTags.titleText
]);

new EditorView({
  doc: `pie showData
    title Preferred Pizza Toppings
    "Cheese" : 300
    "Pepperoni" : 290
    "Mushroom" : 100
    "Pineapple" : 10`,
  extensions: [basicSetup, mermaid(), syntaxHighlighting(myHighlightStyle)],
  parent: document.body,
});

Running this code in the browser should result in the following syntax highlighting.

Mermaid pie chart syntax highlighting with parent tags

When to use Custom Tags vs Parent Tags

It's up to your preference! Though, I personally prefer custom tags 🙂. As stated previously, each Mermaid diagram requires its own Lezer grammar file, which essentially means each diagram uses its own "language." The language used in Mermaid diagrams is very different than typical programming languages and therefore will have tokens that don't have a corresponding match in CodeMirror's list of tags.

The custom tags in the @edgetools/codemirror-lang-mermaid package provide a bit more control of what tokens get syntax highlighting. Notice that in the pie chart example above, t.string is both the parent of pieTags.string and pieTags.titleText. I created two custom tags to style the TitleText token differently than the String tokens. However, if your app already has a color theme and you don't want the overhead of custom tags, then continue using the parent tags.

Do note that not all custom tags have parent tags (i.e. most of the mindmap tags) which means you'll still need to use custom tags for those tokens. The mindmap diagram has special syntax highlighting that changes depending on how many indentions there are on each line.

Extensions

The @edgetools/codemirror-lang-mermaid package provides support for the following extension:

By enabling the foldByIndent extension, the CodeMirror editor will add code folding support across all Mermaid diagrams. Code folding is a common feature in text editors and IDEs that let you hide ("fold") parts of a document. This can help make the document easier to read or manage.

Example usage:

import { basicSetup, EditorView } from 'codemirror';
import { mermaid, mindmapTags } from '@edgetools/codemirror-lang-mermaid';
import { HighlightStyle, syntaxHighlighting } from '@codemirror/language';

const myHighlightStyle = HighlightStyle.define([
  { tag: mindmapTags.diagramName, color: '#9650c8' },
  { tag: mindmapTags.lineText1, color: '#ce9178' },
  { tag: mindmapTags.lineText2, color: 'green' },
  { tag: mindmapTags.lineText3, color: 'red' },
  { tag: mindmapTags.lineText4, color: 'magenta' },
  { tag: mindmapTags.lineText5, color: '#569cd6' },
]);

new EditorView({
  doc: `mindmap
  root((mindmap))
    Origins
      Long history
      ::icon(fa fa-book)
      Popularisation
        British popular psychology author Tony Buzan
    Research
      On effectiveness<br/>and features
      On Automatic creation
        Uses
            Creative techniques
            Strategic planning
            Argument mapping
    Tools
      Pen and paper
      Mermaid
`,
  extensions: [basicSetup, mermaid(), syntaxHighlighting(myHighlightStyle), foldByIndent()],
  parent: document.body,
});

By adding foldByIndent to your list of extensions, code folding by indents will be enabled. Keep in mind that this example is using the basicSetup extension, provided by CodeMirror, which adds arrows on all lines that provide code folding support. The foldByIndent extension will fold code based on the number of indents on the current line and the number of indents on the following lines.

Mindmap diagram with code folding enabled

版本与发布记录

@edgetools/codemirror-lang-mermaid 共发布 1 个版本,首次发布于 2026/09/13;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.6.0latest2026/09/132 个月前

关于 @edgetools/codemirror-lang-mermaid 的常见问题

@edgetools/codemirror-lang-mermaid 是什么?

Mermaid language support for CodeMirror

如何安装 @edgetools/codemirror-lang-mermaid?

在项目目录运行 npm install @edgetools/codemirror-lang-mermaid,或使用 pnpm add / yarn add / bun add。

@edgetools/codemirror-lang-mermaid 支持 TypeScript 吗?

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

@edgetools/codemirror-lang-mermaid 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@edgetools/codemirror-lang-mermaid 可以商用吗?

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

@edgetools/codemirror-lang-mermaid 的下载量有多少?

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

@edgetools/codemirror-lang-mermaid 还在维护吗?

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