@@ -9,6 +9,48 @@ const __filename = fileURLToPath(import.meta.url);
9
9
const __dirname = path . dirname ( __filename ) ;
10
10
11
11
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 ( / r e d i r e c t s : \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
+ }
12
54
13
55
// Function to read all HTML files from a directory
14
56
function readHtmlFiles ( dir ) {
@@ -63,7 +105,7 @@ function anchorExists(html, anchor) {
63
105
}
64
106
65
107
// Function to validate links
66
- function validateLinks ( htmlFiles ) {
108
+ function validateLinks ( htmlFiles , redirectDestinations ) {
67
109
let invalidLinks = [ ] ;
68
110
69
111
htmlFiles . forEach ( ( file ) => {
@@ -74,6 +116,13 @@ function validateLinks(htmlFiles) {
74
116
// Split by the first occurrence of `#` to allow fragments with slashes
75
117
const [ pathWithoutHash , ...hashParts ] = link . split ( "#" ) ;
76
118
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
+
77
126
const resolvedPath = path . join ( distFolder , pathWithoutHash ) ;
78
127
const normalizedPath = path . normalize ( resolvedPath ) ;
79
128
const ext = path . extname ( normalizedPath ) ;
@@ -109,6 +158,9 @@ function validateLinks(htmlFiles) {
109
158
110
159
// Main function
111
160
function main ( ) {
161
+ // Get redirect destinations from astro.config.mjs
162
+ const redirectDestinations = getRedirectDestinations ( ) ;
163
+
112
164
const htmlFiles = readHtmlFiles ( distFolder ) ;
113
165
const allLinks = new Set ( ) ;
114
166
@@ -118,7 +170,7 @@ function main() {
118
170
links . forEach ( ( link ) => allLinks . add ( link ) ) ;
119
171
} ) ;
120
172
121
- const invalidLinks = validateLinks ( htmlFiles ) ;
173
+ const invalidLinks = validateLinks ( htmlFiles , redirectDestinations ) ;
122
174
123
175
if ( invalidLinks . length > 0 ) {
124
176
console . log ( chalk . red ( "Invalid Links:" ) ) ;
0 commit comments