文章

mermaid 语法参考(一)流程图

mermaid 语法和要点备忘

mermaid 语法参考(一)流程图

在 chirpy 的帖子中,需要在设置部分加入 mermaid: true 才能开启 mermaid 支持

chirpy 当前的 mermaid 版本为 v11.4.0,有些新语法特性不支持

基本语法

注释

mermaid 使用 %% 进行行注释

Frontformatter

1
2
3
4
5
6
7
8
9
10
---
title: Frontmatter Example
displayMode: compact
config:
  layout: elk
  look: handDrawn
  theme: forest
gantt:
    useWidth: 400
    comp

流程图 flowchart

---
title: 在这里修改图表标题
---
flowchart LR
    节点A("`**四大名著**😊包括`")
    节点B["`西游记<br>水浒传<br>红楼梦
    三国演义`"]
    节点A ---> 节点B

    电话卡@{ shape: notch-rect, label: "nrect" }

图表方向

可取值包括 LR RL TB BT

节点内文本

  • 如果文本包含 Unicode 字符,要用" 包含起来
  • 如果包含有 markdown 文本,需要用 "\\` 包含

节点形状

基本节点形状

符号形状符号形状
[ ]矩形( )圆角矩形
([ ])圆角矩形[[ ]]双竖线矩形
[( )]圆柱形(( ))圆形
[/ /]平行四边形[\ \]反向平行四边形
[/ \]梯形[\ /]梯形
((( )))同心圆[\ /]梯形

扩展节点形状

mermaid v11.3.0+ 引入,语法格式为 nodeName@{ shape: rect,label:"content"}

Semantic NameShape NameShort NameDescriptionAlias Supported
CardNotched Rectanglenotch-rectRepresents a cardcard, notched-rectangle
CollateHourglasshourglassRepresents a collate operationcollate, hourglass
Com LinkLightning BoltboltCommunication linkcom-link, lightning-bolt
CommentCurly BracebraceAdds a commentbrace-l, comment
Comment RightCurly Bracebrace-rAdds a comment 
Comment with braces on both sidesCurly BracesbracesAdds a comment 
Data Input/OutputLean Rightlean-rRepresents input or outputin-out, lean-right
Data Input/OutputLean Leftlean-lRepresents output or inputlean-left, out-in
DatabaseCylindercylDatabase storagecylinder, database, db
DecisionDiamonddiamDecision-making stepdecision, diamond, question
DelayHalf-Rounded RectangledelayRepresents a delayhalf-rounded-rectangle
Direct Access StorageHorizontal Cylinderh-cylDirect access storagedas, horizontal-cylinder
Disk StorageLined Cylinderlin-cylDisk storagedisk, lined-cylinder
DisplayCurved Trapezoidcurv-trapRepresents a displaycurved-trapezoid, display
Divided ProcessDivided Rectanglediv-rectDivided process shapediv-proc, divided-process, divided-rectangle
DocumentDocumentdocRepresents a documentdoc, document
EventRounded RectangleroundedRepresents an eventevent
ExtractTriangletriExtraction processextract, triangle
Fork/JoinFilled RectangleforkFork or join in process flowjoin
Internal StorageWindow Panewin-paneInternal storageinternal-storage, window-pane
JunctionFilled Circlef-circJunction pointfilled-circle, junction
Lined DocumentLined Documentlin-docLined documentlined-document
Lined/Shaded ProcessLined Rectanglelin-rectLined process shapelin-proc, lined-process, lined-rectangle, shaded-process
Loop LimitTrapezoidal Pentagonnotch-pentLoop limit steploop-limit, notched-pentagon
Manual FileFlipped Triangleflip-triManual file operationflipped-triangle, manual-file
Manual InputSloped Rectanglesl-rectManual input stepmanual-input, sloped-rectangle
Manual OperationTrapezoid Base Toptrap-tRepresents a manual taskinv-trapezoid, manual, trapezoid-top
Multi-DocumentStacked DocumentdocsMultiple documentsdocuments, st-doc, stacked-document
Multi-ProcessStacked Rectanglest-rectMultiple processesprocesses, procs, stacked-rectangle
OddOddoddOdd shape 
Paper TapeFlagflagPaper tapepaper-tape
Prepare ConditionalHexagonhexPreparation or condition stephexagon, prepare
Priority ActionTrapezoid Base Bottomtrap-bPriority actionpriority, trapezoid, trapezoid-bottom
ProcessRectanglerectStandard process shapeproc, process, rectangle
StartCirclecircleStarting pointcirc
StartSmall Circlesm-circSmall starting pointsmall-circle, start
StopDouble Circledbl-circRepresents a stop pointdouble-circle
StopFramed Circlefr-circStop pointframed-circle, stop
Stored DataBow Tie Rectanglebow-rectStored databow-tie-rectangle, stored-data
SubprocessFramed Rectanglefr-rectSubprocessframed-rectangle, subproc, subprocess, subroutine
SummaryCrossed Circlecross-circSummarycrossed-circle, summary
Tagged DocumentTagged Documenttag-docTagged documenttag-doc, tagged-document
Tagged ProcessTagged Rectangletag-rectTagged processtag-proc, tagged-process, tagged-rectangle
Terminal PointStadiumstadiumTerminal pointpill, terminal
Text BlockText BlocktextText block 

图标 icon

要使用图标,需要先注册图标包。按照添加自定义图标中的说明进行操作。

flowchart LR
  user(fa:fa-user Tom)-->D(fa:fa-spinner Waiting)

图像 image

1
A@{ img: "url_to_image", label: "Image Label", pos: "t", w: 60, h: 60, constraint: "off" }

链接线

flowchart TD
  A((Start)) --> B(Processing) ==> C((End))
  D --> E & F -.-> G
  U & V --> S & T

链接线形状

符号样式符号样式
---直线-.-虚线
===粗直线~~~隐藏的线
-->右箭头--x右端点为叉号
--o右端点为圆形  
-- text --带文字直线-- text -->带文字和箭头直线

指定链接线 ID

在链接线后指定名称并以 @ 结尾(经测试,chirpy 中的 mermaid v11.4.0 不支持此功能)

1
2
3
flowchart LR
  A e1@---> B
  e1@{ animate: true }

子图

---
displayMode: compact
---
flowchart LR
  subgraph TOP
    direction TB
    subgraph B1
        direction RL
        i1 -->f1
    end
    subgraph B2
        direction BT
        i2 -->f2
    end
  end
  A --> TOP --> B
  B1 --> B2

交互

单击打开链接

1
2
click nodeId callback
click nodeId call callback()

将测试,Chrome 浏览器下不能弹出窗口

---
securityLevel: 'loose'
---
flowchart LR
    A-->B
    B-->C
    C-->D
    D-->E
    click A "https://www.baidu.com" _blank
    click B "https://www.baidu.com" "Open this in a new tab" _blank
    click C href "https://www.baidu.com" _blank
    click D href "https://www.baidu.com" "Open this in a new tab" _blank

样式类

定义样式类

1
classDef className fill:#e0e,stroke:#333,stroke-width:4px;

附着样式类

在节点名称后使用 ::: 符号,也可使用 class 关键字

样式类案例

1
2
3
4
5
6
7
8
9
10
11
flowchart LR
  %% nodeName:::className 可以为节点指定 class
  A --> B --> C --> D --> E --> F:::myNodeClass
  linkStyle 1,3 color:red;
  style A fill:#f9f,stroke:#333,stroke-width:4px
  style B fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5

  %% 定义节点类
  classDef myNodeClass fill:#a12,stroke:#333,stroke-width:1px,round:2px
  %% 为节点附着类
  class C,D myNodeClass;
flowchart LR
  %% nodeName:::className 可以为节点指定 class
  A --> B --> C --> D --> E --> F:::myNodeClass
  linkStyle 1,3 color:red;
  style A fill:#f9f,stroke:#333,stroke-width:4px
  style B fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5

  %% 定义节点类
  classDef myNodeClass fill:#a12,stroke:#333,stroke-width:1px,round:2px
  %% 为节点附着类
  class C,D myNodeClass;

饼图

---
config:
  width: 100
  pie:
    textPosition: 0.5
  themeVariables:
    pieOuterStrokeWidth: "5px"
---
pie showData
    title Key elements in Product X
    "Calcium" : 42.96
    "Potassium" : 50.05
    "Magnesium" : 10.01
    "Iron" :  5

在线资源

本文由作者按照 CC BY 4.0 进行授权