Skip to content

Commit 6407f68

Browse files
committed
Update link validation script to skip redirects
1 parent 9ae41e6 commit 6407f68

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

scripts/validate-links.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,48 @@ const __filename = fileURLToPath(import.meta.url);
99
const __dirname = path.dirname(__filename);
1010

1111
const distFolder = path.join(__dirname, "../dist");
12+
const astroConfigPath = path.join(__dirname, "../astro.config.mjs");
13+
14+
// Function to extract redirect destinations from astro.config.mjs
15+
function getRedirectDestinations() {
16+
try {
17+
const configContent = fs.readFileSync(astroConfigPath, "utf-8");
18+
19+
// Extract the redirects object using regex
20+
const redirectsMatch = configContent.match(/redirects:\s*{([^}]+)}/s);
21+
if (!redirectsMatch) {
22+
console.log(chalk.yellow("No redirects found in astro.config.mjs"));
23+
return [];
24+
}
25+
26+
const redirectsSection = redirectsMatch[1];
27+
28+
// Extract all destination URLs (the values after the colons)
29+
const destinationMatches = redirectsSection.match(/:\s*"([^"]+)"/g);
30+
if (!destinationMatches) {
31+
console.log(chalk.yellow("No redirect destinations found in astro.config.mjs"));
32+
return [];
33+
}
34+
35+
// Clean up the matches to get just the URLs
36+
const destinations = destinationMatches.map(match => {
37+
// Remove the ': "' prefix and '"' suffix
38+
return match.replace(/:\s*"/, '').replace(/"$/, '');
39+
});
40+
41+
console.log(chalk.blue(`Found ${destinations.length} redirect destinations`));
42+
return destinations;
43+
44+
} catch (error) {
45+
console.error(chalk.red(`Error reading astro.config.mjs: ${error.message}`));
46+
return [];
47+
}
48+
}
49+
50+
// Function to check if a URL is a redirect destination
51+
function isRedirectDestination(url, redirectDestinations) {
52+
return redirectDestinations.includes(url);
53+
}
1254

1355
// Function to read all HTML files from a directory
1456
function readHtmlFiles(dir) {
@@ -63,7 +105,7 @@ function anchorExists(html, anchor) {
63105
}
64106

65107
// Function to validate links
66-
function validateLinks(htmlFiles) {
108+
function validateLinks(htmlFiles, redirectDestinations) {
67109
let invalidLinks = [];
68110

69111
htmlFiles.forEach((file) => {
@@ -74,6 +116,13 @@ function validateLinks(htmlFiles) {
74116
// Split by the first occurrence of `#` to allow fragments with slashes
75117
const [pathWithoutHash, ...hashParts] = link.split("#");
76118
const hash = hashParts.length ? hashParts.join("#") : null;
119+
120+
// Check if this is a redirect destination - if so, skip validation
121+
if (isRedirectDestination(pathWithoutHash, redirectDestinations)) {
122+
console.log(chalk.yellow(`Skipping redirect destination: ${pathWithoutHash}`));
123+
return;
124+
}
125+
77126
const resolvedPath = path.join(distFolder, pathWithoutHash);
78127
const normalizedPath = path.normalize(resolvedPath);
79128
const ext = path.extname(normalizedPath);
@@ -109,6 +158,9 @@ function validateLinks(htmlFiles) {
109158

110159
// Main function
111160
function main() {
161+
// Get redirect destinations from astro.config.mjs
162+
const redirectDestinations = getRedirectDestinations();
163+
112164
const htmlFiles = readHtmlFiles(distFolder);
113165
const allLinks = new Set();
114166

@@ -118,7 +170,7 @@ function main() {
118170
links.forEach((link) => allLinks.add(link));
119171
});
120172

121-
const invalidLinks = validateLinks(htmlFiles);
173+
const invalidLinks = validateLinks(htmlFiles, redirectDestinations);
122174

123175
if (invalidLinks.length > 0) {
124176
console.log(chalk.red("Invalid Links:"));

0 commit comments

Comments
 (0)