w3c-validate-html
v1.1.0
Published
Validate HTML offline using the official W3C vnu.jar
Maintainers
Readme
w3c-validate-html
Validate HTML offline using the official W3C vnu.jar
Why? Modern build tools can introduce HTML bugs. w3c-validate-html runs locally and prints concise, clickable errors with line numbers using the same rules as the online W3C validator.
CLI
The easiest way to use this is from the CLI using npx, for example:
# validate a website recursively (default depth 2)
npx w3c-validate-html --target https://example.com --depth 1 --errors-only
# Validate a folder, fail only on errors
npx w3c-validate-html --target ./public --errors-onlyOptions
Option | Alias | Type | Default | Description :-------------|:------|:--------|:-------------------|:--------------------------------------- --target | -t | string | | File, folder, URL or string to validate --depth | | number | 2 | Crawl depth for website validation --concurrency | | number | 4 | Number of concurrent validations --warnings | | number | 1 | Show warnings (0 = off, 1 = on) --exclude | | string | | Comma/space separated URLs to exclude --errors-only | -e | boolean | false | Only show errors --json | | boolean | false | Output results as JSON --same-origin | | boolean | true | Restrict crawl to same origin --strip-query | | boolean | false | Exclude URLs with query strings --user-agent | | string | Mozilla/5.0 (node) | Custom user agent for requests
Output
Errors and warnings include clickable file:line:col links for quick editor navigation.
✖ public/invalid.html
End tag for "h1" seen, but there were unclosed elements. public/invalid.html:7:5
Unclosed element "h1". public/invalid.html:7:5
End of file seen when expecting text or an end tag. public/invalid.html:9:1
✔ public/valid.htmlNode module
You can use this package as a node module to validate a URL, file/folder, or raw HTML string:
Validate a URL
const validate = require('w3c-validate-html');
validate('https://example.com', { warnings: 1, depth: 0 }).then(function(summary) {
console.log(summary);
})
.catch((err) => {
console.error(err);
});Validate a local file or folder
const validate = require('w3c-validate-html');
validate('./tests/fixtures/valid.html', { warnings: 1 }).then(function(summary) {
console.log(summary);
})
.catch((err) => {
console.error(err);
});Validate a HTML string
const validate = require('w3c-validate-html');
var html = '<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Hi</h1></body></html>';
validate(html).then(function(result) {
console.log(result);
})
.catch((err) => {
console.error(err);
});Example response
{
"passed": 0,
"failed": 1,
"results": [
{
"ok": false,
"errors": [
{ "line": 7, "col": 5, "msg": "End tag for \"h1\" seen, but there were unclosed elements." },
{ "line": 7, "col": 5, "msg": "Unclosed element \"h1\"." },
{ "line": 9, "col": 1, "msg": "End of file seen when expecting text or an end tag." }
],
"warnings": []
}
]
}GitHub Action
You can use this in your CI as a Github Action to validate your site's HTML on every push and pull request. The job fails if any pages have HTML errors and a html-report.json artifact is uploaded for review.
name: html-validate
on: [push, pull_request]
jobs:
html-validate:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v4
with:
node-version: 18
- name: validate url
env:
TARGET_URL: https://example.com
run: npx w3c-validate-html --target "$TARGET_URL" --depth 2 --errors-only --json > html-report.json
- uses: actions/upload-artifact@v4
with:
name: html-report
path: html-report.jsonSee also
License
MIT License © Orca Scan - a barcode app with simple barcode tracking APIs.
