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