forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-github-docs.js
More file actions
177 lines (144 loc) · 4.35 KB
/
check-github-docs.js
File metadata and controls
177 lines (144 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import chalk from "chalk";
import cheerio from "cheerio";
import fetch from "node-fetch";
function getPageUrl(url) {
if (typeof url === "string") url = new URL(url);
// Use the URL w/out the hash.
return new URL(url.origin + url.pathname + url.search);
}
const pageCache = {};
function createPage(url) {
let pageUrl = getPageUrl(url);
let cachedPage = pageCache[pageUrl];
if (cachedPage) return cachedPage;
let page = {
url: pageUrl,
anchors: null,
links: null,
checkedLinks: [],
brokenLinks: []
};
pageCache[page.url] = page;
return page;
}
async function getPageAnchorsAndLinks(page) {
if (page.anchors && page.links) return;
let res;
try {
res = await fetch(page.url.toString());
} catch (error) {
console.error(error.message);
return null;
}
if (res.status !== 200) {
throw new Error(`${res.status} error fetching URL: ${page.url}`);
}
let $ = cheerio.load(await res.text());
page.anchors = [];
page.links = [];
// GH puts all user-generated markdown in a <div class="markdown-body">
let isGitHubMarkdown = false;
let selectorContext = undefined;
if (page.url.hostname === "github.com") {
let $markdownBody = $(".markdown-body");
if ($markdownBody.length > 0) {
isGitHubMarkdown = true;
selectorContext = ".markdown-body";
}
}
$("[id]", selectorContext).each((index, a) => {
let id = $(a).attr("id");
// GH prefixes the ids of links that point to themselves with the
// string "user-content-", so you end up with links like
// <a id="user-content-react-router" href="#react-router">
// GH makes these links work by using JavaScript to adjust the page's scroll
// position when the URL fragment id matches an anchor with the "user-content-"
// prefix, so just treat this link as if it had the correct id to begin with
if (isGitHubMarkdown && id.startsWith("user-content-")) {
id = id.replace(/^user-content-/, "");
}
page.anchors.push({ id, text: $(a).text() });
});
$("a[href]", selectorContext).each((index, a) => {
let to = new URL($(a).attr("href"), page.url);
page.links.push({ to, text: $(a).text() });
});
}
function defaultShouldCheckPage(url) {
return true;
}
function defaultShouldCheckLink(url) {
return true;
}
async function checkPageLinks(page, options = {}, checkedPages = []) {
let {
shouldCheckPage = defaultShouldCheckPage,
shouldCheckLink = defaultShouldCheckLink
} = options;
console.log(`Checking ${page.url} ...`);
checkedPages.push(page);
await getPageAnchorsAndLinks(page);
for (let link of page.links) {
if (!shouldCheckLink(link)) continue;
page.checkedLinks.push(link);
// Make sure the link points to a valid page.
let linkedPage = createPage(link.to);
try {
await getPageAnchorsAndLinks(linkedPage);
} catch (error) {
page.brokenLinks.push(link);
continue;
}
// Make sure the link points to a valid anchor on that page.
if (link.to.hash) {
let id = link.to.hash.slice(1);
let anchor = linkedPage.anchors.find(a => a.id === id);
if (anchor == null) {
page.brokenLinks.push(link);
}
}
// Check the page it links to.
if (!checkedPages.includes(linkedPage) && shouldCheckPage(linkedPage)) {
await checkPageLinks(linkedPage, options, checkedPages);
}
}
return checkedPages;
}
const startPage = createPage(
"https://github.com/ReactTraining/react-router/tree/dev/docs"
);
checkPageLinks(startPage, {
shouldCheckPage(page) {
return (
page.url.hostname === "github.com" &&
/^\/ReactTraining\/react-router\/(tree|blob)\/dev\/docs/i.test(
page.url.pathname
)
);
},
shouldCheckLink(link) {
return link.to.hash !== "#TODO";
}
}).then(checkedPages => {
checkedPages.forEach(page => {
let { url, checkedLinks, brokenLinks } = page;
if (brokenLinks.length === 0) {
console.log(
chalk.green(
`Found 0 broken links at ${url} (out of ${checkedLinks.length} total)`
)
);
} else {
console.log(
chalk.red(
`Found ${brokenLinks.length} broken link${
brokenLinks.length === 1 ? "" : "s"
} at ${url} (out of ${checkedLinks.length} total):`
)
);
brokenLinks.forEach(link => {
console.log(" " + link.to);
});
}
});
});