《開源精選》是我們分享Github、Gitee等開源社群中優質項目的欄目,包括技術、學習、實用與各種有趣的内容。本期推薦的是一個基于 Javascript 的圖表繪制工具——Mermaid。
Mermaid 是一個基于 Javascript 的圖表繪制工具,通過解析類 Markdown 的文本文法來實作圖表的建立和動态修改。Mermaid 誕生的主要目的是讓文檔的更新能夠及時跟上開發進度。
繪圖和編寫文檔花費了開發者寶貴的開發時間,而且随着業務的變更,它很快就會過期。 但是如果缺少了圖表或文檔,對于生産力和團隊新人的業務學習都會産生巨大的阻礙。Mermaid 通過允許使用者建立便于修改的圖表來解決這一難題,它也可以作為生産腳本(或其他代碼)的一部分。
如何使用
1 直接在Mermaid線上編輯器中使用:mermaid.live
2 使用Mermaid插件
Mermaid目前在GitLab、Azure Devops、Tuleap、Joplin 、Notion五個平台添加預設插件,可直接使用。其他平台,例如:GitHub、GitBook、Wordpress、Hexo、Vs Code等需要手動添加插件。
檢視支援的平台:https://mermaid-js.github.io/mermaid/#/./integrations
在vs code中使用
3 調用 Mermaid Javascript API
通過 CDN 調用示例:
<html>
<body>
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>
mermaid.initialize({ startOnLoad: true });
</script>
Here is one mermaid diagram:
<div class="mermaid">
graph TD
A[Client] --> B[Load Balancer]
B --> C[Server1]
B --> D[Server2]
</div>
And here is another:
<div class="mermaid">
graph TD
A[Client] -->|tcp_123|
B(Load Balancer)
B -->|tcp_456| C[Server1]
B -->|tcp_456| D[Server2]
</div>
</body>
</html>
或者引用單獨檔案:
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<div class="mermaid">
graph LR
A --- B
B-->C[fa:fa-ban forbidden]
B-->D(fa:fa-spinner);
</div>
<div class="mermaid">
graph TD
A[Client] --> B[Load Balancer]
B --> C[Server1]
B --> D[Server2]
</div>
<script src="The\Path\In\Your\Package\mermaid.js"></script>
<script>
mermaid.initialize({ startOnLoad: true });
</script>
</body>
</html>
在Python 與 mermaid-js 內建的示例:
import base64
from IPython.display import Image, display
import matplotlib.pyplot as plt
def mm(graph):
graphbytes = graph.encode("ascii")
base64_bytes = base64.b64encode(graphbytes)
base64_string = base64_bytes.decode("ascii")
display(Image(url="https://mermaid.ink/img/" + base64_string))
mm("""
graph LR;
A--> B & C & D;
B--> A & E;
C--> A & E;
D--> A & E;
E--> B & C & D;
""")
4 添加 Mermaid 作為依賴項
npm:
npm install -g yarn
yarn:
yarn add mermaid
或者
yarn add --dev mermaid
圖表示例
流程圖
flowchart LR
A[Hard] -->|Text| B(Round)
B --> C{Decision}
C -->|One| D[Result 1]
C -->|Two| E[Result 2]
時序圖
sequenceDiagram
Alice->>John: Hello John, how are you?
loop Healthcheck
John->>John: Fight against hypochondria
end
Note right of John: Rational thoughts!
John-->>Alice: Great!
John->>Bob: How about you?
Bob-->>John: Jolly good!
甘特圖
gantt
section Section
Completed :done, des1, 2014-01-06,2014-01-08
Active :active, des2, 2014-01-07, 3d
Parallel 1 : des3, after des1, 1d
Parallel 2 : des4, after des1, 1d
Parallel 3 : des5, after des3, 1d
Parallel 4 : des6, after des4, 1d
類圖
classDiagram
Class01 <|-- AveryLongClass : Cool
<<Interface>> Class01
Class09 --> C2 : Where am i?
Class09 --* C3
Class09 --|> Class07
Class07 : equals()
Class07 : Object[] elementData
Class01 : size()
Class01 : int chimp
Class01 : int gorilla
class Class10 {
<<service>>
int id
size()
}
狀态圖
stateDiagram-v2
[*] --> Still
Still --> [*]
Still --> Moving
Moving --> Still
Moving --> Crash
Crash --> [*]
使用者體驗旅程圖
journey
title My working day
section Go to work
Make tea: 5: Me
Go upstairs: 3: Me
Do work: 1: Me, Cat
section Go home
Go downstairs: 5: Me
Sit down: 3: Me
—END—
開源協定:MIT
開源位址:https://github.com/mermaid-js/mermaid