// scripts/build.mjs —— 构建入口:合并数据 → 渲染 HTML + 纯文本 → 输出 dist/ import fs from 'node:fs'; import path from 'node:path'; import Handlebars from 'handlebars'; import { ROOT, DIST, loadAll, buildView, buildNav, banner } from './lib.mjs'; // ---------- Handlebars 辅助 ---------- const esc = (s) => Handlebars.escapeExpression(s ?? ''); Handlebars.registerHelper('br', (arr) => new Handlebars.SafeString((arr || []).map((x) => esc(x)).join('
'))); Handlebars.registerHelper('nl', (arr) => (arr || []).join('\n')); Handlebars.registerHelper('joinComma', (arr) => (arr || []).join(', ')); Handlebars.registerHelper('add1', (i) => Number(i) + 1); // ---------- 模板(txt 模板关闭 HTML 转义) ---------- const tplCache = new Map(); function tpl(rel) { if (!tplCache.has(rel)) { const file = path.join(ROOT, 'templates', rel); const isTxt = rel.includes('/txt/') || rel.endsWith('.txt.hbs'); tplCache.set(rel, Handlebars.compile(fs.readFileSync(file, 'utf8'), { strict: false, noEscape: isTxt })); } return tplCache.get(rel); } function writeOut(rel, content) { const file = path.join(DIST, rel); fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, content); } // ---------- 构建 ---------- const data = loadAll(); const views = data.variants.map((v) => buildView(v, data)); const matrix = buildNav(views); const navCtx = { name: data.base.name, contact: data.base.contact, links: data.base.links || [], site: data.base.site, matrix, views, }; const pageTpl = tpl('page.html.hbs'); const resumeTxtTpl = tpl('resume.txt.hbs'); for (const view of views) { // HTML:逐板块渲染后拼装(第一板块为基本信息 header) const rendered = view.sections.map((s) => ({ id: s.id, html: tpl(`html/${s.html}.hbs`)(s.ctx), })); const headerHtml = rendered[0].html; const bodyHtml = rendered.slice(1).map((r) => r.html).join('\n'); const html = pageTpl({ meta: view.meta, theme: view.theme, selfUrl: view.selfUrl, baseUrl: view.merged.site.baseUrl, txtUrl: view.txtPath, pdfUrl: view.pdfUrl, headerHtml, bodyHtml, }); writeOut(path.join(view.companyId, view.roleId, 'index.html'), html); // 纯文本:逐板块渲染 + 横幅标题(板块间空一行) const blocks = view.sections.map((s) => banner(s.title) + tpl(`txt/${s.txt}.txt.hbs`)(s.ctx).trimEnd() + '\n\n'); const txt = resumeTxtTpl({ sections: blocks }).trimEnd() + '\n'; writeOut(path.join('txt', view.companyId, view.roleId, 'index.txt'), txt); } // 导航页与纯文本选择页 writeOut('index.html', tpl('nav.html.hbs')(navCtx)); writeOut('txt/index.html', tpl('txt-index.html.hbs')(navCtx)); // 静态资源 fs.cpSync(path.join(ROOT, 'css'), path.join(DIST, 'css'), { recursive: true }); fs.cpSync(path.join(ROOT, 'img'), path.join(DIST, 'img'), { recursive: true }); console.log(`构建完成:${views.length} 份简历(HTML + txt)+ 导航页 → ${path.relative(ROOT, DIST)}/`); for (const v of views) { console.log(` ${v.path.padEnd(20)} ${v.meta.title}${v.isDefault ? '(默认)' : ''}`); }