-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathmarkdown-link-check
executable file
·265 lines (227 loc) · 9.62 KB
/
markdown-link-check
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env node
'use strict';
let chalk;
const fs = require('fs');
const { promisify } = require('util');
const markdownLinkCheck = promisify(require('./'));
const needle = require('needle');
const path = require('path');
const pkg = require('./package.json');
const { Command } = require('commander');
const program = new Command();
const { ProxyAgent } = require('proxy-agent');
class Input {
constructor(filenameForOutput, stream, opts) {
this.filenameForOutput = filenameForOutput;
this.stream = stream;
this.opts = opts;
}
}
function commaSeparatedPathsList(value) {
return value.split(',');
}
function commaSeparatedCodesList(value, dummyPrevious) {
return value.split(',').map(function(item) {
return parseInt(item, 10);
});
}
/**
* Load all files in the rootFolder and all subfolders that end with .md
*/
function loadAllMarkdownFiles(rootFolder = '.') {
const files = [];
fs.readdirSync(rootFolder).forEach(file => {
const fullPath = path.join(rootFolder, file);
if (fs.lstatSync(fullPath).isDirectory()) {
files.push(...loadAllMarkdownFiles(fullPath));
} else if (fullPath.endsWith('.md')) {
files.push(fullPath);
}
});
return files;
}
function commaSeparatedReportersList(value) {
return value.split(',').map((reporter) => require(path.resolve('reporters', reporter)));
}
function getInputs() {
const inputs = [];
program
.version(pkg.version)
.option('-p, --progress', 'show progress bar')
.option('-c, --config [config]', 'apply a config file (JSON), holding e.g. url specific header configuration')
.option('-q, --quiet', 'displays errors only')
.option('-v, --verbose', 'displays detailed error information')
.option('-i, --ignore <paths>', 'ignore input paths including an ignore path', commaSeparatedPathsList)
.option('-a, --alive <code>', 'comma separated list of HTTP codes to be considered as alive', commaSeparatedCodesList)
.option('-r, --retry', 'retry after the duration indicated in \'retry-after\' header when HTTP code is 429')
.option('--reporters <names>', 'specify reporters to use', commaSeparatedReportersList)
.option('--projectBaseUrl <url>', 'the URL to use for {{BASEURL}} replacement')
.arguments('[filenamesOrDirectorynamesOrUrls...]')
.action(function (filenamesOrUrls) {
let filenameForOutput;
let stream;
if (!filenamesOrUrls.length) {
// read from stdin unless a filename is given
inputs.push(new Input(filenameForOutput, process.stdin, {}));
}
function onError(error) {
console.error(chalk.red('\nERROR: Unable to connect! Please provide a valid URL as an argument.'));
process.exit(1);
}
function onResponse(response) {
if (response.statusCode === 404) {
console.error(chalk.red('\nERROR: 404 - File not found! Please provide a valid URL as an argument.'));
process.exit(1);
}
}
const { ignore } = program.opts();
for (const filenameOrUrl of filenamesOrUrls) {
filenameForOutput = filenameOrUrl;
let baseUrl = '';
// remote file
if (/https?:/.test(filenameOrUrl)) {
stream = needle.get(
filenameOrUrl, { agent: new ProxyAgent(), use_proxy_from_env_var: false }
);
stream.on('error', onError);
stream.on('response', onResponse);
try { // extract baseUrl from supplied URL
const parsed = new URL(filenameOrUrl);
parsed.search = '';
parsed.hash = '';
if (parsed.pathname.lastIndexOf('/') !== -1) {
parsed.pathname = parsed.pathname.substring(0, parsed.pathname.lastIndexOf('/') + 1);
}
baseUrl = parsed.toString();
inputs.push(new Input(filenameForOutput, stream, {baseUrl: baseUrl}));
} catch (err) {
/* ignore error */
}
} else {
// local file or directory
let files = [];
if (fs.statSync(filenameOrUrl).isDirectory()){
files = loadAllMarkdownFiles(filenameOrUrl)
} else {
files = [filenameOrUrl]
}
for (let file of files) {
filenameForOutput = file;
const resolved = path.resolve(filenameForOutput);
// skip paths given if it includes a path to ignore.
// todo: allow ignore paths to be glob or regex instead of just includes?
if (ignore && ignore.some((ignorePath) => resolved.includes(ignorePath))) {
continue;
}
if (process.platform === 'win32') {
baseUrl = 'file://' + path.dirname(resolved).replace(/\\/g, '/');
}
else {
baseUrl = 'file://' + path.dirname(resolved);
}
stream = fs.createReadStream(filenameForOutput);
inputs.push(new Input(filenameForOutput, stream, {baseUrl: baseUrl}));
}
}
}
}
).parse(process.argv);
for (const input of inputs) {
input.opts.showProgressBar = (program.opts().progress === true); // force true or undefined to be true or false.
input.opts.quiet = (program.opts().quiet === true);
input.opts.verbose = (program.opts().verbose === true);
input.opts.retryOn429 = (program.opts().retry === true);
input.opts.aliveStatusCodes = program.opts().alive;
input.opts.reporters = program.opts().reporters ?? [require(path.resolve('reporters', 'default.js'))];
const config = program.opts().config;
if (config) {
input.opts.config = config.trim();
}
if (program.projectBaseUrl) {
input.opts.projectBaseUrl = `file://${program.projectBaseUrl}`;
} else {
// set the default projectBaseUrl to the current working directory, so that `{{BASEURL}}` can be resolved to the project root.
if (process.platform === 'win32') {
input.opts.projectBaseUrl = `file:///${process.cwd().replace(/\\/g, '/')}`;
}
else {
input.opts.projectBaseUrl = `file://${process.cwd()}`;
}
}
}
return inputs;
}
async function loadConfig(config) {
return new Promise((resolve, reject) => {
fs.access(config, (fs.constants || fs).R_OK, function (err) {
if (!err) {
let configStream = fs.createReadStream(config);
let configData = '';
configStream.on('data', function (chunk) {
configData += chunk.toString();
}).on('end', function () {
resolve(JSON.parse(configData));
});
}
else {
console.error(chalk.red('\nERROR: Config file not accessible.'));
process.exit(1);
}
});
});
}
async function processInput(filenameForOutput, stream, opts) {
let markdown = ''; // collect the markdown data, then process it
stream.on('error', function(error) {
if (error.code === 'ENOENT') {
console.error(chalk.red('\nERROR: File not found! Please provide a valid filename as an argument.'));
} else {
console.error(chalk.red(error));
}
return process.exit(1);
});
for await (const chunk of stream) {
markdown += chunk.toString();
}
if (!opts.quiet && filenameForOutput) {
console.log(chalk.cyan('\nFILE: ' + filenameForOutput));
}
if (opts.config) {
let config = await loadConfig(opts.config);
opts.ignorePatterns = config.ignorePatterns;
opts.replacementPatterns = config.replacementPatterns;
opts.httpHeaders = config.httpHeaders;
opts.timeout = config.timeout;
opts.ignoreDisable = config.ignoreDisable;
opts.retryOn429 = config.retryOn429;
opts.retryCount = config.retryCount;
opts.fallbackRetryDelay = config.fallbackRetryDelay;
opts.aliveStatusCodes = config.aliveStatusCodes;
opts.reporters = config.reporters;
}
await runMarkdownLinkCheck(filenameForOutput, markdown, opts);
}
async function runMarkdownLinkCheck(filenameForOutput, markdown, opts) {
const [err, results] = await markdownLinkCheck(markdown, opts)
.then(res => [null, res]).catch(err => [err]);
await Promise.allSettled(
opts.reporters.map(reporter => reporter(err, results, opts, filenameForOutput)
));
if (err) throw null;
else if (results.some((result) => result.status === 'dead')) return;
else return;
}
async function main() {
chalk = (await import('chalk')).default;
const inputs = getInputs();
let isOk = true;
for await (const input of inputs) {
try {
await processInput(input.filenameForOutput, input.stream, input.opts);
} catch (err) {
isOk = false;
}
}
process.exit(isOk ? 0 : 1);
}
main();