-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-validator.js
More file actions
89 lines (78 loc) · 2.58 KB
/
html-validator.js
File metadata and controls
89 lines (78 loc) · 2.58 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
//Original code from blog post: https://sandroroth.com/blog/eleventy-html-validation
/** @type {import("html-validate").ConfigData} */
const validateConfig = {
extends: ["html-validate:recommended"],
elements: [
"html5",
{
th: {
attributes: {
scope: {
required: false
}
}
}
}
],
rules: {
"no-trailing-whitespace": "off",
"void-style": "off",
"no-inline-style": "off",
"long-title": "off",
"no-raw-characters": "off",
"wcag/h63": "off"
}
};
const { HtmlValidate, StaticConfigLoader } = require("html-validate");
const loader = new StaticConfigLoader(validateConfig);
const htmlValidate = new HtmlValidate(loader);
const fs = require("fs");
const { green, red } = require("kleur");
exports.results = {};
exports.storeResults = function storeResults() {
const resultFilePath = "./dist/html-validation.json";
const totalErrors = Object.values(exports.results)
.filter(val => !!val)
.reduce((acc, cur) => {
return (acc = acc + cur.errorCount);
}, 0);
const totalFileCount = Object.values(exports.results).filter(
file => !!file
).length;
let results = {};
results["Total error count"] = totalErrors;
results["Files with errors"] = totalFileCount;
results["Results file path"] = resultFilePath;
if (totalErrors > 0) {
const content = JSON.stringify(exports.results, null, 2);
fs.writeFileSync(resultFilePath, content);
console.table(results);
} else {
console.log(green(`✅ No html errors!`));
}
};
exports.validate = function validate(content) {
if (!this.outputPath) return;
// Only validate 'html' files
if (this.outputPath.split(".").pop() !== "html") return;
const validationResult = htmlValidate.validateString(content);
const lines = content.split(/\r?\n/);
validationResult.results.forEach(result => {
result.filePath = this.outputPath;
result.messages.forEach(message => {
// @ts-ignore
message.codeLines = lines.slice(
// Three lines before + actual line
Math.max(message.line - 4, 0),
// Three lines after
message.line + 3
);
});
});
if (!validationResult.valid) {
console.log(red(`❌ ${this.outputPath}`));
}
exports.results[this.outputPath] = validationResult.valid
? undefined
: validationResult;
};