chore: 新增PRD导出工具并忽略cursor目录

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nixevol
2026-06-28 14:38:17 +08:00
co-authored by Cursor
parent 7da248512e
commit 8574e0ab2a
4 changed files with 321 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
node_modules/
package-lock.json
+305
View File
@@ -0,0 +1,305 @@
import {
Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell,
WidthType, BorderStyle, AlignmentType, HeadingLevel, ShadingType,
PageNumber, Header, Footer, PageBreak, LevelFormat,
VerticalAlign, Bookmark, InternalHyperlink, PageReference,
TabStopType, LeaderType,
} from "docx";
import * as fs from "fs";
const MD_PATH = "E:/Users/Administrator/Documents/YSP/docs/PRD-艺术品可信溯源与认证系统.md";
const OUT_PATH = "E:/Users/Administrator/Documents/YSP/docs/PRD-艺术品可信溯源与认证系统.docx";
// ---------- 设计令牌(商务、简约、整洁) ----------
const C = {
primary: "1F3864", // 主深蓝(章节标题 / 表头)
secondary: "2E5496", // 中蓝(小节标题 / 行内代码)
h4: "404040", // 深灰(子标题)
body: "262626", // 正文近黑
zebra: "F4F7FC", // 表格斑马行浅蓝灰
border: "C9C9C9", // 表格细边框
subtle: "8A8A8A", // 页眉页脚浅灰
codeBg: "F1F3F7", // 行内代码底色
white: "FFFFFF",
coverSub: "595959",
};
const FONT = { ascii: "Calibri", eastAsia: "微软雅黑", hAnsi: "Calibri" };
const FONT_CODE = { ascii: "Consolas", eastAsia: "微软雅黑", hAnsi: "Consolas" };
// ---------- 行内解析(** 加粗 ** 与 `代码`) ----------
function parseInline(text, opts = {}) {
const size = opts.size ?? 21;
const color = opts.color ?? C.body;
const baseBold = opts.bold ?? false;
const pieces = String(text).split(/(\*\*[^*]+\*\*|`[^`]+`)/g).filter((s) => s !== "");
const runs = [];
for (const piece of pieces) {
if (/^\*\*[\s\S]+\*\*$/.test(piece)) {
runs.push(new TextRun({ text: piece.slice(2, -2), bold: true, color, size, font: FONT }));
} else if (/^`[\s\S]+`$/.test(piece)) {
runs.push(new TextRun({
text: piece.slice(1, -1), font: FONT_CODE, color: C.secondary, size: size - 1,
shading: { type: ShadingType.CLEAR, color: "auto", fill: C.codeBg },
}));
} else {
runs.push(new TextRun({ text: piece, bold: baseBold, color, size, font: FONT }));
}
}
if (runs.length === 0) runs.push(new TextRun({ text: "", font: FONT }));
return runs;
}
// ---------- 块级构造器 ----------
function heading(text, mdLevel, bookmarkId) {
const styleMap = { 2: HeadingLevel.HEADING_1, 3: HeadingLevel.HEADING_2, 4: HeadingLevel.HEADING_3 };
const sizeMap = { 2: 30, 3: 25, 4: 22 };
const colorMap = { 2: C.primary, 3: C.secondary, 4: C.h4 };
const spacing = mdLevel === 2 ? { before: 360, after: 160 }
: mdLevel === 3 ? { before: 260, after: 100 }
: { before: 180, after: 70 };
const run = new TextRun({ text, bold: true, color: colorMap[mdLevel], size: sizeMap[mdLevel], font: FONT });
const children = bookmarkId
? [new Bookmark({ id: bookmarkId, children: [run] })]
: [run];
const opts = { heading: styleMap[mdLevel], spacing, keepNext: true, children };
if (mdLevel === 2) opts.border = { bottom: { color: C.primary, size: 6, space: 6, style: BorderStyle.SINGLE } };
return new Paragraph(opts);
}
function para(text) {
return new Paragraph({ spacing: { after: 130, line: 340, lineRule: "auto" }, children: parseInline(text) });
}
function bulletItem(text) {
return new Paragraph({
bullet: { level: 0 },
indent: { left: 360, hanging: 230 },
spacing: { after: 60, line: 320, lineRule: "auto" },
children: parseInline(text),
});
}
function numberItem(text, ref) {
return new Paragraph({
numbering: { reference: ref, level: 0 },
spacing: { after: 60, line: 320, lineRule: "auto" },
children: parseInline(text),
});
}
function tableBorders() {
const b = { style: BorderStyle.SINGLE, size: 4, color: C.border };
return { top: b, bottom: b, left: b, right: b, insideHorizontal: b, insideVertical: b };
}
function buildTable(rows) {
const header = rows[0];
const body = rows.slice(1);
const trs = [];
trs.push(new TableRow({
tableHeader: true,
children: header.map((cell) => new TableCell({
shading: { type: ShadingType.CLEAR, color: "auto", fill: C.primary },
margins: { top: 60, bottom: 60, left: 110, right: 110 },
verticalAlign: VerticalAlign.CENTER,
children: [new Paragraph({ children: [new TextRun({ text: cell, bold: true, color: C.white, size: 21, font: FONT })] })],
})),
}));
body.forEach((r, i) => {
const fill = i % 2 === 1 ? C.zebra : C.white;
trs.push(new TableRow({
children: r.map((cell) => new TableCell({
shading: { type: ShadingType.CLEAR, color: "auto", fill },
margins: { top: 50, bottom: 50, left: 110, right: 110 },
verticalAlign: VerticalAlign.CENTER,
children: [new Paragraph({ spacing: { line: 290, lineRule: "auto" }, children: parseInline(cell, { size: 20 }) })],
})),
}));
});
return new Table({ width: { size: 100, type: WidthType.PERCENTAGE }, borders: tableBorders(), rows: trs });
}
// ---------- Markdown -> 块 ----------
function parseBlocks(md) {
const lines = md.split(/\r?\n/);
const out = [];
const olRefs = [];
const tocEntries = [];
let i = 0;
let olCounter = 0;
let hCounter = 0;
while (i < lines.length) {
const line = lines[i];
if (line.trim() === "") { i++; continue; }
if (line.trim().startsWith("|")) {
const tbl = [];
while (i < lines.length && lines[i].trim().startsWith("|")) { tbl.push(lines[i]); i++; }
const rows = tbl.map((r) => r.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((c) => c.trim()));
const dataRows = rows.filter((r) => !r.every((c) => /^:?-{2,}:?$/.test(c)));
if (dataRows.length > 0) out.push(buildTable(dataRows));
continue;
}
const h = line.match(/^(#{1,6})\s+(.*)$/);
if (h) {
const level = h[1].length;
if (level === 1) { i++; continue; } // 文档主标题已用于封面
const mdLevel = Math.min(level, 4);
const text = h[2].trim();
// 目录只收录 2、3 级标题(章 + 小节),并加书签供跳转与页码引用
if (mdLevel === 2 || mdLevel === 3) {
const bookmarkId = `_h${hCounter++}`;
tocEntries.push({ level: mdLevel, text, bookmarkId });
out.push(heading(text, mdLevel, bookmarkId));
} else {
out.push(heading(text, mdLevel));
}
i++; continue;
}
if (/^\d+\.\s+/.test(line)) {
const ref = `ol${olCounter++}`;
olRefs.push(ref);
while (i < lines.length && /^\d+\.\s+/.test(lines[i])) {
out.push(numberItem(lines[i].replace(/^\d+\.\s+/, ""), ref));
i++;
}
continue;
}
if (/^[-*]\s+/.test(line)) {
while (i < lines.length && /^[-*]\s+/.test(lines[i])) {
out.push(bulletItem(lines[i].replace(/^[-*]\s+/, "")));
i++;
}
continue;
}
if (/^>\s?/.test(line)) {
out.push(new Paragraph({
spacing: { after: 130, line: 320, lineRule: "auto" },
shading: { type: ShadingType.CLEAR, color: "auto", fill: C.zebra },
border: { left: { color: C.secondary, size: 18, space: 14, style: BorderStyle.SINGLE } },
children: parseInline(line.replace(/^>\s?/, ""), { color: C.h4 }),
}));
i++; continue;
}
out.push(para(line));
i++;
}
return { blocks: out, olRefs, tocEntries };
}
// ---------- 手动目录(实体文字 + 圆点引导线 + 页码域 + 可点击跳转) ----------
function buildToc(entries) {
const TAB = 9600; // 右对齐页码的制表位位置(twips)
const paras = [];
for (const e of entries) {
const isChapter = e.level === 2;
const titleRun = new TextRun({
text: e.text,
bold: isChapter,
size: isChapter ? 22 : 21,
color: isChapter ? C.primary : C.h4,
font: FONT,
});
const pageRun = new PageReference(e.bookmarkId, { hyperlink: true });
paras.push(new Paragraph({
tabStops: [{ type: TabStopType.RIGHT, position: TAB, leader: LeaderType.DOT }],
indent: isChapter ? { left: 0 } : { left: 360 },
spacing: { after: isChapter ? 80 : 50, before: isChapter ? 60 : 0, line: 300, lineRule: "auto" },
children: [
new InternalHyperlink({ anchor: e.bookmarkId, children: [titleRun] }),
new TextRun({ text: "\t", font: FONT }),
new TextRun({ children: [pageRun], size: isChapter ? 22 : 21, color: isChapter ? C.primary : C.h4, font: FONT }),
],
}));
}
return paras;
}
// ---------- 封面 ----------
function blank(n) {
return Array.from({ length: n }, () => new Paragraph({ spacing: { after: 0 }, children: [new TextRun({ text: "", font: FONT })] }));
}
function cover() {
const now = new Date();
const dateStr = `${now.getFullYear()} 年 ${now.getMonth() + 1} 月 ${now.getDate()} 日`;
return [
...blank(7),
new Paragraph({ alignment: AlignmentType.CENTER, spacing: { after: 140 }, children: [new TextRun({ text: "艺术品可信溯源与认证系统", bold: true, size: 54, color: C.primary, font: FONT })] }),
new Paragraph({ alignment: AlignmentType.CENTER, spacing: { after: 60 }, children: [new TextRun({ text: "产品需求文档(PRD)", size: 30, color: C.coverSub, font: FONT })] }),
new Paragraph({ alignment: AlignmentType.CENTER, spacing: { before: 160, after: 160 }, border: { bottom: { color: C.secondary, size: 8, space: 10, style: BorderStyle.SINGLE } }, children: [new TextRun({ text: "", font: FONT })] }),
...blank(9),
new Paragraph({ alignment: AlignmentType.CENTER, spacing: { after: 70 }, children: [new TextRun({ text: "文档状态:待甲方确认", size: 24, color: C.h4, font: FONT })] }),
new Paragraph({ alignment: AlignmentType.CENTER, children: [new TextRun({ text: `编制日期:${dateStr}`, size: 24, color: C.h4, font: FONT })] }),
];
}
// ---------- 组装文档 ----------
const md = fs.readFileSync(MD_PATH, "utf8");
const { blocks, olRefs, tocEntries } = parseBlocks(md);
const numberingConfig = olRefs.map((ref) => ({
reference: ref,
levels: [{
level: 0, format: LevelFormat.DECIMAL, text: "%1.", alignment: AlignmentType.START,
style: { run: { font: FONT, color: C.body, size: 21 }, paragraph: { indent: { left: 510, hanging: 320 } } },
}],
}));
const pageHeader = new Header({
children: [new Paragraph({
alignment: AlignmentType.RIGHT,
border: { bottom: { color: C.border, size: 4, space: 4, style: BorderStyle.SINGLE } },
children: [new TextRun({ text: "艺术品可信溯源与认证系统 · PRD", size: 17, color: C.subtle, font: FONT })],
})],
});
const pageFooter = new Footer({
children: [new Paragraph({
alignment: AlignmentType.CENTER,
children: [
new TextRun({ text: "第 ", size: 17, color: C.subtle, font: FONT }),
new TextRun({ children: [PageNumber.CURRENT], size: 17, color: C.subtle, font: FONT }),
new TextRun({ text: " 页 / 共 ", size: 17, color: C.subtle, font: FONT }),
new TextRun({ children: [PageNumber.TOTAL_PAGES], size: 17, color: C.subtle, font: FONT }),
new TextRun({ text: " 页", size: 17, color: C.subtle, font: FONT }),
],
})],
});
const tocTitle = new Paragraph({
spacing: { after: 200 },
border: { bottom: { color: C.primary, size: 6, space: 6, style: BorderStyle.SINGLE } },
children: [new TextRun({ text: "目 录", bold: true, size: 30, color: C.primary, font: FONT })],
});
const tocParas = buildToc(tocEntries);
const PAGE = {
size: { width: 11906, height: 16838 },
margin: { top: 1440, bottom: 1440, left: 1440, right: 1440, header: 720, footer: 600 },
};
const doc = new Document({
creator: "产品团队",
title: "艺术品可信溯源与认证系统 PRD",
description: "艺术品可信溯源与认证系统产品需求文档",
features: { updateFields: true },
styles: { default: { document: { run: { font: FONT, size: 21, color: C.body } } } },
numbering: { config: numberingConfig },
sections: [
{ properties: { page: PAGE }, children: cover() },
{
properties: { page: { ...PAGE, pageNumbers: { start: 1 } } },
headers: { default: pageHeader },
footers: { default: pageFooter },
children: [tocTitle, ...tocParas, new Paragraph({ children: [new PageBreak()] }), ...blocks],
},
],
});
const buf = await Packer.toBuffer(doc);
fs.writeFileSync(OUT_PATH, buf);
console.log("OK:", OUT_PATH, "bytes=", buf.length, "blocks=", blocks.length, "olRefs=", olRefs.length);
+13
View File
@@ -0,0 +1,13 @@
{
"name": "prd-docx-export",
"private": true,
"type": "module",
"version": "1.0.0",
"description": "Generate a business-styled DOCX from the PRD markdown",
"scripts": {
"build": "node build-docx.mjs"
},
"dependencies": {
"docx": "^9.7.1"
}
}