Skip to content

Commit cb773cb

Browse files
author
markzegarelli
committed
code commenting / cleanup
1 parent 864f101 commit cb773cb

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

scripts/checklinks-external.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1+
// A script to check all external links on the docs site
2+
13
const globby = require('globby')
24
const posthtml = require('posthtml')
35
const fs = require('fs')
46
const server = require('browser-sync').create()
57
const checkLinks = require('check-links')
6-
const {
7-
$dataMetaSchema
8-
} = require('ajv')
98
const ora = require('ora')
109

1110

1211

1312
const checkForDeadExternalUrls = async () => {
1413
try {
14+
// Grab all the files from the specified directory, add their paths to a new set
1515
const files = await globby('_site/**/*.html')
1616
const throbber = ora('Link Check Starting').start()
17+
// Use a set here for efficiency, no duplicate values!
1718
const urls = new Set()
1819

20+
// Because we're checking for external URLs only, add every URL that does not start with a `/`,
21+
// or point to 0.0.0.0. Also exclude segmentio GitHub URLs
1922
const ph = posthtml([
2023
require('posthtml-urls')({
2124
eachURL: (url) => {
@@ -28,40 +31,32 @@ const checkForDeadExternalUrls = async () => {
2831
throbber.succeed()
2932
throbber.start('Processing files')
3033

34+
// Run the above logic on each file
3135
files.forEach((file) => {
3236
ph.process(fs.readFileSync(file))
3337
})
34-
throbber.succeed()
35-
throbber.start('Starting server')
36-
await new Promise((resolve) => {
37-
server.init({
38-
port: 3000,
39-
server: {
40-
baseDir: '_site',
41-
},
42-
open: false,
43-
logLevel: 'silent',
44-
},
45-
resolve,
46-
)
47-
throbber.succeed()
48-
})
49-
38+
39+
40+
// Check all the links collected in the section above
5041
throbber.start('Checking the links')
5142
const results = await checkLinks(
5243
Array.from(urls).map((url) =>
5344
url
5445
),
5546
)
47+
// If a link returns 'dead' (404), add it to an array
5648
const deadUrls = Array.from(urls).filter(
5749
(url) => results[url].status === 'dead',
5850
)
59-
51+
52+
// If there are dead URLs, list them here, along with the count. Exit status 1 to indicate an error.
6053
if (deadUrls.length > 0) {
6154
throbber.fail(`Dead URLS: ${deadUrls.length}\n\n`)
6255
console.log(`Dead URLS: ${deadUrls.length}\n\n${deadUrls.join('\n')}`)
6356
process.exit(1)
64-
} else {
57+
}
58+
// Otherwise, claim that all the links work, and exit the process normally.
59+
else {
6560
console.log('All links work!')
6661
process.exit
6762
}

scripts/checklinks-internal.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// A script the check all local links on the docs site
2+
13
const globby = require('globby')
24
const posthtml = require('posthtml')
35
const fs = require('fs')
@@ -8,10 +10,14 @@ const ora = require('ora')
810

911
const 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

Comments
 (0)