focus-guard 3.5.2 → 3.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -3
- package/README.zh-CN.md +14 -4
- package/assets/NOTICE.md +3 -1
- package/docs/ARCH-MAP.md +302 -74
- package/docs/BENCH.md +28 -13
- package/docs/RELEASE-POLICY.md +74 -17
- package/docs/RULES.md +98 -60
- package/docs/THREAT-MODEL.md +3 -3
- package/docs/WRITING-STYLE.md +40 -2
- package/package.json +11 -5
- package/skills/focus-plan/SKILL.md +77 -0
- package/skills/focus-rules/SKILL.md +267 -0
- package/skills/focus-thinking/SKILL.md +8 -255
- package/src/adapters/dsh/eligibility-gate.mjs +75 -35
- package/src/adapters/dsh/session.mjs +22 -0
- package/src/{dsh → adapters/dsh}/toolName.mjs +6 -0
- package/src/core/approvals.mjs +10 -4
- package/src/core/audit-writer.mjs +5 -2
- package/src/core/auditActions.mjs +214 -0
- package/src/core/checkEligibility.mjs +193 -130
- package/src/core/constants.mjs +8 -4
- package/src/core/decisionEngine.mjs +16 -13
- package/src/core/env.mjs +37 -2
- package/src/core/patterns.mjs +3 -1
- package/src/core/prefixStability.mjs +27 -3
- package/src/core/profileLoader.mjs +96 -3
- package/src/core/scratch.mjs +288 -0
- package/src/core/session.mjs +23 -0
- package/src/core/state.mjs +3 -3
- package/src/dsh/audit.mjs +45 -170
- package/src/dsh/bridgeCheck.mjs +81 -0
- package/src/dsh/compactionWatch/announce.mjs +2 -1
- package/src/dsh/compactionWatch.mjs +2 -1
- package/src/dsh/cost-ledger.mjs +174 -0
- package/src/dsh/guard/authz.mjs +3 -3
- package/src/dsh/guard/evidence.mjs +19 -0
- package/src/dsh/guard/retryGuard.mjs +96 -0
- package/src/dsh/guard/risk.mjs +61 -16
- package/src/dsh/guard/tooling.mjs +20 -13
- package/src/dsh/{value-gate.mjs → guard/value-gate.mjs} +6 -0
- package/src/dsh/guard/writing-gate.mjs +117 -0
- package/src/dsh/index.js +4 -7
- package/src/dsh/pipeline.mjs +72 -8
- package/src/dsh/postProgress.mjs +81 -40
- package/src/dsh/preGuard.mjs +12 -3
- package/src/dsh/repeatGuard.mjs +5 -4
- package/src/dsh/seams/pre-step.mjs +74 -23
- package/src/dsh/seams/prompt-rules.mjs +91 -5
- package/src/dsh/{guard → seams}/runtime.mjs +41 -10
- package/src/dsh/seams/session-start.mjs +33 -10
- package/src/dsh/seams/shared.mjs +8 -5
- package/src/dsh/search-hints.mjs +15 -1
- package/src/dsh/sessionScope.mjs +11 -11
- package/src/dsh/stopGuard.mjs +2 -7
- package/src/dsh/tools.mjs +39 -1
- package/src/dsh/traceMeta.mjs +5 -11
- package/src/memory/index.mjs +7 -5
- package/src/memory/workspace.mjs +41 -19
- package/src/profiles/deepseek-flash.mjs +10 -8
- package/src/profiles/deepseek-pro.mjs +9 -8
- package/src/profiles/default.mjs +14 -8
- package/src/profiles/glm.mjs +9 -8
- package/src/profiles/gpt-astra.mjs +11 -9
- package/tools/audit-chain.mjs +29 -4
- package/tools/collect-panel.mjs +3 -24
- package/tools/dead-export.mjs +2 -17
- package/tools/docs-book.mjs +234 -0
- package/tools/docs-drift.mjs +143 -0
- package/tools/docs-lint.mjs +76 -9
- package/tools/docs-refs.mjs +343 -0
- package/tools/gate-audit.mjs +3 -24
- package/tools/handoff.mjs +4 -2
- package/tools/index-stale.mjs +124 -0
- package/tools/install-check.mjs +107 -9
- package/tools/knowledge.mjs +53 -4
- package/tools/layering-audit.mjs +7 -1
- package/tools/legacy-audit.mjs +83 -0
- package/tools/lib/book-style.mjs +42 -1
- package/tools/lib/case-tool.mjs +9 -3
- package/tools/lib/cli.mjs +35 -0
- package/tools/lib/docs-ack.mjs +97 -0
- package/tools/lib/docs-core.mjs +84 -45
- package/tools/lib/docs-files.mjs +73 -0
- package/tools/lib/docs-scope.mjs +46 -0
- package/tools/lib/docs-status.mjs +148 -0
- package/tools/lib/docs-typography.mjs +91 -0
- package/tools/lib/docs-version.mjs +8 -5
- package/tools/lib/dup-block.mjs +117 -0
- package/tools/lib/idioms.mjs +149 -0
- package/tools/lib/import-graph.mjs +8 -3
- package/tools/lib/index-io.mjs +4 -4
- package/tools/lib/index-registry.mjs +101 -0
- package/tools/lib/legacy-core.mjs +178 -0
- package/tools/lib/markdown.mjs +202 -0
- package/tools/lib/modules-doc.mjs +103 -0
- package/tools/lib/modules-list.mjs +396 -0
- package/tools/lib/modules.mjs +360 -0
- package/tools/lib/mutation-ops.mjs +63 -0
- package/tools/lib/parse-block.mjs +58 -0
- package/tools/lib/prefix-cost.mjs +5 -3
- package/tools/lib/ratchet.mjs +22 -8
- package/tools/lib/release-faces.mjs +144 -0
- package/tools/lib/repo-copy.mjs +34 -0
- package/tools/lib/survey-report.mjs +57 -0
- package/tools/lib/where.mjs +139 -0
- package/tools/library-book.mjs +7 -92
- package/tools/library-build.mjs +15 -3
- package/tools/lint.mjs +24 -13
- package/tools/log-audit.mjs +76 -0
- package/tools/memory-audit.mjs +32 -6
- package/tools/memory-distill.mjs +14 -1
- package/tools/memory-recall-eval.mjs +408 -0
- package/tools/module-audit.mjs +2 -17
- package/tools/modules-audit.mjs +110 -0
- package/tools/mutate.mjs +120 -74
- package/tools/panel-baseline.mjs +172 -0
- package/tools/prefix-audit.mjs +10 -2
- package/tools/prefix-drift.mjs +10 -5
- package/tools/registry-check.mjs +200 -0
- package/tools/release.mjs +322 -0
- package/tools/smells.mjs +97 -139
- package/tools/version.mjs +12 -8
- package/docs/AB-PROMPTS.md +0 -123
- package/docs/AB-SESSION.md +0 -161
- package/docs/BENCH-BASELINE.json +0 -95
- package/docs/BRAIN-VIZ.md +0 -94
- package/docs/GATE-INPUTS.json +0 -10
- package/docs/HANDOFF-2026-10-27.md +0 -736
- package/docs/HOOK-ORDERING.md +0 -139
- package/docs/LEGISLATION-LAW.md +0 -104
- package/docs/MCP-4.0-PLAN.md +0 -184
- package/docs/MEMORY-4.5-PLAN.md +0 -415
- package/docs/PROFILE-TUNING.md +0 -147
- package/docs/RATCHET.json +0 -80
- package/docs/TOOL-SCHEMA.json +0 -6
- package/docs/VERSION-PLAN.md +0 -604
- package/docs/architecture-map.mermaid +0 -186
- package/docs/deepseek-pricing-audit.md +0 -65
- package/src/core/staleCleanup.mjs +0 -224
- package/src/dsh/guard/superpowers.mjs +0 -70
- package/src/dsh/workspace.mjs +0 -5
- package/tools/library-index.mjs +0 -91
package/tools/lib/docs-core.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* 文档口径内核(3.3 D5 / D2):版本面 + 相对链接 + 状态头 + INDEX 覆盖。纯函数,零依赖。
|
|
3
3
|
*
|
|
4
4
|
* 机械判定的四件事:
|
|
5
|
-
* 1)
|
|
5
|
+
* 1) **版本面**:八处清单 + `ENGINE_VERSION` 必须等于 CHANGELOG 最新版本段;
|
|
6
6
|
* 2) **相对链接**:仓库内 `.md` 的 `[text](path)` 目标文件必须存在(http/mailto/锚点跳过);
|
|
7
7
|
* 3) **状态头**(D2):权威文档正文必须带 `> **状态**:权威 · 适用版本 … · 最后核对 …`;
|
|
8
8
|
* 4) **INDEX 覆盖**(D2):权威文档必须能从 `docs/INDEX.md` 的链接到达。
|
|
@@ -11,10 +11,13 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import { existsSync, readdirSync, readFileSync } from 'node:fs'
|
|
13
13
|
import { dirname, join, relative, resolve } from 'node:path'
|
|
14
|
+
import { listMarkdownFiles } from './docs-files.mjs'
|
|
15
|
+
import { escapeRe, lineOf } from './where.mjs'
|
|
14
16
|
|
|
15
|
-
/**
|
|
17
|
+
/** 八处清单(与 RELEASE-POLICY 第三节逐字对应;2026-11-22 补 package-lock.json)。 */
|
|
16
18
|
export const VERSION_FILES = [
|
|
17
19
|
'package.json',
|
|
20
|
+
'package-lock.json',
|
|
18
21
|
'marketplace.json',
|
|
19
22
|
'.zcode-plugin/plugin.json',
|
|
20
23
|
'.claude-plugin/plugin.json',
|
|
@@ -39,36 +42,28 @@ export const AUTHORITATIVE_DOCS = [
|
|
|
39
42
|
'packages/core/docs/WRITING-STYLE.md',
|
|
40
43
|
'packages/core/docs/BENCH.md',
|
|
41
44
|
'packages/core/docs/PROFILE-TUNING.md',
|
|
45
|
+
'packages/core/docs/THREAT-MODEL.md',
|
|
42
46
|
]
|
|
43
47
|
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
// 2026-10-31(3.4 D3):`docs/archive/` 是历史证据,链接按旧路径原样保留才不失真,
|
|
48
|
-
// 因此归档文档整体不进 lint(不查链接/状态头);INDEX 仍负责列出它们。
|
|
49
|
-
|
|
50
|
-
/** 遍历仓库内 Markdown(排除运行产物与归档)。 */
|
|
51
|
-
export function listMarkdownFiles(root) {
|
|
52
|
-
const out = []
|
|
53
|
-
const walk = (dir) => {
|
|
54
|
-
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
55
|
-
const full = join(dir, entry.name)
|
|
56
|
-
if (SKIP_SEGMENTS.has(entry.name)) continue
|
|
57
|
-
if (entry.isDirectory()) walk(full)
|
|
58
|
-
else if (entry.name.toLowerCase().endsWith('.md')) out.push(full)
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
walk(root)
|
|
62
|
-
return out
|
|
63
|
-
}
|
|
48
|
+
// 文档清单的真值只有一个(`docs-files.mjs`):`docs-lint` / `docs-status` / `docs-version` /
|
|
49
|
+
// `docs-ack` / `docs-book` 全从这里拿文件。输入少几个文件,判据再对也等于没看。
|
|
50
|
+
export { listMarkdownFiles } from './docs-files.mjs'
|
|
64
51
|
|
|
65
52
|
/** CHANGELOG 最新版本段(`## x.y.z`)。 */
|
|
66
53
|
export function changelogLatest(root) {
|
|
67
|
-
const
|
|
68
|
-
const m = text.match(/^## (\d+\.\d+\.\d+)/m)
|
|
54
|
+
const m = changelogText(root).match(/^## (\d+\.\d+\.\d+)/m)
|
|
69
55
|
return m ? m[1] : null
|
|
70
56
|
}
|
|
71
57
|
|
|
58
|
+
/** 读不到返回空串:CHANGELOG 缺失时由别的判据报,这里不该抛。 */
|
|
59
|
+
function changelogText(root) {
|
|
60
|
+
try {
|
|
61
|
+
return readFileSync(join(root, 'CHANGELOG.md'), 'utf8')
|
|
62
|
+
} catch {
|
|
63
|
+
return ''
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
72
67
|
/** 七个清单的 version + ENGINE_VERSION + CHANGELOG 最新段必须一致。 */
|
|
73
68
|
/**
|
|
74
69
|
* 已推送版本锚点:`tests/changelog-audience.test.mjs` 里的 `PUBLISHED_MAX`。
|
|
@@ -113,21 +108,23 @@ export function checkVersionFace(root) {
|
|
|
113
108
|
const findings = []
|
|
114
109
|
const versions = []
|
|
115
110
|
for (const rel of VERSION_FILES) {
|
|
116
|
-
|
|
111
|
+
let text = ''
|
|
117
112
|
try {
|
|
118
|
-
|
|
113
|
+
text = readFileSync(join(root, rel), 'utf8')
|
|
114
|
+
const json = JSON.parse(text)
|
|
119
115
|
// 根 marketplace.json / .claude-plugin/marketplace.json 的版本在 plugins[0].version;
|
|
120
116
|
// 其余清单是顶层 version。两种形状都认,避免"读不到就静默算 undefined"。
|
|
121
117
|
const value = json.version ?? (Array.isArray(json.plugins) ? json.plugins[0]?.version : undefined)
|
|
122
|
-
|
|
118
|
+
// 行号在读到的那一刻定下:`"version": "x"` 那一行(冒号后有无空格各清单不一,按正则找)。
|
|
119
|
+
versions.push({ file: rel, value, line: lineOf(text, new RegExp(`"version"\\s*:\\s*"${escapeRe(value)}"`)) })
|
|
123
120
|
} catch (error) {
|
|
124
|
-
findings.push({ rule: 'version-unreadable', file: rel, line:
|
|
121
|
+
findings.push({ rule: 'version-unreadable', file: rel, line: 0, detail: String(error && error.message) })
|
|
125
122
|
}
|
|
126
123
|
}
|
|
127
|
-
const
|
|
128
|
-
const constText = readFileSync(
|
|
124
|
+
const constRel = 'packages/core/src/core/constants.mjs'
|
|
125
|
+
const constText = readFileSync(join(root, constRel), 'utf8')
|
|
129
126
|
const em = constText.match(/ENGINE_VERSION\s*=\s*["']([^"']+)["']/)
|
|
130
|
-
versions.push({
|
|
127
|
+
versions.push({ file: constRel, value: em ? em[1] : null, line: lineOf(constText, 'ENGINE_VERSION') })
|
|
131
128
|
|
|
132
129
|
// ① 清单之间一致。基准取**众数**而非第一个:否则"改坏第一个"会把其余全部报红,
|
|
133
130
|
// 一条错变成七条错,红的数量跟病灶无关,人就学会忽略红。
|
|
@@ -145,8 +142,8 @@ export function checkVersionFace(root) {
|
|
|
145
142
|
if (base && v.value !== base) {
|
|
146
143
|
findings.push({
|
|
147
144
|
rule: 'version-inconsistent',
|
|
148
|
-
file: v.
|
|
149
|
-
line:
|
|
145
|
+
file: v.file,
|
|
146
|
+
line: v.line,
|
|
150
147
|
detail: `${v.value} ≠ ${base}(清单之间必须一致)`,
|
|
151
148
|
})
|
|
152
149
|
}
|
|
@@ -158,7 +155,7 @@ export function checkVersionFace(root) {
|
|
|
158
155
|
findings.push({
|
|
159
156
|
rule: 'version-changelog-behind',
|
|
160
157
|
file: 'CHANGELOG.md',
|
|
161
|
-
line:
|
|
158
|
+
line: lineOf(changelogText(root), `## ${latest}`),
|
|
162
159
|
detail: `最新段 ${latest} 落后于清单 ${base}`,
|
|
163
160
|
})
|
|
164
161
|
}
|
|
@@ -175,17 +172,29 @@ export function checkVersionFace(root) {
|
|
|
175
172
|
*
|
|
176
173
|
* 为什么不靠自觉:PATTERNS.md 建库 27 天零条就是"只有规矩没有闸门"的下场。
|
|
177
174
|
*
|
|
178
|
-
* 范围只含**活跃对外文档**:`README` / `CHANGELOG` / `docs/`(排除 `archive
|
|
179
|
-
*
|
|
180
|
-
*
|
|
175
|
+
* 范围只含**活跃对外文档**:`README` / `CHANGELOG` / `docs/` / `packages/core/docs/`(排除 `archive`)。
|
|
176
|
+
* 归档是历史证据,闸门管到它等于逼后人改历史;`vault-notes` 是外部资料摘录,会被重新生成覆盖。
|
|
177
|
+
*
|
|
178
|
+
* 2026-11-28 修两处漏:
|
|
179
|
+
* ① **词表只列了三种搭配**(批示/原话/报告)。于是「人类提议」「人类建议」「人类要求」
|
|
180
|
+
* 「人类定的原话」一路放行——CHANGELOG 与三份包内文档里各躺着一处,闸门全绿。
|
|
181
|
+
* 枚举搭配本身就是在赌"下次会换个说法",改成「人类」后最多 6 字再接**引述主张**的动词/名词。
|
|
182
|
+
* 只收引述类:`确认`/`拍板`/`裁定` 是权限动作(「待人类拍板」「需人类确认」是流程术语),
|
|
183
|
+
* 收进来会把「谁有权批」的描述一起判成违规——第一次扩表就误伤了 4 处。
|
|
184
|
+
* `批示` 更特殊:它在本仓 129 处,绝大多数是**术语定义**(「人类新指令即批示」「人类指令=批示」)。
|
|
185
|
+
* 所以只在它**后面紧跟中文引号**(真的在引原话)时才算违规。直引号不算——术语本身
|
|
186
|
+
* 也可能被引号括起来(ARCHITECTURE.md「人类新指令即"批示"」就是这种)。
|
|
187
|
+
* ② **范围只认根目录 `docs/`**。`packages/core/docs/` 下 60 多处一条没扫过,而那里正是
|
|
188
|
+
* RULES / ARCH-MAP / THREAT-MODEL 这些权威文档的所在地——规则写在离闸门最远的地方。
|
|
181
189
|
*/
|
|
182
|
-
const CITATION_RE =
|
|
190
|
+
const CITATION_RE = /人类[^,。;\n]{0,6}(?:原话|报告|建议|提议|要求|提出|意图|批示\s*[「『])/
|
|
183
191
|
export function checkCitationStyle(root) {
|
|
184
192
|
const findings = []
|
|
185
193
|
const targets = ['README.md', 'CHANGELOG.md']
|
|
186
194
|
for (const abs of listMarkdownFiles(root)) {
|
|
187
195
|
const rel = relative(root, abs).split('\\').join('/')
|
|
188
|
-
|
|
196
|
+
const inScope = rel.startsWith('docs/') || rel.startsWith('packages/core/docs/')
|
|
197
|
+
if (!inScope) continue
|
|
189
198
|
if (rel.startsWith('docs/archive/') || rel.startsWith('docs/vault-notes/')) continue
|
|
190
199
|
targets.push(rel)
|
|
191
200
|
}
|
|
@@ -276,7 +285,7 @@ export function checkStatusHeaders(root, docs = AUTHORITATIVE_DOCS) {
|
|
|
276
285
|
for (const rel of docs) {
|
|
277
286
|
const text = readFileSync(join(root, rel), 'utf8')
|
|
278
287
|
if (!/^> \*\*状态\*\*:权威[^\n]*适用版本[^\n]*最后核对/m.test(text)) {
|
|
279
|
-
findings.push({ rule: 'missing-status-header', file: rel, line:
|
|
288
|
+
findings.push({ rule: 'missing-status-header', file: rel, line: 0, detail: '缺「> **状态**:权威 · 适用版本 … · 最后核对 …」' })
|
|
280
289
|
}
|
|
281
290
|
}
|
|
282
291
|
return findings
|
|
@@ -308,7 +317,7 @@ export function checkIndexCoverage(root, docs = AUTHORITATIVE_DOCS) {
|
|
|
308
317
|
const findings = []
|
|
309
318
|
const indexPath = join(root, 'docs/INDEX.md')
|
|
310
319
|
if (!existsSync(indexPath)) {
|
|
311
|
-
return [{ rule: 'missing-index', file: 'docs/INDEX.md', line:
|
|
320
|
+
return [{ rule: 'missing-index', file: 'docs/INDEX.md', line: 0, detail: '索引文件不存在' }]
|
|
312
321
|
}
|
|
313
322
|
const text = readFileSync(indexPath, 'utf8')
|
|
314
323
|
const reached = new Set()
|
|
@@ -319,9 +328,39 @@ export function checkIndexCoverage(root, docs = AUTHORITATIVE_DOCS) {
|
|
|
319
328
|
}
|
|
320
329
|
for (const rel of docs) {
|
|
321
330
|
if (!reached.has(resolve(root, rel))) {
|
|
322
|
-
findings.push({ rule: 'index-missing', file: rel, line:
|
|
331
|
+
findings.push({ rule: 'index-missing', file: rel, line: 0, detail: '权威文档未出现在 docs/INDEX.md 的链接里' })
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return findings
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 判据:行内代码里的 `npm run <name>` 必须是真实脚本。
|
|
339
|
+
*
|
|
340
|
+
* 为什么值得单独一条:文档最常给的**就是命令**,而命令写错只有读者踩到才知道。
|
|
341
|
+
* 命令名存在与否是硬事实,不需要判断力,属于"机械该做的事"。
|
|
342
|
+
* 只认 `npm run x` 这一种写法(`npm test` / `npx` / 裸 `node` 不查——它们的目标不是本仓 scripts)。
|
|
343
|
+
*/
|
|
344
|
+
export function checkScriptRefs(root, files = listMarkdownFiles(root)) {
|
|
345
|
+
const names = new Set()
|
|
346
|
+
for (const rel of ['package.json', 'packages/core/package.json', 'packages/extended/package.json']) {
|
|
347
|
+
try {
|
|
348
|
+
const j = JSON.parse(readFileSync(join(root, rel), 'utf8'))
|
|
349
|
+
for (const k of Object.keys(j.scripts || {})) names.add(k)
|
|
350
|
+
for (const k of Object.keys(j.bin || {})) names.add(k)
|
|
351
|
+
} catch {
|
|
352
|
+
/* 缺某份清单不影响其他份 */
|
|
323
353
|
}
|
|
324
354
|
}
|
|
355
|
+
const findings = []
|
|
356
|
+
for (const file of files) {
|
|
357
|
+
const rel = relative(root, file).split('\\').join('/')
|
|
358
|
+
readFileSync(file, 'utf8').split(/\r?\n/).forEach((line, i) => {
|
|
359
|
+
for (const m of line.matchAll(/`npm run ([\w:.-]+)`/g)) {
|
|
360
|
+
if (!names.has(m[1])) findings.push({ rule: 'dead-script-ref', file: rel, line: i + 1, detail: `npm run ${m[1]} 不存在于任何 package.json` })
|
|
361
|
+
}
|
|
362
|
+
})
|
|
363
|
+
}
|
|
325
364
|
return findings
|
|
326
365
|
}
|
|
327
366
|
|
|
@@ -333,15 +372,15 @@ export function checkReadmeMetrics(root) {
|
|
|
333
372
|
const lines = readFileSync(file, 'utf8').split(/\r?\n/)
|
|
334
373
|
const findings = []
|
|
335
374
|
if (lines.length > README_LIMITS.lines) {
|
|
336
|
-
findings.push({ rule: 'readme-lines', file: 'README.md', line:
|
|
375
|
+
findings.push({ rule: 'readme-lines', file: 'README.md', line: 0, detail: `${lines.length} 行(上限 ${README_LIMITS.lines})` })
|
|
337
376
|
}
|
|
338
377
|
const headings = lines.filter((l) => /^#{1,6}\s/.test(l)).length
|
|
339
378
|
if (headings > README_LIMITS.headings) {
|
|
340
|
-
findings.push({ rule: 'readme-headings', file: 'README.md', line:
|
|
379
|
+
findings.push({ rule: 'readme-headings', file: 'README.md', line: 0, detail: `${headings} 个标题(上限 ${README_LIMITS.headings})` })
|
|
341
380
|
}
|
|
342
381
|
const tableRows = lines.filter((l) => /^\|/.test(l) && !/^\|[\s:|-]+\|$/.test(l)).length
|
|
343
382
|
if (tableRows > README_LIMITS.tableRows) {
|
|
344
|
-
findings.push({ rule: 'readme-table-rows', file: 'README.md', line:
|
|
383
|
+
findings.push({ rule: 'readme-table-rows', file: 'README.md', line: 0, detail: `${tableRows} 行表格(上限 ${README_LIMITS.tableRows})` })
|
|
345
384
|
}
|
|
346
385
|
lines.forEach((line, i) => {
|
|
347
386
|
// 抹掉 URL / 链接目标 / 行内代码 / ISO 日期:版本号、badge、状态头日期都不算"正文数字"。
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「哪些 Markdown 算文档」——单一出处(2026-11-26 从 `docs-core.mjs` 拆出)。
|
|
3
|
+
*
|
|
4
|
+
* 拆出来的直接原因:`docs-core.mjs` 已 418 行,越过 `smells` 的 400 行阈值。
|
|
5
|
+
* 按本仓的既有判断(见 `docs-version.mjs` / `docs-status.mjs` 的头注释),
|
|
6
|
+
* 越过阈值时**不许抬阈值**,只许拆——抬阈值等于把尺子锯短。
|
|
7
|
+
*
|
|
8
|
+
* 更实质的原因是:这个函数是**所有文档闸门的输入口**(`docs-lint` / `docs-status` /
|
|
9
|
+
* `docs-version` / `docs-ack` / `docs-book` 都从它拿文件清单)。输入少几个文件,
|
|
10
|
+
* 判据再对也等于没看。`gate-inputs.mjs` 记过同一类事故:
|
|
11
|
+
* 「同一根目录、同一个 listMarkdownFiles,差 6 个文件;两次的问题条数都是 5 条,闸门一路绿。」
|
|
12
|
+
*/
|
|
13
|
+
import { execFileSync } from 'node:child_process'
|
|
14
|
+
import { existsSync, readdirSync } from 'node:fs'
|
|
15
|
+
import { join } from 'node:path'
|
|
16
|
+
|
|
17
|
+
// `HANDOFF.md` 是本机私有备忘(在 .git/info/exclude 里),不属于公开文档面,
|
|
18
|
+
// 混进来会让"文档口径"这个数字同时反映私有文件——口径不纯。
|
|
19
|
+
// 2026-11-26:`.workbuddy/` 同理——它是会话记忆目录(未跟踪),里面的 `memory/*.md`
|
|
20
|
+
// 一度被扫进 `docs/STATUS.md`(46 份里多出 1 份本机私货),换台机器就少一份。
|
|
21
|
+
const SKIP_SEGMENTS = new Set([
|
|
22
|
+
'node_modules',
|
|
23
|
+
'.git',
|
|
24
|
+
'.focus-guard',
|
|
25
|
+
'.ai',
|
|
26
|
+
'.workbuddy',
|
|
27
|
+
'self-improvement',
|
|
28
|
+
'archived',
|
|
29
|
+
'archive',
|
|
30
|
+
'backup',
|
|
31
|
+
'HANDOFF.md',
|
|
32
|
+
])
|
|
33
|
+
// 2026-10-31(3.4 D3):`docs/archive/` 是历史证据,链接按旧路径原样保留才不失真,
|
|
34
|
+
// 因此归档文档整体不进 lint(不查链接/状态头);INDEX 仍负责列出它们。
|
|
35
|
+
|
|
36
|
+
/** 遍历仓库内 Markdown(排除运行产物与归档)。 */
|
|
37
|
+
export function listMarkdownFiles(root) {
|
|
38
|
+
// **枚举入库文件,而不是枚举磁盘上的文件**(2026-11-26)。
|
|
39
|
+
//
|
|
40
|
+
// `docs/STATUS.md` 是**入库的生成物**:它由本函数的输出渲染而成,又随代码一起提交。
|
|
41
|
+
// 若输入是「磁盘上有什么」,本机多出的私有文档就会进表,而 CI 上的树没有它们——
|
|
42
|
+
// 同一份 `STATUS.md` 在两台机器上算出两个指纹,`status-table-stale` 于 CI 必红、本地全绿。
|
|
43
|
+
// 3.5.3 的发布闸正是这么红的:本地 46 份 / CI 39 份,差的 7 份全是未入库的 `docs/vault-notes/vendors/*`。
|
|
44
|
+
// 判据:**生成物的输入必须可复现**——换了机器还能重算出同一张表,否则「本地绿」不保证「发得出去」。
|
|
45
|
+
const tracked = trackedMarkdown(root)
|
|
46
|
+
if (tracked) return tracked
|
|
47
|
+
const out = []
|
|
48
|
+
const walk = (dir) => {
|
|
49
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
50
|
+
const full = join(dir, entry.name)
|
|
51
|
+
if (SKIP_SEGMENTS.has(entry.name)) continue
|
|
52
|
+
if (entry.isDirectory()) walk(full)
|
|
53
|
+
else if (entry.name.toLowerCase().endsWith('.md')) out.push(full)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
walk(root)
|
|
57
|
+
return out
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 入库的 Markdown;非 git 目录(`mutate` 副本、打包后的树)返回 null,交由磁盘遍历兜底。 */
|
|
61
|
+
function trackedMarkdown(root) {
|
|
62
|
+
try {
|
|
63
|
+
const raw = execFileSync('git', ['ls-files', '-z', '*.md'], { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 })
|
|
64
|
+
return raw
|
|
65
|
+
.split('\u0000')
|
|
66
|
+
.filter(Boolean)
|
|
67
|
+
.filter((rel) => !rel.split('/').some((seg) => SKIP_SEGMENTS.has(seg)))
|
|
68
|
+
.map((rel) => join(root, rel))
|
|
69
|
+
.filter((abs) => existsSync(abs))
|
|
70
|
+
} catch {
|
|
71
|
+
return null
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文档扫描范围(2026-11-27)
|
|
3
|
+
*
|
|
4
|
+
* `docs:drift` 与 `docs:refs` 必须扫**同一份清单**:一个查「文档里写死的命令/路径是否真实」,
|
|
5
|
+
* 一个查「文档里的交叉引用是否指得到」。口径一旦分叉,同一份文档会出现
|
|
6
|
+
* 「drift 说没问题、refs 说有问题」——两边都自称权威,读者不知道该信谁。
|
|
7
|
+
*
|
|
8
|
+
* 所以清单只在这里定义一次。谁再抄一份,`smells` 的重复块棘轮会当场报出来。
|
|
9
|
+
*/
|
|
10
|
+
import { readdirSync, existsSync } from 'node:fs'
|
|
11
|
+
import { dirname, join, resolve } from 'node:path'
|
|
12
|
+
import { fileURLToPath } from 'node:url'
|
|
13
|
+
|
|
14
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
15
|
+
// 本文件在 `packages/core/tools/lib/`,比 `tools/*.mjs` 深一层——少一级就指到 `packages/core`,
|
|
16
|
+
// 扫描面当场变空,闸门却照样报绿(「扫描 0 份文档 → 问题 0 条」)。
|
|
17
|
+
export const ROOT = resolve(HERE, '..', '..', '..', '..')
|
|
18
|
+
|
|
19
|
+
/** 递归收集 md:归档与 vault 笔记是历史材料,不受当前事实约束。 */
|
|
20
|
+
export function walkDocs(dir, out = []) {
|
|
21
|
+
let entries
|
|
22
|
+
try {
|
|
23
|
+
entries = readdirSync(dir, { withFileTypes: true })
|
|
24
|
+
} catch {
|
|
25
|
+
return out
|
|
26
|
+
}
|
|
27
|
+
for (const e of entries) {
|
|
28
|
+
if (e.name === 'archive' || e.name === 'vault-notes' || e.name === 'node_modules') continue
|
|
29
|
+
const p = join(dir, e.name)
|
|
30
|
+
if (e.isDirectory()) walkDocs(p, out)
|
|
31
|
+
else if (e.name.endsWith('.md')) out.push(p)
|
|
32
|
+
}
|
|
33
|
+
return out
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 扫描面:docs 树 + 四份入口文档;`HANDOFF-*` 是过程台账,不参与当前事实判定。 */
|
|
37
|
+
export function docScope() {
|
|
38
|
+
return [
|
|
39
|
+
...walkDocs(join(ROOT, 'docs')),
|
|
40
|
+
...walkDocs(join(ROOT, 'packages/core/docs')),
|
|
41
|
+
join(ROOT, 'README.md'),
|
|
42
|
+
join(ROOT, 'INSTALL.md'),
|
|
43
|
+
join(ROOT, 'packages/core/README.md'),
|
|
44
|
+
join(ROOT, 'packages/core/README.zh-CN.md'),
|
|
45
|
+
].filter((f) => existsSync(f) && !/HANDOFF-/.test(f))
|
|
46
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文档状态总表(2026-11-19):`docs/STATUS.md` 由本文件生成,**不手写**。
|
|
3
|
+
*
|
|
4
|
+
* ## 起因
|
|
5
|
+
*
|
|
6
|
+
* `docs/INDEX.md` 曾手抄一份「状态 / 适用版本 / 最后核对」表。2026-11-19 实测
|
|
7
|
+
* **18 处与文档自身状态头冲突**——`RULES.md` 自己写 3.3.0·10-28,索引写 3.2.1·10-27;
|
|
8
|
+
* `ARCH-MAP.md` 自己写 3.5.2,索引写 3.2.1。旧判据 `checkIndexCoverage` 只查
|
|
9
|
+
* 「链接能不能点到」,**不查抄的数字对不对**,所以两份真值分叉了二十多天没人发现。
|
|
10
|
+
*
|
|
11
|
+
* 修法不是「再查一遍手抄表」,而是**取消手抄**:真值只有一个(文档自己的状态头),
|
|
12
|
+
* 总表由它生成。手写的东西会腐化,生成的东西只跟着源一起腐化。
|
|
13
|
+
*
|
|
14
|
+
* ## 落后口径
|
|
15
|
+
*
|
|
16
|
+
* `落后` 只对**权威**文档有意义——`参考` 的定义就是「有价值但可能滞后」,
|
|
17
|
+
* `台账` 是过程记录。所以该列对非权威显示 `—`。
|
|
18
|
+
*
|
|
19
|
+
* **不要求「适用版本 == 当前版本」**:文档没改过就说 3.3.0 是**真话**,
|
|
20
|
+
* 逼它改成 3.5.3 才是让闸门逼文档说谎(同 `checkVersionFace` 2026-11-05 的教训)。
|
|
21
|
+
* 本文件只**呈现**落后,不判它对错——判对错需要人。
|
|
22
|
+
*
|
|
23
|
+
* 为什么单独一份而不是塞进 `docs-core.mjs`:那个文件已 366 行,`smells` 阈值 400。
|
|
24
|
+
* 与 `docs-version.mjs` 当初从 docs-core 拆出去是同一个理由。
|
|
25
|
+
*/
|
|
26
|
+
import { readFileSync } from 'node:fs'
|
|
27
|
+
import { join, relative } from 'node:path'
|
|
28
|
+
import { listMarkdownFiles } from './docs-core.mjs'
|
|
29
|
+
import { lineAt, lineOf } from './where.mjs'
|
|
30
|
+
|
|
31
|
+
export const STATUS_DOC = 'docs/STATUS.md'
|
|
32
|
+
|
|
33
|
+
/** 解析状态头。只认前 12 行里的 `> **状态**:X · 适用版本 Y · 最后核对 Z`。 */
|
|
34
|
+
export function parseStatusHeader(text) {
|
|
35
|
+
const head = text.split(/\r?\n/).slice(0, 12).join('\n')
|
|
36
|
+
const m = head.match(/>\s*\*\*状态\*\*\s*[::]\s*([^\s·|((]+)([^\n]*)/)
|
|
37
|
+
if (!m) return null
|
|
38
|
+
const rest = m[2] || ''
|
|
39
|
+
const v = rest.match(/适用版本\s*([^\s·|]+)/)
|
|
40
|
+
const d = rest.match(/最后核对\s*(\d{4}-\d{2}-\d{2})/)
|
|
41
|
+
// 版本写法很杂:`3.3.0(4.5 前置)` / `3.3.2 / 3.5.0 / 4.0-P2` / `4.0 前置`。
|
|
42
|
+
// 只取第一个数值串;取不出 x.y.z 的一律当「无版本面」,不参与落后计算。
|
|
43
|
+
const raw = v ? v[1].split(/[((/、,,]/)[0].trim() : ''
|
|
44
|
+
const numeric = /^\d+\.\d+\.\d+$/.test(raw) ? raw : null
|
|
45
|
+
// 报点用:状态头在第几行。`docs-ack` 报「日期过期」时要指到这一行——日期就写在那儿。
|
|
46
|
+
const line = lineAt(head, m.index)
|
|
47
|
+
return { status: m[1], version: numeric, rawVersion: v ? v[1] : null, date: d ? d[1] : null, line }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 次版本号距离:`3.3.0 → 3.5.3` 得 2(跨大版本按 ×10 折算)。 */
|
|
51
|
+
function minorDistance(from, to) {
|
|
52
|
+
const a = String(from).split('.').map(Number)
|
|
53
|
+
const b = String(to).split('.').map(Number)
|
|
54
|
+
if (a.length !== 3 || b.length !== 3 || [...a, ...b].some((n) => !Number.isFinite(n))) return null
|
|
55
|
+
return (b[0] - a[0]) * 10 + (b[1] - a[1])
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const STATUS_ORDER = { 权威: 0, 参考: 1, 台账: 2, 归档: 3 }
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 收集全仓文档状态(相对路径 + 状态头 + 落后档数 + 距今天数)。
|
|
62
|
+
*
|
|
63
|
+
* **排除 `docs/STATUS.md` 自己**:表里列一行「STATUS.md,权威,落后 0」毫无信息,
|
|
64
|
+
* 却会让生成结果依赖上一次的生成结果——第一次 40 行、第二次 41 行,`checkStatusTable`
|
|
65
|
+
* 的幂等比对永远红。自引用是生成器最经典的坑,直接在源上剪掉。
|
|
66
|
+
*/
|
|
67
|
+
export function collectDocStatus(root, files = listMarkdownFiles(root), now = new Date()) {
|
|
68
|
+
const current = JSON.parse(readFileSync(join(root, 'packages/core/package.json'), 'utf8')).version
|
|
69
|
+
const rows = []
|
|
70
|
+
for (const file of files) {
|
|
71
|
+
const rel = relative(root, file).split('\\').join('/')
|
|
72
|
+
if (rel === STATUS_DOC) continue
|
|
73
|
+
const head = parseStatusHeader(readFileSync(file, 'utf8'))
|
|
74
|
+
const days = head?.date ? Math.floor((now - new Date(`${head.date}T00:00:00`)) / 86400000) : null
|
|
75
|
+
const behind = head?.version ? minorDistance(head.version, current) : null
|
|
76
|
+
rows.push({ rel, status: head?.status || null, version: head ? head.version || head.rawVersion : null, date: head?.date || null, days, behind })
|
|
77
|
+
}
|
|
78
|
+
// 权威在前;同类里落后的在前——「该先看哪份」由排序直接回答,不用读者自己找。
|
|
79
|
+
rows.sort((x, y) => {
|
|
80
|
+
const sx = STATUS_ORDER[x.status] ?? 4
|
|
81
|
+
const sy = STATUS_ORDER[y.status] ?? 4
|
|
82
|
+
if (sx !== sy) return sx - sy
|
|
83
|
+
return (y.behind ?? -1) - (x.behind ?? -1) || x.rel.localeCompare(y.rel)
|
|
84
|
+
})
|
|
85
|
+
return { current, rows }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 渲染 `docs/STATUS.md`。**纯函数**:同输入必得同输出(`checkStatusTable` 靠这条做幂等比对)。 */
|
|
89
|
+
/**
|
|
90
|
+
* 结构指纹:只收**不随「现在」变**的字段(哪份文档、状态、版本、最后核对)。
|
|
91
|
+
*
|
|
92
|
+
* 为什么要它:本表含「距今天数」和生成当天的日期,逐字比对等于**每天都必须重新生成**,
|
|
93
|
+
* 一天不生成 `status-table-stale` 就红——那正是「永久红的闸门等于没有闸门」。
|
|
94
|
+
* 判据改成比指纹:文档集合与状态头一致即可;「多久没核对」由 pre-commit 自动刷新。
|
|
95
|
+
*/
|
|
96
|
+
export function statusKey({ rows }) {
|
|
97
|
+
return rows.map((r) => [r.rel, r.status || '未标', r.version || '—', r.date || '—'].join('|')).join('\n')
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function renderStatusTable({ current, rows }) {
|
|
101
|
+
const count = (s) => rows.filter((r) => r.status === s).length
|
|
102
|
+
const out = [
|
|
103
|
+
'# 文档状态总表(自动生成)',
|
|
104
|
+
'',
|
|
105
|
+
`> **状态**:权威 · 适用版本 ${current} · 最后核对 ${new Date().toISOString().slice(0, 10)}`,
|
|
106
|
+
'',
|
|
107
|
+
'> 由 `npm run docs:status` 从各文档自己的状态头生成,**不要手改**——手改会被 `docs:lint` 判 `status-table-stale`。',
|
|
108
|
+
'> 真值只有一个:每份文档开头那行 `> **状态**:…`。本表只是把它们拼成一页。',
|
|
109
|
+
'> 「落后」= 适用版本比当前次版本号落后几档,**只对权威文档计**(参考/台账按定义可以滞后)。',
|
|
110
|
+
'> 法条(`RULES.md`)的适用版本记的是**最后一次修法**时的产品版本,长期落后是常态、不是欠债。',
|
|
111
|
+
'> 加粗 = 值得先看:落后 > 0,或最后核对已超 45 天。',
|
|
112
|
+
'',
|
|
113
|
+
`合计 ${rows.length} 份:权威 ${count('权威')} / 参考 ${count('参考')} / 台账 ${count('台账')} / 归档 ${count('归档')} / 未标 ${rows.filter((r) => !r.status).length}。`,
|
|
114
|
+
'',
|
|
115
|
+
'| 文档 | 状态 | 适用版本 | 最后核对 | 距今天数 | 落后 |',
|
|
116
|
+
'|---|---|---|---|---|---|',
|
|
117
|
+
]
|
|
118
|
+
for (const r of rows) {
|
|
119
|
+
const behind = r.status !== '权威' ? '—' : r.behind === null ? '—' : r.behind > 0 ? `**${r.behind}**` : '0'
|
|
120
|
+
const days = r.days === null ? '—' : r.days > 45 ? `**${r.days}**` : String(r.days)
|
|
121
|
+
out.push(`| [\`${r.rel}\`](../${r.rel}) | ${r.status || '未标'} | ${r.version || '—'} | ${r.date || '—'} | ${days} | ${behind} |`)
|
|
122
|
+
}
|
|
123
|
+
out.push('')
|
|
124
|
+
out.push('<!-- status-key(判据用,勿改)')
|
|
125
|
+
out.push(statusKey({ rows }))
|
|
126
|
+
out.push('-->')
|
|
127
|
+
out.push('')
|
|
128
|
+
return out.join('\n')
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 判据:`docs/STATUS.md` 的**结构指纹**必须与现算一致(不看距今天数——那个每天都变)。 */
|
|
132
|
+
export function checkStatusTable(root, files = listMarkdownFiles(root)) {
|
|
133
|
+
const target = join(root, STATUS_DOC)
|
|
134
|
+
const want = renderStatusTable(collectDocStatus(root, files))
|
|
135
|
+
let got = ''
|
|
136
|
+
try {
|
|
137
|
+
got = readFileSync(target, 'utf8')
|
|
138
|
+
} catch {
|
|
139
|
+
// 文件都不存在,没有行号可给(0 → 渲染成只有文件名)。
|
|
140
|
+
return [{ rule: 'status-table-stale', file: STATUS_DOC, line: 0, detail: '总表不存在,跑 `npm run docs:status` 生成' }]
|
|
141
|
+
}
|
|
142
|
+
const keyOf = (t) => (t.match(/<!-- status-key[^\n]*\n([\s\S]*?)\n-->/) || [, ''])[1]
|
|
143
|
+
if (keyOf(got) !== keyOf(want)) {
|
|
144
|
+
// 指到指纹块本身:重生成会整块替换,人要看的是"这里和现算不一致"。
|
|
145
|
+
return [{ rule: 'status-table-stale', file: STATUS_DOC, line: lineOf(got, '<!-- status-key'), detail: '总表与各文档状态头不一致,跑 `npm run docs:status` 重新生成' }]
|
|
146
|
+
}
|
|
147
|
+
return []
|
|
148
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 文档体例:表格单元格与加粗跨度的长度判据(2026-11-29)。
|
|
3
|
+
*
|
|
4
|
+
* ## 为什么单列一条判据
|
|
5
|
+
*
|
|
6
|
+
* 这两样不是"写得好不好",是**排版能不能读**,而且都能数出来:
|
|
7
|
+
*
|
|
8
|
+
* - **表格单元格**:`ARCH-MAP` 的状态总表曾有一个 499 字的单元格、缺口清单有 900 字的,
|
|
9
|
+
* 四列的表里塞进整段论证——横向扫不动,纵向又排不成段,两头都不占。
|
|
10
|
+
* 实测(2026-11-29):全仓 6,020 个单元格,>150 字的 68 个、>120 字 102 个。
|
|
11
|
+
* - **加粗**:全仓 4,351 处加粗、占正文 6.9% 的字数。印刷惯例是 1%–2%——
|
|
12
|
+
* 6.9% 等于"重点"覆盖了十四行里的一行。粗体超过 30 字就不再是"标一个词",
|
|
13
|
+
* 而是"把一句话涂黑",读者只能整段跳过。
|
|
14
|
+
*
|
|
15
|
+
* ## 阈值为什么是 150 / 40
|
|
16
|
+
*
|
|
17
|
+
* - 单元格 150:一屏能放下的表宽大约容纳 30–35 个汉字/列,150 字约四五行;
|
|
18
|
+
* 再长读者必然当段落读,那就不该放在表格里。
|
|
19
|
+
* - 加粗 40:这是**版心行长**(38em ≈ 38 字)留两点余量。加粗的两条界线——
|
|
20
|
+
* 「一行放得下」与「一行放不下」——是读者真的看得见的差别;
|
|
21
|
+
* 而 `**原因**` / `**判据用文件 mtime**` 这类标签最长的常见形态在 20 字内,40 已经是宽的。
|
|
22
|
+
*
|
|
23
|
+
* **量长度时不数 URL**:``[文字](超长地址)`` 在屏幕上只显示"文字",
|
|
24
|
+
* 按字符数算会把它误判成长单元格。
|
|
25
|
+
*/
|
|
26
|
+
import { readFileSync } from 'node:fs'
|
|
27
|
+
import { relative } from 'node:path'
|
|
28
|
+
|
|
29
|
+
/** 单元格上限(字)。 */
|
|
30
|
+
export const CELL_MAX = 150
|
|
31
|
+
/** 加粗跨度上限(字)。 */
|
|
32
|
+
export const BOLD_MAX = 40
|
|
33
|
+
|
|
34
|
+
/** 去掉链接目标与折行标记后按字符数计——排版占位是按显示宽度算的。 */
|
|
35
|
+
function displayLength(text) {
|
|
36
|
+
return [...text.replace(/\]\([^)]*\)/g, ']').replace(/<br\s*\/?>/gi, '')].length
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const FENCE = /^\s*(```|~~~)/
|
|
40
|
+
const TABLE_ROW = /^\s*\|/
|
|
41
|
+
const TABLE_SEP = /^\s*\|[-: |]+\|\s*$/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 只查「在用文档」(`docs/archive/` 已由 `listMarkdownFiles` 排除)。
|
|
45
|
+
* 归档是历史证据,按当年的排版原样保留才不失真。
|
|
46
|
+
*/
|
|
47
|
+
export function checkDocTypography(root, files = []) {
|
|
48
|
+
const findings = []
|
|
49
|
+
for (const abs of files) {
|
|
50
|
+
const rel = relative(root, abs).split('\\').join('/')
|
|
51
|
+
let text = ''
|
|
52
|
+
try {
|
|
53
|
+
text = readFileSync(abs, 'utf8')
|
|
54
|
+
} catch {
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
const lines = text.split('\n')
|
|
58
|
+
let inFence = false
|
|
59
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
60
|
+
const line = lines[i]
|
|
61
|
+
if (FENCE.test(line)) {
|
|
62
|
+
inFence = !inFence
|
|
63
|
+
continue
|
|
64
|
+
}
|
|
65
|
+
if (inFence) continue
|
|
66
|
+
if (TABLE_ROW.test(line) && !TABLE_SEP.test(line)) {
|
|
67
|
+
for (const cell of line.split('|').slice(1, -1)) {
|
|
68
|
+
const n = displayLength(cell.trim())
|
|
69
|
+
if (n <= CELL_MAX) continue
|
|
70
|
+
findings.push({
|
|
71
|
+
rule: 'table-cell-long',
|
|
72
|
+
file: rel,
|
|
73
|
+
line: i + 1,
|
|
74
|
+
detail: `单元格 ${n} 字(上限 ${CELL_MAX}):超出部分下沉为表后的注解条目,表内只留结论短语`,
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
for (const m of line.matchAll(/\*\*([^*\n]+)\*\*/g)) {
|
|
79
|
+
const n = displayLength(m[1])
|
|
80
|
+
if (n <= BOLD_MAX) continue
|
|
81
|
+
findings.push({
|
|
82
|
+
rule: 'bold-span-long',
|
|
83
|
+
file: rel,
|
|
84
|
+
line: i + 1,
|
|
85
|
+
detail: `加粗 ${n} 字(上限 ${BOLD_MAX}):加粗只标标签与术语,整句交给正文`,
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return findings
|
|
91
|
+
}
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
import { readFileSync } from 'node:fs'
|
|
22
22
|
import { join, relative } from 'node:path'
|
|
23
23
|
import { listMarkdownFiles, publishedMax } from './docs-core.mjs'
|
|
24
|
+
import { lineAt } from './where.mjs'
|
|
24
25
|
|
|
25
26
|
const VERSION_CLAIM_RES = [
|
|
26
27
|
[/当前(?:版本)?\s*[*`]*v?(\d+\.\d+\.\d+)/g, '当前'],
|
|
@@ -103,16 +104,18 @@ export function checkReadmeVersionFace(root, published = publishedMax(root)) {
|
|
|
103
104
|
}
|
|
104
105
|
const shown = []
|
|
105
106
|
const badge = text.match(/badge\/version-(\d+\.\d+\.\d+)/)
|
|
106
|
-
if (badge) shown.push({
|
|
107
|
+
if (badge) shown.push({ face: '版本徽章', value: badge[1], line: lineAt(text, badge.index) })
|
|
107
108
|
const head = text.match(/适用版本\s*(\d+\.\d+\.\d+)/)
|
|
108
|
-
if (head) shown.push({
|
|
109
|
+
if (head) shown.push({ face: '状态头', value: head[1], line: lineAt(text, head.index) })
|
|
109
110
|
for (const s of shown) {
|
|
110
111
|
if (allowed.has(s.value)) continue
|
|
112
|
+
// 早先 `file` 里塞的是 `README.md badge` 这种自造串——它既不是路径也不是行号,
|
|
113
|
+
// 跳不过去、也读不出原文。现在 file 是真路径,行号指到徽章/状态头那一行。
|
|
111
114
|
findings.push({
|
|
112
115
|
rule: 'version-face-stale',
|
|
113
|
-
file:
|
|
114
|
-
line:
|
|
115
|
-
detail: `${s.value}
|
|
116
|
+
file: rel,
|
|
117
|
+
line: s.line,
|
|
118
|
+
detail: `${s.face}写 ${s.value},既非已推送 ${published},也非清单 ${tree ?? '(读不到)'}`,
|
|
116
119
|
})
|
|
117
120
|
}
|
|
118
121
|
}
|