-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathcheck-internal-links.js
More file actions
308 lines (255 loc) · 7.96 KB
/
check-internal-links.js
File metadata and controls
308 lines (255 loc) · 7.96 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
const path = require('path');
const fs = require('fs');
const { sync: glob } = require('glob');
const DOCS_DIR = './docs';
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
const ASSET_EXTENSIONS = new Set([
'png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'ico',
'pdf',
'zip', 'tar', 'gz', 'tgz',
'mp4', 'mov', 'webm',
'css', 'js', 'map',
]);
function normalizeRoute(p) {
if (!p) return '/';
p = String(p).trim();
// Convert Windows separators to URL separators
p = p.replace(/\\/g, '/');
// Ensure leading slash
if (!p.startsWith('/')) p = '/' + p;
// Collapse duplicate slashes (keep a single leading slash)
p = p.replace(/\/{2,}/g, '/');
// Remove trailing slash (except for root)
if (p.length > 1 && p.endsWith('/')) p = p.slice(0, -1);
return p;
}
function buildValidRoutes(files) {
const validRoutes = new Set();
const canonicalByLower = new Map(); // lower -> canonical (for case-mismatch detection)
for (const file of files) {
// docs/cody/capabilities/chat.mdx → /cody/capabilities/chat
// docs/cody/index.mdx → /cody
// docs/index.mdx → /
let route = file
.replace(/(^|\/)index\.mdx?$/, '') // handles "index.mdx" and "foo/index.mdx"
.replace(/\.mdx?$/, '');
route = normalizeRoute(route);
validRoutes.add(route);
const lower = route.toLowerCase();
if (!canonicalByLower.has(lower)) {
canonicalByLower.set(lower, route);
}
}
return { validRoutes, canonicalByLower };
}
function extractLinks(content) {
const links = [];
// Match various link patterns in MDX
const patterns = [
/\]\((\/[^)\s"'`]+)(?:\s+["'][^"']*["'])?\)/g, // Markdown: [text](/path) or [text](/path "title")
/\bhref\s*=\s*["'](\/[^"']+)["']/g, // JSX: href="/path"
/\bhref\s*=\s*\{\s*["'](\/[^"']+)["']\s*\}/g, // JSX: href={'/path'}
/\bto\s*=\s*["'](\/[^"']+)["']/g, // <Link to="/path">
/\bto\s*=\s*\{\s*["'](\/[^"']+)["']\s*\}/g, // <Link to={'/path'}>
];
// Reference-style link definitions: [ref]: /path
const refLinkPattern = /^\s*\[[^\]]+\]:\s*(\/\S+)/gm;
const lines = content.split('\n');
let inFence = false;
lines.forEach((line, lineIndex) => {
// Skip code fences
if (/^\s*```/.test(line)) {
inFence = !inFence;
return;
}
if (inFence) return;
// Check for reference-style link definitions
let refMatch;
refLinkPattern.lastIndex = 0;
while ((refMatch = refLinkPattern.exec(line)) !== null) {
const fullLink = refMatch[1];
const cleanLink = normalizeRoute(fullLink.split('#')[0].split('?')[0]);
if (!isAssetLink(cleanLink) && !cleanLink.startsWith('/api/')) {
links.push({ link: cleanLink, line: lineIndex + 1 });
}
}
for (const pattern of patterns) {
pattern.lastIndex = 0; // Reset regex state
let match;
while ((match = pattern.exec(line)) !== null) {
const fullLink = match[1];
// Strip anchor and query params for validation
const cleanLink = normalizeRoute(fullLink.split('#')[0].split('?')[0]);
// Skip API routes
if (cleanLink.startsWith('/api/')) {
continue;
}
// Skip asset files (using allowlist)
if (isAssetLink(cleanLink)) {
continue;
}
links.push({
link: cleanLink,
line: lineIndex + 1
});
}
}
});
return links;
}
function isAssetLink(cleanLink) {
const lastSegment = cleanLink.split('/').pop() || '';
const m = lastSegment.match(/\.([a-z0-9]+)$/i);
return m && ASSET_EXTENSIONS.has(m[1].toLowerCase());
}
function findBrokenLinks(files, validRoutes, canonicalByLower) {
const brokenLinks = [];
for (const file of files) {
const filePath = path.join(DOCS_DIR, file);
const content = fs.readFileSync(filePath, 'utf-8');
const links = extractLinks(content);
for (const { link, line } of links) {
// Exact match - link is valid
if (validRoutes.has(link)) continue;
// Check for case mismatch (works on macOS/Windows but fails on Linux CI)
const canonical = canonicalByLower.get(link.toLowerCase());
if (canonical) {
brokenLinks.push({
file: filePath,
line,
link,
suggestion: canonical,
isCaseMismatch: true
});
continue;
}
// Truly broken link
brokenLinks.push({ file: filePath, line, link });
}
}
return brokenLinks;
}
async function sendToSlack(brokenLinks, totalFiles) {
if (!SLACK_WEBHOOK_URL) {
console.log('No SLACK_WEBHOOK_URL provided, printing results locally:');
console.log(`Total docs scanned: ${totalFiles}`);
console.log(`Broken links found: ${brokenLinks.length}`);
if (brokenLinks.length > 0) {
console.log('\nBroken links:');
brokenLinks.forEach(({ file, line, link, suggestion, isCaseMismatch }) => {
if (isCaseMismatch) {
console.log(` ${file}:${line} → ${link} (case mismatch, did you mean ${suggestion}?)`);
} else {
console.log(` ${file}:${line} → ${link}`);
}
});
}
return;
}
// Guard for older Node versions
if (typeof fetch !== 'function') {
throw new Error('Global fetch is not available. Use Node 18+ or add a fetch polyfill.');
}
const date = new Date().toISOString().split('T')[0];
const caseMismatches = brokenLinks.filter(b => b.isCaseMismatch);
const trulyBroken = brokenLinks.filter(b => !b.isCaseMismatch);
const blocks = [
{
type: 'header',
text: {
type: 'plain_text',
text: `📊 Daily Docs Link Check - ${date}`,
emoji: true
}
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Total docs scanned:* ${totalFiles}\n*Broken links:* ${trulyBroken.length}\n*Case mismatches:* ${caseMismatches.length}`
}
}
];
if (trulyBroken.length > 0) {
// Group by file for cleaner output
const grouped = {};
for (const { file, line, link } of trulyBroken) {
if (!grouped[file]) grouped[file] = [];
grouped[file].push({ line, link });
}
let detailText = '*Broken links:*\n';
const entries = Object.entries(grouped).slice(0, 10); // Limit to 10 files
for (const [file, links] of entries) {
const shortFile = file.replace('./docs/', '');
detailText += `*${shortFile}*\n`;
for (const { line, link } of links.slice(0, 5)) { // Limit to 5 links per file
detailText += ` • Line ${line}: \`${link}\`\n`;
}
if (links.length > 5) {
detailText += ` • ... and ${links.length - 5} more\n`;
}
}
if (Object.keys(grouped).length > 10) {
detailText += `\n_... and ${Object.keys(grouped).length - 10} more files with broken links_`;
}
blocks.push({
type: 'section',
text: {
type: 'mrkdwn',
text: detailText
}
});
}
if (caseMismatches.length > 0 && caseMismatches.length <= 20) {
let caseText = '*Case mismatches (will fail on Linux CI):*\n';
for (const { file, line, link, suggestion } of caseMismatches.slice(0, 10)) {
const shortFile = file.replace('./docs/', '');
caseText += `• \`${shortFile}:${line}\`: \`${link}\` → \`${suggestion}\`\n`;
}
if (caseMismatches.length > 10) {
caseText += `_... and ${caseMismatches.length - 10} more_`;
}
blocks.push({
type: 'section',
text: {
type: 'mrkdwn',
text: caseText
}
});
}
if (brokenLinks.length === 0) {
blocks.push({
type: 'section',
text: {
type: 'mrkdwn',
text: '✅ No broken internal links found!'
}
});
}
const response = await fetch(SLACK_WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ blocks })
});
if (!response.ok) {
throw new Error(`Slack webhook failed: ${response.status}`);
}
console.log('Report sent to Slack');
}
async function main() {
// Glob once and reuse
const files = glob('**/*.{md,mdx}', {
cwd: DOCS_DIR,
ignore: ['node_modules/**']
});
console.log('Building valid routes...');
const { validRoutes, canonicalByLower } = buildValidRoutes(files);
console.log(`Found ${validRoutes.size} valid routes`);
console.log('Scanning for broken links...');
const brokenLinks = findBrokenLinks(files, validRoutes, canonicalByLower);
await sendToSlack(brokenLinks, files.length);
}
main().catch(err => {
console.error('Error:', err.message);
process.exit(1);
});