1
1
mirror of https://github.com/theoludwig/html-w3c-validator.git synced 2024-07-20 07:30:11 +02:00

feat: support plain HTML files (not served)

fixes #2
This commit is contained in:
Divlo 2022-02-24 11:55:32 +01:00
parent d92235d79d
commit e032bbe637
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
9 changed files with 431 additions and 478 deletions

View File

@ -2,7 +2,7 @@ name: 'Build'
on: on:
push: push:
branches: [master, develop] branches: [develop]
pull_request: pull_request:
branches: [master, develop] branches: [master, develop]

View File

@ -2,7 +2,7 @@ name: 'Lint'
on: on:
push: push:
branches: [master, develop] branches: [develop]
pull_request: pull_request:
branches: [master, develop] branches: [master, develop]

View File

@ -2,7 +2,7 @@ name: 'Test'
on: on:
push: push:
branches: [master, develop] branches: [develop]
pull_request: pull_request:
branches: [master, develop] branches: [master, develop]

View File

@ -24,7 +24,7 @@
**html-w3c-validator** is a CLI tool to validate multiple html pages using [validator.w3.org](https://validator.w3.org/). **html-w3c-validator** is a CLI tool to validate multiple html pages using [validator.w3.org](https://validator.w3.org/).
You might use a JavaScript framework or simply use HTML: **you should validate your production HTML** and this validation should be part of your CI/CD pipeline (tests, linting, etc.). 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.).
### Why should I validate my HTML pages? ### Why should I validate my HTML pages?
@ -66,9 +66,12 @@ npm install --save-dev html-w3c-validator start-server-and-test
### `.html-w3c-validatorrc.json` ### `.html-w3c-validatorrc.json`
```json ```jsonc
{ {
"urls": ["http://localhost:3000/", "http://localhost:3000/about"] "urls": ["http://localhost:3000/", "http://localhost:3000/about"],
// You can also specify HTML files instead of URLs
"files": ["./index.html", "./about.html"]
} }
``` ```

View File

@ -1,3 +1,4 @@
{ {
"urls": ["http://localhost:3000/", "http://localhost:3000/about"] "urls": ["http://localhost:3000/", "http://localhost:3000/about"],
"files": ["./build/index.html", "./build/about.html"]
} }

View File

