From 2aeea112bef03df1532de516be693001819cfdc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Mon, 23 Nov 2026 11:25:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E6=A9=9F=E7=9A=84=E9=A0=81=E9=9D=A2?= =?UTF-8?q?=E8=A3=A1=EF=BC=8C=E6=B8=85=E5=96=AE=E9=A0=85=E7=9B=AE=E5=BA=95?= =?UTF-8?q?=E4=B8=8B=E7=B8=AE=E6=8E=92=E7=9A=84=E5=9C=96=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E9=A1=AF=E7=A4=BA=E9=8C=AF=E7=9A=84=E9=82=A3=E5=BC=B5=EF=BC=9A?= =?UTF-8?q?=E4=B8=8A=E4=B8=80=E7=89=88=E6=8A=8A=E7=B8=AE=E6=8E=92=E5=9C=A8?= =?UTF-8?q?=E9=A0=85=E7=9B=AE=E5=BA=95=E4=B8=8B=E7=9A=84=E5=85=A7=E5=AE=B9?= =?UTF-8?q?=E4=BA=A4=E7=B5=A6=20pageMd=20=E9=81=9E=E8=BF=B4=E5=8E=BB?= =?UTF-8?q?=E7=95=AB=EF=BC=8C=E6=AF=8F=E4=B8=80=E5=B1=A4=E7=9A=84=E5=9C=96?= =?UTF-8?q?=E8=A1=A8=E7=B7=A8=E8=99=9F=E9=83=BD=E5=BE=9E=200=20=E9=87=8D?= =?UTF-8?q?=E7=AE=97=EF=BC=8C=E5=A4=A7=E8=85=A6=EF=BC=88pages::mermaid=5Fb?= =?UTF-8?q?locks=EF=BC=89=E5=8D=BB=E6=98=AF=E6=95=B4=E9=A0=81=E7=85=A7?= =?UTF-8?q?=E9=A0=86=E5=BA=8F=E6=95=B8=EF=BC=8C=E7=B8=AE=E6=8E=92=E7=9A=84?= =?UTF-8?q?=E4=B9=9F=E7=AE=97=E2=80=94=E2=80=94=E6=B8=85=E5=96=AE=E8=A3=A1?= =?UTF-8?q?=E7=9A=84=E5=9C=96=E9=A1=AF=E7=A4=BA=E6=88=90=E9=A0=81=E9=9D=A2?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=BC=B5=EF=BC=8C=E5=BE=8C=E9=9D=A2=E6=AF=8F?= =?UTF-8?q?=E5=BC=B5=E9=83=BD=E9=8C=AF=E4=B8=80=E4=BD=8D=E3=80=82=E7=8F=BE?= =?UTF-8?q?=E5=9C=A8=E6=95=B4=E9=A0=81=E5=85=B1=E7=94=A8=E4=B8=80=E5=80=8B?= =?UTF-8?q?=E8=A8=88=E6=95=B8=EF=BC=8C=E8=B7=9F=E5=A4=A7=E8=85=A6=E4=B8=80?= =?UTF-8?q?=E8=87=B4=EF=BC=88=E5=A4=9A=E5=B1=A4=E6=B8=85=E5=96=AE=E4=B9=9F?= =?UTF-8?q?=E5=B0=8D=EF=BC=89=EF=BC=9B=E5=BC=95=E7=94=A8=E3=80=81=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E6=A1=86=E8=A3=A1=E7=9A=84=E5=9C=96=E5=A4=A7=E8=85=A6?= =?UTF-8?q?=E4=B8=8D=E6=95=B8=EF=BC=8C=E6=94=B9=E6=88=90=E5=83=8F=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E4=B8=80=E6=A8=A3=E7=94=B1=E6=89=8B=E6=A9=9F=E7=85=A7?= =?UTF-8?q?=E5=AD=97=E7=95=AB=EF=BC=88=E4=BB=A5=E5=89=8D=E6=8B=BF=E5=88=B0?= =?UTF-8?q?=E9=8C=AF=E7=9A=84=E7=AC=AC=200=20=E5=BC=B5=EF=BC=89=E3=80=82?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E7=94=A8=E7=9A=84=E6=98=AF=E7=85=A7=E5=AD=97?= =?UTF-8?q?=E7=95=AB=EF=BC=8C=E4=B8=8D=E5=8F=97=E5=BD=B1=E9=9F=BF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 已驗證: cargo test(906);node mobile/markdown.test.mjs 17 項(清單底下、多層清單、引用裡的圖的編號;項目底下沒收尾的程式碼不吞後面的段落),7eb2d79 的頁面在編號那幾項會失敗;獨立審查:多層清單裡的圖編號 #0–#5 跟大腦完全一致、清單裡的引用和提示框對得上。審查也找到我順手加的「項目底下的程式碼區塊接到收尾那行」會把後面整段吞成程式碼,已撤回,程式碼區塊照 7eb2d79。 未驗證: 實機打開一份清單裡有圖的頁面(只有自動測試);已知沒修:```details 裡面放 ```mermaid 時編號還是會錯(手機的 details 在第一個 ``` 就結束,7eb2d79 以前就這樣)。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm --- mobile/index.html | 19 ++++++++++++------- mobile/markdown.test.mjs | 14 +++++++++++++- src/remote.rs | 2 +- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/mobile/index.html b/mobile/index.html index c9d208e..8da88dc 100644 --- a/mobile/index.html +++ b/mobile/index.html @@ -4698,9 +4698,14 @@ function chartMd(lines) { // ```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). 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 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()); while (i < L.length) { const l = L[i], t = l.trim(); @@ -4710,7 +4715,7 @@ function pageMd(text, id, rev) { const lang = fence[1].toLowerCase(); const body = []; i++; while (i < L.length && !L[i].trim().startsWith("```")) body.push(L[i++]); i++; - if (lang === "mermaid" && id) { out.push(`
圖表載入中…
`); continue; } + if (lang === "mermaid" && id) { out.push(`
圖表載入中…
`); continue; } // (A diagram in a chat: drawn by the brain from its words, as the computer draws it.) if (lang === "mermaid") { // (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; } // ```details : 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 === "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>`); @@ -4751,8 +4756,8 @@ function pageMd(text, id, rev) { if (c) { const k = {info: "note", caution: "warning"}[c[1].toLowerCase()] || c[1].toLowerCase(); 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>`); - } else out.push(`<blockquote>${pageMd(q.join("\n"), id, rev)}</blockquote>`); + 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"), 0, 0)}</blockquote>`); continue; } 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 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 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>`); } out.push(ordered ? `<ol${start !== 1 ? ` start="${start}"` : ""}>${items.join("")}</ol>` : `<ul>${items.join("")}</ul>`); continue; diff --git a/mobile/markdown.test.mjs b/mobile/markdown.test.mjs index 9be813b..101b655 100644 --- a/mobile/markdown.test.mjs +++ b/mobile/markdown.test.mjs @@ -8,7 +8,7 @@ const at = page.indexOf("function pageMd("); if (at < 0) throw new Error("index.html 裡沒有 pageMd"); const src = page.slice(at, page.indexOf("\n}\n", at) + 2); const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[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; 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("第一行\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; diff --git a/src/remote.rs b/src/remote.rs index 6f2cf42..bb45c97 100644 --- a/src/remote.rs +++ b/src/remote.rs @@ -3753,7 +3753,7 @@ mod tests { }; let text = String::from_utf8_lossy(&out.stdout); 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::*;