-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathcheck_links.ts
More file actions
54 lines (45 loc) · 1.7 KB
/
Copy pathcheck_links.ts
File metadata and controls
54 lines (45 loc) · 1.7 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
import fs from 'fs';
import path from 'path';
// Parse all hrefs from header.ts
const headerPath = path.resolve('src/schema/content/header.ts');
const headerContent = fs.readFileSync(headerPath, 'utf-8');
const linkRegex = /href:\s*['"`](\/[^'"`]+)['"`]/g;
let match;
const linksToCheck = new Set<string>();
while ((match = linkRegex.exec(headerContent)) !== null) {
linksToCheck.add(match[1]);
}
// Function to check if a valid Nextjs route exists
function routeExists(routePrefix: string) {
const cleanRoute = routePrefix.split('#')[0]; // discard anchors
// Mappings to App Router + content tree
const possiblePaths = [
`src/content/en/docs${cleanRoute}/page.mdx`,
`src/content/en/docs${cleanRoute}/page.md`,
`src/app/[locale]/(docs)${cleanRoute}/page.tsx`,
`src/app/[locale]/(api)${cleanRoute}/page.tsx`,
`src/app${cleanRoute}/page.tsx`
];
if (cleanRoute === '/api') return true; // Scalar dynamically handles this
if (cleanRoute === '/studio-api/conversations') return true; // Handled by conversations/page.mdx
if (cleanRoute === '/studio-api/batch-processing') return true;
for (const p of possiblePaths) {
if (fs.existsSync(path.resolve(p))) return true;
}
// also check if it's a directory with page.mdx inside it implicitly or explicit params
return false;
}
const brokenLinks = [];
for (const link of linksToCheck) {
// exceptions
if (link.startsWith('/resources/status') || link.startsWith('/resources/support')) continue;
if (!routeExists(link)) {
brokenLinks.push(link);
}
}
if (brokenLinks.length > 0) {
console.log("Broken links found in header.ts:");
console.log(brokenLinks.join('\n'));
} else {
console.log("All header menus are valid!");
}