-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: tests local links in markdown documents
PR-URL: #32359 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
- Loading branch information
Showing
6 changed files
with
84 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const { Worker, isMainThread, workerData: path } = require('worker_threads'); | ||
|
||
function* getLinksRecursively(node) { | ||
if ( | ||
(node.type === 'link' && !node.url.startsWith('#')) || | ||
node.type === 'image' | ||
) { | ||
yield node; | ||
} | ||
for (const child of node.children || []) { | ||
yield* getLinksRecursively(child); | ||
} | ||
} | ||
|
||
if (isMainThread) { | ||
const { extname, join, resolve } = require('path'); | ||
const DIR = resolve(process.argv[2]); | ||
|
||
console.log('Running Markdown link checker...'); | ||
|
||
async function* findMarkdownFilesRecursively(dirPath) { | ||
const fileNames = await fs.promises.readdir(dirPath); | ||
|
||
for (const fileName of fileNames) { | ||
const path = join(dirPath, fileName); | ||
|
||
const stats = await fs.promises.stat(path); | ||
if ( | ||
stats.isDirectory() && | ||
fileName !== 'api' && | ||
fileName !== 'deps' && | ||
fileName !== 'node_modules' | ||
) { | ||
yield* findMarkdownFilesRecursively(path); | ||
} else if (extname(fileName) === '.md') { | ||
yield path; | ||
} | ||
} | ||
} | ||
|
||
function errorHandler(error) { | ||
console.error(error); | ||
process.exitCode = 1; | ||
} | ||
|
||
setImmediate(async () => { | ||
for await (const path of findMarkdownFilesRecursively(DIR)) { | ||
new Worker(__filename, { workerData: path }).on('error', errorHandler); | ||
} | ||
}); | ||
} else { | ||
const unified = require('unified'); | ||
const { pathToFileURL } = require('url'); | ||
|
||
const tree = unified() | ||
.use(require('remark-parse')) | ||
.parse(fs.readFileSync(path)); | ||
|
||
const base = pathToFileURL(path); | ||
for (const node of getLinksRecursively(tree)) { | ||
const targetURL = new URL(node.url, base); | ||
if (targetURL.protocol === 'file:' && !fs.existsSync(targetURL)) { | ||
const error = new Error('Broken link in a Markdown document.'); | ||
const { start } = node.position; | ||
error.stack = | ||
error.stack.substring(0, error.stack.indexOf('\n') + 5) + | ||
`at ${node.type} (${path}:${start.line}:${start.column})`; | ||
throw error; | ||
} | ||
} | ||
} |