-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathcheckrefs.js
503 lines (450 loc) · 16.3 KB
/
checkrefs.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/usr/bin/env node
"use strict";
//to read the file line by line
var readl = require("readl-async");
//to read files referenced by external references
var fs = require("fs");
//to convert ../ and / and ./ to absolute paths
var path = require("path");
//to check whether an URL is reachable
var linkCheck = require("link-check");
//Difference function to compare internal references stored in arrays
function difference(a1, a2) {
var result = [];
for (let i = 0; i < a1.length; i++) {
if (a2.indexOf(a1[i]) === -1) {
result.push(a1[i]);
}
}
return result;
}
//To check for only unique values
function uniq(a) {
var prims = {
boolean: {},
number: {},
string: {}
},
objs = [];
return a.filter(function(item) {
var type = typeof item;
if (type in prims) {
return prims[type].hasOwnProperty(item)
? false
: (prims[type][item] = true);
} else {
return objs.indexOf(item) >= 0 ? false : objs.push(item);
}
});
}
(async () => {
try {
if (process.argv[2] != undefined) {
const stat = await fs.lstatSync(process.argv[2]);
if (stat.isFile()) {
checkReferencesFromFile(process.argv[2]);
} else if (stat.isDirectory()) {
checkReferencesFromFolder(process.argv[2]);
}
} else console.log("Please provide a file or folder path to check!");
} catch (err) {
console.log("\x1b[31m%s\x1b[0m", err);
}
})();
var httpStatusCodes = "200,201";
var cfgHttpStatusCodes = httpStatusCodes.split(",");
for (let a in cfgHttpStatusCodes) {
// convert to array of integer
cfgHttpStatusCodes[a] = parseInt(cfgHttpStatusCodes[a], 10); // Explicitly include base
}
// check the specified file selected
function checkReferencesFromFile(filename) {
checkReferences(filename, path.resolve(filename));
}
// check recursively for the specified folder
function checkReferencesFromFolder(folderPath) {
//walk recursively through the folder and returns file list
function walk(dir) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(file => {
file = dir + "/" + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walk(file));
} else {
/* Is a file */
var fileType = file.split(".").pop();
if (fileType === "adoc") {
results.push(file);
}
}
});
return results;
}
var fileList = walk(folderPath);
var numberFiles = fileList.length;
var fileListShort = "";
fileList.forEach(file => {
fileListShort += path.relative(folderPath, file) + ", ";
});
console.log("Checking: `" + fileListShort + "`");
for (let i = 0; i < numberFiles; i++) {
checkReferences(fileList[i], folderPath);
}
}
// check the references contained in the file
// basePath is a shorter version of the filePath that will be used in log messages
function checkReferences(filePath, basePath) {
// string identifying the current file in any log message
// if a basePath is given, the file will be identified
// relatively to this basePath
var logMsgFilePath;
if (basePath !== undefined) {
logMsgFilePath = path.relative(path.parse(basePath).dir, filePath);
} else {
logMsgFilePath = path.parse(filePath).base;
}
//get directory of current file
var folderPath = path.parse(filePath).dir;
//to ignore everything if it is commented out
var insideCommentBlock = false;
//to hold anchors
var anchorArray = [];
//to hold internal references
var internalRefs = [];
//to hold references to external files
var externalRefs = [];
//to hold references to URLs
var externalURLs = [];
// add an anchor to the anchor array
// raise an error if the anchor was already defined
function registerAnchor(newAnchor) {
if (anchorArray.includes(newAnchor)) {
console.log(
"\x1b[31m%s\x1b[0m",
"- [ ] *" +
logMsgFilePath +
"*: Found duplicate anchors for `" +
newAnchor +
"`."
);
} else {
anchorArray.push(newAnchor);
}
}
//lets read file contents line by line
var reader = new readl(filePath, {
encoding: "utf8",
emptyLines: "true"
});
//Emit this function when one line is read:
reader.on("line", function(line, index, start, end) {
//detect start and end of code blocks
if (line.startsWith("////")) {
insideCommentBlock = !insideCommentBlock;
}
//ignore everything inside comment blocks
if (insideCommentBlock) {
return;
}
//ignore single line comments
if (line.startsWith("//")) {
return;
}
//find if line contains an anchor with format [[anchor]] or [[anchor, description]]
if (line.match(/\[\[[^\]]+\]\]/g)) {
let extractLink = line.match(/\[\[[^\]]+\]\]/g);
//console.log('LINE: '+ line);
//console.log('EXTRACT LINK: '+ extractLink);
for (let i = 0; i < extractLink.length; i++) {
let newAnchor = extractLink[i];
newAnchor = newAnchor.replace("[[", "");
newAnchor = newAnchor.replace("]]", "");
newAnchor = newAnchor.replace(/,.*/g, ""); // take into account ','
registerAnchor(newAnchor);
//console.log('NEW ANCHOR with [[...]]: ' + newAnchor);
}
}
//find if line contains an anchor with format [[[anchor]]] or [[[anchor, something]]]
//this type of format of used for bibliography
if (line.match(/^[\s]*[\*\-][\s]+\[\[\[[^\]]+\]\]\]/g)) {
let extractLink = line.match(/\[\[\[[^\]]+\]\]\]/g);
//console.log('LINE: '+ line);
//console.log('EXTRACT LINK: '+ extractLink);
for (let i = 0; i < extractLink.length; i++) {
let newAnchor = extractLink[i];
newAnchor = newAnchor.replace("[[[", "");
newAnchor = newAnchor.replace("]]]", "");
newAnchor = newAnchor.replace(/,.*/g, ""); // take into account ','
registerAnchor(newAnchor);
//console.log('NEW ANCHOR with [[[...]]]: ' + newAnchor);
}
}
//find if line contains anchor with format [#anchorname] (Inline anchors)
if (line.match(/\[#[^\]]+\]/g)) {
let extractLink = line.match(/\[#[^\]]+\]/g);
for (let i = 0; i < extractLink.length; i++) {
let newAnchor = extractLink[i];
newAnchor = newAnchor.replace("[#", "");
newAnchor = newAnchor.replace("]", "");
registerAnchor(newAnchor);
}
}
//find if line contains anchor with format anchor:anchorname[some text]
if (line.match(/(anchor:)[^\[]+\[[^\]]*\]/g)) {
let extractLink = line.match(/:[^\[]+\[/g);
for (let i = 0; i < extractLink.length; i++) {
let newAnchor = extractLink[i];
newAnchor = newAnchor.replace(":", "");
newAnchor = newAnchor.replace("[", "");
registerAnchor(newAnchor);
}
}
//find internal and external references
//with format <<anchorname>> or <<anchorname, some text>>
//or with format <<file.adoc, some text>> or <<file#anchorname, some text>>
if (line.match(/<<[^\>]+>>/g)) {
//console.log('LINE-----',line)
let extractLink = line.match(/<<[^\>]+>>/g); //there may be more than one matching items
for (let i = 0; i < extractLink.length; i++) {
let newReference = extractLink[i];
newReference = newReference.replace("<<", "");
newReference = newReference.replace(">>", "");
newReference = newReference.replace(/,.*/g, ""); // take into account <<anchor, some text>>
//distinguish internal and external references
if (newReference.match(/(\.adoc)|(#)/g)) {
//add ".adoc" if it is missing
newReference = newReference.replace(/(\.adoc)?#/, ".adoc#");
externalRefs.push(path.resolve(folderPath, newReference));
} else {
internalRefs.push(newReference);
}
}
}
//find internal and external references with format xref:link[text]
if (line.match(/xref:[^\[]+\[[^\]]*\]/g)) {
//console.log('LINE: ' + line);
let extractLink = line.match(/xref:[^\[]+\[/g);
//console.log('extractLink: ' + extractLink)
for (let i = 0; i < extractLink.length; i++) {
let newReference = extractLink[i];
newReference = newReference.replace("xref:", "");
newReference = newReference.replace("[", "");
//distinguish internal and external references
if (newReference.match(/(\.adoc)|(#)/g)) {
//add ".adoc" if it is missing
newReference = newReference.replace(/(\.adoc)?#/, ".adoc#");
externalRefs.push(path.resolve(folderPath, newReference));
} else {
internalRefs.push(newReference);
}
}
}
//find external references that use the link:URI[some description] syntax
if (line.match(/link:[^\[]+\[[^\]]*\]/g)) {
//console.log('LINE-----',line)
let extractLink = line.match(/link:[^\[]+\[/g); //there may be more than one matching items
for (let i = 0; i < extractLink.length; i++) {
let newReference = extractLink[i];
newReference = newReference.replace("link:", "");
newReference = newReference.replace("[", "");
//distinguish local files from URLs
if (newReference.match(/^(http|https):\/\//g)) {
if (
!newReference.match(
/(example\.(?:com|org|test)|http:\/\/localhost|https:\/\/localhost|my\.proxy|http(?:s|):\/\/\$|http(?:s|):\/\/\%|location_of_rpm_server|\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}|someaccount\.|\*\.|mycorp|mydev|youruser|yourrepo|starter-us-east-1|api\.openshift\.com|index\.docker\.io)/
)
)
externalURLs.push(newReference);
} else if (newReference.match(/^(ftp|irc|mailto):\/\//g)) {
// we currently don't handle these
} else {
//assuming it is a local file
//in case of html, drop the part after #, we won't check it
newReference = newReference.replace(/(\.html?5?)#.*/, "$1");
externalRefs.push(path.resolve(folderPath, newReference));
}
}
}
//find URLs that don't use the "link:" macro (e.g. "http://www.google.com")
//this regex is derived from: https://github.com/asciidoctor/atom-language-asciidoc/blob/master/grammars/repositories/inlines/link-macro-grammar.cson
if (
line.match(
/(?:^|<|[\s>\(\)\[\];])((https?|file|ftp|irc):\/\/[^\s\[\]<]*[^\s.,\[\]<\)])/g
)
) {
//console.log('LINE-----',line)
let extractLink = line.match(
/((https?|file|ftp|irc):\/\/[^\s\[\]<]*[^\s.,\[\]<\)])/g
); //there may be more than one matching items
for (let i = 0; i < extractLink.length; i++) {
let newReference = extractLink[i];
//console.log('URL without link: macro: ', newReference);
if (
!newReference.match(
/(example\.(?:com|org|test)|http:\/\/localhost|https:\/\/localhost|my\.proxy|http(?:s|):\/\/\$|http(?:s|):\/\/\%|location_of_rpm_server|\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}|someaccount\.|\*\.|mycorp|mydev|youruser|yourrepo|starter-us-east-1|api\.openshift\.com|index\.docker\.io)/
)
)
externalURLs.push(newReference);
//console.log("PUSHED: " + newReference)
}
}
});
//Emit this function when the file is fully read
reader.on("end", () => {
//console.log(externalURLs)
// returns a promise that resolves to true if the
// external reference exists, and to false otherwise
function checkExternalRef(item) {
var itemOk = true;
//console.log('ITEM: ', item);
return new Promise((resolve, reject) => {
process.nextTick(() => {
var currentLink = item.split("#");
var targetFilePath = currentLink[0];
var targetAnchor = currentLink[1];
// if there is NO ref to an anchor, just check file exists
if (!targetAnchor || targetAnchor.length === 0) {
//console.log(item);
if (!fs.existsSync(targetFilePath)) {
itemOk = false;
var errorLine =
"- [ ] *" +
logMsgFilePath +
"*: Cannot find referenced file: `" +
targetFilePath +
"`";
console.log("\x1b[31m%s\x1b[0m", errorLine);
}
} else {
// try to find the anchor in the target file
try {
var data = fs.readFileSync(targetFilePath, "utf8");
if (data.indexOf("[[" + targetAnchor + "]]") >= 0) {
//all good
//do nothing
//console.log('LINK FINE: ' + targetAnchor);
} else {
// console.log(
// '----------------------ERROR-----------------------'
// );
// console.log(
// 'Cannot find anchor!' +
// targetAnchor +
// ' in file ' +
// item
// );
itemOk = false;
console.log(
"\x1b[31m%s\x1b[0m",
"- [ ] *" +
logMsgFilePath +
"* : Cannot find anchor: `" +
targetAnchor +
"` in `" +
targetFilePath +
"`"
);
}
} catch (err) {
//console.log('ERROR READING FILE', err);
itemOk = false;
console.log(
"\x1b[31m%s\x1b[0m",
"- [ ] *" +
logMsgFilePath +
"*: Cannot find referenced file: `" +
targetFilePath +
"`"
);
}
}
resolve(itemOk);
});
});
}
// returns a promise that resolves to true if the
// URL exists, and to false otherwise
function checkExternalURL(item) {
//console.log('ITEM: ', item);
return new Promise((resolve, reject) => {
process.nextTick(() => {
let opts = {
aliveStatusCodes: cfgHttpStatusCodes
};
linkCheck(item, opts, function(err, result) {
if (result.status === "alive") {
resolve(true);
} else {
console.log(
"\x1b[31m%s\x1b[0m",
"- [ ] *" +
logMsgFilePath +
"*: Cannot reach URL (status code `" +
result.statusCode +
"`): `" +
item +
"`"
);
resolve(false);
}
});
});
});
}
//check all internal references
var notFoundInternal = difference(internalRefs, anchorArray);
if (internalRefs[0] == null) {
//console.log("`" + logMsgFilePath + "`: No internal reference found.");
} else if (notFoundInternal[0] == null) {
/* console.log(
"`" + logMsgFilePath + "`: All internal references are `OK`."
); */
} else {
notFoundInternal.forEach(function(it) {
//console.log('Cannot find the anchor: ' + it + ' in current file.');
console.log(
"\x1b[31m%s\x1b[0m",
"- [ ] *" + logMsgFilePath + "*: Cannot find anchor: `" + it + "`."
);
});
}
//check al external references
if (externalRefs[0] == null) {
//console.log("`" + logMsgFilePath + "`: No external reference found.");
} else {
//get uniques so that we only check them once
externalRefs = uniq(externalRefs);
Promise.all(externalRefs.map(checkExternalRef)).then(returnCodes => {
/* if (!returnCodes.includes(false)) {
console.log(
"`" + logMsgFilePath + "`: All external references are `OK`."
);
} */
});
}
//check if all URLs are reachable
//if (cfgCheckURL) {
if (externalURLs[0] == null) {
//console.log("`" + logMsgFilePath + "`: No URLs found.");
} else {
//get uniques so that we only check them once
externalURLs = uniq(externalURLs);
Promise.all(externalURLs.map(checkExternalURL)).then(returnCodes => {
if (!returnCodes.includes(false)) {
//console.log("`" + logMsgFilePath + "`: All URLs are reachable.");
}
});
}
//}
});
//Emit this function when an error occurs
reader.on("error", function(error) {
//Do some stuff with the error
// ....
});
//Start reading the file
reader.read();
}