monaco-mermaid-ng 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +7 -0
- package/.prettierrc.json +3 -0
- package/LICENSE +21 -0
- package/README.md +177 -0
- package/README.zh-CN.md +177 -0
- package/browser.js +1414 -0
- package/eslint.config.js +86 -0
- package/index.d.ts +3 -0
- package/index.js +1377 -0
- package/package.json +47 -0
- package/tsconfig.json +26 -0
package/.prettierignore
ADDED
package/.prettierrc.json
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Max Qian <astro_air@126.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# monaco-mermaid-ng
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/monaco-mermaid-ng)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
[English](#monaco-mermaid-ng) | [中文](./README.zh-CN.md)
|
|
7
|
+
|
|
8
|
+
Monaco Editor language support for [MermaidJS](https://mermaid.js.org/) - providing syntax highlighting, code completion, and custom themes.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Syntax Highlighting** - Full syntax highlighting for 22+ Mermaid diagram types
|
|
13
|
+
- **Two Themes** - Light (`mermaid`) and dark (`mermaid-dark`) themes with carefully selected colors
|
|
14
|
+
- **Code Completion** - Intelligent code completion with snippets for common diagram structures
|
|
15
|
+
- **Language Configuration** - Auto-closing pairs for brackets, parentheses, and braces
|
|
16
|
+
- **YAML Frontmatter** - Support for Mermaid configuration via YAML frontmatter
|
|
17
|
+
- **Config Directives** - Support for `%%{init: {...}}%%` configuration directives
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install monaco-mermaid-ng
|
|
23
|
+
# or
|
|
24
|
+
yarn add monaco-mermaid-ng
|
|
25
|
+
# or
|
|
26
|
+
pnpm add monaco-mermaid-ng
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Basic Setup
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import * as monaco from 'monaco-editor';
|
|
35
|
+
import initMermaidLanguage from 'monaco-mermaid-ng';
|
|
36
|
+
|
|
37
|
+
// Initialize Mermaid language support
|
|
38
|
+
initMermaidLanguage(monaco);
|
|
39
|
+
|
|
40
|
+
// Create editor with Mermaid language
|
|
41
|
+
const editor = monaco.editor.create(document.getElementById('editor'), {
|
|
42
|
+
value: 'flowchart TD\n A[Start] --> B[End]',
|
|
43
|
+
language: 'mermaid',
|
|
44
|
+
theme: 'mermaid', // or 'mermaid-dark'
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### With @monaco-editor/react
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import Editor, { loader } from '@monaco-editor/react';
|
|
52
|
+
import * as monaco from 'monaco-editor';
|
|
53
|
+
import initMermaidLanguage from 'monaco-mermaid-ng';
|
|
54
|
+
|
|
55
|
+
// Initialize before using the editor
|
|
56
|
+
loader.init().then((monaco) => {
|
|
57
|
+
initMermaidLanguage(monaco);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
function App() {
|
|
61
|
+
return (
|
|
62
|
+
<Editor
|
|
63
|
+
height="400px"
|
|
64
|
+
language="mermaid"
|
|
65
|
+
theme="mermaid-dark"
|
|
66
|
+
defaultValue="flowchart TD\n A[Start] --> B[End]"
|
|
67
|
+
/>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Supported Diagram Types
|
|
73
|
+
|
|
74
|
+
| Category | Diagram Types |
|
|
75
|
+
|----------|---------------|
|
|
76
|
+
| **Flow & Process** | Flowchart, Block Diagram, State Diagram |
|
|
77
|
+
| **Sequence & Interaction** | Sequence Diagram, Journey |
|
|
78
|
+
| **Structure** | Class Diagram, ER Diagram, Requirement Diagram |
|
|
79
|
+
| **Project Management** | Gantt Chart, Timeline, Kanban |
|
|
80
|
+
| **Data Visualization** | Pie Chart, Quadrant Chart, XY Chart, Sankey, Radar Chart, Treemap |
|
|
81
|
+
| **Hierarchy** | Mindmap |
|
|
82
|
+
| **Architecture** | C4 Diagram (Context, Container, Component, Dynamic, Deployment), Architecture Diagram |
|
|
83
|
+
| **Technical** | Git Graph, Packet Diagram |
|
|
84
|
+
|
|
85
|
+
## Themes
|
|
86
|
+
|
|
87
|
+
### Light Theme (`mermaid`)
|
|
88
|
+
|
|
89
|
+
Based on Monaco's `vs` theme with optimized colors for Mermaid syntax.
|
|
90
|
+
|
|
91
|
+
### Dark Theme (`mermaid-dark`)
|
|
92
|
+
|
|
93
|
+
Based on Monaco's `vs-dark` theme with optimized colors for Mermaid syntax.
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
// Switch themes dynamically
|
|
97
|
+
monaco.editor.setTheme('mermaid'); // Light theme
|
|
98
|
+
monaco.editor.setTheme('mermaid-dark'); // Dark theme
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Code Completion & Snippets
|
|
102
|
+
|
|
103
|
+
The package provides intelligent code completion including:
|
|
104
|
+
|
|
105
|
+
- **Diagram Type Keywords** - All diagram type declarations (flowchart, sequenceDiagram, classDiagram, etc.)
|
|
106
|
+
- **Block Keywords** - Structural keywords (subgraph, loop, alt, state, etc.)
|
|
107
|
+
- **Diagram-specific Keywords** - Keywords specific to each diagram type
|
|
108
|
+
- **Snippets** - Ready-to-use templates for common patterns:
|
|
109
|
+
- `loop` - Sequence diagram loop
|
|
110
|
+
- `alt` - Alternative path
|
|
111
|
+
- `opt` - Optional path
|
|
112
|
+
- `par` - Parallel actions
|
|
113
|
+
- `rect` - Background color region
|
|
114
|
+
- `subgraph` - Flowchart subgraph
|
|
115
|
+
- `class` - Class definition
|
|
116
|
+
- `state` - State definition
|
|
117
|
+
- `frontmatter` - YAML configuration block
|
|
118
|
+
- Full diagram templates for mindmap, timeline, quadrantChart, xychart, block, packet, kanban, architecture, radar, treemap
|
|
119
|
+
|
|
120
|
+
## API
|
|
121
|
+
|
|
122
|
+
### `initMermaidLanguage(monaco)`
|
|
123
|
+
|
|
124
|
+
Initializes Mermaid language support on the given Monaco instance.
|
|
125
|
+
|
|
126
|
+
**Parameters:**
|
|
127
|
+
|
|
128
|
+
- `monaco` - The Monaco Editor instance (`typeof Monaco`)
|
|
129
|
+
|
|
130
|
+
**Effects:**
|
|
131
|
+
|
|
132
|
+
- Registers `mermaid` language
|
|
133
|
+
- Registers Monarch tokenizer for syntax highlighting
|
|
134
|
+
- Defines `mermaid` (light) and `mermaid-dark` themes
|
|
135
|
+
- Registers completion item provider
|
|
136
|
+
- Registers language configuration (comments, brackets)
|
|
137
|
+
|
|
138
|
+
## Examples
|
|
139
|
+
|
|
140
|
+
### Flowchart
|
|
141
|
+
|
|
142
|
+
```mermaid
|
|
143
|
+
flowchart TD
|
|
144
|
+
A[Start] --> B{Is it?}
|
|
145
|
+
B -->|Yes| C[OK]
|
|
146
|
+
B -->|No| D[End]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Sequence Diagram
|
|
150
|
+
|
|
151
|
+
```mermaid
|
|
152
|
+
sequenceDiagram
|
|
153
|
+
participant Alice
|
|
154
|
+
participant Bob
|
|
155
|
+
Alice->>Bob: Hello Bob!
|
|
156
|
+
Bob-->>Alice: Hi Alice!
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### With YAML Frontmatter
|
|
160
|
+
|
|
161
|
+
```mermaid
|
|
162
|
+
---
|
|
163
|
+
config:
|
|
164
|
+
theme: forest
|
|
165
|
+
look: handDrawn
|
|
166
|
+
---
|
|
167
|
+
flowchart LR
|
|
168
|
+
A[Start] --> B[End]
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Demo
|
|
172
|
+
|
|
173
|
+
Check out the [demo](./demo) folder for a complete working example with live preview.
|
|
174
|
+
|
|
175
|
+
## License
|
|
176
|
+
|
|
177
|
+
MIT
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# monaco-mermaid-ng
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/monaco-mermaid-ng)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
[English](./README.md) | [中文](#monaco-mermaid-ng)
|
|
7
|
+
|
|
8
|
+
Monaco Editor 的 [MermaidJS](https://mermaid.js.org/) 语言支持 - 提供语法高亮、代码补全和自定义主题。
|
|
9
|
+
|
|
10
|
+
## 特性
|
|
11
|
+
|
|
12
|
+
- **语法高亮** - 支持 22+ 种 Mermaid 图表类型的完整语法高亮
|
|
13
|
+
- **双主题** - 浅色 (`mermaid`) 和深色 (`mermaid-dark`) 主题,精心配色
|
|
14
|
+
- **代码补全** - 智能代码补全,提供常用图表结构的代码片段
|
|
15
|
+
- **语言配置** - 自动闭合括号、圆括号和花括号
|
|
16
|
+
- **YAML Frontmatter** - 支持通过 YAML frontmatter 配置 Mermaid
|
|
17
|
+
- **配置指令** - 支持 `%%{init: {...}}%%` 配置指令
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install monaco-mermaid-ng
|
|
23
|
+
# 或
|
|
24
|
+
yarn add monaco-mermaid-ng
|
|
25
|
+
# 或
|
|
26
|
+
pnpm add monaco-mermaid-ng
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 使用方法
|
|
30
|
+
|
|
31
|
+
### 基础设置
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import * as monaco from 'monaco-editor';
|
|
35
|
+
import initMermaidLanguage from 'monaco-mermaid-ng';
|
|
36
|
+
|
|
37
|
+
// 初始化 Mermaid 语言支持
|
|
38
|
+
initMermaidLanguage(monaco);
|
|
39
|
+
|
|
40
|
+
// 创建使用 Mermaid 语言的编辑器
|
|
41
|
+
const editor = monaco.editor.create(document.getElementById('editor'), {
|
|
42
|
+
value: 'flowchart TD\n A[开始] --> B[结束]',
|
|
43
|
+
language: 'mermaid',
|
|
44
|
+
theme: 'mermaid', // 或 'mermaid-dark'
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 配合 @monaco-editor/react 使用
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import Editor, { loader } from '@monaco-editor/react';
|
|
52
|
+
import * as monaco from 'monaco-editor';
|
|
53
|
+
import initMermaidLanguage from 'monaco-mermaid-ng';
|
|
54
|
+
|
|
55
|
+
// 在使用编辑器前初始化
|
|
56
|
+
loader.init().then((monaco) => {
|
|
57
|
+
initMermaidLanguage(monaco);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
function App() {
|
|
61
|
+
return (
|
|
62
|
+
<Editor
|
|
63
|
+
height="400px"
|
|
64
|
+
language="mermaid"
|
|
65
|
+
theme="mermaid-dark"
|
|
66
|
+
defaultValue="flowchart TD\n A[开始] --> B[结束]"
|
|
67
|
+
/>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## 支持的图表类型
|
|
73
|
+
|
|
74
|
+
| 分类 | 图表类型 |
|
|
75
|
+
|------|----------|
|
|
76
|
+
| **流程与处理** | 流程图 (Flowchart)、块图 (Block Diagram)、状态图 (State Diagram) |
|
|
77
|
+
| **序列与交互** | 时序图 (Sequence Diagram)、用户旅程图 (Journey) |
|
|
78
|
+
| **结构** | 类图 (Class Diagram)、ER 图 (ER Diagram)、需求图 (Requirement Diagram) |
|
|
79
|
+
| **项目管理** | 甘特图 (Gantt Chart)、时间线 (Timeline)、看板 (Kanban) |
|
|
80
|
+
| **数据可视化** | 饼图 (Pie Chart)、象限图 (Quadrant Chart)、XY 图表、桑基图 (Sankey)、雷达图 (Radar Chart)、树状图 (Treemap) |
|
|
81
|
+
| **层级结构** | 思维导图 (Mindmap) |
|
|
82
|
+
| **架构** | C4 图 (Context、Container、Component、Dynamic、Deployment)、架构图 (Architecture Diagram) |
|
|
83
|
+
| **技术** | Git 图 (Git Graph)、数据包图 (Packet Diagram) |
|
|
84
|
+
|
|
85
|
+
## 主题
|
|
86
|
+
|
|
87
|
+
### 浅色主题 (`mermaid`)
|
|
88
|
+
|
|
89
|
+
基于 Monaco 的 `vs` 主题,针对 Mermaid 语法优化配色。
|
|
90
|
+
|
|
91
|
+
### 深色主题 (`mermaid-dark`)
|
|
92
|
+
|
|
93
|
+
基于 Monaco 的 `vs-dark` 主题,针对 Mermaid 语法优化配色。
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
// 动态切换主题
|
|
97
|
+
monaco.editor.setTheme('mermaid'); // 浅色主题
|
|
98
|
+
monaco.editor.setTheme('mermaid-dark'); // 深色主题
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## 代码补全与代码片段
|
|
102
|
+
|
|
103
|
+
本包提供智能代码补全,包括:
|
|
104
|
+
|
|
105
|
+
- **图表类型关键字** - 所有图表类型声明 (flowchart、sequenceDiagram、classDiagram 等)
|
|
106
|
+
- **块级关键字** - 结构性关键字 (subgraph、loop、alt、state 等)
|
|
107
|
+
- **图表专用关键字** - 各图表类型特有的关键字
|
|
108
|
+
- **代码片段** - 常用模式的即用模板:
|
|
109
|
+
- `loop` - 时序图循环
|
|
110
|
+
- `alt` - 条件分支
|
|
111
|
+
- `opt` - 可选路径
|
|
112
|
+
- `par` - 并行操作
|
|
113
|
+
- `rect` - 背景色区域
|
|
114
|
+
- `subgraph` - 流程图子图
|
|
115
|
+
- `class` - 类定义
|
|
116
|
+
- `state` - 状态定义
|
|
117
|
+
- `frontmatter` - YAML 配置块
|
|
118
|
+
- 完整图表模板:mindmap、timeline、quadrantChart、xychart、block、packet、kanban、architecture、radar、treemap
|
|
119
|
+
|
|
120
|
+
## API
|
|
121
|
+
|
|
122
|
+
### `initMermaidLanguage(monaco)`
|
|
123
|
+
|
|
124
|
+
在给定的 Monaco 实例上初始化 Mermaid 语言支持。
|
|
125
|
+
|
|
126
|
+
**参数:**
|
|
127
|
+
|
|
128
|
+
- `monaco` - Monaco Editor 实例 (`typeof Monaco`)
|
|
129
|
+
|
|
130
|
+
**效果:**
|
|
131
|
+
|
|
132
|
+
- 注册 `mermaid` 语言
|
|
133
|
+
- 注册 Monarch 分词器用于语法高亮
|
|
134
|
+
- 定义 `mermaid`(浅色)和 `mermaid-dark` 主题
|
|
135
|
+
- 注册代码补全提供器
|
|
136
|
+
- 注册语言配置(注释、括号)
|
|
137
|
+
|
|
138
|
+
## 示例
|
|
139
|
+
|
|
140
|
+
### 流程图
|
|
141
|
+
|
|
142
|
+
```mermaid
|
|
143
|
+
flowchart TD
|
|
144
|
+
A[开始] --> B{判断}
|
|
145
|
+
B -->|是| C[确定]
|
|
146
|
+
B -->|否| D[结束]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### 时序图
|
|
150
|
+
|
|
151
|
+
```mermaid
|
|
152
|
+
sequenceDiagram
|
|
153
|
+
participant 小明
|
|
154
|
+
participant 小红
|
|
155
|
+
小明->>小红: 你好!
|
|
156
|
+
小红-->>小明: 嗨!
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### 使用 YAML Frontmatter
|
|
160
|
+
|
|
161
|
+
```mermaid
|
|
162
|
+
---
|
|
163
|
+
config:
|
|
164
|
+
theme: forest
|
|
165
|
+
look: handDrawn
|
|
166
|
+
---
|
|
167
|
+
flowchart LR
|
|
168
|
+
A[开始] --> B[结束]
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## 演示
|
|
172
|
+
|
|
173
|
+
查看 [demo](./demo) 文件夹获取带实时预览的完整示例。
|
|
174
|
+
|
|
175
|
+
## 许可证
|
|
176
|
+
|
|
177
|
+
MIT
|