手機的頁面裡,清單項目底下縮排的圖不再顯示錯的那張:上一版把縮排在項目底下的內容交給 pageMd 遞迴去畫,每一層的圖表編號都從 0 重算,大腦(pages::mermaid_blocks)卻是整頁照順序數,縮排的也算——清單裡的圖顯示成頁面第一張,後面每張都錯一位。現在整頁共用一個計數,跟大腦一致(多層清單也對);引用、提示框裡的圖大腦不數,改成像聊天一樣由手機照字畫(以前拿到錯的第 0 張)。聊天用的是照字畫,不受影響。

已驗證: cargo test(906);node mobile/markdown.test.mjs 17 項(清單底下、多層清單、引用裡的圖的編號;項目底下沒收尾的程式碼不吞後面的段落),7eb2d79 的頁面在編號那幾項會失敗;獨立審查:多層清單裡的圖編號 #0–#5 跟大腦完全一致、清單裡的引用和提示框對得上。審查也找到我順手加的「項目底下的程式碼區塊接到收尾那行」會把後面整段吞成程式碼,已撤回,程式碼區塊照 7eb2d79。
未驗證: 實機打開一份清單裡有圖的頁面(只有自動測試);已知沒修:```details 裡面放 ```mermaid 時編號還是會錯(手機的 details 在第一個 ``` 就結束,7eb2d79 以前就這樣)。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
This commit is contained in:
王性驊 2026-11-23 11:25:27 +08:00
parent 7eb2d79aa5
commit 2aeea112be
3 changed files with 26 additions and 9 deletions

View File

@ -4698,9 +4698,14 @@ function chartMd(lines) {
// ```stats: ↑/+ green, ↓/− red. The same rule as src/pages.rs stat_trend: + − - count only with // ```stats: ↑/+ green, ↓/− red. The same rule as src/pages.rs stat_trend: + − - count only with
// something right after them (「-12%」 is down; a lone 「-」 is "no data", 「- 待補」 is neutral). // something right after them (「-12%」 is down; a lone 「-」 is "no data", 「- 待補」 is neutral).
const trend = (t) => /^\s*(↑|\+(?=\S))/.test(t) ? " up" : /^\s*(↓|[−-](?=\S))/.test(t) ? " dn" : ""; const trend = (t) => /^\s*(↑|\+(?=\S))/.test(t) ? " up" : /^\s*(↓|[−-](?=\S))/.test(t) ? " dn" : "";
function pageMd(text, id, rev) { // (ctr: the page's diagrams numbered as the brain numbers them — every ```mermaid in order, one
// indented under a list item too; one in a quote isn't, and is drawn here from its words. Each
// part used to count from 0 again: a diagram under a list item showed the page's first one, and
// every one after it the one before. Not yet: a ```mermaid inside ```details — this page ends the
// details at its first ``` line, the brain counts it.)
function pageMd(text, id, rev, ctr) {
const L = String(text || "").replace(/\r/g, "").split("\n"); const L = String(text || "").replace(/\r/g, "").split("\n");
const out = []; let i = 0, chart = 0; const out = [], c = ctr || {n: 0}; let i = 0;
const cells = (l) => l.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((c) => c.trim()); const cells = (l) => l.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((c) => c.trim());
while (i < L.length) { while (i < L.length) {
const l = L[i], t = l.trim(); const l = L[i], t = l.trim();
@ -4710,7 +4715,7 @@ function pageMd(text, id, rev) {
const lang = fence[1].toLowerCase(); const body = []; i++; const lang = fence[1].toLowerCase(); const body = []; i++;
while (i < L.length && !L[i].trim().startsWith("```")) body.push(L[i++]); while (i < L.length && !L[i].trim().startsWith("```")) body.push(L[i++]);
i++; i++;
if (lang === "mermaid" && id) { out.push(`<div class="chart" data-src="api/pages/${id}/chart/${chart++}${rev ? "?rev=" + rev : ""}"><div class="wait">圖表載入中…</div></div>`); continue; } if (lang === "mermaid" && id) { out.push(`<div class="chart" data-src="api/pages/${id}/chart/${c.n++}${rev ? "?rev=" + rev : ""}"><div class="wait">圖表載入中…</div></div>`); continue; }
// (A diagram in a chat: drawn by the brain from its words, as the computer draws it.) // (A diagram in a chat: drawn by the brain from its words, as the computer draws it.)
if (lang === "mermaid") { if (lang === "mermaid") {
// (Drawn once: a chat is redrawn on every change, and the picture must not blink or be asked for again.) // (Drawn once: a chat is redrawn on every change, and the picture must not blink or be asked for again.)
@ -4719,7 +4724,7 @@ function pageMd(text, id, rev) {
continue; continue;
} }
// ```details <title>: folded under one quiet line (細節 without a title); the inside is the page's Markdown. // ```details <title>: folded under one quiet line (細節 without a title); the inside is the page's Markdown.
if (lang === "details") { const title = t.replace(/^```\s*details/i, "").trim() || "細節"; out.push(`<details class="dt"><summary>${esc(title)}</summary><div class="dt-in">${pageMd(body.join("\n"), id, rev)}</div></details>`); continue; } if (lang === "details") { const title = t.replace(/^```\s*details/i, "").trim() || "細節"; out.push(`<details class="dt"><summary>${esc(title)}</summary><div class="dt-in">${pageMd(body.join("\n"), id, rev, c)}</div></details>`); continue; }
if (lang === "chart") { const html = chartMd(body); if (html) { out.push(html); continue; } } if (lang === "chart") { const html = chartMd(body); if (html) { out.push(html); continue; } }
if (lang === "stats") { if (lang === "stats") {
const tiles = body.map(cells).filter((c) => c.length >= 2 && c[0]).map((c) => `<div class="stat"><div class="k">${inl(c[0])}</div><div class="v${trend(c[1])}${c[1].length > 10 ? " long" : ""}">${inl(c[1])}</div>${c[2] ? `<div class="s${trend(c[2])}">${inl(c[2])}</div>` : ""}</div>`); const tiles = body.map(cells).filter((c) => c.length >= 2 && c[0]).map((c) => `<div class="stat"><div class="k">${inl(c[0])}</div><div class="v${trend(c[1])}${c[1].length > 10 ? " long" : ""}">${inl(c[1])}</div>${c[2] ? `<div class="s${trend(c[2])}">${inl(c[2])}</div>` : ""}</div>`);
@ -4751,8 +4756,8 @@ function pageMd(text, id, rev) {
if (c) { if (c) {
const k = {info: "note", caution: "warning"}[c[1].toLowerCase()] || c[1].toLowerCase(); const k = {info: "note", caution: "warning"}[c[1].toLowerCase()] || c[1].toLowerCase();
q[0] = c[2]; q[0] = c[2];
out.push(`<div class="call k-${k}"><div class="ct">${ic(CALL_IC[k], 15)}${CALL[k]}</div>${pageMd(q.join("\n"), id, rev)}</div>`); out.push(`<div class="call k-${k}"><div class="ct">${ic(CALL_IC[k], 15)}${CALL[k]}</div>${pageMd(q.join("\n"), 0, 0)}</div>`);
} else out.push(`<blockquote>${pageMd(q.join("\n"), id, rev)}</blockquote>`); } else out.push(`<blockquote>${pageMd(q.join("\n"), 0, 0)}</blockquote>`);
continue; continue;
} }
if (/^([-*+•]|\d+[.)])\s+/.test(t)) { if (/^([-*+•]|\d+[.)])\s+/.test(t)) {
@ -4777,7 +4782,7 @@ function pageMd(text, id, rev) {
const words = task ? `${task[1] === " " ? "☐" : "☑"} ${inl(task[2])}` : inl(head); const words = task ? `${task[1] === " " ? "☐" : "☑"} ${inl(task[2])}` : inl(head);
const lines = under.filter((x) => x.trim()), cut = Math.min(...lines.map(indent)); const lines = under.filter((x) => x.trim()), cut = Math.min(...lines.map(indent));
const plain = lines.length === under.length && lines.every((x) => !/^(#{1,6}\s|```|>|\||([-*+•]|\d+[.)])\s)/.test(x.trim())); const plain = lines.length === under.length && lines.every((x) => !/^(#{1,6}\s|```|>|\||([-*+•]|\d+[.)])\s)/.test(x.trim()));
const inner = !lines.length ? "" : plain ? lines.map((x) => "<br>" + inl(x.trim())).join("") : pageMd(under.map((x) => x.slice(Math.min(cut, indent(x)))).join("\n"), id, rev); const inner = !lines.length ? "" : plain ? lines.map((x) => "<br>" + inl(x.trim())).join("") : pageMd(under.map((x) => x.slice(Math.min(cut, indent(x)))).join("\n"), id, rev, c);
items.push(`<li${task ? ' class="task"' : ""}>${words}${inner}</li>`); items.push(`<li${task ? ' class="task"' : ""}>${words}${inner}</li>`);
} }
out.push(ordered ? `<ol${start !== 1 ? ` start="${start}"` : ""}>${items.join("")}</ol>` : `<ul>${items.join("")}</ul>`); continue; out.push(ordered ? `<ol${start !== 1 ? ` start="${start}"` : ""}>${items.join("")}</ol>` : `<ul>${items.join("")}</ul>`); continue;

View File

@ -8,7 +8,7 @@ const at = page.indexOf("function pageMd(");
if (at < 0) throw new Error("index.html 裡沒有 pageMd"); if (at < 0) throw new Error("index.html 裡沒有 pageMd");
const src = page.slice(at, page.indexOf("\n}\n", at) + 2); const src = page.slice(at, page.indexOf("\n}\n", at) + 2);
const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c]); const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c]);
const pageMd = new Function("inl", "esc", `${src}\nreturn pageMd;`)(esc, esc); const pageMd = new Function("inl", "esc", "DIAGRAMS", "isDark", "ic", `${src}\nreturn pageMd;`)(esc, esc, new Map(), () => false, () => "");
let failed = 0; let failed = 0;
const check = (what, got, want) => { const check = (what, got, want) => {
@ -29,4 +29,16 @@ check("待辦清單", pageMd("- [ ] 甲\n- [x] 乙"), '<ul><li class="task">☐
check("清單後面空一行接段落:段落不算進去", pageMd("1. 甲\n2. 乙\n\n結論"), "<ol><li>甲</li><li>乙</li></ol><p>結論</p>"); check("清單後面空一行接段落:段落不算進去", pageMd("1. 甲\n2. 乙\n\n結論"), "<ol><li>甲</li><li>乙</li></ol><p>結論</p>");
check("一般段落不受影響", pageMd("第一行\n第二行\n\n第二段"), "<p>第一行<br>第二行</p><p>第二段</p>"); check("一般段落不受影響", pageMd("第一行\n第二行\n\n第二段"), "<p>第一行<br>第二行</p><p>第二段</p>");
// 頁面的圖照大腦的編號:清單底下縮排的也算;引用裡的大腦不算,手機自己畫。
const charts = (h) => [...h.matchAll(/chart\/(\d+)|data-mermaid="([^"]*)"/g)].map((x) => x[1] !== undefined ? "#" + x[1] : "畫:" + x[2]).join(" ");
check("清單底下的圖:編號接著算", charts(pageMd("```mermaid\nA\n```\n\n1. 步驟:\n ```mermaid\n B\n ```\n\n```mermaid\nC\n```", 5, 0)), "#0 #1 #2");
check("details 後面的圖:照常從 0", charts(pageMd("```details 更多\n說明\n```\n```mermaid\nC\n```", 5, 0)), "#0");
check("多層清單裡的圖:編號接著算", charts(pageMd("1. 甲\n ```mermaid\n A\n ```\n - 乙\n ```mermaid\n B\n ```\n2. 丙\n\n```mermaid\nC\n```", 5, 0)), "#0 #1 #2");
check("引用裡的圖:不佔編號、手機自己畫", charts(pageMd("> ```mermaid\n> Q\n> ```\n\n```mermaid\nC\n```", 5, 0)), "畫:Q #0");
// 項目底下沒收尾的程式碼:只到這一點為止,後面的段落和標題不被吞成程式碼。
{
const h = pageMd("- 執行:\n ```bash\n npm install\n\n後面的段落\n\n## 標題");
check("項目底下沒收尾的程式碼:不吞後面的", `${h.includes("<code>npm install</code></pre></li></ul><p>後面的段落</p>")}|${h.lastIndexOf("標題") > h.lastIndexOf("</ul>")}`, "true|true");
}
if (failed) process.exitCode = 1; if (failed) process.exitCode = 1;

View File

@ -3753,7 +3753,7 @@ mod tests {
}; };
let text = String::from_utf8_lossy(&out.stdout); let text = String::from_utf8_lossy(&out.stdout);
assert!(out.status.success() && !text.contains("FAIL"), "{text}{}", String::from_utf8_lossy(&out.stderr)); assert!(out.status.success() && !text.contains("FAIL"), "{text}{}", String::from_utf8_lossy(&out.stderr));
assert!(text.matches("OK").count() >= 12, "{text}"); assert!(text.matches("OK").count() >= 17, "{text}");
} }
use super::*; use super::*;