Skip to content

Commit 66d7e38

Browse files
committed
updated breadcrumbs script
1 parent f782dab commit 66d7e38

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

utils/breadcrumbs.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,65 @@
11
import * as fs from 'fs/promises';
22
import * as path from 'path';
3+
import matter from 'gray-matter';
34

45
const rootDir = path.join(process.cwd(), 'pages');
56
const errors: string[] = [];
67

8+
interface PageInfo {
9+
title: string;
10+
url: string;
11+
}
12+
13+
async function getReferencedPages(breadcrumbContent: string): Promise<string[]> {
14+
// Extract URLs from Card components in the breadcrumb file
15+
const cardRegex = /<Card[^>]*href="([^"]+)"[^>]*>/g;
16+
const urls: string[] = [];
17+
let match;
18+
19+
while ((match = cardRegex.exec(breadcrumbContent)) !== null) {
20+
urls.push(match[1]);
21+
}
22+
23+
return urls;
24+
}
25+
726
async function checkDirectory(dirPath: string): Promise<void> {
827
const entries = await fs.readdir(dirPath, { withFileTypes: true });
928

29+
// First, find the breadcrumb file for this directory
30+
const dirName = path.basename(dirPath);
31+
const breadcrumbFile = path.join(dirPath, `${dirName}.mdx`);
32+
let referencedPages: string[] = [];
33+
34+
try {
35+
const breadcrumbContent = await fs.readFile(breadcrumbFile, 'utf-8');
36+
referencedPages = await getReferencedPages(breadcrumbContent);
37+
} catch (error) {
38+
// Only check for missing references if not in root directory
39+
if (dirPath !== rootDir) {
40+
errors.push(`Missing breadcrumb file for directory: ${path.relative(rootDir, dirPath)}`);
41+
return;
42+
}
43+
}
44+
45+
// Check each entry in the directory
1046
for (const entry of entries) {
1147
if (entry.name.startsWith('_') || entry.name.startsWith('.')) continue;
1248

1349
const fullPath = path.join(dirPath, entry.name);
1450

1551
if (entry.isDirectory()) {
16-
// Skip if it's the root directory
17-
if (fullPath === rootDir) continue;
18-
19-
// Check if breadcrumb file exists for this directory
20-
const breadcrumbFile = path.join(dirPath, `${entry.name}.mdx`);
21-
try {
22-
await fs.access(breadcrumbFile);
23-
} catch {
24-
const relativePath = path.relative(rootDir, dirPath);
25-
errors.push(`Missing breadcrumb file for directory: ${relativePath}/${entry.name}`);
26-
}
27-
2852
// Recursively check subdirectories
2953
await checkDirectory(fullPath);
54+
} else if ((entry.name.endsWith('.md') || entry.name.endsWith('.mdx')) &&
55+
entry.name !== `${dirName}.mdx`) {
56+
// Check if this page is referenced in the breadcrumb
57+
const relativePath = '/' + path.relative(rootDir, fullPath)
58+
.replace(/\.(md|mdx)$/, '');
59+
60+
if (!referencedPages.includes(relativePath)) {
61+
errors.push(`Page not referenced in breadcrumb: ${relativePath}`);
62+
}
3063
}
3164
}
3265
}
@@ -40,7 +73,7 @@ async function main() {
4073
errors.forEach(error => console.error(`- ${error}`));
4174
process.exit(1);
4275
} else {
43-
console.log('All directories have breadcrumb files.');
76+
console.log('All pages are properly referenced in breadcrumb navigation.');
4477
}
4578
} catch (error) {
4679
console.error('Error checking breadcrumbs:', error);

0 commit comments

Comments
 (0)