@@ -4,85 +4,85 @@ import core from "@actions/core";
4
4
const navigationTimeout = 120000 ; // Set the navigation timeout to 120 seconds (120,000 milliseconds)
5
5
6
6
function arrayToHTMLList ( array ) {
7
- let html = "<ul>" ;
7
+ let html = "<ul>" ;
8
8
9
- for ( let i = 0 ; i < array . length ; i ++ ) {
10
- html += "<li>" + array [ i ] + "</li>" ;
11
- }
9
+ for ( let i = 0 ; i < array . length ; i ++ ) {
10
+ html += "<li>" + array [ i ] + "</li>" ;
11
+ }
12
12
13
- html += "</ul>" ;
13
+ html += "</ul>" ;
14
14
15
- return html ;
15
+ return html ;
16
16
}
17
17
18
18
async function checkLinks ( ) {
19
- const browser = await puppeteer . launch ( {
20
- headless : "new" ,
21
- } ) ;
22
- const page = await browser . newPage ( ) ;
23
-
24
- const sitemapUrl = "https://developers.cloudflare.com/sitemap.xml" ;
25
- await page . goto ( sitemapUrl , { timeout : navigationTimeout } ) ;
26
-
27
- const sitemapLinks = await page . $$eval ( "url loc" , ( elements ) =>
28
- elements . map ( ( el ) => el . textContent )
29
- ) ;
30
-
31
- const visitedLinks = [ ] ;
32
- const brokenLinks = [ ] ;
33
-
34
- for ( const link of sitemapLinks ) {
35
- if ( ! link ) {
36
- continue ; // Skip if the link is empty
37
- }
38
-
39
- await page . goto ( link , {
40
- waitUntil : "networkidle0" ,
41
- timeout : navigationTimeout ,
42
- } ) ;
43
-
44
- const pageLinks = await page . $$eval ( "a" , ( elements ) =>
45
- elements . map ( ( el ) => el . href )
46
- ) ;
47
-
48
- for ( const pageLink of pageLinks ) {
49
- if ( ! pageLink || visitedLinks . includes ( pageLink ) ) {
50
- continue ; // Skip if the pageLink is empty or has already been visited
51
- }
52
-
53
- if (
54
- pageLink . includes ( "developers.cloudflare.com/api/operations/" ) ||
55
- pageLink . startsWith ( "/api/operations/" )
56
- ) {
57
- console . log ( `Evaluating link: ${ pageLink } ` ) ;
58
- await page . goto ( pageLink , {
59
- waitUntil : "networkidle0" ,
60
- timeout : navigationTimeout ,
61
- } ) ;
62
- visitedLinks . push ( pageLink ) ;
63
-
64
- const statusCode = await page . evaluate ( ( ) => {
65
- return {
66
- url : window . location . href ,
67
- } ;
68
- } ) ;
69
- if ( statusCode . url === "https://developers.cloudflare.com/api/" ) {
70
- brokenLinks . push ( pageLink ) ;
71
- }
72
- }
73
- }
74
- }
75
-
76
- await browser . close ( ) ;
77
- console . log ( "Broken links:" ) ;
78
- console . log ( brokenLinks ) ;
79
- if ( brokenLinks . length > 0 ) {
80
- core . setOutput ( "brokenLinks" , arrayToHTMLList ( brokenLinks ) ) ;
81
- }
82
- process . exit ( 0 ) ;
19
+ const browser = await puppeteer . launch ( {
20
+ headless : "new" ,
21
+ } ) ;
22
+ const page = await browser . newPage ( ) ;
23
+
24
+ const sitemapUrl = "https://developers.cloudflare.com/sitemap.xml" ;
25
+ await page . goto ( sitemapUrl , { timeout : navigationTimeout } ) ;
26
+
27
+ const sitemapLinks = await page . $$eval ( "url loc" , ( elements ) =>
28
+ elements . map ( ( el ) => el . textContent ) ,
29
+ ) ;
30
+
31
+ const visitedLinks = [ ] ;
32
+ const brokenLinks = [ ] ;
33
+
34
+ for ( const link of sitemapLinks ) {
35
+ if ( ! link ) {
36
+ continue ; // Skip if the link is empty
37
+ }
38
+
39
+ await page . goto ( link , {
40
+ waitUntil : "networkidle0" ,
41
+ timeout : navigationTimeout ,
42
+ } ) ;
43
+
44
+ const pageLinks = await page . $$eval ( "a" , ( elements ) =>
45
+ elements . map ( ( el ) => el . href ) ,
46
+ ) ;
47
+
48
+ for ( const pageLink of pageLinks ) {
49
+ if ( ! pageLink || visitedLinks . includes ( pageLink ) ) {
50
+ continue ; // Skip if the pageLink is empty or has already been visited
51
+ }
52
+
53
+ if (
54
+ pageLink . includes ( "developers.cloudflare.com/api/operations/" ) ||
55
+ pageLink . startsWith ( "/api/operations/" )
56
+ ) {
57
+ console . log ( `Evaluating link: ${ pageLink } ` ) ;
58
+ await page . goto ( pageLink , {
59
+ waitUntil : "networkidle0" ,
60
+ timeout : navigationTimeout ,
61
+ } ) ;
62
+ visitedLinks . push ( pageLink ) ;
63
+
64
+ const statusCode = await page . evaluate ( ( ) => {
65
+ return {
66
+ url : window . location . href ,
67
+ } ;
68
+ } ) ;
69
+ if ( statusCode . url === "https://developers.cloudflare.com/api/" ) {
70
+ brokenLinks . push ( pageLink ) ;
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ await browser . close ( ) ;
77
+ console . log ( "Broken links:" ) ;
78
+ console . log ( brokenLinks ) ;
79
+ if ( brokenLinks . length > 0 ) {
80
+ core . setOutput ( "brokenLinks" , arrayToHTMLList ( brokenLinks ) ) ;
81
+ }
82
+ process . exit ( 0 ) ;
83
83
}
84
84
85
85
checkLinks ( ) . catch ( ( error ) => {
86
- console . error ( error ) ;
87
- process . exit ( 1 ) ;
86
+ console . error ( error ) ;
87
+ process . exit ( 1 ) ;
88
88
} ) ;
0 commit comments