mirror of
https://github.com/eternity4719/HowToLiveBetter.git
synced 2026-11-20 08:40:44 +08:00
ce378ca0f7
一天里正文常改好几轮,Release 说明和三样产物都只写日期,分不出手里是哪一版。 四处人能看见的时间统一成「2026-09-19 13:05(北京时间)」:Release 说明、 PDF 扉页与版本说明、EPUB 版本说明、离线 HTML 页脚。commit 短号原样保留。 CI 跑在 UTC 上,工作流用 date -u -d '+8 hours' 换算,不依赖 runner 的时区库; node 那三套共用 tools/lib/book.mjs 新增的 buildStamp(),按 Asia/Shanghai 格式化。 EPUB 的 dc:date 仍是纯日期(规范要求),完整时间本来就在 dcterms:modified 里。 顺带:tools/offline/build.mjs 自己又实现了一遍 ROOT/REPO/SITE/read/gitCommit, 改成从公共模块 import,删掉重复实现。
55 lines
2.7 KiB
JavaScript
55 lines
2.7 KiB
JavaScript
// README 的结构解析和文件清单:EPUB(tools/epub)和 PDF(tools/pdf)两套构建共用。
|
||
// 只认 README 里的结构,不维护文件名单——新增一节或一篇长文,两套构建都自动跟上。
|
||
import { readFileSync } from 'node:fs';
|
||
import { resolve, dirname } from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
import { execSync } from 'node:child_process';
|
||
|
||
export const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../..');
|
||
export const REPO = 'https://github.com/eternity4719/HowToLiveBetter';
|
||
export const SITE = 'https://eternity4719.github.io/HowToLiveBetter/';
|
||
export const TITLE = '高性价比人生指南';
|
||
export const RELEASE = `${REPO}/releases/download/epub-latest`;
|
||
|
||
export const read = p => readFileSync(resolve(ROOT, p), 'utf8');
|
||
export const unique = arr => [...new Set(arr)];
|
||
|
||
export function gitCommit() {
|
||
try {
|
||
return execSync('git rev-parse HEAD', { cwd: ROOT, stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim();
|
||
} catch {
|
||
return process.env.GITHUB_SHA ?? '';
|
||
}
|
||
}
|
||
|
||
// 正文一天可能改好几轮,只给日期分不出是哪一版,所以精确到分钟。
|
||
// CI 跑在 UTC 上,统一按北京时间显示,免得下载的人按自己那边的日期对不上。
|
||
export function buildStamp() {
|
||
return new Intl.DateTimeFormat('sv-SE', { timeZone: 'Asia/Shanghai', dateStyle: 'short', timeStyle: 'short' }).format(new Date());
|
||
}
|
||
|
||
export function stripBackLink(md) {
|
||
return md.replace(/^\[← 回总目录\]\([^)]*\)\s*\n/, '');
|
||
}
|
||
|
||
// README 里从某个标题到下一个标题之间的一段
|
||
export function readBook() {
|
||
const readme = read('README.md');
|
||
const lines = readme.split('\n');
|
||
const between = (from, to) => {
|
||
const a = lines.findIndex(l => l.startsWith(from));
|
||
const b = lines.findIndex((l, i) => i > a && l.startsWith(to));
|
||
if (a < 0 || b < 0) throw new Error(`README 里找不到 ${from} 到 ${to} 这一段`);
|
||
return lines.slice(a, b).join('\n');
|
||
};
|
||
const description = between('# 高性价比人生指南', '[![')
|
||
.split('\n').slice(1).map(l => l.replace(/<[^>]+>/g, '').trim()).filter(Boolean).join('');
|
||
const frontMd = between('## 这本书想回答的问题', '## 目录');
|
||
const contentsMd = between('## 目录', '## 正文')
|
||
.split('\n\n').filter(p => !p.includes('index.html')).join('\n\n');
|
||
const bookFiles = unique([...contentsMd.matchAll(/\]\((book\/[^)#]+\.md)\)/g)].map(m => m[1]));
|
||
const docFiles = unique([...readme.matchAll(/\]\((docs\/[^)#/]+\.md)\)/g)].map(m => m[1]));
|
||
if (bookFiles.length === 0) throw new Error('README 目录里没找到 book/ 文件');
|
||
return { readme, description, frontMd, contentsMd, bookFiles, docFiles };
|
||
}
|