-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathdeadLinkChecker.js
More file actions
460 lines (410 loc) · 12.6 KB
/
deadLinkChecker.js
File metadata and controls
460 lines (410 loc) · 12.6 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const globby = require('globby');
const chalk = require('chalk');
const CONTENT_DIR = path.join(__dirname, '../src/content');
const PUBLIC_DIR = path.join(__dirname, '../public');
const fileCache = new Map();
const anchorMap = new Map(); // Map<filepath, Set<anchorId>>
const contributorMap = new Map(); // Map<anchorId, URL>
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
const redirectMap = new Map(); // Map<source, destination>
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
const redirectMap = new Map(); // Map<source, destination>
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
const redirectMap = new Map(); // Map<source, destination>
>>>>>>> e9a7cb1b6ca1659b42d81555ecef0cd554b7a983
let errorCodes = new Set();
async function readFileWithCache(filePath) {
if (!fileCache.has(filePath)) {
try {
const content = await fs.promises.readFile(filePath, 'utf8');
fileCache.set(filePath, content);
} catch (error) {
throw new Error(`Failed to read file ${filePath}: ${error.message}`);
}
}
return fileCache.get(filePath);
}
async function fileExists(filePath) {
try {
await fs.promises.access(filePath, fs.constants.R_OK);
return true;
} catch {
return false;
}
}
function getMarkdownFiles() {
// Convert Windows paths to POSIX for globby compatibility
const baseDir = CONTENT_DIR.replace(/\\/g, '/');
const patterns = [
path.posix.join(baseDir, '**/*.md'),
path.posix.join(baseDir, '**/*.mdx'),
];
return globby.sync(patterns);
}
function extractAnchorsFromContent(content) {
const anchors = new Set();
// MDX-style heading IDs: {/*anchor-id*/}
const mdxPattern = /\{\/\*([a-zA-Z0-9-_]+)\*\/\}/g;
let match;
while ((match = mdxPattern.exec(content)) !== null) {
anchors.add(match[1].toLowerCase());
}
// HTML id attributes
const htmlIdPattern = /\sid=["']([a-zA-Z0-9-_]+)["']/g;
while ((match = htmlIdPattern.exec(content)) !== null) {
anchors.add(match[1].toLowerCase());
}
// Markdown heading with explicit ID: ## Heading {#anchor-id}
const markdownHeadingPattern = /^#+\s+.*\{#([a-zA-Z0-9-_]+)\}/gm;
while ((match = markdownHeadingPattern.exec(content)) !== null) {
anchors.add(match[1].toLowerCase());
}
return anchors;
}
async function buildAnchorMap(files) {
for (const filePath of files) {
const content = await readFileWithCache(filePath);
const anchors = extractAnchorsFromContent(content);
if (anchors.size > 0) {
anchorMap.set(filePath, anchors);
}
}
}
function extractLinksFromContent(content) {
const linkPattern = /\[([^\]]*)\]\(([^)]+)\)/g;
const links = [];
let match;
while ((match = linkPattern.exec(content)) !== null) {
const [, linkText, linkUrl] = match;
if (linkUrl.startsWith('/') && !linkUrl.startsWith('//')) {
const lines = content.substring(0, match.index).split('\n');
const line = lines.length;
const lastLineStart =
lines.length > 1 ? content.lastIndexOf('\n', match.index - 1) + 1 : 0;
const column = match.index - lastLineStart + 1;
links.push({
text: linkText,
url: linkUrl,
line,
column,
});
}
}
return links;
}
async function findTargetFile(urlPath) {
// Check if it's an image or static asset that might be in the public directory
const imageExtensions = [
'.png',
'.jpg',
'.jpeg',
'.gif',
'.svg',
'.ico',
'.webp',
];
const hasImageExtension = imageExtensions.some((ext) =>
urlPath.toLowerCase().endsWith(ext)
);
if (hasImageExtension || urlPath.includes('.')) {
// Check in public directory (with and without leading slash)
const publicPaths = [
path.join(PUBLIC_DIR, urlPath),
path.join(PUBLIC_DIR, urlPath.substring(1)),
];
for (const p of publicPaths) {
if (await fileExists(p)) {
return p;
}
}
}
const possiblePaths = [
path.join(CONTENT_DIR, urlPath + '.md'),
path.join(CONTENT_DIR, urlPath + '.mdx'),
path.join(CONTENT_DIR, urlPath, 'index.md'),
path.join(CONTENT_DIR, urlPath, 'index.mdx'),
// Without leading slash
path.join(CONTENT_DIR, urlPath.substring(1) + '.md'),
path.join(CONTENT_DIR, urlPath.substring(1) + '.mdx'),
path.join(CONTENT_DIR, urlPath.substring(1), 'index.md'),
path.join(CONTENT_DIR, urlPath.substring(1), 'index.mdx'),
];
for (const p of possiblePaths) {
if (await fileExists(p)) {
return p;
}
}
return null;
}
async function validateLink(link) {
const urlAnchorPattern = /#([a-zA-Z0-9-_]+)$/;
const anchorMatch = link.url.match(urlAnchorPattern);
const urlWithoutAnchor = link.url.replace(urlAnchorPattern, '');
if (urlWithoutAnchor === '/') {
return {valid: true};
}
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
=======
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
>>>>>>> e9a7cb1b6ca1659b42d81555ecef0cd554b7a983
// Check for redirects
if (redirectMap.has(urlWithoutAnchor)) {
const redirectDestination = redirectMap.get(urlWithoutAnchor);
if (
redirectDestination.startsWith('http://') ||
redirectDestination.startsWith('https://')
) {
return {valid: true};
}
const redirectedLink = {
...link,
url: redirectDestination + (anchorMatch ? anchorMatch[0] : ''),
};
return validateLink(redirectedLink);
}
<<<<<<< HEAD
<<<<<<< HEAD
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
>>>>>>> e9a7cb1b6ca1659b42d81555ecef0cd554b7a983
// Check if it's an error code link
const errorCodeMatch = urlWithoutAnchor.match(/^\/errors\/(\d+)$/);
if (errorCodeMatch) {
const code = errorCodeMatch[1];
if (!errorCodes.has(code)) {
return {
valid: false,
reason: `Error code ${code} not found in React error codes`,
};
}
return {valid: true};
}
// Check if it's a contributor link on the team or acknowledgements page
if (
anchorMatch &&
(urlWithoutAnchor === '/community/team' ||
urlWithoutAnchor === '/community/acknowledgements')
) {
const anchorId = anchorMatch[1].toLowerCase();
if (contributorMap.has(anchorId)) {
const correctUrl = contributorMap.get(anchorId);
if (correctUrl !== link.url) {
return {
valid: false,
reason: `Contributor link should be updated to: ${correctUrl}`,
};
}
return {valid: true};
} else {
return {
valid: false,
reason: `Contributor link not found`,
};
}
}
const targetFile = await findTargetFile(urlWithoutAnchor);
if (!targetFile) {
return {
valid: false,
reason: `Target file not found for: ${urlWithoutAnchor}`,
};
}
// Only check anchors for content files, not static assets
if (anchorMatch && targetFile.startsWith(CONTENT_DIR)) {
const anchorId = anchorMatch[1].toLowerCase();
// TODO handle more special cases. These are usually from custom MDX components that include
// a Heading from src/components/MDX/Heading.tsx which automatically injects an anchor tag.
switch (anchorId) {
case 'challenges':
case 'recap': {
return {valid: true};
}
}
const fileAnchors = anchorMap.get(targetFile);
if (!fileAnchors || !fileAnchors.has(anchorId)) {
return {
valid: false,
reason: `Anchor #${anchorMatch[1]} not found in ${path.relative(
CONTENT_DIR,
targetFile
)}`,
};
}
}
return {valid: true};
}
async function processFile(filePath) {
const content = await readFileWithCache(filePath);
const links = extractLinksFromContent(content);
const deadLinks = [];
for (const link of links) {
const result = await validateLink(link);
if (!result.valid) {
deadLinks.push({
file: path.relative(process.cwd(), filePath),
line: link.line,
column: link.column,
text: link.text,
url: link.url,
reason: result.reason,
});
}
}
return {deadLinks, totalLinks: links.length};
}
async function buildContributorMap() {
const teamFile = path.join(CONTENT_DIR, 'community/team.md');
const teamContent = await readFileWithCache(teamFile);
const teamMemberPattern = /<TeamMember[^>]*permalink=["']([^"']+)["']/g;
let match;
while ((match = teamMemberPattern.exec(teamContent)) !== null) {
const permalink = match[1];
contributorMap.set(permalink, `/community/team#${permalink}`);
}
const ackFile = path.join(CONTENT_DIR, 'community/acknowledgements.md');
const ackContent = await readFileWithCache(ackFile);
const contributorPattern = /\*\s*\[([^\]]+)\]\(([^)]+)\)/g;
while ((match = contributorPattern.exec(ackContent)) !== null) {
const name = match[1];
const url = match[2];
const hyphenatedName = name.toLowerCase().replace(/\s+/g, '-');
if (!contributorMap.has(hyphenatedName)) {
contributorMap.set(hyphenatedName, url);
}
}
}
async function fetchErrorCodes() {
try {
const response = await fetch(
'https://raw.githubusercontent.com/facebook/react/main/scripts/error-codes/codes.json'
);
if (!response.ok) {
throw new Error(`Failed to fetch error codes: ${response.status}`);
}
const codes = await response.json();
errorCodes = new Set(Object.keys(codes));
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
console.log(chalk.gray(`Fetched ${errorCodes.size} React error codes\n`));
=======
console.log(chalk.gray(`Fetched ${errorCodes.size} React error codes`));
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
console.log(chalk.gray(`Fetched ${errorCodes.size} React error codes`));
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
console.log(chalk.gray(`Fetched ${errorCodes.size} React error codes`));
>>>>>>> e9a7cb1b6ca1659b42d81555ecef0cd554b7a983
} catch (error) {
throw new Error(`Failed to fetch error codes: ${error.message}`);
}
}
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
=======
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
>>>>>>> e9a7cb1b6ca1659b42d81555ecef0cd554b7a983
async function buildRedirectsMap() {
try {
const vercelConfigPath = path.join(__dirname, '../vercel.json');
const vercelConfig = JSON.parse(
await fs.promises.readFile(vercelConfigPath, 'utf8')
);
if (vercelConfig.redirects) {
for (const redirect of vercelConfig.redirects) {
redirectMap.set(redirect.source, redirect.destination);
}
console.log(
chalk.gray(`Loaded ${redirectMap.size} redirects from vercel.json`)
);
}
} catch (error) {
console.log(
chalk.yellow(
`Warning: Could not load redirects from vercel.json: ${error.message}\n`
)
);
}
}
<<<<<<< HEAD
<<<<<<< HEAD
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
>>>>>>> e9a7cb1b6ca1659b42d81555ecef0cd554b7a983
async function main() {
const files = getMarkdownFiles();
console.log(chalk.gray(`Checking ${files.length} markdown files...`));
await fetchErrorCodes();
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
await buildRedirectsMap();
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
await buildRedirectsMap();
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
await buildRedirectsMap();
>>>>>>> e9a7cb1b6ca1659b42d81555ecef0cd554b7a983
await buildContributorMap();
await buildAnchorMap(files);
const filePromises = files.map((filePath) => processFile(filePath));
const results = await Promise.all(filePromises);
const deadLinks = results.flatMap((r) => r.deadLinks);
const totalLinks = results.reduce((sum, r) => sum + r.totalLinks, 0);
if (deadLinks.length > 0) {
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
console.log('\n');
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
console.log('\n');
>>>>>>> e07ac94bc2c1ffd817b13930977be93325e5bea9
=======
console.log('\n');
>>>>>>> e9a7cb1b6ca1659b42d81555ecef0cd554b7a983
for (const link of deadLinks) {
console.log(chalk.yellow(`${link.file}:${link.line}:${link.column}`));
console.log(chalk.reset(` Link text: ${link.text}`));
console.log(chalk.reset(` URL: ${link.url}`));
console.log(` ${chalk.red('✗')} ${chalk.red(link.reason)}\n`);
}
console.log(
chalk.red(
`\nFound ${deadLinks.length} dead link${
deadLinks.length > 1 ? 's' : ''
} out of ${totalLinks} total links\n`
)
);
process.exit(1);
}
console.log(chalk.green(`\n✓ All ${totalLinks} links are valid!\n`));
process.exit(0);
}
main().catch((error) => {
console.log(chalk.red(`Error: ${error.message}`));
process.exit(1);
});