Mermaid 主題與設定
自訂 Mermaid 圖表的配色、主題變數與全域設定選項
Overview##
Mermaid 內建五種主題,也支援透過 themeVariables 完全自訂配色。本篇說明如何選擇主題、自訂變數,以及常用的 Configuration 選項。
Built-in Themes##
Mermaid 提供五種預設主題:
| 主題 | 風格 | 適用場景 |
|---|---|---|
default | 淺色、藍色系 | 一般用途 |
dark | 深色背景 | 深色模式網站 |
forest | 綠色系 | 自然、環保主題 |
neutral | 灰色系、低對比 | 簡報、正式文件 |
base | 無樣式底板 | 完全自訂配色的起點 |
透過 initialize() 設定:
mermaid.initialize({
theme: 'neutral',
});
Tip
如果你想自訂配色,使用 base 主題搭配 themeVariables。其他主題的變數會互相覆蓋,自訂效果不穩定。
themeVariables##
base 主題提供一組可自訂的 CSS 變數,控制圖表的每個視覺元素。
通用變數###
| 變數 | 說明 | 範例值 |
|---|---|---|
primaryColor | 主要節點背景色 | #e8f5e9 |
primaryBorderColor | 主要節點邊框色 | #388e3c |
primaryTextColor | 主要節點文字色 | #1b5e20 |
secondaryColor | 次要節點背景色 | #f5f5f5 |
tertiaryColor | 第三層級背景色 | #fafafa |
lineColor | 連線 / 箭頭顏色 | #616161 |
textColor | 一般文字顏色 | #333333 |
fontFamily | 字型 | 'system-ui, sans-serif' |
fontSize | 字體大小 | '14px' |
Flowchart 專用變數###
| 變數 | 說明 |
|---|---|
nodeBorder | 節點邊框色 |
clusterBkg | Subgraph 背景色 |
clusterBorder | Subgraph 邊框色 |
edgeLabelBackground | Edge label 背景色 |
Sequence Diagram 專用變數###
| 變數 | 說明 |
|---|---|
actorBkg | 參與者背景色 |
actorBorder | 參與者邊框色 |
actorTextColor | 參與者文字色 |
activationBorderColor | Activation bar 邊框色 |
signalColor | 箭頭顏色 |
noteBkgColor | Note 背景色 |
noteBorderColor | Note 邊框色 |
設定範例###
以下是本站使用的綠色主題設定:
mermaid.initialize({
theme: 'base',
themeVariables: {
primaryColor: '#e8f5e9',
primaryBorderColor: '#388e3c',
primaryTextColor: '#1b5e20',
lineColor: '#616161',
secondaryColor: '#f5f5f5',
tertiaryColor: '#fafafa',
fontFamily: 'ui-sans-serif, system-ui, sans-serif',
fontSize: '14px',
},
});
效果:
Directive Override##
除了全域設定,你也可以在單張圖表中用 %% directive 覆蓋主題:
Warning
Directive 的語法是 %%{init: {...}}%%,必須放在圖表定義的第一行。注意大括號和引號的配對,少一個就會導致整張圖渲染失敗。
Configuration Options##
除了主題,initialize() 還有許多控制行為的選項。
通用設定###
mermaid.initialize({
startOnLoad: false, // false = manual render
securityLevel: 'strict', // strict | loose | antiscript | sandbox
logLevel: 'error', // debug | info | warn | error | fatal
fontFamily: 'arial',
});
| 選項 | 類型 | 預設值 | 說明 |
|---|---|---|---|
startOnLoad | boolean | true | 頁面載入時自動渲染 |
securityLevel | string | 'strict' | 安全層級,可用 strict、loose、antiscript、sandbox |
logLevel | string | 'error' | Console 日誌層級 |
fontFamily | string | '"trebuchet ms"' | 全域字型 |
maxTextSize | number | 50000 | 圖表文字最大長度 |
maxEdges | number | 500 | 最大 edge 數量 |
Flowchart 設定###
mermaid.initialize({
flowchart: {
htmlLabels: true, // label uses HTML rendering
curve: 'basis', // edge curve: basis | linear | cardinal
padding: 15, // node padding
nodeSpacing: 50, // horizontal spacing
rankSpacing: 50, // vertical spacing
useMaxWidth: true, // auto fit container width
},
});
| 選項 | 類型 | 預設值 | 說明 |
|---|---|---|---|
htmlLabels | boolean | true | 使用 HTML 渲染 label |
curve | string | 'basis' | 連線曲線類型 |
padding | number | 15 | 節點內距 |
nodeSpacing | number | 50 | 節點水平間距 |
rankSpacing | number | 50 | 節點垂直間距 |
Sequence Diagram 設定###
mermaid.initialize({
sequence: {
diagramMarginX: 50, // diagram horizontal margin
diagramMarginY: 10, // diagram vertical margin
actorMargin: 50, // space between actors
width: 150, // actor box width
height: 65, // actor box height
boxMargin: 10, // loop/alt box margin
showSequenceNumbers: true, // same as autonumber
useMaxWidth: true,
},
});
| 選項 | 類型 | 預設值 | 說明 |
|---|---|---|---|
actorMargin | number | 50 | 參與者之間的間距 |
width | number | 150 | 參與者方塊寬度 |
height | number | 65 | 參與者方塊高度 |
showSequenceNumbers | boolean | false | 自動編號 |
mirrorActors | boolean | true | 底部重複顯示參與者 |
Gantt 設定###
mermaid.initialize({
gantt: {
titleTopMargin: 25,
barHeight: 20,
barGap: 4,
topPadding: 50,
sidePadding: 75,
numberSectionStyles: 4,
axisFormat: '%Y-%m-%d',
},
});
Astro Integration##
本站的 Mermaid 設定在 DocLayout.astro 中:
import mermaid from 'mermaid';
async function initMermaid() {
mermaid.initialize({
startOnLoad: false,
theme: 'base',
themeVariables: {
primaryColor: '#e8f5e9',
primaryBorderColor: '#388e3c',
primaryTextColor: '#1b5e20',
lineColor: '#616161',
secondaryColor: '#f5f5f5',
tertiaryColor: '#fafafa',
fontFamily: 'ui-sans-serif, system-ui, sans-serif',
fontSize: '14px',
},
});
// Convert Shiki code blocks to mermaid divs
document.querySelectorAll('pre[data-language="mermaid"]').forEach((pre) => {
const code = pre.querySelector('code');
if (!code) return;
const raw = code.innerText;
const div = document.createElement('div');
div.className = 'mermaid';
div.textContent = raw;
pre.replaceWith(div);
});
await mermaid.run({ querySelector: '.mermaid' });
}
initMermaid();
document.addEventListener('astro:page-load', initMermaid);
Note
因為 Astro 用 Shiki 處理 code block,>> 會被轉成 >>。用 innerText 而非 textContent 讀取內容,確保 HTML entities 被正確解碼。
Summary##
- 使用
base主題搭配themeVariables做完全自訂,避免其他主題的變數互相覆蓋 primaryColor、primaryBorderColor、lineColor是影響最大的三個變數- 單張圖表可以用
%%{init: {...}}%%directive 覆蓋全域設定 securityLevel預設strict即可;loose、antiscript、sandbox則依信任程度與互動需求調整- Flowchart 的
curve選項影響連線的視覺風格(basis較圓滑,linear為直線) - Sequence diagram 用
actorMargin和width控制排版密度
留言 (0)
登入後即可留言