Files
resume/scripts/lib.mjs
T
wonder 0b9db1d401
Deploy Resume / deploy (push) Failing after 36s
重构:两维度四变体数据驱动简历站点
- 数据分层:base/experiences(事实) + companies(企业维度) + roles(岗位维度) + variants(变体覆盖),Handlebars 模板构建生成 4 份 HTML + 4 份纯文本 + 导航页
- 路由:根路由导航页(默认推荐 私企-全栈),/private/{dev,support}、/soe/{dev,support},旧地址 301 兼容
- 支持/交付岗不改写经历 bullet(与私企-dev 逐字一致),仅专业技能适配
- 移除 CSDN 与文档站链接
- 样式拆分 base + theme-private/theme-soe,打印样式去底色、条目不跨页
- Docker 多阶段构建(node 构建 → nginx 托管),新增 check.mjs 自检与 serve.mjs 本地预览
- 删除旧手写简历(resume/soe/other/txt)与重复样式
2026-11-03 12:20:24 +08:00

247 lines
9.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// scripts/lib.mjs —— 数据加载、分层合并、视图构建(build.mjs 与 check.mjs 共用)
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import YAML from 'yaml';
export const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
export const DIST = path.join(ROOT, 'dist');
export const CIRCLED = ['①', '②', '③', '④', '⑤'];
const BANNER_WIDTH = 80;
export const banner = (title) =>
`${'='.repeat(BANNER_WIDTH)}\n【${title}】\n${'='.repeat(BANNER_WIDTH)}\n\n`;
// ---------- 数据加载 ----------
export function readYaml(rel) {
return YAML.parse(fs.readFileSync(path.join(ROOT, rel), 'utf8'));
}
function stem(f) {
return f.replace(/\.ya?ml$/i, '');
}
export function keyByStem(rel) {
const dir = path.join(ROOT, rel);
const out = {};
for (const f of fs.readdirSync(dir).filter((x) => /\.ya?ml$/i.test(x)).sort()) {
out[stem(f)] = YAML.parse(fs.readFileSync(path.join(dir, f), 'utf8'));
}
return out;
}
export function loadAll() {
const base = readYaml('data/base.yaml');
const expMap = keyByStem('data/experiences');
base.experiences = expMap;
return {
base,
companies: keyByStem('data/companies'),
roles: keyByStem('data/roles'),
variants: Object.values(keyByStem('data/variants')),
};
}
// ---------- 深合并(对象合并,数组整体替换,后层覆盖前层) ----------
export const isObj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
export function deepMerge(a, b) {
if (b === undefined) return a;
if (Array.isArray(b) || Array.isArray(a)) return b;
if (isObj(a) && isObj(b)) {
const out = { ...a };
for (const k of Object.keys(b)) out[k] = deepMerge(a[k], b[k]);
return out;
}
return b;
}
// 递归收集对象键路径(数组视为叶子),用于跨层同键检查
export function keyPaths(obj, prefix = '', out = new Set()) {
if (!isObj(obj)) return out;
for (const [k, v] of Object.entries(obj)) {
const p = prefix ? `${prefix}.${k}` : k;
if (isObj(v)) keyPaths(v, p, out);
else out.add(p);
}
return out;
}
// ---------- 工具 ----------
export function normalizeList(items) {
return (items || []).map((it) => (typeof it === 'string' ? { text: it } : { ...it }));
}
function orderedExperiences(merged, kind, orderKey) {
const all = Object.values(merged.experiences || {}).filter((e) => e && e.kind === kind);
const order = (merged.order && merged.order[orderKey]) || [];
const listed = order.map((id) => all.find((e) => e.id === id)).filter(Boolean);
const rest = all.filter((e) => !order.includes(e.id));
return [...listed, ...rest];
}
// ---------- 板块注册表:板块 id → 标题 / html 模板 / txt 模板 ----------
export const SECTION_REGISTRY = {
education: { title: '教育经历', html: 'education', txt: 'education' },
honors: { title: '荣誉奖项', html: 'honors', txt: 'honors' },
internships: { title: '实习经历', html: 'entries', txt: 'entries' },
campus: { title: '校园经历', html: 'campus', txt: 'campus' },
projects: { title: '项目经历', html: 'projects', txt: 'projects' },
certificates: { title: '证书', html: 'simple-list', txt: 'simple-list', listClass: 'exp-items' },
studentWork: { title: '学生工作', html: 'student-work', txt: 'student-work' },
socialPractice: { title: '社会实践', html: 'social-practice', txt: 'social-practice' },
skills: { title: '专业技能', html: 'simple-list', txt: 'simple-list', listClass: 'skill-items', sep: ': ' },
selfEval: { title: '自我评价', html: 'simple-list', txt: 'simple-list', listClass: 'skill-items', sep: ':' },
};
// ---------- 变体视图构建 ----------
export function buildView(v, data) {
const company = data.companies[v.company];
const role = data.roles[v.role];
if (!company) throw new Error(`变体 ${v.company}-${v.role} 引用了未知企业维度 "${v.company}"`);
if (!role) throw new Error(`变体 ${v.company}-${v.role} 引用了未知岗位维度 "${v.role}"`);
// 合并优先级:base → companies → roles → variants.overrides
const merged = deepMerge(deepMerge(deepMerge(data.base, company), role), v.overrides || {});
const variantPath = `/${v.company}/${v.role}`;
const selfUrl = `${merged.site.baseUrl}${variantPath}`;
const intentParts = (merged.intent || []).map((text, i) => ({ num: CIRCLED[i] || `${i + 1}.`, text }));
const links = [
{ label: '线上简历', url: selfUrl, display: selfUrl },
...(merged.links || []).map((l) => ({ ...l, display: l.text || l.url })),
];
const headerCfg = merged.header || {};
const header = {
layout: headerCfg.layout === 'formal' ? 'formal' : 'compact',
photo: merged.photo || null,
politicalStatus: merged.politicalStatus || null,
headline: merged.headline || null,
intentParts,
intentLine: intentParts.map((p) => `${p.num} ${p.text}`).join(' '),
techTags: merged.techTags || [],
techTagsText: (merged.techTags || []).join(', '),
showLinks: headerCfg.showLinks !== false,
showExpCert: headerCfg.showExpCert !== false,
links,
};
// 荣誉:展示方式与筛选由企业维度 honorDisplay 控制
const hd = merged.honorDisplay || {};
let honors = (merged.honors || []).map((h) => ({ ...h }));
if (Array.isArray(hd.show) && hd.show.length) {
honors = hd.show.map((id) => honors.find((h) => h.id === id)).filter(Boolean);
}
const honorsView = {
honors,
left: honors.filter((h) => h.cert),
right: honors.filter((h) => !h.cert),
layoutTwoCol: hd.layout === 'two-col',
showDate: hd.display === 'date',
};
const projCfg = merged.projects || {};
const baseCtx = { ...merged, header, title: '', listClass: '', sep: '' };
const camp = orderedExperiences(merged, 'camp', 'campus')[0] || null;
// 基本信息(header)恒为第一板块,模板按企业维度版式选择
const headerSection = {
id: 'header',
title: '基本信息',
html: `header-${header.layout}`,
txt: `header-${header.layout}`,
ctx: { ...baseCtx, title: '基本信息' },
};
const contentSections = (v.sections || []).map((id) => {
const reg = SECTION_REGISTRY[id];
if (!reg) throw new Error(`变体 ${v.company}-${v.role} 声明了未知板块 "${id}"`);
const common = { ...baseCtx, title: reg.title, listClass: reg.listClass || 'skill-items', sep: reg.sep || ':' };
switch (id) {
case 'education':
return { id, ...reg, ctx: common };
case 'honors':
return { id, ...reg, ctx: { ...common, ...honorsView } };
case 'internships':
return {
id, ...reg,
ctx: {
...common,
items: orderedExperiences(merged, 'internship', 'internships').map((e) => ({ ...e, bullets: e.bullets || [] })),
},
};
case 'projects':
return {
id, ...reg,
ctx: {
...common,
projIntroDashed: projCfg.introStyle === 'dashed',
showWorkTitle: projCfg.showWorkTitle === true,
items: orderedExperiences(merged, 'project', 'projects').map((e) => ({ ...e, bullets: e.bullets || [] })),
},
};
case 'campus':
return { id, ...reg, ctx: { ...common, line: (merged.campus && merged.campus.line) || [], featured: camp } };
case 'certificates':
return { id, ...reg, ctx: { ...common, items: normalizeList(merged.certificates) } };
case 'studentWork':
return { id, ...reg, ctx: { ...common, items: merged.studentWork || [] } };
case 'socialPractice':
return { id, ...reg, ctx: { ...common, practice: merged.socialPractice || {} } };
case 'skills':
case 'selfEval':
return { id, ...reg, ctx: { ...common, items: normalizeList(merged[id]) } };
default:
return { id, ...reg, ctx: common };
}
});
const meta = v.meta || {};
const title = meta.title || `何朝晖 · ${role.navLabel}(${company.navLabel}版)`;
const description = meta.description || `${merged.name}个人简历 - ${role.navLabel}(${company.navLabel}投递版)`;
return {
id: `${v.company}-${v.role}`,
companyId: v.company,
roleId: v.role,
company,
role,
path: `${variantPath}/`,
txtPath: `/txt${variantPath}`,
selfUrl,
theme: company.theme || v.company,
meta: { title, description },
nav: v.nav || {},
isDefault: v.default === true,
merged,
header,
honors: honorsView,
sections: [headerSection, ...contentSections],
};
}
// ---------- 导航矩阵 ----------
function orderIdx(list, x) {
const i = list.indexOf(x);
return i === -1 ? 99 : i;
}
export function buildNav(views) {
const companyOrder = ['private', 'soe'];
const roleOrder = ['dev', 'support'];
const companies = [...new Set(views.map((v) => v.companyId))].sort((a, b) => orderIdx(companyOrder, a) - orderIdx(companyOrder, b));
const roles = [...new Set(views.map((v) => v.roleId))].sort((a, b) => orderIdx(roleOrder, a) - orderIdx(roleOrder, b));
return {
companies: companies.map((c) => ({ id: c, navLabel: views.find((v) => v.companyId === c).company.navLabel })),
rows: roles.map((r) => ({
id: r,
navLabel: views.find((v) => v.roleId === r).role.navLabel,
cells: companies
.map((c) => views.find((v) => v.companyId === c && v.roleId === r))
.filter(Boolean),
})),
};
}