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::*;