135 lines
4.1 KiB
JavaScript
135 lines
4.1 KiB
JavaScript
// Generate perler-lab/src/lib/faq-data.ts from docs/FAQ-DATA.md (T-106/T-107 contract).
|
||
// Usage: node scripts/generate-faq-data.mjs (run from perler-lab/)
|
||
import { readFileSync, writeFileSync } from 'node:fs';
|
||
import { dirname, resolve } from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
|
||
const ROOT = dirname(dirname(fileURLToPath(import.meta.url))); // perler-lab/
|
||
const mdPath = resolve(ROOT, '../docs/FAQ-DATA.md');
|
||
const outPath = resolve(ROOT, 'src/lib/faq-data.ts');
|
||
const md = readFileSync(mdPath, 'utf8');
|
||
|
||
// zh/en labels per category id (derived from FAQ-DATA.md section headers + appendix table)
|
||
const CATEGORY_LABELS = {
|
||
'bead-selection': { zh: '选豆', en: 'Bead Selection' },
|
||
'color-matching': { zh: '配色与图纸生成', en: 'Color Matching & Generation' },
|
||
printing: { zh: '打印', en: 'Printing' },
|
||
ironing: { zh: '熨烫', en: 'Ironing' },
|
||
framing: { zh: '裱框', en: 'Framing' },
|
||
redemption: { zh: '兑换码与权益', en: 'Redemption & Credits' },
|
||
'ai-zone': { zh: 'AI 专区', en: 'AI Zone' },
|
||
product: { zh: '平台与使用', en: 'Platform & Usage' },
|
||
};
|
||
|
||
const FIELD_RE = /^\s*-\s+\*\*(\w+)\*\*:\s*(.*)$/;
|
||
|
||
/** Parse FAQ items: blocks starting with "### FAQ-XX" containing "- **field**: value" lines. */
|
||
function parseItems(text) {
|
||
const items = [];
|
||
const lines = text.split('\n');
|
||
let cur = null;
|
||
|
||
const flush = () => {
|
||
if (cur && cur.category && cur.questionZh && cur.answerZh) {
|
||
items.push(cur);
|
||
}
|
||
cur = null;
|
||
};
|
||
|
||
for (const line of lines) {
|
||
const heading = line.match(/^###\s+(FAQ-\d+)/);
|
||
if (heading) {
|
||
flush();
|
||
cur = { id: heading[1] };
|
||
continue;
|
||
}
|
||
if (!cur) continue;
|
||
if (line.trim() === '---') {
|
||
flush();
|
||
continue;
|
||
}
|
||
const m = line.match(FIELD_RE);
|
||
if (m) {
|
||
const [, key, value] = m;
|
||
if (key === 'category') {
|
||
cur.category = value.split('/')[0].trim(); // "bead-selection / 选豆"
|
||
} else if (key === 'question_zh') cur.questionZh = value.trim();
|
||
else if (key === 'question_en') cur.questionEn = value.trim();
|
||
else if (key === 'answer_zh') cur.answerZh = value.trim();
|
||
else if (key === 'answer_en') cur.answerEn = value.trim();
|
||
}
|
||
}
|
||
flush();
|
||
return items;
|
||
}
|
||
|
||
const items = parseItems(md);
|
||
|
||
// group by category preserving md order; build typed TS output
|
||
const byCat = new Map();
|
||
for (const it of items) {
|
||
if (!byCat.has(it.category)) byCat.set(it.category, []);
|
||
byCat.get(it.category).push(it);
|
||
}
|
||
|
||
const esc = (s) => s.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${');
|
||
|
||
const catBlocks = [];
|
||
for (const [catId, list] of byCat) {
|
||
const labels = CATEGORY_LABELS[catId];
|
||
if (!labels) throw new Error('Unknown category: ' + catId);
|
||
const itemLines = list.map(
|
||
(it) => ` {
|
||
id: '${it.id}',
|
||
questionZh: \`${esc(it.questionZh)}\`,
|
||
questionEn: \`${esc(it.questionEn ?? '')}\`,
|
||
answerZh: \`${esc(it.answerZh)}\`,
|
||
answerEn: \`${esc(it.answerEn ?? '')}\`,
|
||
},`,
|
||
);
|
||
catBlocks.push(` {
|
||
id: '${catId}',
|
||
labelZh: '${labels.zh}',
|
||
labelEn: '${labels.en}',
|
||
items: [
|
||
${itemLines.join('\n')}
|
||
],
|
||
},`);
|
||
}
|
||
|
||
const total = items.length;
|
||
const header = `/**
|
||
* PERLER LAB — 帮助中心 FAQ 数据(T-106)
|
||
*
|
||
* 数据源: docs/FAQ-DATA.md(T-107,共 ${total} 条,8 个分类)。
|
||
* 由脚本生成(question/answer 双语言 zh+en),供 HelpPage 渲染。
|
||
* 变更流程: 修改 FAQ-DATA.md → 重新运行 node scripts/generate-faq-data.mjs。
|
||
*/
|
||
|
||
export interface FaqItem {
|
||
/** FAQ 编号,如 FAQ-01。 */
|
||
id: string;
|
||
questionZh: string;
|
||
questionEn: string;
|
||
answerZh: string;
|
||
answerEn: string;
|
||
}
|
||
|
||
export interface FaqCategory {
|
||
/** 分类标签(过滤/展示用),如 'bead-selection'。 */
|
||
id: string;
|
||
labelZh: string;
|
||
labelEn: string;
|
||
items: readonly FaqItem[];
|
||
}
|
||
|
||
export const FAQ_CATEGORIES: readonly FaqCategory[] = [
|
||
${catBlocks.join('\n')}
|
||
];
|
||
|
||
export const FAQ_TOTAL: number = ${total};
|
||
`;
|
||
|
||
writeFileSync(outPath, header, 'utf8');
|
||
console.log(`OK: ${items.length} items across ${byCat.size} categories -> ${outPath}`);
|