1
+ // A script the check all local links on the docs site
2
+
1
3
const globby = require ( 'globby' )
2
4
const posthtml = require ( 'posthtml' )
3
5
const fs = require ( 'fs' )
@@ -8,10 +10,14 @@ const ora = require('ora')
8
10
9
11
const checkForDeadLocalUrls = async ( ) => {
10
12
try {
13
+ // Grab all the files from the specified directory, add their paths to a new set
11
14
const files = await globby ( '_site/**/*.html' )
12
15
const throbber = ora ( 'Link Check Starting' ) . start ( )
16
+ // Use a set here for efficiency, no duplicate values!
13
17
const urls = new Set ( )
14
18
19
+ // Logic for collecting the list of URLs to check
20
+ // If the link starts with `/docs/`, replace that with a localhost:3000 domain, and add it to the list.
15
21
const ph = posthtml ( [
16
22
require ( 'posthtml-urls' ) ( {
17
23
eachURL : ( url ) => {
@@ -22,14 +28,16 @@ const checkForDeadLocalUrls = async () => {
22
28
} ) ,
23
29
] )
24
30
throbber . succeed ( )
25
- throbber . start ( 'Processing files' )
26
31
32
+ // Using the logic above, iterate through the entire list of files
33
+ throbber . start ( 'Processing files' )
27
34
files . forEach ( ( file ) => {
28
35
ph . process ( fs . readFileSync ( file ) )
29
36
} )
30
37
throbber . succeed ( )
31
- throbber . start ( 'Starting server' )
32
38
39
+ // Spin up a lightweight browsersync server to check each URL
40
+ throbber . start ( 'Starting server' )
33
41
await new Promise ( ( resolve ) => {
34
42
server . init ( {
35
43
port : 3000 ,
@@ -44,17 +52,20 @@ const checkForDeadLocalUrls = async () => {
44
52
throbber . succeed ( )
45
53
} )
46
54
55
+ // Check the links against the local browsersync site
47
56
const results = await checkLinks (
48
57
Array . from ( urls ) . map ( ( url ) =>
49
58
url
50
59
) ,
51
60
)
61
+
62
+ // If a link returns 'dead' (404), add it to an array
52
63
const deadUrls = Array . from ( urls ) . filter (
53
64
( url ) => results [ url ] . status === 'dead' ,
54
65
)
55
66
67
+ // For ease of checking, replace the localhost domain with the live domain, add those to a new array.
56
68
let broke = [ ]
57
-
58
69
deadUrls . forEach ( url => {
59
70
link = url . replace ( 'http://localhost:3000' , 'https://segment.com/docs' )
60
71
if ( ! link . endsWith ( '/' ) ) {
@@ -64,17 +75,27 @@ const checkForDeadLocalUrls = async () => {
64
75
} ) ;
65
76
66
77
67
-
78
+ // Sometimes, we redirect urls based on jekyll settings, or a setting an app-nginx.
79
+ // For those, we want to remove them from the list of dead links, because they aren't dead.
80
+
81
+ // app-nginx redirects
68
82
const redirects = [ 'https://segment.com/docs/guides/usage-and-billing/' , 'https://segment.com/docs/connections/sources/catalog/libraries/website/plugins/' , 'https://segment.com/docs/assets/docs.bundle.js/' ]
83
+
84
+ // Redirects generated by Jekyll
85
+ // Pull the redirects json file
69
86
const data = require ( '../_site/redirects.json' )
87
+ // Grab the 'from' redirect
70
88
Object . keys ( data ) . forEach ( key => {
71
89
if ( ! key . endsWith ( '/' ) ) {
72
90
key = key + '/'
73
91
}
74
92
redirects . push ( 'https://segment.com/docs' + key . replace ( '/docs' , '' ) )
75
93
} )
94
+ // Remove the redirect urls from the list of broken URLs
76
95
broke = broke . filter ( val => ! redirects . includes ( val ) ) ;
77
96
97
+ // If there are dead URLs, list them here, along with the count. Exit status 1 to indicate an error.
98
+
78
99
if ( broke . length > 0 ) {
79
100
throbber . fail ( `Dead URLS: ${ broke . length } \n\n` )
80
101
console . log ( `Dead URLS: ${ broke . length } \n\n${ broke . join ( '\n' ) } ` )
0 commit comments