Skip to content

Commit e51c42d

Browse files
aduh95targos
authored andcommittedApr 22, 2020
doc: tests local links in markdown documents
PR-URL: #32359 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 9c84d77 commit e51c42d

File tree

6 files changed

+84
-9
lines changed

6 files changed

+84
-9
lines changed
 

‎Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ test-doc: doc-only lint ## Builds, lints, and verifies the docs.
629629
else \
630630
$(PYTHON) tools/test.py $(PARALLEL_ARGS) doctool; \
631631
fi
632+
$(NODE) tools/doc/checkLinks.js .
632633

633634
test-known-issues: all
634635
$(PYTHON) tools/test.py $(PARALLEL_ARGS) known_issues

‎doc/guides/collaborator-guide.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
This document explains how Collaborators manage the Node.js project.
3939
Collaborators should understand the
40-
[guidelines for new contributors](CONTRIBUTING.md) and the
41-
[project governance model](GOVERNANCE.md).
40+
[guidelines for new contributors](../../CONTRIBUTING.md) and the
41+
[project governance model](../../GOVERNANCE.md).
4242

4343
## Issues and Pull Requests
4444

@@ -50,7 +50,7 @@ request. See [Who to CC in the issue tracker](#who-to-cc-in-the-issue-tracker).
5050

5151
Always show courtesy to individuals submitting issues and pull requests. Be
5252
welcoming to first-time contributors, identified by the GitHub
53-
![First-time contributor](./doc/first_timer_badge.png) badge.
53+
![First-time contributor](../first_timer_badge.png) badge.
5454

5555
For first-time contributors, check if the commit author is the same as the pull
5656
request author. This way, once their pull request lands, GitHub will show them
@@ -474,7 +474,7 @@ $ git checkout master
474474
```
475475

476476
Update the tree (assumes your repo is set up as detailed in
477-
[CONTRIBUTING.md](./doc/guides/contributing/pull-requests.md#step-1-fork)):
477+
[CONTRIBUTING.md](./contributing/pull-requests.md#step-1-fork)):
478478

479479
```text
480480
$ git fetch upstream

‎doc/guides/cpp-style-guide.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# C++ Style Guide
22

3-
See also the [C++ codebase README](src/README.md) for C++ idioms in the Node.js
4-
codebase not related to stylistic issues.
3+
See also the [C++ codebase README](../../src/README.md) for C++ idioms in the
4+
Node.js codebase not related to stylistic issues.
55

66
## Table of Contents
77

‎doc/guides/releases.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ accordingly by removing the bold styling from the previous release.
322322
If this release includes new APIs then it is necessary to document that they
323323
were first added in this version. The relevant commits should already include
324324
`REPLACEME` tags as per the example in the
325-
[docs README](../tools/doc/README.md). Check for these tags with `grep REPLACEME
326-
doc/api/*.md`, and substitute this node version with `sed -i
325+
[docs README](../../tools/doc/README.md). Check for these tags with `grep
326+
REPLACEME doc/api/*.md`, and substitute this node version with `sed -i
327327
"s/REPLACEME/$VERSION/g" doc/api/*.md` or `perl -pi -e "s/REPLACEME/$VERSION/g"
328328
doc/api/*.md`.
329329

‎onboarding.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ onboarding session.
8383
* Be nice about closing issues! Let people know why, and that issues and PRs
8484
can be reopened if necessary
8585

86-
* [**See "Labels"**](./onboarding-extras.md#labels)
86+
* [**See "Labels"**](./doc/guides/onboarding-extras.md#labels)
8787
* There is [a bot](https://github.com/nodejs-github-bot/github-bot) that
8888
applies subsystem labels (for example, `doc`, `test`, `assert`, or `buffer`)
8989
so that we know what parts of the code base the pull request modifies. It is

‎tools/doc/checkLinks.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const { Worker, isMainThread, workerData: path } = require('worker_threads');
5+
6+
function* getLinksRecursively(node) {
7+
if (
8+
(node.type === 'link' && !node.url.startsWith('#')) ||
9+
node.type === 'image'
10+
) {
11+
yield node;
12+
}
13+
for (const child of node.children || []) {
14+
yield* getLinksRecursively(child);
15+
}
16+
}
17+
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+
}
41+
}
42+
}
43+
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+
58+
const tree = unified()
59+
.use(require('remark-parse'))
60+
.parse(fs.readFileSync(path));
61+
62+
const base = pathToFileURL(path);
63+
for (const node of getLinksRecursively(tree)) {
64+
const targetURL = new URL(node.url, base);
65+
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;
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)
Please sign in to comment.