1
1
mirror of https://github.com/theoludwig/html-w3c-validator.git synced 2024-12-08 00:45:37 +01:00
html-w3c-validator/README.md

116 lines
4.8 KiB
Markdown
Raw Permalink Normal View History

2022-01-06 19:52:25 +01:00
<h1 align="center">html-w3c-validator</h1>
<p align="center">
2024-02-02 15:44:03 +01:00
<strong>CLI for validating HTML pages using <a href="https://validator.w3.org/">validator.w3.org</a>.</strong>
2022-01-06 19:52:25 +01:00
</p>
<p align="center">
2023-12-26 22:43:05 +01:00
<a href="./CONTRIBUTING.md"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" alt="CONTRIBUTING" /></a>
2022-01-06 19:52:25 +01:00
<a href="./LICENSE"><img src="https://img.shields.io/badge/licence-MIT-blue.svg" alt="Licence MIT"/></a>
<a href="./CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg" alt="Contributor Covenant" /></a>
<br />
2023-12-26 22:43:05 +01:00
<a href="https://github.com/theoludwig/html-w3c-validator/actions/workflows/build.yml"><img src="https://github.com/theoludwig/html-w3c-validator/actions/workflows/build.yml/badge.svg?branch=develop" alt="Build" /></a>
<a href="https://github.com/theoludwig/html-w3c-validator/actions/workflows/lint.yml"><img src="https://github.com/theoludwig/html-w3c-validator/actions/workflows/lint.yml/badge.svg?branch=develop" alt="Lint" /></a>
<a href="https://github.com/theoludwig/html-w3c-validator/actions/workflows/test.yml"><img src="https://github.com/theoludwig/html-w3c-validator/actions/workflows/test.yml/badge.svg?branch=develop" alt="Test" /></a>
2022-01-06 19:52:25 +01:00
<br />
<a href="https://conventionalcommits.org"><img src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg" alt="Conventional Commits" /></a>
<a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg" alt="semantic-release" /></a>
<a href="https://www.npmjs.com/package/html-w3c-validator"><img src="https://img.shields.io/npm/v/html-w3c-validator.svg" alt="npm version"></a>
</p>
## 📜 About
2024-02-02 15:44:03 +01:00
**html-w3c-validator** is a CLI tool to validate HTML pages using [validator.w3.org](https://validator.w3.org/).
2022-01-06 19:52:25 +01:00
You might use a JavaScript framework or simply use HTML but **you should always validate your production HTML** and this validation should be part of your CI/CD pipeline (tests, linting, etc.).
2022-01-06 19:52:25 +01:00
2024-02-02 15:44:03 +01:00
### Why is HTML page validation important?
2022-01-06 19:52:25 +01:00
Quote from [https://validator.w3.org/docs/help.html#why-validate](https://validator.w3.org/docs/help.html#why-validate):
> One of the important maxims of computer programming is: "Be conservative in what you produce; be liberal in what you accept."
>
> Browsers follow the second half of this maxim by accepting Web pages and trying to display them even if they're not legal HTML. Usually this means that the browser will try to make educated guesses about what you probably meant. The problem is that different browsers (or even different versions of the same browser) will make different guesses about the same illegal construct; worse, if your HTML is really pathological, the browser could get hopelessly confused and produce a mangled mess, or even crash.
## ⚙️ Getting Started
You can combine **html-w3c-validator** with [start-server-and-test](https://github.com/bahmutov/start-server-and-test) to validate HTML pages of your project.
### Prerequisites
2024-01-30 01:16:56 +01:00
[Node.js](https://nodejs.org/) >= 16.0.0
2022-01-06 19:52:25 +01:00
### Installation (with [start-server-and-test](https://github.com/bahmutov/start-server-and-test))
```sh
npm install --save-dev html-w3c-validator start-server-and-test
```
## ⚙️ Configuration
### `package.json`
2024-02-02 15:44:03 +01:00
```jsonc
2022-01-06 19:52:25 +01:00
{
"scripts": {
// Command to start the server serving your HTML pages (e.g: using vercel/serve)
2022-02-19 16:55:36 +01:00
"start": "serve \"./build\"",
2022-01-06 19:52:25 +01:00
// Command to validate your HTML pages
2024-02-02 15:44:03 +01:00
"test:html-w3c-validator": "start-server-and-test \"start\" \"http://127.0.0.1:3000\" \"html-w3c-validator\"",
},
2022-01-06 19:52:25 +01:00
}
```
2022-01-06 22:34:58 +01:00
### `.html-w3c-validatorrc.json`
2022-01-06 19:52:25 +01:00
2024-02-02 15:44:03 +01:00
```jsonc
2022-01-06 19:52:25 +01:00
{
"$schema": "./node_modules/html-w3c-validator/schema/schema.json",
2024-02-02 15:35:18 +01:00
// URLs to validate.
"urls": ["http://127.0.0.1:3000/", "http://127.0.0.1:3000/about"],
2024-02-02 15:35:18 +01:00
// Files to validate.
"files": ["./index.html", "./about.html"],
2024-02-02 15:35:18 +01:00
// Specify the severities of the validator messages to report. (default: ["warning", "error"])
2024-02-02 15:44:03 +01:00
"severities": ["info", "warning", "error"],
2022-01-06 19:52:25 +01:00
}
```
## Usage
```sh
node --run test:html-w3c-validator
2022-01-06 19:52:25 +01:00
```
2022-01-06 22:34:58 +01:00
Example of output (in case of success):
2022-01-06 19:52:25 +01:00
```txt
✔ Validating http://127.0.0.1:3000/
✔ Validating http://127.0.0.1:3000/about
2022-01-06 19:52:25 +01:00
2022-01-06 22:34:58 +01:00
Success: HTML validation (W3C) passed! 🎉
2022-01-06 19:52:25 +01:00
```
2022-01-06 22:34:58 +01:00
See the [./example](./example) folder for practical usage.
2022-01-06 19:52:25 +01:00
### Options
```text
2024-02-02 15:44:03 +01:00
--current-working-directory <path> The current working directory (default: `process.cwd()`).
2024-02-02 14:47:31 +01:00
-V, --version Output the version number.
-h, --help Display help for command.
2022-01-06 19:52:25 +01:00
```
## 💡 Contributing
Anyone can help to improve the project, submit a Feature Request, a bug report or even correct a simple spelling mistake.
The steps to contribute can be found in the [CONTRIBUTING.md](./CONTRIBUTING.md) file.
## 📄 License
[MIT](./LICENSE)