#!/usr/bin/env node /** * 依据 rules/*.md 的 frontmatter 与正文 H3 标题生成 rules/INDEX.md。 * 单一数据源:文档清单来自 rules/ 目录(NN-topic.md),条目标题/级别来自正文 H3(`### PREFIX-NNN 标题`)。 * * 用法:node tools/gen-rules-index.mjs */ import { readFileSync, writeFileSync, readdirSync } from 'node:fs'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { extractLevel, stripLevel, extractEntrySource } from './lib/rule-level.mjs'; const ROOT = dirname(dirname(fileURLToPath(import.meta.url))); const RULES_DIR = join(ROOT, 'rules'); const DOC_RE = /^\d{2}-[a-z0-9-]+\.md$/; function anchor(id) { return '#' + id.toLowerCase(); } /** 解析 frontmatter(仅支持本仓库用到的标量/数组写法)。 */ function parseFrontmatter(text) { const m = text.match(/^---\r?\n([\s\S]*?)\r?\n---/); if (!m) return {}; const out = {}; for (const line of m[1].split(/\r?\n/)) { const kv = line.match(/^([a-z_]+):\s*(.*)$/i); if (kv) out[kv[1]] = kv[2].trim(); } return out; } // 文档清单 = rules/ 下 NN-topic.md(不含 INDEX / CHANGELOG / _template) const files = readdirSync(RULES_DIR) .filter((n) => DOC_RE.test(n)) .sort() .map((n) => { const rel = `rules/${n}`; const fm = parseFrontmatter(readFileSync(join(RULES_DIR, n), 'utf8')); return { file: rel, prefix: fm.prefix || '', topic: fm.topic || '', source: fm.source || '', loadWhen: fm.load_when || '', }; }); const rows = []; let levelMissing = 0; for (const f of files) { const abs = join(ROOT, f.file); let text; try { text = readFileSync(abs, 'utf8'); } catch { console.error(`[warn] 缺失文件:${f.file}`); continue; } const lines = text.split(/\r?\n/); const re = /^###\s+([A-Z]{2,6}-\d{3})\s+(.+?)\s*$/; // 条目范围 = 本条 H3 到下一个 H2/H3(与 check-rules-index.mjs 的取值口径一致), // 用于在该条目内查找条目级 `> 来源:` 覆盖。 const heads = []; lines.forEach((line, i) => { const m = line.match(re); if (m) heads.push({ id: m[1], title: m[2], i }); }); heads.forEach((h) => { if (!h.id.startsWith(f.prefix + '-')) return; // 跳过正文中引用的他文件编号 let end = lines.length; for (let i = h.i + 1; i < lines.length; i++) { if (/^#{2,3}\s/.test(lines[i])) { end = i; break; } } const body = lines.slice(h.i, end).join('\n'); const level = extractLevel(h.title); if (!level) { levelMissing++; console.error(`[warn] ${f.file} ${h.id} 标题未标注级别(MUST/SHOULD),INDEX 级别列为空:${h.title}`); } rows.push({ id: h.id, title: stripLevel(h.title), level, file: f.file, anchor: anchor(h.id), // 条目级 `> 来源:` 优先于文件级 frontmatter 默认值(USE-008)。 source: extractEntrySource(body) || f.source, }); }); } const fileOrder = new Map(files.map((f, i) => [f.file, i])); rows.sort((a, b) => { const fa = fileOrder.get(a.file); const fb = fileOrder.get(b.file); if (fa !== fb) return fa - fb; return a.id.localeCompare(b.id); }); const out = []; out.push('---'); out.push('id: rules/INDEX.md'); out.push('topic: 规则全量索引(编号 → 文件 → 锚点)'); out.push('load_when: 按编号直达某条规则;确认某条规则属于哪篇文档'); out.push('generated: ' + new Date().toISOString().slice(0, 10)); out.push('---'); out.push(''); out.push('# 规则全量索引(INDEX)'); out.push(''); out.push('> **用途**:agent 只需读本文件即可按编号定位到具体文件与锚点,无需通读原文(控制上下文体积,见 USE-001)。'); out.push('> **生成方式**:`node tools/gen-rules-index.mjs`;**请勿手工编辑**。一致性由 `node tools/check-rules-index.mjs` 校验。'); out.push(''); out.push(`共 ${rows.length} 条规则,分布于 ${files.length} 份文档。`); out.push(''); out.push('| 编号 | 标题 | 级别 | 文件 | 锚点 | 来源 |'); out.push('|---|---|---|---|---|---|'); for (const r of rows) { out.push(`| ${r.id} | ${r.title} | ${r.level} | \`${r.file}\` | \`${r.anchor}\` | ${r.source} |`); } out.push(''); out.push('## 文件索引'); out.push(''); out.push('| 文件 | 前缀 | 主题 | 来源 | 加载时机 |'); out.push('|---|---|---|---|---|'); for (const f of files) { out.push(`| \`${f.file}\` | ${f.prefix} | ${f.topic} | ${f.source} | ${f.loadWhen} |`); } out.push(''); out.push('## 本地规则(local-rules/)'); out.push(''); out.push('项目自有规则独立编号(前缀 `LOC`),不进入本索引;登记表见 `AGENTS.md` 第五节,一致性由 `node tools/check-rules-index.mjs` 校验。'); out.push(''); writeFileSync(join(RULES_DIR, 'INDEX.md'), out.join('\n'), 'utf8'); console.log(`[ok] rules/INDEX.md 已生成:${rows.length} 条规则 / ${files.length} 份文档。`); if (levelMissing) process.exitCode = 1;