Second Brain: Crafted, Curated, Connected, Compounded on 10月02日 21:13
Mermaid:用文本描述动态生成图表
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

Mermaid 是一款基于 JavaScript 的图表和流程图工具,它允许用户通过编写类似 Markdown 的文本定义来动态创建和修改图表。这对于技术文档撰写者尤为方便,因为他们可以在文章中直接嵌入和更新图表,无需额外图形工具。Mermaid 支持多种图表类型,如流程图、甘特图、序列图等,并能生成 Git 和协作友好的图像。虽然 PlantUML 功能更强大,但 Mermaid 因其简洁性和广泛的 Markdown 支持,成为 GitHub、Obsidian 等平台的标准。

✨ **文本驱动的图表生成**:Mermaid 允许用户仅通过编写类似 Markdown 的文本来创建和编辑图表,极大地简化了图表制作流程,特别适合技术文档的编写和维护。用户可以轻松地在文章、笔记或书籍中更新图表,提高效率。

🚀 **广泛的应用场景与支持**:Mermaid 支持多种图表类型,包括流程图(可定义方向、箭头样式)、甘特图、数据库图表示等。它能够生成 Git 和协作友好的图像,并被 GitHub、Obsidian 等主流 Markdown 编辑器和平台原生支持,使其成为一种通用标准。

💡 **简洁易用与社区标准**:相较于功能更强大的 PlantUML,Mermaid 以其简洁性和开箱即用的特性脱颖而出。它易于学习和使用,能直接在大多数网站上工作,是 Markdown 生态系统中图表生成的首选工具。

🛠️ **解决图表布局与连接难题**:文章详细介绍了 Mermaid 在处理复杂图表布局(如子图的连接)时可能遇到的问题,并提供了解决方案,例如通过显式声明节点再定义连接来优化布局,以及如何正确地将子图与其外部图表进行连接,避免错误。

JavaScript-based diagramming and charting tool that renders Markdown-inspired text definitions to create and modify diagrams dynamically.

Generate images like below with just text:

Making it git and collaboration ready.

A simple example you can see inDAG.

# Why I use it

I am a technical author, and I love writing, but I don’t like creating diagrams. I’ve been using Markdown for many, many years, and for a couple of years, all my diagrams have been created based on Mermaid. I love it; not only can I update and maintain images better in my second brain and articles or even in my book by updating them quickly (change a typo or search-replace).

I can also generate an initial Mermaid based on the article I have written. And then add my ideas to it. I recommend it to anyone. I also use Mermaid Charts to beautify Mermaid if I need to publish somewhere else as an image. Just paste the mermaid and choose the style. PS: For more complex diagrams I have found PlantUML works great too. But I love Mermaid for its simplicity, and it works out of the box on all my websites, too. Reddit

# Configs (Vertical/Horizontal)

    TD or TB: This declares the flowchart is oriented from top to bottomLR: Horizontal

More on About Mermaid.

# Charts

# Examples

# Bar chart

gantt    title Gantt Chart with Mermaid    dateFormat X    axisFormat %s    section Apple    Apple   : 0, 100    section Bananas    75   : 0, 75    section Oranges    150   : 0, 150
gantt    title Gantt Chart with Mermaid    dateFormat X    axisFormat %s    Apple   : 0, 100    Bananas   : 0, 75    Oranges   : 0, 150

# Flow

Apparently, Mermaid can also showcase database objects as shown here:

graph TD    USER(User) -->|uses| VIEW(View)    VIEW -->|sees| CONTROLLER(Controller)    CONTROLLER -->|manipulates| MODEL(Model)    CONTROLLER -->|requests| SEMANTIC(Semantic Layer)    SEMANTIC -->|applies business logic| MODEL    MODEL -->|updates| VIEW    SEMANTIC -->|abstracts data access| VIEW    MODEL -->|stores data| Database    Database[(Database)]

# SubGraphs

Kubernetes

# Different Arrows (Dotted, Text)

flowchart LR    A1-->B1[Normal arrow]    A2---B2[Open link]    A3-- This is the text! ---B3[Text on open link]    A4---|This is the text|B4[Alternative text on open link]    A5-->|text|B5[Arrow with text]    A6-- text -->B6[Alternative arrow with text]    A7-.->B7[Dotted arrow]    A8-. dotted text .->B8[Dotted with text]    A9==>B9[Thick arrow]    A10== thick text ==>B10[Thick with text]    A11~~~B11[Invisible link]

More on Flowcharts Syntax | Mermaid.

# Troubleshoot

# Flow positioning (Ordering)

I couldn’t bring human on top, rearranging does not help

%%{init: {'theme':'base'}}%%graph TD    subgraph "With GenBI"        H2[👤 Human] -->|"Prompt"| AI[🧠 AI Model]        AI -->|"Generate"| Preview[🔍 Preview]        P2 -->|"Iterate"| H2        Preview -->|"Ship"| P2[🚀 Production]        Preview -->|"Refine"| H2    end

This one has it:

%%{init: {'theme':'base'}}%%graph TD    subgraph "With GenBI"        H2[HUMAN]        AI[AI Model]        PRE[Preview]        P2[Production]                H2 -->|Prompt| AI        AI -->|Generate| PRE        PRE -->|Ship| P2        PRE -->|Refine| H2        P2 -->|Iterate| H2    end

The difference is

    Node Declaration vs Flow Declaration
    The second diagram explicitly declares all nodes first, then defines connections separately:
123456
H2[HUMAN] AI[AI Model] PRE[Preview] P2[Production] H2 -->|Prompt| AI
    The first diagram combines node declaration with flow in one line:
1
H2[Human] -->|"Prompt"| AI[AI Model]`
    By declaring nodes separately first (second diagram), Mermaid treats them as independent elements that can be positioned more freelyWhen nodes are declared within the flow (first diagram), Mermaid tries to optimize the flow direction, which can lead to different automatic positioning

# Pointing Subgraph to outergraph

Ah, I understand the issue now. The error occurs because there’s a reference to Core as a direct node in MergeSCD2 --> Core, but Core is actually a subgraph. In Mermaid, you can’t directly connect to a subgraph - you need to connect to nodes within the subgraph.
TypeError: Cannot read properties of undefined (reading ‘x’) · Issue #5064 · mermaid-js/mermaid · GitHub

# Alternatives

More powerful seems to be PlantUML, but Mermaid is the standard for Markdown and supported for most web like GitHub, Obsidian, Quartz, etc.


Origin:
References:

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Mermaid 图表生成 Markdown 技术文档 JavaScript AI艺术
相关文章