1+ // A script the check all local links on the docs site
2+
13const globby = require ( 'globby' )
24const posthtml = require ( 'posthtml' )
35const fs = require ( 'fs' )
@@ -8,10 +10,14 @@ const ora = require('ora')
810
911const checkForDeadLocalUrls = async ( ) => {
1012 try {
13+ // Grab all the files from the specified directory, add their paths to a new set
1114 const files = await globby ( '_site/**/*.html' )
1215 const throbber = ora ( 'Link Check Starting' ) . start ( )
16+ // Use a set here for efficiency, no duplicate values!
1317 const urls = new Set ( )
1418
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.
1521 const ph = posthtml ( [
1622 require ( 'posthtml-urls' ) ( {
1723 eachURL : ( url ) => {
@@ -22,14 +28,16 @@ const checkForDeadLocalUrls = async () => {
2228 } ) ,
2329 ] )
2430 throbber . succeed ( )
25- throbber . start ( 'Processing files' )
2631
32+ // Using the logic above, iterate through the entire list of files
33+ throbber . start ( 'Processing files' )
2734 files . forEach ( ( file ) => {
2835 ph . process ( fs . readFileSync ( file ) )
2936 } )
3037 throbber . succeed ( )
31- throbber . start ( 'Starting server' )
3238
39+ // Spin up a lightweight browsersync server to check each URL
40+ throbber . start ( 'Starting server' )
3341 await new Promise ( ( resolve ) => {
3442 server . init ( {
3543 port : 3000 ,
@@ -44,17 +52,20 @@ const checkForDeadLocalUrls = async () => {
4452 throbber . succeed ( )
4553 } )
4654
55+ // Check the links against the local browsersync site
4756 const results = await checkLinks (
4857 Array . from ( urls ) . map ( ( url ) =>
4958 url
5059 ) ,
5160 )
61+
62+ // If a link returns 'dead' (404), add it to an array
5263 const deadUrls = Array . from ( urls ) . filter (
5364 ( url ) => results [ url ] . status === 'dead' ,
5465 )
5566
67+ // For ease of checking, replace the localhost domain with the live domain, add those to a new array.
5668 let broke = [ ]
57-
5869 deadUrls . forEach ( url => {
5970 link = url . replace ( 'http://localhost:3000' , 'https://segment.com/docs' )
6071 if ( ! link . endsWith ( '/' ) ) {
@@ -64,17 +75,27 @@ const checkForDeadLocalUrls = async () => {
6475 } ) ;
6576
6677
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
6882 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
6986 const data = require ( '../_site/redirects.json' )
87+ // Grab the 'from' redirect
7088 Object . keys ( data ) . forEach ( key => {
7189 if ( ! key . endsWith ( '/' ) ) {
7290 key = key + '/'
7391 }
7492 redirects . push ( 'https://segment.com/docs' + key . replace ( '/docs' , '' ) )
7593 } )
94+ // Remove the redirect urls from the list of broken URLs
7695 broke = broke . filter ( val => ! redirects . includes ( val ) ) ;
7796
97+ // If there are dead URLs, list them here, along with the count. Exit status 1 to indicate an error.
98+
7899 if ( broke . length > 0 ) {
79100 throbber . fail ( `Dead URLS: ${ broke . length } \n\n` )
80101 console . log ( `Dead URLS: ${ broke . length } \n\n${ broke . join ( '\n' ) } ` )
0 commit comments