工欲善其事,必先利其器,Markdown和Mermaid的梦幻联动(1)

该文章Github地址:https://github.com/AntonyCheng/typora-notes/tree/master/chapter03-mermaid【有条件的情况下推荐直接访问GitHub以获取最新的代码更新】

在此介绍一下作者开源的SpringBoot项目初始化模板(Github仓库地址:https://github.com/AntonyCheng/spring-boot-init-template【有条件的情况下推荐直接访问GitHub以获取最新的代码更新】& CSDN文章地址:https://blog.csdn.net/AntonyCheng/article/details/136555245),该模板集成了最常见的开发组件,同时基于修改配置文件实现组件的装载,除了这些,模板中还有非常丰富的整合示例,同时单体架构也非常适合SpringBoot框架入门,如果觉得有意义或者有帮助,欢迎Star & Issues & PR!

1.Mermaid 功能

Mermaid可以让我们使用文本和代码创建图表和可视化效果:

它主要包括以下图像的创建:

Mermaid { F l o w c h a r t − 流程图 S e q u e n c e   D i a g r a m − 时序图 C l a s s   D i a g r a m − 类图 S t a t e   D i a g r a m − 状态图 E n t i t y   R e l a t i o n s h i p   D i a g r a m − 实体关系图 U s e r   J o u r n e y − 用户行程图 G a n t t − 甘特图 P i e   C h a r t − 饼状图 R e q u i r e m e n t   D i a g r a m − 需求图 O t h e r   E x a m p l e s ⋯ \textbf{Mermaid}\begin{cases} \bold{Flowchart-流程图} \\ \bold{Sequence\ Diagram-时序图} \\ \bold{Class\ Diagram-类图} \\ \bold{State\ Diagram-状态图} \\ \bold{Entity\ Relationship\ Diagram-实体关系图} \\ \bold{User\ Journey-用户行程图} \\ \bold{Gantt-甘特图} \\ \bold{Pie\ Chart-饼状图} \\ \bold{Requirement\ Diagram-需求图} \\ \bold{Other\ Examples\cdots} \end{cases} Mermaid Flowchart流程图Sequence Diagram时序图Class Diagram类图State Diagram状态图Entity Relationship Diagram实体关系图User Journey用户行程图Gantt甘特图Pie Chart饼状图Requirement Diagram需求图Other Examples

2.Mermaid介绍

  • 它是一种基于 JavaScript 的图表和图表工具,可呈现受 Markdown 启发的文本定义以动态创建和修改图表

  • 而在 typora 中我们可以输入 ```mermaid 然后敲击回车,即可初始化一张空白的图。

3.流程图(Flowchart)

  • 所有流程图都由节点、几何形状和边缘、箭头或线条组成。 Mermaid 代码定义了这些节点和边的生成和交互方式。
  • 它还可以容纳不同的箭头类型、多向箭头以及与子图的链接。

3.1.节点

例如下:

id
flowchart LR
	id
  • 这个节点名为 id ,被展示到了这个盒子里。

3.2.文本节点

例如下:

This is the text in the box
flowchart LR
	id1[This is the text in the box]
  • 可以用[]的形式将文本内容框在box1当中,随后外层的box1就成为了一个定义语句。

3.3.图形

  • 这个模块会声明流程图的方向。
  • 下例声明该流程图是从上到下的(TD 或者 TB)。

例如下:

start
continue
finally
flowchart TD
	start-->continue-->finally
start
continue
finally
flowchart TD
	id1(start)-->id2(continue)-->id3(finally)
start
continue
finally
flowchart TD
	id1(start)
	id2(continue)
	id3(finally)
	id1-->id2-->id3

3.4.流程图方向

{ T B − 从上到下 ( T o p   T o   B o t t o m ) T D − 从上到下 ( T o p   T o   D o w n ) B T − 从下到上 ( B o t t o m   T o   T o p ) R L − 从右到左 ( R i g h t   T o   L e f t ) L R − 从左到右 ( L e f t   T o   R i g h t ) \begin{cases} TB - \textbf{从上到下}(Top \ To \ Bottom) \\\\ TD - \textbf{从上到下}(Top \ To \ Down) \\\\ BT - \textbf{从下到上}(Bottom \ To \ Top) \\\\ RL - \textbf{从右到左}(Right \ To \ Left) \\\\ LR -\textbf{从左到右}(Left \ To \ Right) \end{cases} TB从上到下Top To BottomTD从上到下Top To DownBT从下到上Bottom To TopRL从右到左Right To LeftLR从左到右Left To Right

3.5.节点形状

  1. 圆角:
A node with round edges
flowchart LR
id(A node with round edges)
  1. 圆边:
A stadium-shaped node
flowchart LR
id([A stadium-shaped node])
  1. 子程序框:
A node in a subroutine shape
flowchart LR
id[[A node in a subroutine shape]]
  1. 圆柱节点:
A Database
flowchart LR
id[(A Database)]
  1. 圆形:
The sentence in the circle
flowchart LR
id((The sentence in the circle))
  1. 标签图:
A node in an asymmetric shape
flowchart LR
id>A node in an asymmetric shape]
  1. 菱形:
A rhombus
flowchart LR
id{A rhombus}
  1. 六边形:
A hexagon node
flowchart LR
id{{A hexagon node}}
  1. 平行四边形:
Parallelogram
flowchart LR
id[/Parallelogram/]
  1. 平行四边形(反):
Parallelogram alt
flowchart LR
id[\Parallelogram alt\]
  1. 梯形:
Trapezoid
flowchart LR
id[/Trapezoid\]
  1. 梯形(反):
Trapezoid alt
flowchart LR
id[\Trapezoid alt/]

3.6.线条

  1. 箭头线条:
a
b
flowchart LR
a-->b
  1. 正常线条:
a
b
flowchart LR
a---b
  1. 字段线条:
this is a sentence!
this is a sentence!
a
b
c
d
flowchart LR
a--this is a sentence!---b
c--this is a sentence!-->d
this is a sentense!
this is a sentense!
a
b
c
d
flowchart LR
a---|this is a sentense!|b
c-->|this is a sentense!|d
  1. 虚线条:
text
text
a
-b
c
d
e
f
g
h
flowchart LR
a-.--b
c-.->d
e-.text.-f
g-.text.->h
  1. 加粗线条:
text
a
b
c
d
flowchart LR
a==>b
c==text==>d
  1. 连接线:
text1
text2
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
flowchart LR
a--text1-->b--text2---c
d--- e & f ---g
h--> i & j -->k
l & m --> n & o --- p & q
其实可以一步一步来
a
c
d
b
e
f
flowchart LR
a-->c
a-->d
b-->c
b-->d
c---e
d---e
c---f
d---f
  1. 特殊线条:
a
b
c
d
e
f
g
flowchart  LR
a--ob--xc
d o--o e <--> f x--xg

8.线条的长短:

  • 线条长短可以用额外的破折号来实现:
Yes
No
Start
Is it ?
OK
Rethink
End
flowchart TD
a(Start)
b{Is it ?}
c[OK]
d[Rethink]
e[End]
a-->b
b--Yes-->c-->d-->b
b--No---->e
  • 这里需要看看代码, a,b,c,d,e 依次降级(高度依次下降),de需要降两级,所以加了两条破折号。

  • 注意线条的长度可能会超过渲染引擎的请求等级,简而言之应该有限设置。

  1. 总结:
长度123
正常线条------------
箭头线条-->--->---->
加粗线条============
加粗箭头线条==>===>====>
虚线-.--..--...-
箭头虚线-.->-..->-...->

3.7.转义字符的实体代码

  • 可以将文本放在引号内以呈现更麻烦的字符。如下例所示:
This is the(text) in the box
flowchart LR
id1["This is the(text) in the box"]
  • 但是我们要转义特殊的字符:
A double quote: "
>:>
<:<
#:#
©:©
flowchart LR
A["A double quote: #quot;"]-->B[">:#gt;"]-->C["<:#lt;"]-->D["#:#35;"]-->E["#copy;:#copy;"]
  • 可以看出这里是支持HTML5的字符名称的。

3.8.子图

示例:

flowchart LR
	subgraph title
		graph definition
		···
	end
	subgraph title
		graph dedinition
		···
	end
	···

我们来写一个我家和舅舅家的一个关系图:

unclefamily
myfamily
grandfamily
couple
mather
sister
brother
couple
couple
mather
mather
father
father
mycousin
myuncle
myaunt
me
mymather
myfather
grandmather
grandfather
flowchart TB
subgraph grandfamily
grandfather<--couple-->grandmather
end
subgraph myfamily
mymather-- mather-->me
myfather-->me
end
subgraph unclefamily
myuncle-->mycousin
myaunt-->mycousin
end
mymather-- sister -->myuncle
me-- brother -->mycousin
myuncle<-- couple -->myaunt
mymather<--couple-->myfather
grandmather--mather-->mymather & myuncle
grandfather--father-->mymather & myuncle

当然也可以序数三个家庭之间的关系:

parent
parent
sister and brother
unclefamily
mycousin
myuncle
myaunt
myfamily
mather
me
mymather
myfather
grandfamily
couple
grandmather
grandfather
flowchart TB
subgraph grandfamily
grandfather<--couple-->grandmather
end
subgraph myfamily
mymather-- mather-->me
myfather-->me
end
subgraph unclefamily
myuncle-->mycousin
myaunt-->mycousin
end
grandfamily-- parent -->myfamily
grandfamily-- parent -->unclefamily
myfamily<--sister and brother-->unclefamily

当然啦,既然flowchart中可以嵌入subgraph,那么subgraph里面也可以嵌入graph呀!

grandfamily
unclefamily
myfamily
couple
mather
sister
brother
couple
couple
mather
mather
father
father
sister and brother
grandmather
grandfather
myuncle
mycousin
myaunt
mymather
me
myfather
flowchart TB
subgraph grandfamily
	grandfather<--couple-->grandmather
	subgraph unclefamily
		myuncle-->mycousin
		myaunt-->mycousin
	end
	subgraph myfamily
		mymather-- mather-->me
		myfather-->me
	end
end
mymather-- sister -->myuncle
me-- brother -->mycousin
myuncle<-- couple -->myaunt
mymather<--couple-->myfather
myfamily<--sister and brother----->unclefamily
grandmather--mather-->mymather & myuncle
grandfather--father-->mymather & myuncle

3.10.样式和类

  1. 节点样式:
Start
Stop
flowchart LR
id1(Start)
id2(Stop)
style id1 fill:#f9f,stroke:#000,stroke-width:4px
style id2 fill:#ccc,stroke:#f66,stroke-width:5px,color:#fff,stroke-dasharray:5 5
id1<---->id2

我们对比HTML5的样式来看:

HTML5样式Mermaid样式
background-colorfill
colorcolor
border-colorstroke
border-widthstroke-width
border-stylestroke-dasharray
  1. JavaScript样式类:

    示例如下:

    flowchart LR
    ···:::class1
    ···:::class2
    ···:::class3
    ···
    classDef class1 fill···
    classDef class2 fill···
    classDef class3 fill···
    ···
    

    例如下:

    A
    B
    flowchart LR
    A:::className1-->B:::className2
    classDef className1 fill:#abc;
    classDef className2 fill:#cba;
    

4.时序图(SequenceDiagram)

  • 时序图是一种交互图,它显示了进程如何相互操作以及以何种顺序运行。
  • 注意:end是一个会破坏语法的特殊字符,如果end的使用不可避免,那就应该用{},[],()将其框起来。

示例如下:

Alice John Hello John,how are you? Great! See you later! Alice John
sequenceDiagram
Alice->>John:Hello John,how are you?
John-->>Alice:Great!
Alice->>John:See you later!

4.1.句法

  1. 参与者:

    • 参与者可以像本页第一个示例中那样隐式定义。参与者或参与者按在图表源文本中的出现顺序呈现。有时,我们可能希望以不同于他们在第一条消息中出现的顺序显示参与者。可以通过执行以下操作来指定participant的出现顺序:
    Alice Bob Hi Bob! Hi Alice! Alice Bob
    sequenceDiagram
    participant Alice
    participant Bob
    Alice->>Bob:Hi Bob!
    Bob->>Alice:Hi Alice!
    
  2. 缩写:

    • 参与者可以被设置为一个描述性的标签:
    Alice Bob Hello john,how are you? Great! Alice Bob
    sequenceDiagram
    participant A as Alice
    participant B as Bob
    A->>B:Hello john,how are you?
    B->>A:Great!
    

4.2.箭头样式

类型描述
->没有箭头的实线
-->没有箭头的虚线
->>带箭头的实线
-->>带箭头的虚线
-x带十字的实线
--x带十字的虚线
a b s s s s s s a b
sequenceDiagram
a->b:s
a-->b:s
a->>b:s
a-->>b:s
a-xb:s
a--xb:s

4.3.激活

  • 一个参与者可以被激活,也可以被屏蔽,activatedeactivate可以用来声明:
Alice John Hello John,how are you? Great Alice John
sequenceDiagram
Alice->>John:Hello John,how are you?
activate John
John-->>Alice:Great
deactivate John
  • 其实我们还可以用一些格式上的缩写,比如通过+/-来实现这一功能:
Alice John Hello John,how are you? Great! Alice John
sequenceDiagram
Alice->>+John:Hello John,how are you?
John->>-Alice:Great!
  • 激活状态能够实现在相同的参与者身上:
Alice John Hello John,how are you? John,can you hear me? Hi Alice,I can hear you! I feel great! Alice John
sequenceDiagram
Alice->>+John:Hello John,how are you?
Alice->>+John:John,can you hear me?
John-->>-Alice:Hi Alice,I can hear you!
John-->>-Alice:I feel great!

4.4.笔记

  • 我们也可以在时序图旁可以添加笔记,我们可以用right of left of over来实现:
John Alice Text in note Text in note Text in note John Alice
sequenceDiagram
participant John
participant Alice
Note left of John:Text in note
Note right of Alice:Text in note
Note over Alice,John:Text in note

4.5.循环

  • 我们还可以在时序图中运用循环,示例如下:
sequenceDiagram
A->B:···
loop Loop_text
	B->A:···
end
Alice John Hello John,how are you? Great! loop [EverySecond] Alice John
sequenceDiagram
Alice->John:Hello John,how are you?
loop EverySecond
	John->Alice:Great!
end

4.6.判断

  • 我们还可以在时序图中加入判断语句,如下例:
sequenceDiagram
alt Describing_text
	···
else Describing_text
	···
end
  • 如果else语句没有触发,我们还可以给一个反馈,如下例:
sequenceDiagram
alt Describing_text
	···
else Describing_text
	···
end
pot Extra_response
	···
end
Alice Bob Hello Bob,how are you? Not so good :( Feeling fresh like a daisy alt [is sick] [is well] Thanks for asking opt [Extra_response] Alice Bob
sequenceDiagram
Alice->>Bob:Hello Bob,how are you?
alt is sick
	Bob-->>Alice:Not so good :(
else is well
	Bob-->>Alice:Feeling fresh like a daisy
end
opt Extra_response
	Bob-->>Alice:Thanks for asking
end

4.7.平行线

  • 我们可以用一种方式来表示同时发生的事情(平行发生):
sequenceDiagram
par [action1]
	···
and [action2]
	···
and [action3]
	···
···
and [actionN]
	···
end
  • 我们来写一个传递message的时序图:
Alice Bob Coco Dave Eize message message par [Alice to Bob] [Alice to Coco] message message message par [Bob to Dave] par [Coco to Dave] [Coco to Eize] OK OK OK OK Alice Bob Coco Dave Eize
sequenceDiagram
participant A as Alice
participant B as Bob
participant C as Coco
participant D as Dave
participant E as Eize
par Alice to Bob
	A-->>B:message
and Alice to Coco
	A-->>C:message
end
par Coco to Dave
	C-->>D:message
and Coco to Eize
	C-->>E:message
	par Bob to Dave
		B-->>D:message
	end
end
E->>A:OK
D->>A:OK
C->>A:OK
B->>A:OK
  • 可以看出时序图中的平行并发也能够内嵌。

4.8.背景高亮

  • 我们还可以在时序图中设置背景的高亮属性,示例如下:
rect rgb(,,)
	···	
end
rect rgba(,,,)
	···
end
  • 例如下:
Alice Bob Alice calls Bob. Hello Bob, how are you? Bob,can you hear me? Hi Alice,I can hear you! I feel great! I Did you want to go to the game tonight Yeah! See you there. Alice Bob
sequenceDiagram
participant Alice
participant Bob
rect rgb(191,223,255)
note right of Alice:Alice calls Bob.
Alice->>+Bob:Hello Bob, how are you?
rect rgba(200,150,255,.5)
Alice->>+Bob:Bob,can you hear me?
Bob-->-Alice:Hi Alice,I can hear you!
end
Bob-->>+Alice:I feel great!
end
Alice->>+Bob:I Did you want to go to the game tonight
Bob-->>-Alice:Yeah! See you there.

4.9.注释

  • Mermaid中的注释是由%%完成的:
Alice Bob display! Alice Bob
sequenceDiagram
Alice-->>Bob:display!
%%BoB-->>Alice:no display!

4.10.转义字符的实体代码

例如下:

A B I ♥ you ! I ♥ you ∞ times more! A B
sequenceDiagram
A-->>B:I #9829; you !
B-->>A:I #9829; you #infin; times more!

下一章:工欲善其事,必先利其器,Markdown和Mermaid的梦幻联动(2)

  • 22
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mermaid是一个用于渲染流程图、时序图、甘特图、类图、状态图和饼图等的工具,它可以用来画实体关系图、需求图等。\[1\]Mermaid可以解析渲染Markdown中的Mermaid语法,使得在Markdown中使用Mermaid语法来绘制流程图等变得更加方便。\[2\]Markdown是一种轻量级标记语言,除了编辑文字外,还支持插入图片、表格、公式等。很多博客平台都支持使用Markdown来编辑文章。而Mermaid是基于Markdown的一个库,用于渲染Markdown中的Mermaid语法,使得在Markdown中绘制流程图等变得更加简洁和方便。\[3\]所以,MermaidMarkdown是两个不同的工具,但可以结合使用,通过在Markdown中使用Mermaid语法来绘制流程图等。 #### 引用[.reference_title] - *1* *3* [markdown图表语法Mermaid介绍](https://blog.csdn.net/u010698107/article/details/124371850)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [mermaid - markdownmermaid语法解析渲染](https://blog.csdn.net/weixin_39651356/article/details/127620778)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值