-
Notifications
You must be signed in to change notification settings - Fork 168
/
rules.js
95 lines (85 loc) · 2.63 KB
/
rules.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const Rule = require("html-validate").Rule;
class HtmlTitle extends Rule {
documentation(context) {
return {
description: "Title of the page must be defined"
};
}
setup() {
this.on("dom:ready", event => {
const title = event.document.querySelector('html head title');
if (title === null) {
this.report(event.document.querySelector('html'), "Missing <title> tag in the <head>");
}
else if (!title.textContent || title.textContent.trim().length === 0) {
this.report(title, "No title specified");
}
else if (title.textContent[0] === '-') {
this.report(title, "Title starts with the dash `-`, add the `title` front matter to the Hugo content");
}
else if (title.textContent.startsWith('Untitled')) {
this.report(title, "Title starts with the `Untitled` make sure that the Asciidoc file starts with level 1 heading");
}
});
}
}
class RelativeLinks extends Rule {
documentation(context) {
return {
description: "Within the Camel site we should use only relative links"
};
}
setup() {
this.on("dom:ready", event => {
const anchors = event.document.getElementsByTagName('a');
if (anchors === null) {
return;
}
anchors.forEach(a => {
const href = a.getAttribute("href");
if (href && href.value.startsWith("https://camel.apache.org")) {
this.report(a, `For links within camel.apache.org use relative links, found: ${href.value}`);
}
});
});
}
}
class StructuredData extends Rule {
documentation(context) {
return {
description: "Validates JSON-LD according to schema.org"
};
}
setup() {
let start = -1;
this.on('tag:open', event => {
if (event.target.nodeName === 'script') {
start = event.target.location.offset;
}
});
this.on('tag:close', async event => {
const tag = event.previous;
if (event.target.nodeName === 'script' && tag.hasAttribute('type') && tag.getAttribute('type').value === 'application/ld+json') {
const startIdx = data.indexOf('>', start) + 1;
const endIdx = event.target.location.offset - 1; // omit the opening tag angled bracket
const content = data.substring(startIdx, endIdx);
try {
JSON.parse(content);
} catch (err) {
this.report(tag, `Unable to parse JSON-LD as JSON: ${err}`);
}
}
});
}
}
let data;
module.exports = {
setup(source) {
data = source.data;
},
rules: {
"camel/title": HtmlTitle,
"camel/relative-links": RelativeLinks,
"camel/structured-data": StructuredData
}
};