-
Notifications
You must be signed in to change notification settings - Fork 87
/
verify-guide-links.js
42 lines (37 loc) · 1.06 KB
/
verify-guide-links.js
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
import getGuides from '../src/routes/guide/[lang]/_data.js';
const sections = JSON.parse(getGuides().en);
const ids = new Map();
const errors = [];
function addId(id, title) {
if (ids.has(id)) {
errors.push(
`The id "${id}" in section "${title}" conflicts with the same id in section "${ids.get(id)}".`
);
}
ids.set(id, title);
}
const idRegExp = / id="[^"]+/g;
for (const section of sections) {
addId(section.slug, section.metadata.title);
const matches = section.html.match(idRegExp) || [];
for (const match of matches) {
addId(match.slice(5), section.metadata.title);
}
}
const linkRegExp = / href="guide\/en\/#[^"]+/g;
for (const section of sections) {
const matches = section.html.match(linkRegExp) || [];
for (const match of matches) {
const id = match.slice(17);
if (!ids.has(id)) {
errors.push(
`The link "${id}" in section "${section.metadata.title}" does not correspond to an existing id.`
);
}
}
}
if (errors.length > 0) {
throw new Error(errors.join('\n'));
} else {
console.log('All ids are unique, all anchor targets found.');
}