-
Notifications
You must be signed in to change notification settings - Fork 344
/
.link-checker.js
78 lines (74 loc) · 2.65 KB
/
.link-checker.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
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
const HTMLParser = require('node-html-parser');
// Checks object for attribute and returns value.
// If not found on first pass, recursively checks
// nested objects and arrays of nested object(s)
// until attribute is found. If not found,
// returns undefined.
const getAttributeValue = (obj, attribute) => {
if (typeof obj !== 'object' || obj === null) return undefined;
if (Object.prototype.hasOwnProperty.call(obj, attribute))
return obj[attribute];
if (Array.isArray(obj)) {
for (const element of obj) {
const attributeValue = getAttributeValue(element, attribute);
if (attributeValue !== undefined) return attributeValue;
}
} else {
for (const key in obj) {
const attributeValue = getAttributeValue(obj[key], attribute);
if (attributeValue !== undefined) return attributeValue;
}
}
return undefined;
};
module.exports = {
filesToIgnore: [
// For example:
// 'content/shared/templates/example-usage-warning.html',
],
excludedLinks: {
'content/patterns/menubar/examples/menubar-navigation.html': [
'#ex1 [role=menuitem]',
],
'content/patterns/treeview/examples/treeview-navigation.html': [
'#ex1 [role=treeitem]',
],
'content/patterns/carousel/examples/carousel-2-tablist.html': [
'.carousel-image a',
],
},
hashCheckHandlers: [
{
name: 'github',
pattern: /^https:\/\/github\.com\/.*/,
matchHash: (ids, hash, { reactPartial }) => {
if (reactPartial) {
// This is where the react-partial keeps data about READMEs and other *.md files
const richText = getAttributeValue(reactPartial, 'richText');
if (richText !== undefined) {
const html = HTMLParser.parse(richText);
const githubIds = html
.querySelectorAll('[id]')
.map((idElement) => idElement.getAttribute('id'));
return githubIds.includes(`user-content-${hash}`);
}
}
return ids.includes(hash) || ids.includes(`user-content-${hash}`);
},
getPartial: (html) => {
return html
.querySelectorAll('react-partial')
.filter(
(partialElement) =>
partialElement.getAttribute('partial-name') === 'repos-overview' // This is the partial that handles the READMEs
)
.flatMap((element) => element.getElementsByTagName('script'))
.map((element) => JSON.parse(element.innerHTML))[0];
},
},
],
ignoreHashesOnExternalPagesMatchingRegex: [
// Some hash links are resolved with JS and are therefore difficult to check algorithmically
/^https:\/\/html\.spec\.whatwg\.org\/multipage\//,
],
};