|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const fs = require('fs'); |
4 | | -const { Worker, isMainThread, workerData: path } = require('worker_threads'); |
| 4 | +const { extname, join, resolve } = require('path'); |
| 5 | +const unified = require('unified'); |
| 6 | +const { pathToFileURL } = require('url'); |
| 7 | +const DIR = resolve(process.argv[2]); |
| 8 | + |
| 9 | +console.log('Running Markdown link checker...'); |
| 10 | +findMarkdownFilesRecursively(DIR); |
5 | 11 |
|
6 | 12 | function* getLinksRecursively(node) { |
7 | | - if ( |
8 | | - (node.type === 'link' && !node.url.startsWith('#')) || |
9 | | - node.type === 'image' |
10 | | - ) { |
| 13 | + if (node.url && !node.url.startsWith('#')) { |
11 | 14 | yield node; |
12 | 15 | } |
13 | 16 | for (const child of node.children || []) { |
14 | 17 | yield* getLinksRecursively(child); |
15 | 18 | } |
16 | 19 | } |
17 | 20 |
|
18 | | -if (isMainThread) { |
19 | | - const { extname, join, resolve } = require('path'); |
20 | | - const DIR = resolve(process.argv[2]); |
21 | | - |
22 | | - console.log('Running Markdown link checker...'); |
23 | | - |
24 | | - async function* findMarkdownFilesRecursively(dirPath) { |
25 | | - const fileNames = await fs.promises.readdir(dirPath); |
26 | | - |
27 | | - for (const fileName of fileNames) { |
28 | | - const path = join(dirPath, fileName); |
29 | | - |
30 | | - const stats = await fs.promises.stat(path); |
31 | | - if ( |
32 | | - stats.isDirectory() && |
33 | | - fileName !== 'api' && |
34 | | - fileName !== 'deps' && |
35 | | - fileName !== 'node_modules' |
36 | | - ) { |
37 | | - yield* findMarkdownFilesRecursively(path); |
38 | | - } else if (extname(fileName) === '.md') { |
39 | | - yield path; |
40 | | - } |
| 21 | +function findMarkdownFilesRecursively(dirPath) { |
| 22 | + const entries = fs.readdirSync(dirPath, { withFileTypes: true }); |
| 23 | + |
| 24 | + for (const entry of entries) { |
| 25 | + const path = join(dirPath, entry.name); |
| 26 | + |
| 27 | + if ( |
| 28 | + entry.isDirectory() && |
| 29 | + entry.name !== 'api' && |
| 30 | + entry.name !== 'deps' && |
| 31 | + entry.name !== 'node_modules' |
| 32 | + ) { |
| 33 | + findMarkdownFilesRecursively(path); |
| 34 | + } else if (entry.isFile() && extname(entry.name) === '.md') { |
| 35 | + checkFile(path); |
41 | 36 | } |
42 | 37 | } |
| 38 | +} |
43 | 39 |
|
44 | | - function errorHandler(error) { |
45 | | - console.error(error); |
46 | | - process.exitCode = 1; |
47 | | - } |
48 | | - |
49 | | - setImmediate(async () => { |
50 | | - for await (const path of findMarkdownFilesRecursively(DIR)) { |
51 | | - new Worker(__filename, { workerData: path }).on('error', errorHandler); |
52 | | - } |
53 | | - }); |
54 | | -} else { |
55 | | - const unified = require('unified'); |
56 | | - const { pathToFileURL } = require('url'); |
57 | | - |
| 40 | +function checkFile(path) { |
58 | 41 | const tree = unified() |
59 | 42 | .use(require('remark-parse')) |
60 | 43 | .parse(fs.readFileSync(path)); |
@@ -63,12 +46,9 @@ if (isMainThread) { |
63 | 46 | for (const node of getLinksRecursively(tree)) { |
64 | 47 | const targetURL = new URL(node.url, base); |
65 | 48 | if (targetURL.protocol === 'file:' && !fs.existsSync(targetURL)) { |
66 | | - const error = new Error('Broken link in a Markdown document.'); |
67 | | - const { start } = node.position; |
68 | | - error.stack = |
69 | | - error.stack.substring(0, error.stack.indexOf('\n') + 5) + |
70 | | - `at ${node.type} (${path}:${start.line}:${start.column})`; |
71 | | - throw error; |
| 49 | + const { line, column } = node.position.start; |
| 50 | + console.error(`Broken link at ${path}:${line}:${column} (${node.url})`); |
| 51 | + process.exitCode = 1; |
72 | 52 | } |
73 | 53 | } |
74 | 54 | } |