diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml
new file mode 100644
index 000000000..b87bbe090
--- /dev/null
+++ b/.github/workflows/test.yaml
@@ -0,0 +1,20 @@
+name: Test
+
+on:
+ push:
+ branches: [master, develop]
+ pull_request:
+
+permissions:
+ contents: read
+
+jobs:
+ mermaid:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 20
+ - name: Build the fixture site and check mermaid injection
+ run: node test/run.js
diff --git a/_config.yml b/_config.yml
index 815b498bc..637fdc957 100644
--- a/_config.yml
+++ b/_config.yml
@@ -754,15 +754,25 @@ post:
# 流程图,基于 mermaid-js,具体请见:https://fluid.ist/docs/guide/#mermaid-流程图
# Flow chart, based on mermaid-js, see: https://fluid.ist/docs/en/guide/#mermaid
mermaid:
- # 开启后文章默认可用,自定义页面如需使用,需在 Front-matter 中指定 `mermaid: true`
- # If you want to use mermaid on the custom page, you need to set `mermaid: true` in Front-matter
+ # 开启后文章默认可用;页面会自动检测正文中的 mermaid 图表(代码块、`{% mermaid %}` 标签、
+ # `
`),一般无需再写 Front-matter。
+ # 如果图表是运行时注入的(例如加密插件、动态加载的评论内容),或页面未引入 markdown 正文样式,
+ # 仍可在 Front-matter 中指定 `mermaid: true` 强制加载脚本。
+ # Once enabled, posts work out of the box: the theme detects mermaid diagrams in the
+ # content (code fences, the `{% mermaid %}` tag and `
`), so the
+ # Front-matter flag is usually unnecessary. Set `mermaid: true` in Front-matter when
+ # diagrams are injected at runtime (encrypted posts, dynamically loaded content) or
+ # when the page does not include the markdown body styles.
enable: false
- # 开启后,只有在文章 Front-matter 里指定 `mermaid: true` 才会在文章页启动公式转换,以便在页面不包含公式时提高加载速度
- # If true, only set `mermaid: true` in Front-matter will enable mermaid, to load faster when the page does not contain mermaid
+ # 开启后,只有当页面包含 mermaid 图表或 Front-matter 指定 `mermaid: true` 时才加载脚本,以便在页面不包含图表时提高加载速度
+ # 指定 `mermaid: false` 可强制关闭
+ # If true, the script is only loaded when the page contains mermaid diagrams or `mermaid: true` is set in Front-matter, saving the load on pages without diagrams
+ # Set `mermaid: false` to opt out explicitly
specific: false
- # See: http://mermaid-js.github.io/mermaid/
+ # 配置项与 mermaid 版本保持一致,可参考:https://mermaid.js.org/config/schema-docs/config.html
+ # Options follow the mermaid version schema, see: https://mermaid.js.org/config/schema-docs/config.html
options: { theme: 'default' }
# 评论插件
@@ -1136,7 +1146,7 @@ static_prefix:
clipboard: https://lib.baomitu.com/clipboard.js/2.0.11/
- mermaid: https://lib.baomitu.com/mermaid/8.14.0/
+ mermaid: https://cdn.jsdelivr.net/npm/mermaid@11.16.0/dist/
valine: https://lib.baomitu.com/valine/1.5.1/
diff --git a/layout/_partials/head.ejs b/layout/_partials/head.ejs
index 648e06493..75292e00c 100644
--- a/layout/_partials/head.ejs
+++ b/layout/_partials/head.ejs
@@ -5,12 +5,20 @@ var keywords = page.keywords || config.keywords
if (keywords instanceof Array) {
keywords = keywords.join(',')
}
-var description = page.description || page.excerpt || (is_post() && page.content) || config.description
+var description = page.description || page.excerpt || page.content || config.description
if (description) {
- description = strip_html(description).substring(0, 200).trim().replace(/\n/g, ' ')
+ // 先剥标签再去实体,避免 "&" 这类转义原样进入 meta 和 JSON-LD
+ // Strip tags first, then decode entities so escaped text stays readable in metadata
+ // 代码块内容不参与摘要,避免 meta 里出现整段源码
+ // Code blocks never belong in a summary
+ description = unescape_html(strip_html(strip_mermaid(description).replace(/
in content' },
+ { page: 'plain', loads: false, why: 'no mermaid block' },
+ { page: 'not-mermaid', loads: false, why: '```not-mermaid fence is not a diagram' },
+ { page: 'escaped-sample', loads: false, why: 'mermaid example escaped inside an html fence' },
+ { page: 'raw-language', loads: false, why: 'raw language-mermaid class is not rendered by run()' },
+ { page: 'opt-out', loads: false, why: 'mermaid: false in Front-matter' }
+ ];
+
+ for (const item of cases) {
+ const html = site.html(item.page);
+ expectEqual(`${item.page}: mermaid.min.js`, html.includes('mermaid.min.js'), item.loads);
+ }
+
+ const withFlag = site.html('with-flag');
+ expectMatch('with-flag: startOnLoad forced off', withFlag, /Object\.assign\(\{\}, \{[^}]*\}, \{ startOnLoad: false \}\)/);
+ expectMatch('with-flag: theme options passed through', withFlag, /"theme":"forest"/);
+ expectNoMatch('with-flag: no legacy init()', withFlag, /mermaid\.init\s*\(/);
+ expectMatch('with-flag: querySelector explicit', withFlag, /querySelector:\s*['"]\.mermaid['"]/);
+ // suppressErrors 会吞掉 run() 的 reject,让失败无法观测
+ // suppressErrors swallows run()'s rejection, hiding failures
+ expectMatch('with-flag: run() only passes querySelector', withFlag, /mermaid\.run\(\{\s*querySelector:\s*['"]\.mermaid['"]\s*\}\)/);
+ expectMatch('with-flag: load failures are reported', withFlag, /Failed to load mermaid/);
+
+ // 代码块类名与高亮不能被误伤
+ // Code blocks must keep their classes and highlighting
+ expectMatch('with-flag: mermaid code block preserved', withFlag, /class="[^"]*\bmermaid\b[^"]*"/);
+ const notMermaid = site.html('not-mermaid');
+ expectMatch('not-mermaid: language class preserved', notMermaid, /class="[^"]*not-mermaid[^"]*"/);
+ expectMatch('not-mermaid: hljs class preserved', notMermaid, /class="hljs not-mermaid"/);
+ expectNoMatch('not-mermaid: no mermaid class rewrite', notMermaid, /class="mermaid"/);
+
+ // 正文里的 div.mermaid 不能被代码块适配器包成 code-wrapper
+ // A raw div.mermaid must not be wrapped like a code block
+ const divMermaid = site.html('div-mermaid');
+ expectMatch('div-mermaid: block preserved', divMermaid, /
/);
+ expectNoMatch('div-mermaid: not wrapped as code', divMermaid, /
/);
+
+ // 代码围栏里的示例必须保持为高亮文本,不能变成真实图表节点
+ // An escaped example must stay highlighted text, not become a diagram node
+ const escaped = site.html('escaped-sample');
+ expectNoMatch('escaped-sample: no real mermaid element', escaped, /
/);
+ expectMatch('escaped-sample: kept as highlighted html', escaped, /class="hljs-tag"/);
+
+ const found = mermaidWarnings(site.warnings);
+ expectEqual(
+ 'enabled: warnings only for the opted-out page',
+ found.map((line) => (line.includes('opt-out') ? 'opt-out' : line)).join(','),
+ 'opt-out'
+ );
+}
+
+async function testDisabledBuild(modules) {
+ const site = await buildSite('disabled', DISABLED_CONFIG, modules);
+
+ for (const page of ['with-flag', 'block-only', 'tag-only', 'div-mermaid', 'opt-out', 'plain']) {
+ expectEqual(`disabled/${page}: mermaid.min.js`, site.html(page).includes('mermaid.min.js'), false);
+ }
+
+ const found = mermaidWarnings(site.warnings);
+ const pagesWithWarning = found.map((line) => {
+ const match = line.match(/"([^"]+)"/);
+ return match ? match[1].split('/')[0] : line;
+ }).sort();
+ expectEqual(
+ 'disabled: warns exactly for pages with renderable diagrams',
+ pagesWithWarning.join(','),
+ ['block-only', 'div-mermaid', 'opt-out', 'tag-only', 'with-flag'].join(',')
+ );
+}
+
+async function testHighlightLineNumberBuild(modules) {
+ const site = await buildSite('hljs-lineno', HLJS_LINENO_CONFIG, modules);
+
+ const blockOnly = site.html('block-only');
+ expectEqual('hljs+line_number/block-only: mermaid.min.js', blockOnly.includes('mermaid.min.js'), true);
+ expectMatch('hljs+line_number/block-only: figure unwrapped', blockOnly, /graph TD\n {2}A --> B\n<\/code>/);
+ expectNoMatch('hljs+line_number/block-only: figure removed', blockOnly, //);
+ expectMatch('hljs+line_number/not-mermaid: hljs class kept', notMermaid, /class="hljs not-mermaid"/);
+ expectMatch('hljs+line_number/not-mermaid: line numbers kept', notMermaid, /class="gutter"/);
+}
+
+async function testPrismBuild(modules) {
+ const site = await buildSite('prism', PRISM_CONFIG, modules);
+
+ const blockOnly = site.html('block-only');
+ expectEqual('prism/block-only: mermaid.min.js', blockOnly.includes('mermaid.min.js'), true);
+ expectMatch('prism/block-only: language-mermaid rewritten', blockOnly, //);
+ expectNoMatch('prism/block-only: no leftover language class', blockOnly, /class="[^"]*language-mermaid[^"]*"/);
+ expectNoMatch('prism/block-only: line numbers removed', blockOnly, /line-numbers-rows/);
+ expectNoMatch('prism/block-only: pre attributes removed', blockOnly, /