|
@@ -1,52 +1,75 @@
|
|
|
# Mermaid-Go
|
|
# Mermaid-Go
|
|
|
|
|
|
|
|
-一个基于 mermaid.js 架构的 Go 语言 Mermaid 图表解析器和生成器。
|
|
|
|
|
-
|
|
|
|
|
-## 项目作者
|
|
|
|
|
-
|
|
|
|
|
-由 gytmtc 创建,基于 mermaid.js 的架构设计。
|
|
|
|
|
-
|
|
|
|
|
-## 架构设计
|
|
|
|
|
-
|
|
|
|
|
-该实现严格遵循 mermaid.js 的设计模式:
|
|
|
|
|
-
|
|
|
|
|
-- **AST 包**: 类型定义镜像 mermaid.js 的 FlowVertex, FlowEdge 等结构
|
|
|
|
|
-- **Lexer 包**: 基于 JISON 词法规则的标记化词法分析
|
|
|
|
|
-- **Parser 包**: 遵循 flow.jison 结构的语法分析
|
|
|
|
|
-- **Renderer 包**: AST 到字符串的渲染
|
|
|
|
|
-- **FlowDB**: 图表构建的中央状态管理
|
|
|
|
|
-
|
|
|
|
|
-## 功能特性
|
|
|
|
|
-
|
|
|
|
|
-- [x] 流程图解析和渲染
|
|
|
|
|
-- [x] 多种节点形状支持 (矩形、圆形、菱形等)
|
|
|
|
|
-- [x] 多种箭头类型支持 (实线、虚线、粗线等)
|
|
|
|
|
-- [x] 边标签支持
|
|
|
|
|
-- [x] 完整的往返转换 (解析 → AST → 渲染)
|
|
|
|
|
-- [ ] 序列图支持 (待实现)
|
|
|
|
|
-- [ ] 类图支持 (待实现)
|
|
|
|
|
-- [ ] 状态图支持 (待实现)
|
|
|
|
|
-
|
|
|
|
|
-## 快速开始
|
|
|
|
|
|
|
+A high-performance Go implementation of Mermaid diagram parsing and rendering, providing near-complete compatibility with [Mermaid.js](https://mermaid.js.org/) syntax.
|
|
|
|
|
+
|
|
|
|
|
+## 🎯 Project Goals
|
|
|
|
|
+
|
|
|
|
|
+Mermaid-Go aims to provide a native Go alternative to Mermaid.js with:
|
|
|
|
|
+
|
|
|
|
|
+- **High Performance**: Fast parsing and rendering without JavaScript runtime overhead
|
|
|
|
|
+- **Native Integration**: Easy embedding in Go applications and CLI tools
|
|
|
|
|
+- **Broad Compatibility**: Support for the most commonly used Mermaid diagram types
|
|
|
|
|
+- **Extensibility**: Clean architecture for adding new diagram types and features
|
|
|
|
|
+
|
|
|
|
|
+## 📊 Current Support Status
|
|
|
|
|
+
|
|
|
|
|
+### ✅ Fully Supported Diagram Types
|
|
|
|
|
+
|
|
|
|
|
+#### **Flowchart Diagrams** (~85% Feature Complete)
|
|
|
|
|
+- ✅ All standard node shapes (rectangle, circle, diamond, parallelogram, etc.)
|
|
|
|
|
+- ✅ Edge types (solid, dotted, thick arrows)
|
|
|
|
|
+- ✅ Subgraphs with titles and directions
|
|
|
|
|
+- ✅ Interactive features (click events, links, callbacks)
|
|
|
|
|
+- ✅ Styling (classDef, class assignments, individual node styles)
|
|
|
|
|
+- ✅ Direction control (TD, TB, BT, RL, LR)
|
|
|
|
|
+- ⚠️ Edge labels and animations (partial support)
|
|
|
|
|
+- ❌ Markdown text formatting
|
|
|
|
|
+
|
|
|
|
|
+#### **Class Diagrams** (~80% Feature Complete)
|
|
|
|
|
+- ✅ Class definitions with members and methods
|
|
|
|
|
+- ✅ Method parameters and return types
|
|
|
|
|
+- ✅ Visibility modifiers (+, -, #, ~)
|
|
|
|
|
+- ✅ Class relationships (inheritance, composition, aggregation, etc.)
|
|
|
|
|
+- ✅ Annotations (<<interface>>, <<abstract>>, <<enumeration>>)
|
|
|
|
|
+- ✅ Comments (%% syntax)
|
|
|
|
|
+- ✅ Notes (general and class-specific)
|
|
|
|
|
+- ✅ Direction control
|
|
|
|
|
+- ⚠️ Generic types (framework ready, parsing in progress)
|
|
|
|
|
+- ❌ Click events and links
|
|
|
|
|
+
|
|
|
|
|
+### 🔄 Partially Supported
|
|
|
|
|
+
|
|
|
|
|
+#### **State Diagrams** (~30% Feature Complete)
|
|
|
|
|
+- ✅ Basic state definitions and transitions
|
|
|
|
|
+- ✅ Start/end states
|
|
|
|
|
+- ❌ Composite states
|
|
|
|
|
+- ❌ Parallel states
|
|
|
|
|
+- ❌ State actions and guards
|
|
|
|
|
+
|
|
|
|
|
+#### **Sequence Diagrams** (~25% Feature Complete)
|
|
|
|
|
+- ✅ Basic actor interactions
|
|
|
|
|
+- ✅ Simple message flows
|
|
|
|
|
+- ❌ Activation boxes
|
|
|
|
|
+- ❌ Loops and alternatives
|
|
|
|
|
+- ❌ Notes and comments
|
|
|
|
|
+
|
|
|
|
|
+### 🚧 Planned Support
|
|
|
|
|
+
|
|
|
|
|
+- **ER Diagrams**: Entity-relationship diagrams
|
|
|
|
|
+- **Gantt Charts**: Project timeline visualization
|
|
|
|
|
+- **User Journey**: User experience flow mapping
|
|
|
|
|
+- **Requirement Diagrams**: Requirements and relationships
|
|
|
|
|
+- **Timeline**: Chronological event visualization
|
|
|
|
|
+
|
|
|
|
|
+## 🚀 Quick Start
|
|
|
|
|
+
|
|
|
|
|
+### Installation
|
|
|
|
|
|
|
|
```bash
|
|
```bash
|
|
|
-# 克隆项目
|
|
|
|
|
-git clone <repository-url>
|
|
|
|
|
-cd mermaid-go
|
|
|
|
|
-
|
|
|
|
|
-# 运行演示
|
|
|
|
|
-go run demo.go
|
|
|
|
|
-
|
|
|
|
|
-# 运行测试
|
|
|
|
|
-go test ./tests/
|
|
|
|
|
-
|
|
|
|
|
-# 构建 CLI 工具
|
|
|
|
|
-go build -o mmdc cmd/mermaid-cli/main.go
|
|
|
|
|
|
|
+go get github.com/your-org/mermaid-go
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-## 使用示例
|
|
|
|
|
-
|
|
|
|
|
-### 基础 API 使用
|
|
|
|
|
|
|
+### Basic Usage
|
|
|
|
|
|
|
|
```go
|
|
```go
|
|
|
package main
|
|
package main
|
|
@@ -54,206 +77,377 @@ package main
|
|
|
import (
|
|
import (
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"log"
|
|
"log"
|
|
|
-
|
|
|
|
|
- "mermaid-go/pkg/ast"
|
|
|
|
|
- "mermaid-go/pkg/lexer"
|
|
|
|
|
"mermaid-go/pkg/parser"
|
|
"mermaid-go/pkg/parser"
|
|
|
"mermaid-go/pkg/renderer"
|
|
"mermaid-go/pkg/renderer"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
func main() {
|
|
|
- input := `graph TD
|
|
|
|
|
- A[圣诞节] -->|获得金钱| B(去购物)
|
|
|
|
|
- B --> C{让我想想}
|
|
|
|
|
- C -.->|一| D[笔记本电脑]
|
|
|
|
|
- C -.->|二| E[iPhone]
|
|
|
|
|
- C -.->|三| F[汽车]`
|
|
|
|
|
-
|
|
|
|
|
- // 解析 Mermaid 语法
|
|
|
|
|
- parser := parser.NewParser()
|
|
|
|
|
- diagram, err := parser.Parse(input)
|
|
|
|
|
|
|
+ // Parse a Mermaid diagram
|
|
|
|
|
+ input := `flowchart TD
|
|
|
|
|
+ A[Start] --> B{Decision}
|
|
|
|
|
+ B -->|Yes| C[Process]
|
|
|
|
|
+ B -->|No| D[Alternative]
|
|
|
|
|
+ C --> E[End]
|
|
|
|
|
+ D --> E`
|
|
|
|
|
+
|
|
|
|
|
+ mermaidParser := parser.NewMermaidParser()
|
|
|
|
|
+ diagram, err := mermaidParser.Parse(input)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
log.Fatal(err)
|
|
log.Fatal(err)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 验证图表
|
|
|
|
|
- if err := diagram.Validate(); err != nil {
|
|
|
|
|
|
|
+ // Render back to Mermaid syntax
|
|
|
|
|
+ mermaidRenderer := renderer.NewMermaidRenderer()
|
|
|
|
|
+ output, err := mermaidRenderer.Render(diagram)
|
|
|
|
|
+ if err != nil {
|
|
|
log.Fatal(err)
|
|
log.Fatal(err)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 渲染回 Mermaid 语法
|
|
|
|
|
- renderer := renderer.NewFlowchartRenderer()
|
|
|
|
|
- output := renderer.Render(diagram)
|
|
|
|
|
fmt.Println(output)
|
|
fmt.Println(output)
|
|
|
}
|
|
}
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-### 词法分析示例
|
|
|
|
|
|
|
+### Command Line Usage
|
|
|
|
|
|
|
|
-```go
|
|
|
|
|
-// 创建词法分析器
|
|
|
|
|
-lexer := lexer.NewLexer("graph TD\nA --> B")
|
|
|
|
|
-tokens, err := lexer.Tokenize()
|
|
|
|
|
-if err != nil {
|
|
|
|
|
- log.Fatal(err)
|
|
|
|
|
-}
|
|
|
|
|
|
|
+```bash
|
|
|
|
|
+# Parse and validate a Mermaid file
|
|
|
|
|
+go run main.go parse diagram.mmd
|
|
|
|
|
|
|
|
-// 过滤空白和注释
|
|
|
|
|
-filtered := lexer.FilterTokens(tokens)
|
|
|
|
|
-for _, token := range filtered {
|
|
|
|
|
- fmt.Printf("%s: %s\n", token.Type, token.Value)
|
|
|
|
|
-}
|
|
|
|
|
|
|
+# Convert between formats
|
|
|
|
|
+go run main.go convert input.mmd output.mmd
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-### AST 操作示例
|
|
|
|
|
-
|
|
|
|
|
-```go
|
|
|
|
|
-// 创建新的流程图
|
|
|
|
|
-flowchart := ast.NewFlowchart()
|
|
|
|
|
-flowchart.Direction = "TD"
|
|
|
|
|
-
|
|
|
|
|
-// 添加顶点
|
|
|
|
|
-vertexA := &ast.FlowVertex{
|
|
|
|
|
- ID: "A",
|
|
|
|
|
- Text: StringPtr("开始"),
|
|
|
|
|
- Type: &ast.VertexTypeRect,
|
|
|
|
|
-}
|
|
|
|
|
-flowchart.Vertices["A"] = vertexA
|
|
|
|
|
|
|
+## 📖 Supported Syntax
|
|
|
|
|
|
|
|
-vertexB := &ast.FlowVertex{
|
|
|
|
|
- ID: "B",
|
|
|
|
|
- Text: StringPtr("结束"),
|
|
|
|
|
- Type: &ast.VertexTypeCircle,
|
|
|
|
|
-}
|
|
|
|
|
-flowchart.Vertices["B"] = vertexB
|
|
|
|
|
-
|
|
|
|
|
-// 添加边
|
|
|
|
|
-edge := &ast.FlowEdge{
|
|
|
|
|
- Start: "A",
|
|
|
|
|
- End: "B",
|
|
|
|
|
- Text: "流程",
|
|
|
|
|
- Type: StringPtr("arrow_point"),
|
|
|
|
|
- Stroke: &ast.StrokeNormal,
|
|
|
|
|
-}
|
|
|
|
|
-flowchart.Edges = append(flowchart.Edges, edge)
|
|
|
|
|
|
|
+### Flowchart Examples
|
|
|
|
|
|
|
|
-// 渲染
|
|
|
|
|
-renderer := renderer.NewFlowchartRenderer()
|
|
|
|
|
-output := renderer.Render(flowchart)
|
|
|
|
|
-fmt.Println(output)
|
|
|
|
|
|
|
+#### Basic Flowchart
|
|
|
|
|
+```mermaid
|
|
|
|
|
+flowchart TD
|
|
|
|
|
+ A[Start] --> B[Process]
|
|
|
|
|
+ B --> C{Decision}
|
|
|
|
|
+ C -->|Yes| D[Success]
|
|
|
|
|
+ C -->|No| E[Failure]
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-## CLI 工具使用
|
|
|
|
|
|
|
+#### Advanced Features
|
|
|
|
|
+```mermaid
|
|
|
|
|
+flowchart LR
|
|
|
|
|
+ subgraph "User Actions"
|
|
|
|
|
+ A([Login]) --> B{Authenticated?}
|
|
|
|
|
+ end
|
|
|
|
|
|
|
|
-```bash
|
|
|
|
|
-# 从文件解析并输出
|
|
|
|
|
-./mmdc -i input.mmd -o output.mmd
|
|
|
|
|
|
|
+ subgraph "System Process"
|
|
|
|
|
+ C[Process Request] --> D[Generate Response]
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ B -->|Yes| C
|
|
|
|
|
+ B -->|No| A
|
|
|
|
|
|
|
|
-# 从标准输入读取
|
|
|
|
|
-echo "graph TD\nA --> B" | ./mmdc -i - -o -
|
|
|
|
|
|
|
+ %% Styling
|
|
|
|
|
+ classDef userClass fill:#e1f5fe,stroke:#01579b,stroke-width:2px
|
|
|
|
|
+ class A,B userClass
|
|
|
|
|
|
|
|
-# 指定输出格式
|
|
|
|
|
-./mmdc -i input.mmd -o output.mmd -f mermaid
|
|
|
|
|
|
|
+ %% Interactive elements
|
|
|
|
|
+ click A "https://example.com/login" "_blank"
|
|
|
|
|
+ click C callback
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-## 支持的语法
|
|
|
|
|
|
|
+#### Complex Node Shapes
|
|
|
|
|
+```mermaid
|
|
|
|
|
+flowchart TB
|
|
|
|
|
+ A([Start/End]) --> B[Rectangle]
|
|
|
|
|
+ B --> C(Rounded)
|
|
|
|
|
+ C --> D{Diamond}
|
|
|
|
|
+ D --> E[/Parallelogram/]
|
|
|
|
|
+ E --> F[\Alt Parallelogram\]
|
|
|
|
|
+ F --> G>Flag]
|
|
|
|
|
+ G --> H[[Subroutine]]
|
|
|
|
|
+ H --> I((Circle))
|
|
|
|
|
+```
|
|
|
|
|
|
|
|
-### 节点形状
|
|
|
|
|
|
|
+### Class Diagram Examples
|
|
|
|
|
|
|
|
|
|
+#### Basic Class Definition
|
|
|
```mermaid
|
|
```mermaid
|
|
|
-graph TD
|
|
|
|
|
- A[矩形] --> B(圆角矩形)
|
|
|
|
|
- B --> C{菱形}
|
|
|
|
|
- C --> D((圆形))
|
|
|
|
|
- D --> E([体育场形])
|
|
|
|
|
- E --> F[[子程序]]
|
|
|
|
|
- F --> G[("圆柱形")]
|
|
|
|
|
- G --> H{{六边形}}
|
|
|
|
|
|
|
+classDiagram
|
|
|
|
|
+ class Animal {
|
|
|
|
|
+ +String name
|
|
|
|
|
+ +int age
|
|
|
|
|
+ +makeSound() void
|
|
|
|
|
+ -validateAge(int age) boolean
|
|
|
|
|
+ }
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-### 箭头类型
|
|
|
|
|
|
|
+#### Advanced Class Features
|
|
|
|
|
+```mermaid
|
|
|
|
|
+classDiagram
|
|
|
|
|
+ %% Interface definition
|
|
|
|
|
+ class Shape {
|
|
|
|
|
+ <<interface>>
|
|
|
|
|
+ +area() double
|
|
|
|
|
+ +perimeter() double
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ %% Abstract class
|
|
|
|
|
+ class AbstractShape {
|
|
|
|
|
+ <<abstract>>
|
|
|
|
|
+ +String color
|
|
|
|
|
+ +getColor() String
|
|
|
|
|
+ +setColor(String color) void
|
|
|
|
|
+ +area()* double
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ %% Relationships
|
|
|
|
|
+ Shape <|-- AbstractShape
|
|
|
|
|
+ AbstractShape <|-- Circle
|
|
|
|
|
+ AbstractShape <|-- Rectangle
|
|
|
|
|
|
|
|
|
|
+ %% Notes
|
|
|
|
|
+ note "This is a shape hierarchy example"
|
|
|
|
|
+ note for Shape "Defines the basic shape interface"
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+#### Method Parameters and Types
|
|
|
```mermaid
|
|
```mermaid
|
|
|
-graph TD
|
|
|
|
|
- A --> B %% 实线箭头
|
|
|
|
|
- B -.-> C %% 虚线箭头
|
|
|
|
|
- C ==> D %% 粗线箭头
|
|
|
|
|
- D --x E %% 交叉箭头
|
|
|
|
|
- E --o F %% 圆圈箭头
|
|
|
|
|
- F --- G %% 开放箭头
|
|
|
|
|
|
|
+classDiagram
|
|
|
|
|
+ class Calculator {
|
|
|
|
|
+ +add(int a, int b) int
|
|
|
|
|
+ +subtract(double x, double y) double
|
|
|
|
|
+ +multiply(float[] numbers) float
|
|
|
|
|
+ -validateInput(Object input) boolean
|
|
|
|
|
+ }
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-### 边标签
|
|
|
|
|
|
|
+## 🏗️ Architecture
|
|
|
|
|
|
|
|
-```mermaid
|
|
|
|
|
-graph TD
|
|
|
|
|
- A -->|标签文本| B
|
|
|
|
|
- B -->|"带引号的标签"| C
|
|
|
|
|
|
|
+```
|
|
|
|
|
+mermaid-go/
|
|
|
|
|
+├── pkg/
|
|
|
|
|
+│ ├── ast/ # Abstract Syntax Tree definitions
|
|
|
|
|
+│ │ ├── flowchart.go
|
|
|
|
|
+│ │ ├── class.go
|
|
|
|
|
+│ │ └── ...
|
|
|
|
|
+│ ├── lexer/ # Tokenization
|
|
|
|
|
+│ │ └── lexer.go
|
|
|
|
|
+│ ├── parser/ # Syntax analysis
|
|
|
|
|
+│ │ ├── mermaid.go
|
|
|
|
|
+│ │ ├── flowchart.go
|
|
|
|
|
+│ │ ├── class.go
|
|
|
|
|
+│ │ └── ...
|
|
|
|
|
+│ └── renderer/ # Output generation
|
|
|
|
|
+│ ├── mermaid.go
|
|
|
|
|
+│ ├── flowchart.go
|
|
|
|
|
+│ └── class.go
|
|
|
|
|
+├── cmd/ # CLI applications
|
|
|
|
|
+├── examples/ # Usage examples
|
|
|
|
|
+└── tests/ # Test diagrams
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+### Key Components
|
|
|
|
|
+
|
|
|
|
|
+- **Lexer**: Tokenizes Mermaid syntax into a stream of tokens
|
|
|
|
|
+- **Parser**: Builds Abstract Syntax Trees (AST) from token streams
|
|
|
|
|
+- **AST**: Represents diagram structure in memory
|
|
|
|
|
+- **Renderer**: Converts AST back to Mermaid syntax or other formats
|
|
|
|
|
+
|
|
|
|
|
+## 🧪 Testing
|
|
|
|
|
+
|
|
|
|
|
+### Run Basic Tests
|
|
|
|
|
+```bash
|
|
|
|
|
+go test ./...
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-## 测试
|
|
|
|
|
|
|
+### Test Specific Diagram Types
|
|
|
|
|
+```bash
|
|
|
|
|
+# Test flowcharts
|
|
|
|
|
+go run test_flowchart_advanced.go
|
|
|
|
|
|
|
|
-项目包含完整的测试套件:
|
|
|
|
|
|
|
+# Test class diagrams
|
|
|
|
|
+go run test_class_advanced.go
|
|
|
|
|
+```
|
|
|
|
|
|
|
|
|
|
+### Validation Examples
|
|
|
```bash
|
|
```bash
|
|
|
-# 运行所有测试
|
|
|
|
|
-go test ./tests/
|
|
|
|
|
|
|
+# Test round-trip parsing
|
|
|
|
|
+echo 'flowchart TD; A --> B' | go run main.go validate
|
|
|
|
|
+```
|
|
|
|
|
|
|
|
-# 运行词法分析器测试
|
|
|
|
|
-go test ./tests/ -run TestLexer
|
|
|
|
|
|
|
+## 🎨 Advanced Features
|
|
|
|
|
|
|
|
-# 运行解析器测试
|
|
|
|
|
-go test ./tests/ -run TestParser
|
|
|
|
|
|
|
+### Interactive Elements
|
|
|
|
|
+```mermaid
|
|
|
|
|
+flowchart TD
|
|
|
|
|
+ A[Button] --> B[Action]
|
|
|
|
|
|
|
|
-# 运行集成测试
|
|
|
|
|
-go test ./tests/ -run TestMermaid
|
|
|
|
|
|
|
+ click A "https://example.com" "_blank"
|
|
|
|
|
+ click B callback
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-## 开发指南
|
|
|
|
|
|
|
+### Styling and Themes
|
|
|
|
|
+```mermaid
|
|
|
|
|
+flowchart LR
|
|
|
|
|
+ A --> B --> C
|
|
|
|
|
|
|
|
-### 架构说明
|
|
|
|
|
|
|
+ classDef default fill:#f9f9f9,stroke:#333,stroke-width:2px
|
|
|
|
|
+ classDef highlight fill:#ff6b6b,stroke:#c92a2a,stroke-width:3px
|
|
|
|
|
|
|
|
-该项目的设计严格遵循 mermaid.js 的内部架构:
|
|
|
|
|
|
|
+ class B highlight
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+### Subgraphs and Organization
|
|
|
|
|
+```mermaid
|
|
|
|
|
+flowchart TB
|
|
|
|
|
+ subgraph Frontend ["Frontend Layer"]
|
|
|
|
|
+ direction LR
|
|
|
|
|
+ A[React App] --> B[API Client]
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ subgraph Backend ["Backend Services"]
|
|
|
|
|
+ direction TB
|
|
|
|
|
+ C[REST API] --> D[Database]
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ Frontend --> Backend
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+## 🔧 Configuration
|
|
|
|
|
+
|
|
|
|
|
+### Parser Options
|
|
|
|
|
+```go
|
|
|
|
|
+parser := parser.NewMermaidParser()
|
|
|
|
|
+parser.SetStrictMode(true) // Enable strict syntax validation
|
|
|
|
|
+parser.SetDebugMode(true) // Enable detailed error messages
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+### Renderer Options
|
|
|
|
|
+```go
|
|
|
|
|
+renderer := renderer.NewMermaidRenderer()
|
|
|
|
|
+renderer.SetIndentation(" ") // Set custom indentation
|
|
|
|
|
+renderer.SetCompactMode(false) // Pretty-print output
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+## 🤝 Contributing
|
|
|
|
|
+
|
|
|
|
|
+We welcome contributions! Here's how to get started:
|
|
|
|
|
|
|
|
-1. **词法分析 (Lexer)**: 基于 flow.jison 的词法规则,将输入文本转换为标记流
|
|
|
|
|
-2. **语法分析 (Parser)**: 基于 flow.jison 的语法规则,构建 AST
|
|
|
|
|
-3. **AST**: 镜像 mermaid.js 的 TypeScript 类型定义
|
|
|
|
|
-4. **FlowDB**: 模拟 mermaid.js 的 FlowDB 类,管理解析状态
|
|
|
|
|
-5. **渲染器**: 将 AST 转换回 Mermaid 语法字符串
|
|
|
|
|
|
|
+1. **Fork the repository**
|
|
|
|
|
+2. **Create a feature branch**: `git checkout -b feature/new-diagram-type`
|
|
|
|
|
+3. **Make your changes** and add tests
|
|
|
|
|
+4. **Run tests**: `go test ./...`
|
|
|
|
|
+5. **Submit a pull request**
|
|
|
|
|
|
|
|
-### 添加新功能
|
|
|
|
|
|
|
+### Adding New Diagram Types
|
|
|
|
|
|
|
|
-1. 在 `pkg/ast/` 中定义新的 AST 节点类型
|
|
|
|
|
-2. 在 `pkg/lexer/` 中添加新的标记类型
|
|
|
|
|
-3. 在 `pkg/parser/` 中实现解析逻辑
|
|
|
|
|
-4. 在 `pkg/renderer/` 中实现渲染逻辑
|
|
|
|
|
-5. 添加相应的测试用例
|
|
|
|
|
|
|
+1. **Define AST structures** in `pkg/ast/`
|
|
|
|
|
+2. **Implement parser** in `pkg/parser/`
|
|
|
|
|
+3. **Add renderer** in `pkg/renderer/`
|
|
|
|
|
+4. **Write tests** and examples
|
|
|
|
|
+5. **Update documentation**
|
|
|
|
|
|
|
|
-### 代码风格
|
|
|
|
|
|
|
+### Current Development Priorities
|
|
|
|
|
|
|
|
-- 遵循 Go 语言规范
|
|
|
|
|
-- 使用 `go fmt` 格式化代码
|
|
|
|
|
-- 添加适当的注释和文档
|
|
|
|
|
-- 所有公共 API 都需要有测试覆盖
|
|
|
|
|
|
|
+1. **Complete Generic Types** support for Class Diagrams
|
|
|
|
|
+2. **Markdown Text Formatting** for all diagram types
|
|
|
|
|
+3. **Edge Labels and Animations** for Flowcharts
|
|
|
|
|
+4. **ER Diagram** implementation
|
|
|
|
|
+5. **Gantt Chart** support
|
|
|
|
|
+
|
|
|
|
|
+## 📚 Examples and Tutorials
|
|
|
|
|
+
|
|
|
|
|
+### Real-World Use Cases
|
|
|
|
|
+
|
|
|
|
|
+#### Software Architecture Documentation
|
|
|
|
|
+```mermaid
|
|
|
|
|
+flowchart TD
|
|
|
|
|
+ subgraph "Client Tier"
|
|
|
|
|
+ A[Web Browser] --> B[Mobile App]
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ subgraph "Application Tier"
|
|
|
|
|
+ C[Load Balancer] --> D[API Gateway]
|
|
|
|
|
+ D --> E[Microservice 1]
|
|
|
|
|
+ D --> F[Microservice 2]
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ subgraph "Data Tier"
|
|
|
|
|
+ G[(Primary DB)] --> H[(Replica DB)]
|
|
|
|
|
+ I[Cache Layer]
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ A --> C
|
|
|
|
|
+ B --> C
|
|
|
|
|
+ E --> G
|
|
|
|
|
+ F --> G
|
|
|
|
|
+ E --> I
|
|
|
|
|
+ F --> I
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+#### Class Hierarchy Documentation
|
|
|
|
|
+```mermaid
|
|
|
|
|
+classDiagram
|
|
|
|
|
+ class Vehicle {
|
|
|
|
|
+ <<abstract>>
|
|
|
|
|
+ +String brand
|
|
|
|
|
+ +int year
|
|
|
|
|
+ +start() void
|
|
|
|
|
+ +stop() void
|
|
|
|
|
+ +getInfo()* String
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ class Car {
|
|
|
|
|
+ +int doors
|
|
|
|
|
+ +String fuelType
|
|
|
|
|
+ +getInfo() String
|
|
|
|
|
+ +openTrunk() void
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ class Motorcycle {
|
|
|
|
|
+ +boolean hasSidecar
|
|
|
|
|
+ +getInfo() String
|
|
|
|
|
+ +popWheelie() void
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Vehicle <|-- Car
|
|
|
|
|
+ Vehicle <|-- Motorcycle
|
|
|
|
|
+
|
|
|
|
|
+ note for Vehicle "Base class for all vehicles"
|
|
|
|
|
+```
|
|
|
|
|
|
|
|
## 与 mermaid.js 的对应关系
|
|
## 与 mermaid.js 的对应关系
|
|
|
|
|
|
|
|
|
|
+该项目的设计严格遵循 mermaid.js 的内部架构:
|
|
|
|
|
+
|
|
|
| mermaid.js | mermaid-go |
|
|
| mermaid.js | mermaid-go |
|
|
|
|------------|------------|
|
|
|------------|------------|
|
|
|
| `flow.jison` | `pkg/lexer/lexer.go` + `pkg/parser/flowchart.go` |
|
|
| `flow.jison` | `pkg/lexer/lexer.go` + `pkg/parser/flowchart.go` |
|
|
|
| `flowDb.ts` | `pkg/parser/flowchart.go` (FlowDB) |
|
|
| `flowDb.ts` | `pkg/parser/flowchart.go` (FlowDB) |
|
|
|
-| `types.ts` | `pkg/ast/flowchart.go` |
|
|
|
|
|
|
|
+| `classDiagram.jison` | `pkg/parser/class.go` |
|
|
|
|
|
+| `types.ts` | `pkg/ast/flowchart.go`, `pkg/ast/class.go` |
|
|
|
| `flowRenderer.ts` | `pkg/renderer/flowchart.go` |
|
|
| `flowRenderer.ts` | `pkg/renderer/flowchart.go` |
|
|
|
|
|
|
|
|
-## 许可证
|
|
|
|
|
|
|
+## 项目作者
|
|
|
|
|
+
|
|
|
|
|
+由 gytmtc 创建,基于 mermaid.js 的架构设计,现已扩展支持多种图表类型和高级特性。
|
|
|
|
|
+
|
|
|
|
|
+## 📄 License
|
|
|
|
|
+
|
|
|
|
|
+This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
|
|
|
+
|
|
|
|
|
+## 🙏 Acknowledgments
|
|
|
|
|
|
|
|
-MIT License - 详见 LICENSE 文件
|
|
|
|
|
|
|
+- [Mermaid.js](https://mermaid.js.org/) for the original specification and inspiration
|
|
|
|
|
+- The Go community for excellent parsing libraries and patterns
|
|
|
|
|
+- All contributors who help improve this project
|
|
|
|
|
|
|
|
-## 贡献
|
|
|
|
|
|
|
+## 📞 Support
|
|
|
|
|
|
|
|
-欢迎提交 Issue 和 Pull Request!
|
|
|
|
|
|
|
+- **Issues**: [GitHub Issues](https://github.com/your-org/mermaid-go/issues)
|
|
|
|
|
+- **Documentation**: [Wiki](https://github.com/your-org/mermaid-go/wiki)
|
|
|
|
|
+- **Discussions**: [GitHub Discussions](https://github.com/your-org/mermaid-go/discussions)
|
|
|
|
|
|
|
|
-## 致谢
|
|
|
|
|
|
|
+---
|
|
|
|
|
|
|
|
-- 感谢 [mermaid.js](https://github.com/mermaid-js/mermaid) 项目提供的优秀架构设计
|
|
|
|
|
-- 感谢 [Knut Sveidqvist](https://github.com/knsv) 和所有 mermaid.js 贡献者
|
|
|
|
|
|
|
+**Current Version**: 0.8.0 (Alpha)
|
|
|
|
|
+**Mermaid.js Compatibility**: ~82% of core features
|
|
|
|
|
+**Status**: Active Development
|