@ -31,15 +31,16 @@
"@commitlint/cli": "16.2.1", "@commitlint/cli": "16.2.1",
"@commitlint/config-conventional": "16.2.1", "@commitlint/config-conventional": "16.2.1",
"@swc/cli": "0.1.55", "@swc/cli": "0.1.55",
"@swc/core": "1.2.143", "@swc/core": "1.2.144",
"@swc/jest": "0.2.17", "@swc/jest": "0.2.17",
"@types/html-validator": "5.0.2", "@types/html-validator": "5.0.2",
"@types/jest": "27.4.0", "@types/jest": "27.4.1",
"@types/mock-fs": "4.13.1", "@types/mock-fs": "4.13.1",
"@types/node": "17.0.18", "@types/node": "17.0.21",
"@typescript-eslint/eslint-plugin": "5.12.1",
"editorconfig-checker": "4.0.2", "editorconfig-checker": "4.0.2",
"eslint": "8.9.0", "eslint": "8.9.0",
"eslint-config-conventions": "1.0.1", "eslint-config-conventions": "1.1.0",
"eslint-config-prettier": "8.4.0", "eslint-config-prettier": "8.4.0",
"eslint-plugin-import": "2.25.4", "eslint-plugin-import": "2.25.4",
"eslint-plugin-prettier": "4.0.0", "eslint-plugin-prettier": "4.0.0",
@ -53,7 +54,7 @@
"lint-staged": "12.3.4", "lint-staged": "12.3.4",
"markdownlint-cli": "0.31.1", "markdownlint-cli": "0.31.1",
"mock-fs": "5.1.2", "mock-fs": "5.1.2",
"pinst": "2.1.6", "pinst": "3.0.0",
"prettier": "2.5.1", "prettier": "2.5.1",
"rimraf": "3.0.2", "rimraf": "3.0.2",
"semantic-release": "19.0.2", "semantic-release": "19.0.2",
@ -1810,8 +1811,7 @@
"../node_modules/@types/json-schema": { "../node_modules/@types/json-schema": {
"version": "7.0.9", "version": "7.0.9",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT"
"peer": true
}, },
"../node_modules/@types/json5": { "../node_modules/@types/json5": {
"version": "0.0.29", "version": "0.0.29",
@ -1878,7 +1878,6 @@
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"@typescript-eslint/scope-manager": "5.12.0", "@typescript-eslint/scope-manager": "5.12.0",
"@typescript-eslint/type-utils": "5.12.0", "@typescript-eslint/type-utils": "5.12.0",
@ -1938,7 +1937,6 @@
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"@typescript-eslint/types": "5.12.0", "@typescript-eslint/types": "5.12.0",
"@typescript-eslint/visitor-keys": "5.12.0" "@typescript-eslint/visitor-keys": "5.12.0"
@ -1955,7 +1953,6 @@
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"@typescript-eslint/utils": "5.12.0", "@typescript-eslint/utils": "5.12.0",
"debug": "^4.3.2", "debug": "^4.3.2",
@ -1981,7 +1978,6 @@
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"engines": { "engines": {
"node": "^12.22.0 || ^14.17.0 || >=16.0.0" "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
}, },
@ -1994,7 +1990,6 @@
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"peer": true,
"dependencies": { "dependencies": {
"@typescript-eslint/types": "5.12.0", "@typescript-eslint/types": "5.12.0",
"@typescript-eslint/visitor-keys": "5.12.0", "@typescript-eslint/visitor-keys": "5.12.0",
@ -2021,7 +2016,6 @@
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"@types/json-schema": "^7.0.9", "@types/json-schema": "^7.0.9",
"@typescript-eslint/scope-manager": "5.12.0", "@typescript-eslint/scope-manager": "5.12.0",
@ -2045,7 +2039,6 @@
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"@typescript-eslint/types": "5.12.0", "@typescript-eslint/types": "5.12.0",
"eslint-visitor-keys": "^3.0.0" "eslint-visitor-keys": "^3.0.0"
@ -4161,7 +4154,6 @@
"version": "5.1.1", "version": "5.1.1",
"dev": true, "dev": true,
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"peer": true,
"dependencies": { "dependencies": {
"esrecurse": "^4.3.0", "esrecurse": "^4.3.0",
"estraverse": "^4.1.1" "estraverse": "^4.1.1"
@ -4290,7 +4282,6 @@
"version": "4.3.0", "version": "4.3.0",
"dev": true, "dev": true,
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"peer": true,
"engines": { "engines": {
"node": ">=4.0" "node": ">=4.0"
} }
@ -12126,7 +12117,6 @@
"version": "3.21.0", "version": "3.21.0",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"peer": true,
"dependencies": { "dependencies": {
"tslib": "^1.8.1" "tslib": "^1.8.1"
}, },
@ -12140,8 +12130,7 @@
"../node_modules/tsutils/node_modules/tslib": { "../node_modules/tsutils/node_modules/tslib": {
"version": "1.14.1", "version": "1.14.1",
"dev": true, "dev": true,
"license": "0BSD", "license": "0BSD"
"peer": true
}, },
"../node_modules/typanion": { "../node_modules/typanion": {
"version": "3.7.1", "version": "3.7.1",
@ -14299,17 +14288,18 @@
"@commitlint/cli": "16.2.1", "@commitlint/cli": "16.2.1",
"@commitlint/config-conventional": "16.2.1", "@commitlint/config-conventional": "16.2.1",
"@swc/cli": "0.1.55", "@swc/cli": "0.1.55",
"@swc/core": "1.2.143", "@swc/core": "1.2.144",
"@swc/jest": "0.2.17", "@swc/jest": "0.2.17",
"@types/html-validator": "5.0.2", "@types/html-validator": "5.0.2",
"@types/jest": "27.4.0", "@types/jest": "27.4.1",
"@types/mock-fs": "4.13.1", "@types/mock-fs": "4.13.1",
"@types/node": "17.0.18", "@types/node": "17.0.21",
"@typescript-eslint/eslint-plugin": "5.12.1",
"chalk": "4.1.2", "chalk": "4.1.2",
"clipanion": "3.1.0", "clipanion": "3.1.0",
"editorconfig-checker": "4.0.2", "editorconfig-checker": "4.0.2",
"eslint": "8.9.0", "eslint": "8.9.0",
"eslint-config-conventions": "1.0.1", "eslint-config-conventions": "1.1.0",
"eslint-config-prettier": "8.4.0", "eslint-config-prettier": "8.4.0",
"eslint-plugin-import": "2.25.4", "eslint-plugin-import": "2.25.4",
"eslint-plugin-prettier": "4.0.0", "eslint-plugin-prettier": "4.0.0",
@ -14325,7 +14315,7 @@
"markdownlint-cli": "0.31.1", "markdownlint-cli": "0.31.1",
"mock-fs": "5.1.2", "mock-fs": "5.1.2",
"ora": "5.4.1", "ora": "5.4.1",
"pinst": "2.1.6", "pinst": "3.0.0",
"prettier": "2.5.1", "prettier": "2.5.1",
"read-pkg": "5.2.0", "read-pkg": "5.2.0",
"rimraf": "3.0.2", "rimraf": "3.0.2",
@ -15537,8 +15527,7 @@
}, },
"@types/json-schema": { "@types/json-schema": {
"version": "7.0.9", "version": "7.0.9",
"dev": true, "dev": true
"peer": true
}, },
"@types/json5": { "@types/json5": {
"version": "0.0.29", "version": "0.0.29",
@ -15593,7 +15582,6 @@
"@typescript-eslint/eslint-plugin": { "@typescript-eslint/eslint-plugin": {
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"peer": true,
"requires": { "requires": {
"@typescript-eslint/scope-manager": "5.12.0", "@typescript-eslint/scope-manager": "5.12.0",
"@typescript-eslint/type-utils": "5.12.0", "@typescript-eslint/type-utils": "5.12.0",
@ -15620,7 +15608,6 @@
"@typescript-eslint/scope-manager": { "@typescript-eslint/scope-manager": {
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"peer": true,
"requires": { "requires": {
"@typescript-eslint/types": "5.12.0", "@typescript-eslint/types": "5.12.0",
"@typescript-eslint/visitor-keys": "5.12.0" "@typescript-eslint/visitor-keys": "5.12.0"
@ -15629,7 +15616,6 @@
"@typescript-eslint/type-utils": { "@typescript-eslint/type-utils": {
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"peer": true,
"requires": { "requires": {
"@typescript-eslint/utils": "5.12.0", "@typescript-eslint/utils": "5.12.0",
"debug": "^4.3.2", "debug": "^4.3.2",
@ -15638,13 +15624,11 @@
}, },
"@typescript-eslint/types": { "@typescript-eslint/types": {
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true
"peer": true
}, },
"@typescript-eslint/typescript-estree": { "@typescript-eslint/typescript-estree": {
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"peer": true,
"requires": { "requires": {
"@typescript-eslint/types": "5.12.0", "@typescript-eslint/types": "5.12.0",
"@typescript-eslint/visitor-keys": "5.12.0", "@typescript-eslint/visitor-keys": "5.12.0",
@ -15658,7 +15642,6 @@
"@typescript-eslint/utils": { "@typescript-eslint/utils": {
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"peer": true,
"requires": { "requires": {
"@types/json-schema": "^7.0.9", "@types/json-schema": "^7.0.9",
"@typescript-eslint/scope-manager": "5.12.0", "@typescript-eslint/scope-manager": "5.12.0",
@ -15671,7 +15654,6 @@
"@typescript-eslint/visitor-keys": { "@typescript-eslint/visitor-keys": {
"version": "5.12.0", "version": "5.12.0",
"dev": true, "dev": true,
"peer": true,
"requires": { "requires": {
"@typescript-eslint/types": "5.12.0", "@typescript-eslint/types": "5.12.0",
"eslint-visitor-keys": "^3.0.0" "eslint-visitor-keys": "^3.0.0"
@ -17041,7 +17023,6 @@
"eslint-scope": { "eslint-scope": {
"version": "5.1.1", "version": "5.1.1",
"dev": true, "dev": true,
"peer": true,
"requires": { "requires": {
"esrecurse": "^4.3.0", "esrecurse": "^4.3.0",
"estraverse": "^4.1.1" "estraverse": "^4.1.1"
@ -17105,8 +17086,7 @@
}, },
"estraverse": { "estraverse": {
"version": "4.3.0", "version": "4.3.0",
"dev": true, "dev": true
"peer": true
}, },
"esutils": { "esutils": {
"version": "2.0.3", "version": "2.0.3",
@ -22256,15 +22236,13 @@
"tsutils": { "tsutils": {
"version": "3.21.0", "version": "3.21.0",
"dev": true, "dev": true,
"peer": true,
"requires": { "requires": {
"tslib": "^1.8.1" "tslib": "^1.8.1"
}, },
"dependencies": { "dependencies": {
"tslib": { "tslib": {
"version": "1.14.1", "version": "1.14.1",
"dev": true, "dev": true
"peer": true
} }
} }
}, },

705
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -54,15 +54,16 @@
"@commitlint/cli": "16.2.1", "@commitlint/cli": "16.2.1",
"@commitlint/config-conventional": "16.2.1", "@commitlint/config-conventional": "16.2.1",
"@swc/cli": "0.1.55", "@swc/cli": "0.1.55",
"@swc/core": "1.2.143", "@swc/core": "1.2.144",
"@swc/jest": "0.2.17", "@swc/jest": "0.2.17",
"@types/html-validator": "5.0.2", "@types/html-validator": "5.0.2",
"@types/jest": "27.4.0", "@types/jest": "27.4.1",
"@types/mock-fs": "4.13.1", "@types/mock-fs": "4.13.1",
"@types/node": "17.0.18", "@types/node": "17.0.21",
"@typescript-eslint/eslint-plugin": "5.12.1",
"editorconfig-checker": "4.0.2", "editorconfig-checker": "4.0.2",
"eslint": "8.9.0", "eslint": "8.9.0",
"eslint-config-conventions": "1.0.1", "eslint-config-conventions": "1.1.0",
"eslint-config-prettier": "8.4.0", "eslint-config-prettier": "8.4.0",
"eslint-plugin-import": "2.25.4", "eslint-plugin-import": "2.25.4",
"eslint-plugin-prettier": "4.0.0", "eslint-plugin-prettier": "4.0.0",
@ -76,7 +77,7 @@
"lint-staged": "12.3.4", "lint-staged": "12.3.4",
"markdownlint-cli": "0.31.1", "markdownlint-cli": "0.31.1",
"mock-fs": "5.1.2", "mock-fs": "5.1.2",
"pinst": "2.1.6", "pinst": "3.0.0",
"prettier": "2.5.1", "prettier": "2.5.1",
"rimraf": "3.0.2", "rimraf": "3.0.2",
"semantic-release": "19.0.2", "semantic-release": "19.0.2",

View File

@ -4,7 +4,10 @@ import fs from 'node:fs'
import { Command } from 'clipanion' import { Command } from 'clipanion'
import chalk from 'chalk' import chalk from 'chalk'
import ora from 'ora' import ora from 'ora'
import validateHTML, { ValidationMessageLocationObject } from 'html-validator' import validateHTML, {
ValidationMessageLocationObject,
ParsedJsonAsValidationResults
} from 'html-validator'
import { table } from 'table' import { table } from 'table'
import { isExistingPath } from './utils/isExistingPath.js' import { isExistingPath } from './utils/isExistingPath.js'
@ -13,11 +16,12 @@ const CURRENT_DIRECTORY = process.cwd()
const CONFIG_FILE_NAME = '.html-w3c-validatorrc.json' const CONFIG_FILE_NAME = '.html-w3c-validatorrc.json'
interface Config { interface Config {
urls: string[] urls?: string[]
files?: string[]
} }
interface Error { interface Error {
url: string data: string
messagesTable: string[][] messagesTable: string[][]
} }
@ -39,59 +43,104 @@ export class HTMLValidatorCommand extends Command {
const configData = await fs.promises.readFile(configPath, { const configData = await fs.promises.readFile(configPath, {
encoding: 'utf-8' encoding: 'utf-8'
}) })
let config: Config = { urls: [] } let config: Config = { urls: [], files: [] }
let isValidConfig = true let isValidConfig = true
try { try {
config = JSON.parse(configData) config = JSON.parse(configData)
} catch { } catch {
isValidConfig = false isValidConfig = false
} }
isValidConfig = isValidConfig && Array.isArray(config.urls) isValidConfig =
isValidConfig &&
(Array.isArray(config.urls) || Array.isArray(config.urls))
if (!isValidConfig) { if (!isValidConfig) {
throw new Error( throw new Error(
`Invalid config file at ${configPath}. Please check the syntax.` `Invalid config file at ${configPath}. Please check the syntax.`
) )
} }
const urls =
config.urls == null
? []
: config.urls.map((url) => {
return { type: 'url', data: url }
})
const files =
config.files == null
? []
: config.files.map((file) => {
return { type: 'file', data: file }
})
const dataToValidate = [...urls, ...files]
const errors: Error[] = [] const errors: Error[] = []
let isValid = true let isValid = true
for (const url of config.urls) { for (const { data, type } of dataToValidate) {
const loader = ora(`Validating ${url}`).start() const loader = ora(`Validating ${data}`).start()
const result = await validateHTML({ try {
url, const options = {
format: 'json', format: 'json' as 'json' | undefined
isLocal: true }
}) let result: ParsedJsonAsValidationResults | undefined
const isValidHTML = result.messages.length === 0 if (type === 'url') {
if (isValidHTML) { result = await validateHTML({
loader.succeed() url: data,
} else { isLocal: true,
loader.fail() ...options
const messagesTable: string[][] = [] })
for (const message of result.messages) { } else if (type === 'file') {
const row: string[] = [] const htmlPath = path.resolve(CURRENT_DIRECTORY, data)
if (message.type === 'error') { if (!(await isExistingPath(htmlPath))) {
row.push(chalk.red(message.type)) throw new Error(
} else { `No file found at ${htmlPath}. Please check the path.`
row.push(chalk.yellow(message.type))
}
row.push(message.message)
const violation = message as ValidationMessageLocationObject
if (violation.extract != null) {
row.push(
`line: ${violation.lastLine}, column: ${violation.firstColumn}-${violation.lastColumn}`
) )
} }
messagesTable.push(row) const html = await fs.promises.readFile(htmlPath, {
encoding: 'utf-8'
})
result = await validateHTML({
data: html,
...options
})
} else {
throw new Error('Invalid type')
} }
errors.push({ url, messagesTable }) const isValidHTML = result.messages.length === 0
if (isValidHTML) {
loader.succeed()
} else {
loader.fail()
const messagesTable: string[][] = []
for (const message of result.messages) {
const row: string[] = []
if (message.type === 'error') {
row.push(chalk.red(message.type))
} else {
row.push(chalk.yellow(message.type))
}
row.push(message.message)
const violation = message as ValidationMessageLocationObject
if (violation.extract != null) {
row.push(
`line: ${violation.lastLine}, column: ${violation.firstColumn}-${violation.lastColumn}`
)
}
messagesTable.push(row)
}
errors.push({ data, messagesTable })
isValid = false
}
} catch (error) {
loader.fail()
isValid = false isValid = false
if (error instanceof Error) {
const messagesTable: string[][] = [[error.message]]
errors.push({ data, messagesTable })
}
} }
} }
if (!isValid) { if (!isValid) {
for (const error of errors) { for (const error of errors) {
console.error(`\n${error.url}`) console.error(`\n${error.data}`)
console.error(table(error.messagesTable)) console.error(table(error.messagesTable))
console.error('------------------------------') console.error('------------------------------')
} }