mirror of
https://github.com/eternity4719/HowToLiveBetter.git
synced 2026-11-20 08:40:44 +08:00
检索网页、离线版、PDF 也拆超长收益、备注和来源,四个出口同一套规则
上一个提交只改了 EPUB,网页、离线单文件、PDF 上同一条收益仍是一整块。这次四个出口都拆, 正文 md 仍不动(index.html 按行解析、sync-stats 按「- 来源:」行数链接都依赖一个字段一行)。 - tools/lib/split-items.mjs:导出 splitGain / splitSrc。来源栏改用 index.html 原有 splitSrc 的规则(括号外的「;」一条一行),四个出口的来源拆法从此一致。 - index.html:在原有 splitSrc 旁边加同一份 splitGain,收益、备注拆开时渲染成一块一行的列表, 两份规则用 // <split-rules> 标出来。离线单文件用的是同一个 index.html,自动跟上。 - tools/pdf/build.mjs:read 之后同样套 splitLongItems。 - tools/check-split.mjs:从 index.html 抽出 <split-rules> 那段,和 split-items.mjs 在全书 2013 个收益/备注/来源字段上逐条比对;也查拆完能否逐字拼回原文、** 有没有被拆开。 CI 新增「拆分规则一致性检查」job,和交叉引用检查一样不挡发布。 - 换话题规则排除「该 / 本 / 此 / 这 / 同 / 上述」开头:「该办法第五条」是接着上文讲的,不另起。 - CLAUDE.md 电子版那段记一笔:规则两份要同步。 验证:check-split 0 处问题(拆开 554 个字段);check-plain、check-refs --check、 sync-stats --check 全过;epubcheck 5.4.0 0 errors / 0 warnings;pandoc 3.11 + typst 0.15.1 出 PDF 正常;本地起服务器和 file:// 打开离线单文件,第 31 节第 1 条的收益都拆成 11 行,控制台无报错。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X6s254TjQjvF9YgR8uK1LE
This commit is contained in:
@@ -66,6 +66,18 @@ jobs:
|
||||
node-version: 22
|
||||
- run: node tools/check-plain.mjs
|
||||
|
||||
# 拆分规则一致性检查:超长收益、备注和来源的拆分规则有两份(tools/lib/split-items.mjs 给 EPUB、PDF,
|
||||
# index.html 里 <split-rules> 那段给网页和离线版),只改了一边就会四个出口长得不一样。理由同上,不挡发布。
|
||||
split:
|
||||
name: 拆分规则一致性检查
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
- run: node tools/check-split.mjs
|
||||
|
||||
# 统计数字检查:README、index.html、og.html 里的条目数、A/B/C 数、链接数等是否和 book/ 对得上。
|
||||
# 本地 sync-stats.mjs 要记得跑才会更新,网页上直接合并的 PR 完全经过不了它。理由同上,不挡发布。
|
||||
# 只查不改:让 CI 往 main 推提交会和本地分叉,而且 Ubuntu 没有微软雅黑,og.png 截不对。
|
||||
|
||||
@@ -94,6 +94,7 @@
|
||||
README 开头「这本书想回答的问题」表**一节只占一行**(2026-09-19 用户定):同一个链接贴两遍,读者要在同一个目的地上停两次,两行之间又没有逻辑差别。一节覆盖面宽就在一行里用逗号串几个问题、一个问号收尾(第 19 节「加班费、年休假该怎么算,被裁该拿多少补偿,上班受了伤怎么认定和拿钱?」是范式),不要拆成两行。当天把第 6、8、24 节原有的两行都并掉了,现在是 32 行对 32 节。
|
||||
|
||||
电子版三样,全由 `.github/workflows/book.yml` 在 main 上正文改动后自动生成,挂到固定 Release `epub-latest`(下载链接不变,README 引用的就是它),PR 只生成校验不发布;产物都进 `dist/`(已 gitignore)。**产物本身不入库**,正文几乎天天改,提交二进制只会过时和撑大历史(2026-09-18 issue #12 定的方案)。README 的结构解析和文件清单收在 `tools/lib/book.mjs`,三套构建共用,新增节或长文不用改脚本。**但新增 docs 长文必须手工挂四处链接**(2026-09-21 连漏两次,两次都是用户发现的):① README 顶部导航表格里「长文」那一格(HTML 表格,单元格里必须留空行、用 markdown 写成「[x](docs/x.md)」,写成 `<a href>` 就不被下面那个正则认;2026-09-28 导航区改成表格后不再带「(长文)」后缀);② README 末尾「长文另见」那句;③ README 对应节的目录行(写成「长文见 [docs/x.md](docs/x.md)。」);④ index.html 里 `class="doc-links"` 那一行,URL 要百分号编码。核对办法:`grep -c "docs/<名>.md" README.md` 应为 3,`grep -c "<名>" index.html` 应为 1。第 ① 处是硬性的——`book.mjs` 第 54 行靠正则 `\]\((docs/[^)#/]+\.md)\)` 从 README 里扒长文清单,只在 book/ 条目里链接,EPUB、PDF、离线单文件三套构建都收不到它。sync-stats.ps1 不检查这三处。
|
||||
- **超长的收益、备注和来源在四个出口里都拆成子条目,正文 md 不动**:收益、备注超过 300 字的,在句号处、且下一句开始引用新依据(「X看《…》」「《某条例》」「刑法第…条」「另一项试验」「案例二」等)时断开;来源按括号外的「;」一条一行。规则有两份,`tools/lib/split-items.mjs` 给 EPUB、PDF,index.html 里 `// <split-rules>` 那段给检索网页和离线版,**改一边要同步改另一边**,`node tools/check-split.mjs` 拿全书逐条比对两边结果,也查拆完能不能逐字拼回原文、`**` 有没有被拆开(CI 里是「拆分规则一致性检查」job)。正文仍一个字段一行,index.html 按行解析、sync-stats 按「- 来源:」行数链接都不受影响。
|
||||
- EPUB:`tools/epub/build.mjs`(只依赖 marked,zip 自己打),CI 里跑 epubcheck。本地 `cd tools/epub && npm ci && npm run build`。
|
||||
- 离线单文件 HTML:`tools/offline/build.mjs`(零依赖)把正文**和 docs 长文**内联进 index.html 的 `window.__CORPUS__`(`parts` 放 book/、`docs` 放长文),双击就能看、离线可用;index.html 的 `init()` 认这个变量就不发请求,改 init 的取数逻辑要同时顾到这条路。**检索页的长文是就地弹窗打开的,不跳 GitHub**:点击处统一拦「指向 `docs/*.md` 的 GitHub 链接」(页头那行长文、条目备注里 `hrefOf()` 转出来的相对链接,形状都是这一个),弹窗自带一份可点的章节目录和 scrollspy,markdown 由 index.html 里那个只认标题/段落/列表/表格的小渲染器处理,不引库。长文里如果开始用引用块、代码块这类新语法,要同步加进那个渲染器。
|
||||
- PDF:`tools/pdf/build.mjs` + `tools/pdf/template.typ`,pandoc 转 typst、typst 排版(A4、每节另起一页、页眉带节名、目录带页码、书签到条目级)。本地要 pandoc ≥ 3.1 和 typst ≥ 0.13,用环境变量 `PANDOC`、`TYPST` 指到可执行文件;CI 里按 workflow 顶部 env 里钉的版本下载。版面只改 template.typ。
|
||||
|
||||
+57
-2
@@ -218,6 +218,11 @@ html.dark .switch .sun{display:none} html.dark .switch .moon{display:block}
|
||||
.rows .k{color:var(--t3);font-weight:500;font-size:13px;line-height:1.75;padding-top:1px}
|
||||
.rows .v{color:var(--t1);min-width:0;overflow-wrap:anywhere}
|
||||
.rows .v.note{color:var(--t2)}
|
||||
/* 超长的收益、备注按换话题拆成几块,一块一行,圆点挂在左边 */
|
||||
.rows .v ul.chunks{margin:0;padding:0;list-style:none}
|
||||
.rows .v ul.chunks li{position:relative;padding-left:14px;margin-bottom:6px}
|
||||
.rows .v ul.chunks li:last-child{margin-bottom:0}
|
||||
.rows .v ul.chunks li::before{content:"";position:absolute;left:2px;top:.75em;width:4px;height:4px;border-radius:50%;background:var(--t3)}
|
||||
.rows .v strong,.human strong,.intro strong,.src .body strong{font-weight:600;color:var(--t1)}
|
||||
.src{margin-top:14px;border-top:1px solid var(--divider);padding-top:10px}
|
||||
.src summary{list-style:none;cursor:pointer;font-size:13px;font-weight:500;color:var(--t2);display:flex;align-items:center;gap:6px;user-select:none}
|
||||
@@ -691,6 +696,47 @@ function renderText(el, text, terms, withTerms = true, urlsLast = false){
|
||||
for (const a of held) el.appendChild(a);
|
||||
if (withTerms) termify(el);
|
||||
}
|
||||
// <split-rules> 这一段和 tools/lib/split-items.mjs 是同一套规则,EPUB、PDF 用那份,网页和离线版用这份。
|
||||
// 改一边要同步改另一边,`node tools/check-split.mjs` 会拿全书逐条比对两边拆出来的结果。
|
||||
// 收益、备注超过 300 字的,在句号处、且下一句开始引用新的依据时拆成几块,一块一行
|
||||
const GAIN_MIN_LEN = 300, GAIN_MIN_CHUNK = 25;
|
||||
const GAIN_TOPIC_START = [
|
||||
/^[^,。「」《》:;()]{1,12}看(《|[^,。「」]{0,10}法[:「(第])/,
|
||||
/^《[^》]{2,40}》/,
|
||||
/^(?!该|本|此|这|同|上述)[一-龥]{1,12}法(第[一二三四五六七八九十百零]+条|「)/,
|
||||
/^(另一|另有一|还有一|再一|又一)(项|篇)/,
|
||||
/^(国内|国外|国内外)也有/,
|
||||
/^案例[一二三四五六七八九十]/,
|
||||
/^(同一天|同一试验|同一指南|同一研究)/,
|
||||
/^(美国|英国|欧洲|世界卫生组织|WHO|中国)[^,。]{0,20}(指南|共识|建议|学会)/,
|
||||
/^(先说|再说|最后说|先看|再看|最后看)/,
|
||||
/^一(项|篇)[^,。]{0,20}(试验|研究|综述|分析|调查)/,
|
||||
/^(芬兰|北欧|英国|美国|日本|韩国|瑞典|丹麦|挪威|德国|法国|澳大利亚|加拿大|荷兰|台湾|香港|上海|北京)[^,。]{0,25}(研究|数据库|试验|队列|调查)/,
|
||||
];
|
||||
function splitGain(text){
|
||||
if (!text || text.length < GAIN_MIN_LEN) return [text];
|
||||
const sentences = []; let buf = '', depth = 0;
|
||||
for (const c of text){
|
||||
if ('「『《('.includes(c)) depth++;
|
||||
else if ('」』》)'.includes(c)) depth = Math.max(0, depth - 1);
|
||||
buf += c;
|
||||
if (c === '。' && depth === 0){ sentences.push(buf); buf = ''; }
|
||||
}
|
||||
if (buf) sentences.push(buf);
|
||||
const chunks = [];
|
||||
for (const s of sentences){
|
||||
if (chunks.length && GAIN_TOPIC_START.some(re => re.test(s))) chunks.push(s);
|
||||
else if (chunks.length) chunks[chunks.length - 1] += s;
|
||||
else chunks.push(s);
|
||||
}
|
||||
const out = [];
|
||||
for (const c of chunks){
|
||||
if (out.length && c.length < GAIN_MIN_CHUNK) out[out.length - 1] += c;
|
||||
else out.push(c);
|
||||
}
|
||||
if (out.length > 1 && out[0].length < GAIN_MIN_CHUNK) out.splice(0, 2, out[0] + out[1]);
|
||||
return out;
|
||||
}
|
||||
// 来源栏是「;」串起来的若干条文献,拆成带序号的列表,一条一行
|
||||
// 括号和引号里的分号不算分隔符:法条原文里有「……代理或者追认;……」,条款注里有「第五项,修改刑法第三百九十条;自 2024 年 3 月 1 日起施行」
|
||||
const SRC_OPEN = '「『(', SRC_CLOSE = '」』)';
|
||||
@@ -707,6 +753,15 @@ function splitSrc(text){
|
||||
parts.push(buf);
|
||||
return parts.map(s => s.trim()).filter(Boolean);
|
||||
}
|
||||
// </split-rules>
|
||||
function renderGain(el, text, terms){
|
||||
const parts = splitGain(text);
|
||||
if (parts.length < 2){ renderText(el, text, terms); return; }
|
||||
el.textContent = '';
|
||||
const ul = document.createElement('ul'); ul.className = 'chunks';
|
||||
for (const p of parts){ const li = document.createElement('li'); renderText(li, p, terms); ul.appendChild(li); }
|
||||
el.appendChild(ul);
|
||||
}
|
||||
function renderSrc(el, text, terms){
|
||||
el.textContent = '';
|
||||
const parts = splitSrc(text);
|
||||
@@ -1084,8 +1139,8 @@ function renderCard(card, terms, key){
|
||||
renderText(f.cost, e.cost, terms);
|
||||
f.human.hidden = !e.human;
|
||||
if (e.human) renderText(f.human, e.human, terms);
|
||||
renderText(f.gain, e.gain, terms);
|
||||
renderText(f.note, e.note, terms);
|
||||
renderGain(f.gain, e.gain, terms);
|
||||
renderGain(f.note, e.note, terms);
|
||||
const nSrc = renderSrc(f.src, e.src, terms);
|
||||
f.srcN.textContent = nSrc > 1 ? ` · ${nSrc} 条` : '';
|
||||
const inBody = (e.title+e.human+e.cost+e.gain+e.note).toLowerCase();
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// 收益、备注、来源的拆分规则有两份:tools/lib/split-items.mjs(EPUB、PDF)和 index.html 里
|
||||
// <split-rules> 那一段(检索网页、离线单文件)。这个脚本拿全书每一条比对两边拆出来的结果,
|
||||
// 顺带查拆完能不能逐字拼回原文、加粗的 ** 有没有被拆到两块里。
|
||||
// 用法:node tools/check-split.mjs 有问题退出码 1
|
||||
import { readdirSync } from 'node:fs';
|
||||
import vm from 'node:vm';
|
||||
import { read, readBook } from './lib/book.mjs';
|
||||
import { splitGain, splitSrc } from './lib/split-items.mjs';
|
||||
|
||||
const html = read('index.html');
|
||||
const m = html.match(/\/\/ <split-rules>[\s\S]*?\/\/ <\/split-rules>/);
|
||||
if (!m) { console.error('index.html 里找不到 // <split-rules> … // </split-rules> 这一段'); process.exit(1); }
|
||||
const ctx = {};
|
||||
vm.runInNewContext(`${m[0]}\nthis.splitGain = splitGain; this.splitSrc = splitSrc;`, ctx);
|
||||
|
||||
const { docFiles } = readBook();
|
||||
const files = [...readdirSync(new URL('../book/', import.meta.url)).filter(f => f.endsWith('.md')).map(f => `book/${f}`), ...docFiles];
|
||||
const problems = [];
|
||||
let total = 0, split = 0;
|
||||
for (const f of files) {
|
||||
read(f).split('\n').forEach((line, i) => {
|
||||
const mm = line.match(/^\s*- (收益|备注|来源):(.*)$/);
|
||||
if (!mm) return;
|
||||
const [, label, body] = mm;
|
||||
const at = `${f}:${i + 1}(${label})`;
|
||||
const lib = label === '来源' ? splitSrc(body) : splitGain(body);
|
||||
const web = label === '来源' ? ctx.splitSrc(body) : ctx.splitGain(body);
|
||||
total++;
|
||||
if (lib.length > 1) split++;
|
||||
if (JSON.stringify(lib) !== JSON.stringify(web)) problems.push(`${at}:split-items.mjs 拆成 ${lib.length} 块,index.html 拆成 ${web.length} 块`);
|
||||
if (label !== '来源' && lib.join('') !== body) problems.push(`${at}:拆完拼不回原文`);
|
||||
if (lib.length > 1 && lib.some(c => (c.match(/\*\*/g) || []).length % 2)) problems.push(`${at}:有一块里的 ** 不成对,加粗被拆开了`);
|
||||
});
|
||||
}
|
||||
for (const p of problems) console.log(p);
|
||||
console.log(`${files.length} 个文件,${total} 个收益/备注/来源字段,拆开 ${split} 个,问题 ${problems.length} 处`);
|
||||
process.exit(problems.length ? 1 : 0);
|
||||
+55
-61
@@ -1,17 +1,23 @@
|
||||
// 把一行写到底的超长「收益 / 备注 / 来源」列表项,在换话题、换出处的地方拆成子条目。
|
||||
// 只改版式,不动正文的字:拆出来的子条目按顺序拼回去,和原来那一行逐字相同(来源栏里文献之间那个「;」换成了换行)。
|
||||
// 起因是电子书里一条「收益」常常一整段一千多字(比如第 31 节第 1 条把十二条路的门槛写在一行里),
|
||||
// 在阅读器里就是一整块,找不到哪句讲哪条路。目前只接在 EPUB 构建里;PDF 想用的话同样在 read 之后套一层。
|
||||
// 把一行写到底的超长「收益 / 备注」和串成一行的「来源」,在换话题、换出处的地方拆成子条目。
|
||||
// 起因是一条「收益」常常一整段一千多字(比如第 31 节第 1 条把十二条路的门槛写在一行里),
|
||||
// 读的时候是一整块,找不到哪句讲哪条路。
|
||||
//
|
||||
// 只改版式,不动正文的字:拆出来的块按顺序拼回去和原文逐字相同(来源栏丢掉的只是分隔用的「;」)。
|
||||
// 正文 md 仍然一个字段一行,index.html 的按行解析、sync-stats 的链接计数都不受影响。
|
||||
//
|
||||
// 四个出口用同一套规则:EPUB、PDF 在构建时调 splitLongItems;检索网页和离线单文件在 index.html 里
|
||||
// 有一份同样的 splitGain / splitSrc。**改这里的规则要同步改 index.html 里那份**,
|
||||
// `node tools/check-split.mjs` 会拿全书逐条比对两边拆出来的结果。
|
||||
|
||||
const MIN_LEN = 300; // 短于这个长度的项不拆
|
||||
const MIN_CHUNK = 25; // 拆出来太短的块并回上一块
|
||||
export const GAIN_MIN_LEN = 300; // 收益、备注短于这个字数的不拆
|
||||
export const GAIN_MIN_CHUNK = 25; // 拆出来太短的块并回上一块
|
||||
|
||||
// 句号之后,下一句以这些开头,就当作换了一条依据
|
||||
const TOPIC_START = [
|
||||
/^[^,。「」《》:;()]{1,12}看(《|[^,。「」]{0,10}法[:「(第])/, // 当兵看兵役法:/ 考公务员看《…》
|
||||
/^《[^》]{2,40}》/, // 《某条例》……
|
||||
/^[一-龥]{1,12}法(第[一二三四五六七八九十百零]+条|「)/, // 刑法第…条 / 义务教育法「
|
||||
/^(另一|另有一|还有一|再一|又一)(项|篇)/, // 另一项试验
|
||||
export const GAIN_TOPIC_START = [
|
||||
/^[^,。「」《》:;()]{1,12}看(《|[^,。「」]{0,10}法[:「(第])/, // 当兵看兵役法:/ 考公务员看《…》
|
||||
/^《[^》]{2,40}》/, // 《某条例》……
|
||||
/^(?!该|本|此|这|同|上述)[一-龥]{1,12}法(第[一二三四五六七八九十百零]+条|「)/, // 刑法第…条 / 义务教育法「;「该办法第五条」是接着上文,不算
|
||||
/^(另一|另有一|还有一|再一|又一)(项|篇)/, // 另一项试验
|
||||
/^(国内|国外|国内外)也有/,
|
||||
/^案例[一二三四五六七八九十]/,
|
||||
/^(同一天|同一试验|同一指南|同一研究)/,
|
||||
@@ -21,71 +27,59 @@ const TOPIC_START = [
|
||||
/^(芬兰|北欧|英国|美国|日本|韩国|瑞典|丹麦|挪威|德国|法国|澳大利亚|加拿大|荷兰|台湾|香港|上海|北京)[^,。]{0,25}(研究|数据库|试验|队列|调查)/,
|
||||
];
|
||||
|
||||
function splitTopLevel(text, sep, keepSep) {
|
||||
// 在不处于 「」《》()() <> 里面的位置按 sep 拆
|
||||
const out = [];
|
||||
let depth = 0, start = 0;
|
||||
const open = '「《((', close = '」》))';
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const c = text[i];
|
||||
if (c === '<' && /^<https?:/.test(text.slice(i, i + 9))) {
|
||||
const j = text.indexOf('>', i);
|
||||
if (j > 0) { i = j; continue; }
|
||||
}
|
||||
if (open.includes(c)) depth++;
|
||||
else if (close.includes(c)) depth = Math.max(0, depth - 1);
|
||||
else if (c === sep && depth === 0) {
|
||||
out.push(text.slice(start, keepSep ? i + 1 : i));
|
||||
start = i + 1;
|
||||
}
|
||||
// 收益、备注:按不在「」『』《》()里的句号切句,遇到换话题的句子另起一块
|
||||
export function splitGain(text) {
|
||||
if (!text || text.length < GAIN_MIN_LEN) return [text];
|
||||
const sentences = [];
|
||||
let buf = '', depth = 0;
|
||||
for (const c of text) {
|
||||
if ('「『《('.includes(c)) depth++;
|
||||
else if ('」』》)'.includes(c)) depth = Math.max(0, depth - 1);
|
||||
buf += c;
|
||||
if (c === '。' && depth === 0) { sentences.push(buf); buf = ''; }
|
||||
}
|
||||
out.push(text.slice(start));
|
||||
return out.filter(s => s.trim());
|
||||
}
|
||||
|
||||
function mergeShort(chunks) {
|
||||
const out = [];
|
||||
for (const c of chunks) {
|
||||
if (out.length && c.length < MIN_CHUNK) out[out.length - 1] += c;
|
||||
else out.push(c);
|
||||
}
|
||||
// 第一块也太短就并到第二块前面
|
||||
if (out.length > 1 && out[0].length < MIN_CHUNK) out.splice(0, 2, out[0] + out[1]);
|
||||
return out;
|
||||
}
|
||||
|
||||
function splitBenefit(body) {
|
||||
const sentences = splitTopLevel(body, '。', true);
|
||||
if (buf) sentences.push(buf);
|
||||
const chunks = [];
|
||||
for (const s of sentences) {
|
||||
const t = s.trimStart();
|
||||
if (chunks.length && TOPIC_START.some(re => re.test(t))) chunks.push(t);
|
||||
if (chunks.length && GAIN_TOPIC_START.some(re => re.test(s))) chunks.push(s);
|
||||
else if (chunks.length) chunks[chunks.length - 1] += s;
|
||||
else chunks.push(s);
|
||||
}
|
||||
return mergeShort(chunks);
|
||||
}
|
||||
|
||||
function splitSources(body) {
|
||||
// 一条文献以 <网址> 收尾,后面跟「;」再接下一条
|
||||
const parts = splitTopLevel(body, ';', false);
|
||||
const chunks = [];
|
||||
for (const p of parts) {
|
||||
if (chunks.length && /<https?:/.test(chunks[chunks.length - 1])) chunks.push(p.trim());
|
||||
else if (chunks.length) chunks[chunks.length - 1] += ';' + p;
|
||||
else chunks.push(p.trim());
|
||||
const out = [];
|
||||
for (const c of chunks) {
|
||||
if (out.length && c.length < GAIN_MIN_CHUNK) out[out.length - 1] += c;
|
||||
else out.push(c);
|
||||
}
|
||||
return chunks;
|
||||
if (out.length > 1 && out[0].length < GAIN_MIN_CHUNK) out.splice(0, 2, out[0] + out[1]);
|
||||
return out;
|
||||
}
|
||||
|
||||
// 来源:「;」串起来的若干条文献,一条一行。和 index.html 原有的 splitSrc 同一条规则:
|
||||
// 括号和引号里的分号不算分隔符(法条原文里有「……代理或者追认;……」)
|
||||
export function splitSrc(text) {
|
||||
const parts = []; let buf = '', depth = 0;
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const c = text[i];
|
||||
if ('「『('.includes(c)) depth++;
|
||||
if ('」』)'.includes(c)) depth = Math.max(0, depth - 1);
|
||||
const semi = c === ';' || (c === ';' && /\s/.test(text[i - 1] || '') && /\s/.test(text[i + 1] || ''));
|
||||
if (semi && depth === 0) { parts.push(buf); buf = ''; continue; }
|
||||
buf += c;
|
||||
}
|
||||
parts.push(buf);
|
||||
return parts.map(s => s.trim()).filter(Boolean);
|
||||
}
|
||||
|
||||
// 子条目开头像「1. 」会被 markdown 认成有序列表,转义掉
|
||||
const safe = s => s.replace(/^(\d+)([.)])(\s)/, '$1\\$2$3');
|
||||
|
||||
// 给 EPUB、PDF 用:把 md 里的长字段改写成「- 收益:」加一层子列表
|
||||
export function splitLongItems(md) {
|
||||
return md.split('\n').map(line => {
|
||||
const m = line.match(/^(\s*)- (收益|备注|来源):(.*)$/);
|
||||
if (!m || line.length < MIN_LEN) return line;
|
||||
if (!m) return line;
|
||||
const [, indent, label, body] = m;
|
||||
const chunks = label === '来源' ? splitSources(body) : splitBenefit(body);
|
||||
const chunks = label === '来源' ? splitSrc(body) : splitGain(body);
|
||||
if (chunks.length < 2) return line;
|
||||
return `${indent}- ${label}:\n` + chunks.map(c => `${indent} - ${safe(c.trim())}`).join('\n');
|
||||
}).join('\n');
|
||||
|
||||
+2
-1
@@ -7,6 +7,7 @@ import { writeFileSync, mkdirSync, statSync } from 'node:fs';
|
||||
import { resolve, dirname, posix, basename } from 'node:path';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { ROOT, REPO, SITE, TITLE, read, readBook, gitCommit, buildStamp, stripBackLink } from '../lib/book.mjs';
|
||||
import { splitLongItems } from '../lib/split-items.mjs';
|
||||
|
||||
const OUT = resolve(ROOT, process.argv[2] ?? 'dist/HowToLiveBetter.pdf');
|
||||
const WORK = resolve(ROOT, 'dist/pdf-build.md');
|
||||
@@ -25,7 +26,7 @@ docFiles.forEach((f, i) => anchorOf.set(f, `doc-${i + 1}`));
|
||||
const pages = [
|
||||
{ src: 'README.md', md: `# 前言\n\n${description}\n\n${frontMd}`, anchor: 'front' },
|
||||
{ src: 'README.md', md: contentsMd.replace(/^## 目录/, '# 各节简介'), anchor: 'contents' },
|
||||
...[...bookFiles, ...docFiles].map(src => ({ src, md: stripBackLink(read(src)), anchor: anchorOf.get(src) })),
|
||||
...[...bookFiles, ...docFiles].map(src => ({ src, md: splitLongItems(stripBackLink(read(src))), anchor: anchorOf.get(src) })),
|
||||
{ src: 'README.md', md: aboutMd(), anchor: 'about' },
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user