A Node module with optional settings for validating HTML pages using the "validator.w3.org/nu" or "whatwg" standard. Forked from html-validator
Available on npm:
npm install @saiballo/html-validator-enhanced --save
Create a function like this if you want to validate a html fragment:
(async () => {
const validator = require("@saiballo/html-validator-enhanced");
const options = {
data: "<p>not correct</h1></p>",
isFragment: true,
format: "json"
}
try {
const result = await validator(options);
console.log(result);
} catch (error) {
console.error(error);
}
})()
If you need to validate a local page with "data" attribute:
(async () => {
const validator = require("@saiballo/html-validator-enhanced");
const fs = require("node:fs");
const options = {
data: fs.readFileSync("page-to-validate.html", "utf8"),
format: "json"
}
try {
const result = await validator(options);
console.log(result);
} catch (error) {
console.error(error);
}
})()
The complete default configuration that you can override is:
const options = {
"validator": "WHATWG",
"isLocal": false,
"isFragment": false,
"format": "json",
"ignore": [],
"whatwgConfig": {
"elements": [],
"rules": {}
}
}
// Possible values: "WHATWG" | "https://validator.w3.org/nu/"
//"WHATWG" is recommended. it will validate against the WHATWG standards.
"validator": "WHATWG"
// set this to true if you want to validate local urls
"isLocal": false
// set this to true only if all your pages are not a complete document
// if only certain pages are fragments, you can use "isFragment: true" in these pages as front matter data
"isFragment": false
// this is the formatting of the returned data. it supports json (default), html, xhtml, xml, gnu and text for W3C and only json for WHATWG
"format": "json"
// only for "WHATWG" validator. string or array of strings or rules (when using WHATWG) you want the checker to remove in the response. even partial text.
// e.g. "ignore": ["Mismatched close-tag, expected '</div>' but found '</body>'", "another partial error response text"]
"ignore": [],
// only for "WHATWG" validator. additional configuration for elements and rules
"whatwgConfig": {
"elements": [],
"rules": {}
}
// with this example, you add a custom tag called "customtag" to the valid tags in the validation. see https://html-validate.org/guide/metadata/simple-component.html
// additionally, you set the "heading-level" rule to "on" instead of "off". see https://html-validate.org/rules/index.html
"whatwgConfig": {
"elements": [
{
"customtag": {
"flow": true,
"phrasing": true
}
}
],
"rules": {
"heading-level": "error"
}
}
- Lorenzo "Saibal" Forti