markdown_link_checker_sc 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +42 -17
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -22,9 +22,14 @@ program
22
22
  )
23
23
  .option("-l, --log [value]", "Export some logs for debugging. ", false)
24
24
  .option(
25
- "-f, --files <files...>",
26
- "List of files to process (path relative to -d). Default is all files.",
27
- []
25
+ "-f, --files <path>",
26
+ "JSON file with array of files to report on (default is all files). Paths are relative relative to -d by default, but -r can be used to set a different root.",
27
+ ""
28
+ )
29
+ .option(
30
+ "-r, --root <path>",
31
+ "Directory to prepend before file paths in the JSON directory. Default is same as directory. Useful if directory is not your repo root",
32
+ ""
28
33
  )
29
34
  .parse(process.argv);
30
35
 
@@ -33,9 +38,22 @@ program
33
38
 
34
39
  const options = program.opts();
35
40
 
36
- if (options.log == "quick") {
37
- for (const file of options.files) {
38
- console.log(file);
41
+ // Function for loading JSON file that contains files to report on
42
+ async function loadJSONFileToReportOn(filePath) {
43
+ try {
44
+ const fileContent = await fs.promises.readFile(filePath, "utf8");
45
+ let filesArray = JSON.parse(fileContent);
46
+ // Values default to option directory if not specified
47
+ if (!options.root) {
48
+ options.root = options.directory;
49
+ }
50
+ // Prepend the full path.
51
+ filesArray = filesArray.map((str) => path.join(options.root, str));
52
+ //console.log(filesArray);
53
+ return filesArray;
54
+ } catch (error) {
55
+ console.error(`Error reading file: ${error.message}`);
56
+ process.exit(1);
39
57
  }
40
58
  }
41
59
 
@@ -384,18 +402,14 @@ function filterErrors(errors) {
384
402
  // This method filters all errors against settings in the command line - such as pages to output.
385
403
  let filteredErrors = errors;
386
404
  // Filter results on specified file names (if any specified)
405
+ //console.log(`Number pages to filter: ${options.files.length}`);
387
406
  if (options.files.length > 0) {
388
407
  filteredErrors = errors.filter((error) => {
389
408
  //console.log(`Error: ${error}`);
390
409
  //console.log(JSON.stringify(error, null, 2));
391
410
  //console.log(`Error page: ${error.page}`);
392
- //Strip off the directory prefix for comparison to passed values (which must not start with / or \)
393
- let pathOnly = error.page.split(options.directory)[1];
394
- if (pathOnly.startsWith("/") || pathOnly.startsWith("\\")) {
395
- pathOnly = pathOnly.slice(1);
396
- }
397
- //console.log(`Pathonly: ${pathOnly}`);
398
- return options.files.includes(pathOnly);
411
+
412
+ return options.files.includes(error.page);
399
413
  });
400
414
  }
401
415
  // Filter on other things - such as errors.
@@ -438,14 +452,14 @@ function outputErrors(results) {
438
452
  console.log(`${page}`);
439
453
  for (const error of sortedByPageErrors[page]) {
440
454
  if (error.type == "InternalLinkMissingFile") {
441
- console.log(` ${error.type}: ${error.linkUrl}`);
455
+ console.log(`- ${error.type}: ${error.linkUrl}`);
442
456
  //console.log(` ${error.type}: ${error.linkAnchor}, linkURL: ${error.linkUrl}`);
443
457
  // { "type": "InternalLinkMissingFile", "page": `${page.page_file}`, "linkUrl": `${link.linkUrl}`, "linkText": `${link.linkText}`, "linkUrlFilePath": `${linkAbsoluteFilePath}` };
444
458
  } else if (error.type == "InternalLocalMissingAnchor") {
445
459
  // missing anchor in linked file that exists.
446
460
  //console.log(error);
447
461
  console.log(
448
- ` ${error.type}: #${error.linkAnchor} (not found in current file)`
462
+ `- ${error.type}: #${error.linkAnchor} (not found in current file)`
449
463
  );
450
464
  //console.log(` ${error.type}: #${error.linkAnchor} (heading/anchor missing?)`);
451
465
  //console.log(` #${error.linkAnchor} - Internal anchor not found`);
@@ -456,7 +470,7 @@ function outputErrors(results) {
456
470
  // missing anchor in linked file that exists.
457
471
  //console.log(error);
458
472
  console.log(
459
- ` ${error.type}: #${error.linkAnchor} not found in ${error.linkUrlFilePath}`
473
+ `- ${error.type}: #${error.linkAnchor} not found in ${error.linkUrlFilePath}`
460
474
  );
461
475
  //console.log(` ${error.type}: #${error.linkAnchor} (heading/anchor missing?)`);
462
476
  //console.log(` #${error.linkAnchor} - Internal anchor not found`);
@@ -464,7 +478,7 @@ function outputErrors(results) {
464
478
  //console.log(` Internal anchor not found: #${error.linkAnchor} `);
465
479
  // { "type": "InternalMissingAnchor", "page": `${page.page_file}`, "linkAnchor": `${link.linkAnchor}`, "linkUrl": `${link.linkUrl}`, "linktext": `${link.linkText}`, "linkUrlFilePath": `${linkAbsoluteFilePath}` };
466
480
  } else if (error.type == "InternalLinkToHTML") {
467
- console.log(` ${error.type}: ${error.linkUrl} (should be ".md"?)`);
481
+ console.log(`- ${error.type}: ${error.linkUrl} (should be ".md"?)`);
468
482
  //console.log(` ${error.type}: linkURL: ${error.linkUrl} ends in ".html"`);
469
483
  // { "type": "InternalLinkToHTML", "page": `${page.page_file}`, "linkUrl": `${link.linkUrl}`, "linkText": `${link.linkText}`, "linkUrlFilePath": `${linkAbsoluteFilePath}` };
470
484
  } else {
@@ -477,6 +491,17 @@ function outputErrors(results) {
477
491
  }
478
492
 
479
493
  (async () => {
494
+ options.files
495
+ ? (options.files = await loadJSONFileToReportOn(options.files))
496
+ : (options.files = []);
497
+ if (options.log == "quick") {
498
+ for (const file of options.files) {
499
+ console.log(file);
500
+ }
501
+ }
502
+
503
+ //const object = filesToProcessJSONFilePath ? await convertFileToObject(filePath) : [];
504
+
480
505
  const results = await processDirectory(
481
506
  options.directory,
482
507
  options.headingAnchorSlugify
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown_link_checker_sc",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "Markdown Link Checker",
5
5
  "main": "index.js",
6
6
  "scripts": {