// 手機的 Markdown 清單(mobile/index.html 的 pageMd,聊天和頁面都用它):照 Markdown 的讀法—— // 空一行再接同一種項目還是同一個清單(以前每一點各自成一個清單,每一點都是「1.」,11/23),編號從 // 第一點寫的數字起,縮排在項目底下的是那一點的(續行或子清單,以前子項目被攤平成同一層、一起編號)。 import { readFileSync } from "node:fs"; const page = readFileSync(new URL("./index.html", import.meta.url), "utf8"); 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", "DIAGRAMS", "isDark", "ic", `${src}\nreturn pageMd;`)(esc, esc, new Map(), () => false, () => ""); let failed = 0; const check = (what, got, want) => { if (got === want) console.log(`OK ${what}`); else { failed++; console.log(`FAIL ${what}:\n 得到 ${got}\n 應該 ${want}`); } }; check("空行隔開、每點都寫 1.:同一個清單", pageMd("1. 甲\n\n1. 乙\n\n1. 丙"), "
中間一段
結論
"); check("一般段落不受影響", pageMd("第一行\n第二行\n\n第二段"), "第一行
第二行
第二段
"); // 頁面的圖照大腦的編號:清單底下縮排的也算;引用裡的大腦不算,手機自己畫。 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("npm install後面的段落
")}|${h.lastIndexOf("標題") > h.lastIndexOf("")}`, "true|true"); } if (failed) process.exitCode = 1;