diff --git a/index.html b/index.html index a087f15..1bbd977 100644 --- a/index.html +++ b/index.html @@ -785,6 +785,12 @@ function wire(){ } /* ---------- 启动 ---------- */ +async function readText(path){ + const res = await fetch(path, {cache:'no-cache'}); + if (!res.ok) throw new Error('http '+res.status+':'+path); + return res.text(); +} + (async function init(){ const status = document.getElementById('status'); const statusText = document.getElementById('status-t'), statusBar = document.getElementById('status-b'); @@ -792,19 +798,22 @@ function wire(){ statusText.textContent = `正在读取正文 ${done}/${total}`; statusBar.style.width = (done / total * 100).toFixed(1) + '%'; }; + // 离线单文件版把正文内联在 window.__CORPUS__ 里(tools/offline/build.mjs 生成),此时不发任何请求 + const EMBED = window.__CORPUS__; try { - if (location.protocol === 'file:') throw new Error('file'); - const r = await fetch('README.md', {cache:'no-cache'}); - if (!r.ok) throw new Error('http '+r.status); - const md = await r.text(); + if (!EMBED && location.protocol === 'file:') throw new Error('file'); + const md = EMBED ? EMBED.readme : await readText('README.md'); const files = [...new Set(Array.from(md.matchAll(/\]\((book\/[^)]+\.md)\)/g), m => m[1]))].sort(); if (!files.length) throw new Error('empty'); let done = 0; - progress(0, files.length); + if (!EMBED) progress(0, files.length); const parts = await Promise.all(files.map(async f => { - const res = await fetch(f, {cache:'no-cache'}); - if (!res.ok) throw new Error('http '+res.status+':'+f); - const text = await res.text(); + if (EMBED) { + const text = EMBED.parts[f]; + if (text === undefined) throw new Error('离线副本里缺 '+f); + return text; + } + const text = await readText(f); progress(++done, files.length); return text; })); diff --git a/tools/offline/build.mjs b/tools/offline/build.mjs new file mode 100644 index 0000000..59a84e6 --- /dev/null +++ b/tools/offline/build.mjs @@ -0,0 +1,69 @@ +// 把 index.html + README + book/*.md 打成一个自包含的 HTML:双击就能看,不用服务器、不用联网。 +// 用法:node tools/offline/build.mjs [输出路径] 默认输出 dist/HowToLiveBetter.html +// 正文内联进 window.__CORPUS__,index.html 的 init() 认这个变量就不再发请求; +// 站内相对链接改成线上地址,侧栏图片转成 data URI,其余一个字不动。 +import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'; +import { resolve, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { execSync } from 'node:child_process'; + +const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); +const OUT = resolve(ROOT, process.argv[2] ?? 'dist/HowToLiveBetter.html'); +const REPO = 'https://github.com/eternity4719/HowToLiveBetter'; +const SITE = 'https://eternity4719.github.io/HowToLiveBetter/'; +const DATE = new Date().toISOString().slice(0, 10); +const COMMIT = gitCommit(); + +const read = p => readFileSync(resolve(ROOT, p), 'utf8'); + +function gitCommit() { + try { + return execSync('git rev-parse HEAD', { cwd: ROOT, stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim(); + } catch { + return process.env.GITHUB_SHA ?? ''; + } +} + +// ---------- 正文 ---------- +const readme = read('README.md'); +const files = [...new Set([...readme.matchAll(/\]\((book\/[^)]+\.md)\)/g)].map(m => m[1]))].sort(); +if (!files.length) throw new Error('README 目录里没找到 book/ 文件,离线版会是空的'); +const corpus = { readme, parts: Object.fromEntries(files.map(f => [f, read(f)])) }; +// { + if (!html.includes(needle)) throw new Error(`index.html 里找不到${label},离线版脚本要跟着改:${needle}`); +}; + +// 相对链接在本地打开时是死的,改成线上地址 +must('href="README.md"', ' README.md 链接'); +must('href="book/"', ' book/ 链接'); +html = html + .replaceAll('href="README.md"', `href="${REPO}/blob/main/README.md"`) + .replaceAll('href="book/"', `href="${REPO}/tree/main/book"`) + .replaceAll('在线版 为准。
`); + +// 正文要在主脚本之前就位 +const mainScript = '\n${mainScript}`); + +mkdirSync(dirname(OUT), { recursive: true }); +writeFileSync(OUT, html); +const kb = n => (n / 1024 | 0) + ' KB'; +console.log(`已生成 ${OUT}:${files.length} 个正文文件,${kb(Buffer.byteLength(html))}(其中正文 ${kb(Buffer.byteLength(corpusJson))})`);