-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit-docs.mjs
More file actions
359 lines (306 loc) · 10.5 KB
/
audit-docs.mjs
File metadata and controls
359 lines (306 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
function argValue(name) {
const index = process.argv.indexOf(name);
if (index === -1) {
return null;
}
return process.argv[index + 1] ?? null;
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const workspaceRoot = path.resolve(__dirname, "..");
const dateStamp = new Date().toISOString().slice(0, 10);
const docsRoot = path.resolve(
argValue("--docs-root") ?? path.join(workspaceRoot, "prototypes", "docusaurus", "docs")
);
const outputPath = path.resolve(
argValue("--output") ?? path.join(workspaceRoot, `VALIDATION_REPORT_${dateStamp}.md`)
);
const failOn = argValue("--fail-on") ?? "none";
const severityRank = {
info: 0,
warn: 1,
error: 2,
};
const semverPattern = /\b(\d+)\.(\d+)\.(\d+)\b/g;
const markdownLinkPattern = /\[[^\]]+\]\(([^)]+)\)/g;
const legacyTermPattern =
/\b(webpacker|turbolinks 2|react-hot-loader|asset pipeline|rails 5\.0|react_on_rails\s+1[0-5]\.)\b/gi;
const react18Pattern = /react 18/gi;
const placeholderPattern = /\b(blah blah|TODO|TBD|xxx)\b/i;
const reactOnRailsContextPattern =
/\b(react[\s_-]?on[\s_-]?rails(?:[\s_-]?(?:pro|rsc))?|react_on_rails(?:_(?:pro|rsc))?|react-on-rails(?:-(?:pro|rsc))?)\b/i;
if (!["none", "info", "warn", "error"].includes(failOn)) {
throw new Error(`Invalid --fail-on value: ${failOn}`);
}
function compareVersionTuple(a, b) {
for (let i = 0; i < 3; i += 1) {
if (a[i] < b[i]) return -1;
if (a[i] > b[i]) return 1;
}
return 0;
}
function safeRoute(relativePath) {
return `/docs/${relativePath.replace(/\.(md|mdx)$/i, "")}`;
}
async function walkFiles(dir, callback, relativePrefix = "") {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const rel = relativePrefix ? path.join(relativePrefix, entry.name) : entry.name;
const abs = path.join(dir, entry.name);
if (entry.isDirectory()) {
await walkFiles(abs, callback, rel);
continue;
}
if (entry.isFile()) {
await callback(abs, rel);
}
}
}
function createLineInfo(content) {
const lines = content.split(/\r?\n/);
const lineInfo = [];
let inFence = false;
let fenceMarker = null;
for (const line of lines) {
const trimmed = line.trimStart();
const fenceMatch = trimmed.match(/^(```+|~~~+)/);
if (fenceMatch) {
const marker = {
char: fenceMatch[1][0],
length: fenceMatch[1].length,
};
lineInfo.push({text: line, inFence: true});
if (!inFence) {
inFence = true;
fenceMarker = marker;
} else if (marker.char === fenceMarker?.char && marker.length >= fenceMarker.length) {
inFence = false;
fenceMarker = null;
}
continue;
}
lineInfo.push({text: line, inFence});
}
return lineInfo;
}
function stripInlineCode(line) {
return line.replace(/`[^`]+`/g, "");
}
function lineNumbersForPattern(lineInfo, pattern, options = {}) {
const {includeFenced = false, transform = (line) => line} = options;
const result = [];
lineInfo.forEach(({text, inFence}, index) => {
if (!includeFenced && inFence) {
return;
}
const candidate = transform(text);
if (pattern.test(candidate)) {
result.push(index + 1);
}
pattern.lastIndex = 0;
});
return result;
}
function findVersionsBelowFloor(lineInfo, floorTuple) {
const versions = [];
for (const {text, inFence} of lineInfo) {
if (inFence || !reactOnRailsContextPattern.test(text)) {
continue;
}
let match;
semverPattern.lastIndex = 0;
while ((match = semverPattern.exec(text)) !== null) {
const tuple = [Number(match[1]), Number(match[2]), Number(match[3])];
if (compareVersionTuple(tuple, floorTuple) < 0) {
versions.push(match[0]);
}
}
}
return [...new Set(versions)];
}
function findSuspiciousLinks(lineInfo) {
const issues = [];
const content = lineInfo
.filter(({inFence}) => !inFence)
.map(({text}) => text)
.join("\n");
let match;
while ((match = markdownLinkPattern.exec(content)) !== null) {
const target = match[1].trim();
if (target.startsWith("#-")) {
issues.push(`Anchor starts with '-' (${target}).`);
}
if (target.includes("docs/release-notes/")) {
issues.push(`Likely broken relative path (${target}).`);
}
if (target.includes("www.shakacode.com/react-on-rails/docs/")) {
issues.push(`Legacy docs domain link (${target}).`);
}
if (target.includes("www.shakacode.com/react-on-rails-pro/docs/")) {
issues.push(`Legacy Pro docs domain link (${target}).`);
}
}
return [...new Set(issues)];
}
function addFinding(findings, severity, message) {
findings.push({severity, message});
}
function evaluatePage(relativePath, content) {
const lineInfo = createLineInfo(content);
const findings = [];
const firstContentLine = lineInfo.find(({text}) => text.trim().length > 0)?.text ?? "";
const isReleaseNotes = relativePath.includes("release-notes/");
if (!firstContentLine.startsWith("# ")) {
addFinding(findings, "warn", "Missing top-level `#` heading; normalize to a single H1.");
}
const lowVersions = isReleaseNotes ? [] : findVersionsBelowFloor(lineInfo, [16, 4, 0]);
if (lowVersions.length > 0) {
addFinding(
findings,
"info",
`Mentions React on Rails versions below 16.4.0 (${lowVersions.slice(0, 4).join(", ")}); verify these are intentionally historical.`
);
}
const suspiciousLinks = findSuspiciousLinks(lineInfo);
for (const issue of suspiciousLinks) {
addFinding(findings, "error", issue);
}
const proseContent = lineInfo
.filter(({inFence}) => !inFence)
.map(({text}) => text)
.join("\n");
const legacyMentions = (proseContent.match(legacyTermPattern) ?? []).length;
if (legacyMentions >= 3) {
addFinding(
findings,
"info",
"Heavy legacy terminology detected; consider archiving or adding a legacy warning."
);
}
const react18Mentions = lineNumbersForPattern(lineInfo, react18Pattern);
if (react18Mentions.length > 0 && !isReleaseNotes) {
addFinding(
findings,
"warn",
`Mentions React 18 on line(s) ${react18Mentions.slice(0, 5).join(", ")}; confirm whether this should now say React 19.`
);
}
const placeholderLines = lineNumbersForPattern(lineInfo, placeholderPattern, {
transform: stripInlineCode,
});
if (placeholderLines.length > 0) {
addFinding(
findings,
"error",
`Placeholder text found on line(s) ${placeholderLines.join(", ")}.`
);
}
if (lineInfo.length > 450 && !isReleaseNotes) {
addFinding(findings, "info", "Long page (>450 lines); consider splitting for navigability.");
}
return {
relativePath: relativePath.split(path.sep).join("/"),
route: safeRoute(relativePath.split(path.sep).join("/")),
findings,
};
}
function findingSummary(pages) {
const findings = pages.flatMap((page) => page.findings);
const counts = {
error: findings.filter((finding) => finding.severity === "error").length,
warn: findings.filter((finding) => finding.severity === "warn").length,
info: findings.filter((finding) => finding.severity === "info").length,
};
return {
counts,
pageCounts: {
withFindings: pages.filter((page) => page.findings.length > 0).length,
withErrors: pages.filter((page) => page.findings.some((finding) => finding.severity === "error")).length,
withWarnings: pages.filter((page) => page.findings.some((finding) => finding.severity === "warn")).length,
withInfo: pages.filter((page) => page.findings.some((finding) => finding.severity === "info")).length,
},
};
}
function buildReport(pages) {
const total = pages.length;
const clean = total - pages.filter((page) => page.findings.length > 0).length;
const generatedAt = new Date().toISOString();
const relativeDocsRoot = path.relative(workspaceRoot, docsRoot).split(path.sep).join("/") || ".";
const summary = findingSummary(pages);
const header = [
"# Docs Validation Report",
"",
`Generated: ${generatedAt}`,
`Docs root: \`${relativeDocsRoot}\``,
`Pages scanned: ${total}`,
`Pages with findings: ${summary.pageCounts.withFindings}`,
`Pages clean: ${clean}`,
`Pages with errors: ${summary.pageCounts.withErrors}`,
`Pages with warnings: ${summary.pageCounts.withWarnings}`,
`Pages with info: ${summary.pageCounts.withInfo}`,
`Errors: ${summary.counts.error}`,
`Warnings: ${summary.counts.warn}`,
`Info: ${summary.counts.info}`,
"",
"## Page-by-page comments",
"",
];
const body = pages
.map((page) => {
const lines = [`### ${page.relativePath}`, `Route: \`${page.route}\``];
if (page.findings.length === 0) {
lines.push("- `[ok]` No findings detected.");
} else {
for (const finding of page.findings) {
lines.push(`- \`[${finding.severity}]\` ${finding.message}`);
}
}
lines.push("");
return lines.join("\n");
})
.join("\n");
return `${header.join("\n")}\n${body}`;
}
function shouldFailAudit(pages) {
if (failOn === "none") {
return false;
}
const threshold = severityRank[failOn];
return pages.some((page) =>
page.findings.some((finding) => severityRank[finding.severity] >= threshold)
);
}
async function main() {
const pages = [];
await walkFiles(docsRoot, async (absolutePath, relativePath) => {
if (!relativePath.endsWith(".md") && !relativePath.endsWith(".mdx")) {
return;
}
const content = await fs.readFile(absolutePath, "utf8");
pages.push(evaluatePage(relativePath, content));
});
pages.sort((a, b) => a.relativePath.localeCompare(b.relativePath));
const report = buildReport(pages);
const summary = findingSummary(pages);
await fs.writeFile(outputPath, report, "utf8");
console.log(`Wrote docs report: ${outputPath}`);
console.log(`Pages scanned: ${pages.length}`);
console.log(`Pages with findings: ${summary.pageCounts.withFindings}`);
console.log(`Pages with errors: ${summary.pageCounts.withErrors}`);
console.log(`Pages with warnings: ${summary.pageCounts.withWarnings}`);
console.log(`Pages with info: ${summary.pageCounts.withInfo}`);
console.log(`Errors: ${summary.counts.error}`);
console.log(`Warnings: ${summary.counts.warn}`);
console.log(`Info: ${summary.counts.info}`);
if (shouldFailAudit(pages)) {
console.error(`Failing docs audit because ${failOn}+ findings were detected.`);
process.exitCode = 1;
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});