mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
- Migrated to ESM because some dependencies now require it. - Did not update `highlight.js` to v11 because it has many breaking changes. - Used non-deprecated `highlight.js` API. Refs: https://github.com/highlightjs/highlight.js/issues/2277 Fixes: https://github.com/nodejs/node/issues/38938 Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: https://github.com/nodejs/node/pull/38966 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
79 lines
2.3 KiB
JavaScript
79 lines
2.3 KiB
JavaScript
import fs from 'fs';
|
|
import { extname, join, resolve } from 'path';
|
|
import { pathToFileURL } from 'url';
|
|
|
|
import gfm from 'remark-gfm';
|
|
import markdown from 'remark-parse';
|
|
import unified from 'unified';
|
|
|
|
const DIR = resolve(process.argv[2]);
|
|
|
|
console.log('Running Markdown link checker...');
|
|
findMarkdownFilesRecursively(DIR);
|
|
|
|
function* getLinksRecursively(node) {
|
|
if (node.url && !node.url.startsWith('#')) {
|
|
yield node;
|
|
}
|
|
for (const child of node.children || []) {
|
|
yield* getLinksRecursively(child);
|
|
}
|
|
}
|
|
|
|
function findMarkdownFilesRecursively(dirPath) {
|
|
const entries = fs.readdirSync(dirPath, { withFileTypes: true });
|
|
|
|
for (const entry of entries) {
|
|
const path = join(dirPath, entry.name);
|
|
|
|
if (
|
|
entry.isDirectory() &&
|
|
entry.name !== 'build' &&
|
|
entry.name !== 'changelogs' &&
|
|
entry.name !== 'deps' &&
|
|
entry.name !== 'fixtures' &&
|
|
entry.name !== 'gyp' &&
|
|
entry.name !== 'node_modules' &&
|
|
entry.name !== 'out' &&
|
|
entry.name !== 'tmp'
|
|
) {
|
|
findMarkdownFilesRecursively(path);
|
|
} else if (entry.isFile() && extname(entry.name) === '.md') {
|
|
checkFile(path);
|
|
}
|
|
}
|
|
}
|
|
|
|
function checkFile(path) {
|
|
const tree = unified()
|
|
.use(markdown)
|
|
.use(gfm)
|
|
.parse(fs.readFileSync(path));
|
|
|
|
const base = pathToFileURL(path);
|
|
let previousDefinitionLabel;
|
|
for (const node of getLinksRecursively(tree)) {
|
|
const targetURL = new URL(node.url, base);
|
|
if (targetURL.protocol === 'file:' && !fs.existsSync(targetURL)) {
|
|
const { line, column } = node.position.start;
|
|
console.error((process.env.GITHUB_ACTIONS ?
|
|
`::error file=${path},line=${line},col=${column}::` : '') +
|
|
`Broken link at ${path}:${line}:${column} (${node.url})`);
|
|
process.exitCode = 1;
|
|
}
|
|
if (node.type === 'definition') {
|
|
if (previousDefinitionLabel &&
|
|
previousDefinitionLabel > node.label) {
|
|
const { line, column } = node.position.start;
|
|
console.error((process.env.GITHUB_ACTIONS ?
|
|
`::error file=${path},line=${line},col=${column}::` : '') +
|
|
`Unordered reference at ${path}:${line}:${column} (` +
|
|
`"${node.label}" should be before "${previousDefinitionLabel}")`
|
|
);
|
|
process.exitCode = 1;
|
|
}
|
|
previousDefinitionLabel = node.label;
|
|
}
|
|
}
|
|
}
|