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:
push:
branches: [master, develop]
branches: [develop]
pull_request:
branches: [master, develop]

View File

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

View File

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

View File

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