From c6ccd97fe28f2c00dccf65f3532d0a6d75d16fe0 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 30 Jun 2021 12:19:24 +0200 Subject: [PATCH] doc,tools: remove `checkLinks.mjs` The checks made by this are now integrated to `remark-preset-lint-node`, there are no reason to keep it around anymore. PR-URL: https://github.com/nodejs/node/pull/39206 Reviewed-By: Rich Trott --- Makefile | 2 -- tools/doc/checkLinks.mjs | 77 ---------------------------------------- 2 files changed, 79 deletions(-) delete mode 100644 tools/doc/checkLinks.mjs diff --git a/Makefile b/Makefile index 8ee578f98a0168..a339c3f96f6a2e 100644 --- a/Makefile +++ b/Makefile @@ -578,12 +578,10 @@ test-doc: doc-only lint-md ## Builds, lints, and verifies the docs. else \ $(PYTHON) tools/test.py $(PARALLEL_ARGS) doctool; \ fi - $(NODE) tools/doc/checkLinks.mjs . .PHONY: test-doc-ci test-doc-ci: doc-only $(PYTHON) tools/test.py --shell $(NODE) $(TEST_CI_ARGS) $(PARALLEL_ARGS) doctool - $(NODE) tools/doc/checkLinks.mjs . test-known-issues: all $(PYTHON) tools/test.py $(PARALLEL_ARGS) known_issues diff --git a/tools/doc/checkLinks.mjs b/tools/doc/checkLinks.mjs deleted file mode 100644 index cb302ea27fefb6..00000000000000 --- a/tools/doc/checkLinks.mjs +++ /dev/null @@ -1,77 +0,0 @@ -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; - } - } -}