feat(vloop): 页面里的图改为 mermaid 渲染

9 个章节的 20 张 ASCII 图改为 mermaid 图。站点自带 mermaid 11.15.0,由 assets/doc.js 按需加载并渲染。
05 章的 import 边是邻接清单。63 条边画成一张图不可读,因此保留原文。
验证:node tools/build-nav.js 与 node tools/check-pages.js 均退出 0。headless Chrome 逐页渲染,svg 数与图块数一致。
This commit is contained in:
2026-11-03 21:49:47 +08:00
parent 142f4f8559
commit f171808072
20 changed files with 4089 additions and 433 deletions

View File

@@ -82,6 +82,7 @@
function enhanceCode() {
$$("pre").forEach(function (pre) {
if (pre.classList.contains("mermaid")) return; // 图交给 mermaid 渲染,不做代码块包装
if (pre.parentNode && pre.parentNode.classList.contains("codewrap")) return;
var lang = pre.dataset.lang || "text";
var name = pre.dataset.name || "";
@@ -126,6 +127,40 @@
});
}
/* ------------------------------ 图:mermaid ------------------------------
页面里的图写成 <pre class="mermaid" data-lang="mermaid">,正文即 mermaid 源码。
data-name 用作图题;渲染失败时 mermaid 会把错误画进容器,源码仍可读。 */
function enhanceDiagram() {
var nodes = $$("pre.mermaid");
if (!nodes.length) return;
nodes.forEach(function (n) {
var name = n.dataset.name || "";
if (name) {
var cap = document.createElement("div");
cap.className = "figcap";
cap.textContent = name;
n.parentNode.insertBefore(cap, n);
}
});
var s = document.createElement("script");
s.src = "assets/mermaid.min.js";
s.onload = function () {
if (!window.mermaid) return;
mermaid.initialize({
startOnLoad: false,
securityLevel: "loose",
theme: "neutral",
fontFamily: '-apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif',
flowchart: { useMaxWidth: true, htmlLabels: true, curve: "basis" },
sequence: { useMaxWidth: true, wrap: true, mirrorActors: false },
state: { useMaxWidth: true }
});
try { mermaid.run({ nodes: nodes }); } catch (e) { console.error("mermaid:", e); }
};
document.head.appendChild(s);
}
/* ------------------------- 卡片交互(目录页 / 篇章页) -------------------------
页面上存在 .chapcard 时才启用:已读勾选(localStorage)、进度条、
搜索过滤、展开/收起全部小节。章节页没有这些元素,整段逻辑自动空转。
@@ -239,6 +274,7 @@
function init() {
initTheme();
enhanceCode();
enhanceDiagram();
centerActive();
initCards();
initCardTools();