mirror of
				https://github.com/theoludwig/html-w3c-validator.git
				synced 2025-05-21 23:21:29 +02:00 
			
		
		
		
	Compare commits
	
		
			11 Commits
		
	
	
		
			v1.5.1
			...
			e53c80d578
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| e53c80d578 | |||
| 5f63d749e8 | |||
| 67b70feb15 | |||
| 6b28c6d224 | |||
| a1340b2729 | |||
| 92320385b5 | |||
| 8934ac1b7a | |||
| 09731d4749 | |||
| 2e0e136355 | |||
| 04a2742e46 | |||
| 75f862d0fd | 
| @@ -1,16 +1,19 @@ | ||||
| { | ||||
|   "extends": ["conventions", "prettier"], | ||||
|   "plugins": ["prettier", "import", "unicorn"], | ||||
|   "parserOptions": { | ||||
|     "project": "./tsconfig.json" | ||||
|   }, | ||||
|   "env": { | ||||
|     "node": true | ||||
|   }, | ||||
|   "root": true, | ||||
|   "extends": ["conventions"], | ||||
|   "plugins": ["import", "promise", "unicorn"], | ||||
|   "rules": { | ||||
|     "prettier/prettier": "error", | ||||
|     "import/extensions": ["error", "always"], | ||||
|     "unicorn/prevent-abbreviations": "error", | ||||
|     "unicorn/prefer-node-protocol": "error" | ||||
|   } | ||||
|     "unicorn/prevent-abbreviations": "error" | ||||
|   }, | ||||
|   "overrides": [ | ||||
|     { | ||||
|       "files": ["*.ts", "*.tsx"], | ||||
|       "parser": "@typescript-eslint/parser", | ||||
|       "plugins": ["@typescript-eslint"], | ||||
|       "parserOptions": { | ||||
|         "project": "./tsconfig.json" | ||||
|       } | ||||
|     } | ||||
|   ] | ||||
| } | ||||
|   | ||||
							
								
								
									
										4
									
								
								.github/workflows/build.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.github/workflows/build.yml
									
									
									
									
										vendored
									
									
								
							| @@ -10,10 +10,10 @@ jobs: | ||||
|   build: | ||||
|     runs-on: "ubuntu-latest" | ||||
|     steps: | ||||
|       - uses: "actions/checkout@v4.1.1" | ||||
|       - uses: "actions/checkout@v4.1.6" | ||||
|  | ||||
|       - name: "Setup Node.js" | ||||
|         uses: "actions/setup-node@v4.0.1" | ||||
|         uses: "actions/setup-node@v4.0.2" | ||||
|         with: | ||||
|           node-version: "lts/*" | ||||
|           cache: "npm" | ||||
|   | ||||
							
								
								
									
										5
									
								
								.github/workflows/lint.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.github/workflows/lint.yml
									
									
									
									
										vendored
									
									
								
							| @@ -10,10 +10,10 @@ jobs: | ||||
|   lint: | ||||
|     runs-on: "ubuntu-latest" | ||||
|     steps: | ||||
|       - uses: "actions/checkout@v4.1.1" | ||||
|       - uses: "actions/checkout@v4.1.6" | ||||
|  | ||||
|       - name: "Setup Node.js" | ||||
|         uses: "actions/setup-node@v4.0.1" | ||||
|         uses: "actions/setup-node@v4.0.2" | ||||
|         with: | ||||
|           node-version: "lts/*" | ||||
|           cache: "npm" | ||||
| @@ -23,6 +23,5 @@ jobs: | ||||
|  | ||||
|       - run: 'npm run lint:commit -- --to "${{ github.sha }}"' | ||||
|       - run: "npm run lint:editorconfig" | ||||
|       - run: "npm run lint:markdown" | ||||
|       - run: "npm run lint:eslint" | ||||
|       - run: "npm run lint:prettier" | ||||
|   | ||||
							
								
								
									
										4
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							| @@ -13,13 +13,13 @@ jobs: | ||||
|       pull-requests: "write" | ||||
|       id-token: "write" | ||||
|     steps: | ||||
|       - uses: "actions/checkout@v4.1.1" | ||||
|       - uses: "actions/checkout@v4.1.6" | ||||
|         with: | ||||
|           fetch-depth: 0 | ||||
|           persist-credentials: false | ||||
|  | ||||
|       - name: "Setup Node.js" | ||||
|         uses: "actions/setup-node@v4.0.1" | ||||
|         uses: "actions/setup-node@v4.0.2" | ||||
|         with: | ||||
|           node-version: "lts/*" | ||||
|           cache: "npm" | ||||
|   | ||||
							
								
								
									
										4
									
								
								.github/workflows/test.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.github/workflows/test.yml
									
									
									
									
										vendored
									
									
								
							| @@ -10,10 +10,10 @@ jobs: | ||||
|   test: | ||||
|     runs-on: "ubuntu-latest" | ||||
|     steps: | ||||
|       - uses: "actions/checkout@v4.1.1" | ||||
|       - uses: "actions/checkout@v4.1.6" | ||||
|  | ||||
|       - name: "Setup Node.js" | ||||
|         uses: "actions/setup-node@v4.0.1" | ||||
|         uses: "actions/setup-node@v4.0.2" | ||||
|         with: | ||||
|           node-version: "lts/*" | ||||
|           cache: "npm" | ||||
|   | ||||
							
								
								
									
										3
									
								
								.husky/commit-msg
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										3
									
								
								.husky/commit-msg
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| #!/usr/bin/env sh | ||||
|  | ||||
| npm run lint:commit -- --edit | ||||
							
								
								
									
										4
									
								
								.husky/pre-commit
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										4
									
								
								.husky/pre-commit
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,4 @@ | ||||
| #!/usr/bin/env sh | ||||
|  | ||||
| npm run lint:staged | ||||
| npm run build:typescript | ||||
| @@ -1,6 +1,6 @@ | ||||
| { | ||||
|   "*": ["editorconfig-checker"], | ||||
|   "*.{js,ts,jsx,tsx}": ["prettier --write", "eslint --fix"], | ||||
|   "*.{yml,json}": ["prettier --write"], | ||||
|   "*.md": ["prettier --write", "markdownlint --dot --fix"] | ||||
|   "**/*": ["editorconfig-checker", "prettier --write --ignore-unknown"], | ||||
|   "**/*.{js,jsx,ts,tsx}": [ | ||||
|     "eslint --fix --max-warnings 0 --report-unused-disable-directives" | ||||
|   ] | ||||
| } | ||||
|   | ||||
| @@ -1,11 +0,0 @@ | ||||
| { | ||||
|   "config": { | ||||
|     "extends": "markdownlint/style/prettier", | ||||
|     "relative-links": true, | ||||
|     "default": true, | ||||
|     "MD033": false | ||||
|   }, | ||||
|   "globs": ["**/*.{md,mdx}"], | ||||
|   "ignores": ["**/node_modules"], | ||||
|   "customRules": ["markdownlint-rule-relative-links"] | ||||
| } | ||||
							
								
								
									
										2
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								LICENSE
									
									
									
									
									
								
							| @@ -1,6 +1,6 @@ | ||||
| # MIT License | ||||
|  | ||||
| Copyright (c) Théo LUDWIG | ||||
| Copyright (c) Théo LUDWIG <contact@theoludwig.fr> | ||||
|  | ||||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
| of this software and associated documentation files (the "Software"), to deal | ||||
|   | ||||
							
								
								
									
										28
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								README.md
									
									
									
									
									
								
							| @@ -1,9 +1,7 @@ | ||||
| <h1 align="center">html-w3c-validator</h1> | ||||
|  | ||||
| <p align="center"> | ||||
|   <strong>CLI for validating multiple html pages using <a href="https://validator.w3.org/">validator.w3.org</a>.</strong> | ||||
| </p> | ||||
|  | ||||
|   <strong>CLI for validating HTML pages using <a href="https://validator.w3.org/">validator.w3.org</a>.</strong> | ||||
| </p> | ||||
|  | ||||
| <p align="center"> | ||||
| @@ -22,11 +20,11 @@ | ||||
|  | ||||
| ## 📜 About | ||||
|  | ||||
| **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 HTML pages using [validator.w3.org](https://validator.w3.org/). | ||||
|  | ||||
| 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 is HTML page validation important? | ||||
|  | ||||
| Quote from [https://validator.w3.org/docs/help.html#why-validate](https://validator.w3.org/docs/help.html#why-validate): | ||||
|  | ||||
| @@ -40,7 +38,7 @@ You can combine **html-w3c-validator** with [start-server-and-test](https://gith | ||||
|  | ||||
| ### Prerequisites | ||||
|  | ||||
| - [Node.js](https://nodejs.org/) >= 16.0.0 | ||||
| [Node.js](https://nodejs.org/) >= 16.0.0 | ||||
|  | ||||
| ### Installation (with [start-server-and-test](https://github.com/bahmutov/start-server-and-test)) | ||||
|  | ||||
| @@ -59,8 +57,8 @@ npm install --save-dev html-w3c-validator start-server-and-test | ||||
|     "start": "serve \"./build\"", | ||||
|  | ||||
|     // Command to validate your HTML pages | ||||
|     "test:html-w3c-validator": "start-server-and-test \"start\" \"http://127.0.0.1:3000\" \"html-w3c-validator\"" | ||||
|   } | ||||
|     "test:html-w3c-validator": "start-server-and-test \"start\" \"http://127.0.0.1:3000\" \"html-w3c-validator\"", | ||||
|   }, | ||||
| } | ||||
| ``` | ||||
|  | ||||
| @@ -68,13 +66,16 @@ npm install --save-dev html-w3c-validator start-server-and-test | ||||
|  | ||||
| ```jsonc | ||||
| { | ||||
|   "$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/master/schema/html-w3c-validatorrc-schema.json", | ||||
|  | ||||
|   // URLs to validate. | ||||
|   "urls": ["http://127.0.0.1:3000/", "http://127.0.0.1:3000/about"], | ||||
|  | ||||
|   // You can also specify HTML files instead of URLs | ||||
|   // Files to validate. | ||||
|   "files": ["./index.html", "./about.html"], | ||||
|  | ||||
|   // Specify the severities of the validator (default: ["warning", "error"]) | ||||
|   "severities": ["info", "warning", "error"] | ||||
|   // Specify the severities of the validator messages to report. (default: ["warning", "error"]) | ||||
|   "severities": ["info", "warning", "error"], | ||||
| } | ||||
| ``` | ||||
|  | ||||
| @@ -98,8 +99,9 @@ See the [./example](./example) folder for practical usage. | ||||
| ### Options | ||||
|  | ||||
| ```text | ||||
| -V, --version       Output the version number. | ||||
| -h, --help          Display help for command. | ||||
| --current-working-directory <path>  The current working directory (default: `process.cwd()`). | ||||
| -V, --version                       Output the version number. | ||||
| -h, --help                          Display help for command. | ||||
| ``` | ||||
|  | ||||
| ## 💡 Contributing | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| { | ||||
|   "$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/develop/schema/html-w3c-validatorrc-schema.json", | ||||
|   "urls": ["http://127.0.0.1:3000/", "http://127.0.0.1:3000/about"], | ||||
|   "files": ["./build/index.html", "./build/about.html"] | ||||
| } | ||||
|   | ||||
							
								
								
									
										300
									
								
								example/package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										300
									
								
								example/package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -6,7 +6,7 @@ | ||||
|     "test:html-w3c-validator": "start-server-and-test \"start\" \"http://127.0.0.1:3000\" \"html-w3c-validator\"" | ||||
|   }, | ||||
|   "devDependencies": { | ||||
|     "serve": "14.2.1", | ||||
|     "serve": "14.2.3", | ||||
|     "start-server-and-test": "2.0.3", | ||||
|     "html-w3c-validator": "file:.." | ||||
|   } | ||||
|   | ||||
							
								
								
									
										7517
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										7517
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										68
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										68
									
								
								package.json
									
									
									
									
									
								
							| @@ -1,7 +1,7 @@ | ||||
| { | ||||
|   "name": "html-w3c-validator", | ||||
|   "version": "0.0.0-development", | ||||
|   "description": "CLI for validating multiple html pages using validator.w3.org.", | ||||
|   "description": "CLI for validating HTML pages using validator.w3.org.", | ||||
|   "public": true, | ||||
|   "types": "module", | ||||
|   "type": "module", | ||||
| @@ -33,61 +33,55 @@ | ||||
|     "provenance": true | ||||
|   }, | ||||
|   "engines": { | ||||
|     "node": ">=16.0.0", | ||||
|     "npm": ">=9.0.0" | ||||
|     "node": ">=16.0.0" | ||||
|   }, | ||||
|   "scripts": { | ||||
|     "build": "rimraf ./build && swc ./src --out-dir ./build", | ||||
|     "build": "swc ./src --out-dir ./build --strip-leading-paths --delete-dir-on-start", | ||||
|     "build:typescript": "tsc", | ||||
|     "start": "node --enable-source-maps build/index.js", | ||||
|     "lint:commit": "commitlint", | ||||
|     "lint:editorconfig": "editorconfig-checker", | ||||
|     "lint:markdown": "markdownlint-cli2", | ||||
|     "lint:eslint": "eslint . --max-warnings 0 --report-unused-disable-directives --ignore-path .gitignore", | ||||
|     "lint:prettier": "prettier . --check", | ||||
|     "lint:staged": "lint-staged", | ||||
|     "test": "node --enable-source-maps --test build/", | ||||
|     "release": "semantic-release" | ||||
|     "test": "node --enable-source-maps --test", | ||||
|     "release": "semantic-release", | ||||
|     "postinstall": "husky", | ||||
|     "prepublishOnly": "pinst --disable", | ||||
|     "postpublish": "pinst --enable" | ||||
|   }, | ||||
|   "dependencies": { | ||||
|     "chalk": "5.3.0", | ||||
|     "clipanion": "3.2.1", | ||||
|     "html-validator": "6.0.1", | ||||
|     "log-symbols": "6.0.0", | ||||
|     "ora": "8.0.1", | ||||
|     "read-pkg": "9.0.1", | ||||
|     "table": "6.8.1" | ||||
|     "table": "6.8.2" | ||||
|   }, | ||||
|   "devDependencies": { | ||||
|     "@commitlint/cli": "18.4.3", | ||||
|     "@commitlint/config-conventional": "18.4.3", | ||||
|     "@swc/cli": "0.1.63", | ||||
|     "@swc/core": "1.3.101", | ||||
|     "@tsconfig/strictest": "2.0.2", | ||||
|     "@types/html-validator": "5.0.6", | ||||
|     "@types/mock-fs": "4.13.4", | ||||
|     "@types/node": "20.10.5", | ||||
|     "@types/sinon": "17.0.2", | ||||
|     "@typescript-eslint/eslint-plugin": "6.16.0", | ||||
|     "@typescript-eslint/parser": "6.16.0", | ||||
|     "editorconfig-checker": "5.1.2", | ||||
|     "eslint": "8.56.0", | ||||
|     "eslint-config-conventions": "13.1.0", | ||||
|     "eslint-config-prettier": "9.1.0", | ||||
|     "@commitlint/cli": "19.2.2", | ||||
|     "@commitlint/config-conventional": "19.2.2", | ||||
|     "@swc/cli": "0.3.12", | ||||
|     "@swc/core": "1.5.7", | ||||
|     "@tsconfig/strictest": "2.0.5", | ||||
|     "@types/node": "20.12.12", | ||||
|     "@types/sinon": "17.0.3", | ||||
|     "@typescript-eslint/eslint-plugin": "7.10.0", | ||||
|     "@typescript-eslint/parser": "7.10.0", | ||||
|     "editorconfig-checker": "5.1.5", | ||||
|     "eslint": "8.57.0", | ||||
|     "eslint-config-conventions": "14.2.0", | ||||
|     "eslint-plugin-import": "2.29.1", | ||||
|     "eslint-plugin-prettier": "5.1.2", | ||||
|     "eslint-plugin-promise": "6.1.1", | ||||
|     "eslint-plugin-unicorn": "50.0.1", | ||||
|     "execa": "8.0.1", | ||||
|     "lint-staged": "15.2.0", | ||||
|     "markdownlint-cli2": "0.11.0", | ||||
|     "markdownlint-rule-relative-links": "2.1.2", | ||||
|     "mock-fs": "5.2.0", | ||||
|     "prettier": "3.1.1", | ||||
|     "rimraf": "5.0.5", | ||||
|     "semantic-release": "22.0.12", | ||||
|     "serve": "14.2.1", | ||||
|     "sinon": "17.0.1", | ||||
|     "typescript": "5.3.3" | ||||
|     "eslint-plugin-unicorn": "53.0.0", | ||||
|     "execa": "9.1.0", | ||||
|     "husky": "9.0.11", | ||||
|     "lint-staged": "15.2.4", | ||||
|     "pinst": "3.0.0", | ||||
|     "prettier": "3.2.5", | ||||
|     "semantic-release": "23.1.1", | ||||
|     "serve": "14.2.3", | ||||
|     "sinon": "18.0.0", | ||||
|     "typescript": "5.4.5" | ||||
|   } | ||||
| } | ||||
|   | ||||
							
								
								
									
										50
									
								
								schema/html-w3c-validatorrc-schema.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								schema/html-w3c-validatorrc-schema.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,50 @@ | ||||
| { | ||||
|   "$schema": "http://json-schema.org/draft-07/schema#", | ||||
|   "$id": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/master/schema/html-w3c-validatorrc-schema.json", | ||||
|   "title": "html-w3c-validator configuration schema", | ||||
|   "type": "object", | ||||
|   "properties": { | ||||
|     "$schema": { | ||||
|       "description": "JSON Schema URI", | ||||
|       "type": "string", | ||||
|       "format": "uri", | ||||
|       "default": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/master/schema/html-w3c-validatorrc-schema.json" | ||||
|     }, | ||||
|     "urls": { | ||||
|       "description": "URLs to validate.", | ||||
|       "type": "array", | ||||
|       "default": [], | ||||
|       "minItems": 1, | ||||
|       "items": { | ||||
|         "description": "URL to validate.", | ||||
|         "type": "string", | ||||
|         "format": "uri", | ||||
|         "minLength": 1 | ||||
|       } | ||||
|     }, | ||||
|     "files": { | ||||
|       "description": "Files to validate.", | ||||
|       "type": "array", | ||||
|       "minItems": 1, | ||||
|       "default": [], | ||||
|       "items": { | ||||
|         "description": "File to validate.", | ||||
|         "type": "string", | ||||
|         "format": "uri-reference", | ||||
|         "minLength": 1 | ||||
|       } | ||||
|     }, | ||||
|     "severities": { | ||||
|       "description": "Specify the severities to report.", | ||||
|       "type": "array", | ||||
|       "minItems": 1, | ||||
|       "default": ["warning", "error"], | ||||
|       "items": { | ||||
|         "description": "Severity to report.", | ||||
|         "type": "string", | ||||
|         "enum": ["error", "warning", "info"] | ||||
|       } | ||||
|     } | ||||
|   }, | ||||
|   "additionalProperties": false | ||||
| } | ||||
| @@ -1,19 +1,15 @@ | ||||
| import path from "node:path" | ||||
| import fs from "node:fs" | ||||
| import path from "node:path" | ||||
|  | ||||
| import { Command, Option } from "clipanion" | ||||
| import * as typanion from "typanion" | ||||
| import chalk from "chalk" | ||||
| import ora from "ora" | ||||
| import { Command, Option } from "clipanion" | ||||
| import logSymbols from "log-symbols" | ||||
| import type { | ||||
|   ValidationMessageLocationObject, | ||||
|   ParsedJsonAsValidationResults, | ||||
| } from "html-validator" | ||||
| import validateHTML from "html-validator" | ||||
| import ora from "ora" | ||||
| import { table } from "table" | ||||
| import * as typanion from "typanion" | ||||
|  | ||||
| import { isExistingPath } from "./utils/isExistingPath.js" | ||||
| import type { ValidateHTMLResult } from "./validateHTML.js" | ||||
| import { getHTMLFromURL, validateHTML } from "./validateHTML.js" | ||||
|  | ||||
| export const CONFIG_FILE_NAME = ".html-w3c-validatorrc.json" | ||||
|  | ||||
| @@ -50,7 +46,7 @@ const printResults = (results: Result[]): void => { | ||||
| export class HTMLValidatorCommand extends Command { | ||||
|   static override usage = { | ||||
|     description: | ||||
|       "CLI for validating multiple html pages using <https://validator.w3.org/>.", | ||||
|       "CLI for validating HTML pages using <https://validator.w3.org/>.", | ||||
|   } | ||||
|  | ||||
|   public currentWorkingDirectory = Option.String( | ||||
| @@ -65,22 +61,20 @@ export class HTMLValidatorCommand extends Command { | ||||
|   public async execute(): Promise<number> { | ||||
|     const configPath = path.join(this.currentWorkingDirectory, CONFIG_FILE_NAME) | ||||
|     try { | ||||
|       if (!(await isExistingPath(configPath))) { | ||||
|       let configData: string | ||||
|       try { | ||||
|         configData = await fs.promises.readFile(configPath, { | ||||
|           encoding: "utf-8", | ||||
|         }) | ||||
|       } catch (error) { | ||||
|         throw new Error( | ||||
|           `No config file found at ${configPath}. Please create "${CONFIG_FILE_NAME}".`, | ||||
|         ) | ||||
|       } | ||||
|       const configData = await fs.promises.readFile(configPath, { | ||||
|         encoding: "utf-8", | ||||
|       }) | ||||
|       let config: Config = { urls: [], files: [] } | ||||
|       let isValidConfig = true | ||||
|       try { | ||||
|         config = JSON.parse(configData) | ||||
|       } catch { | ||||
|         isValidConfig = false | ||||
|       } | ||||
|       if (!isValidConfig) { | ||||
|         throw new Error( | ||||
|           `Invalid config file at "${configPath}". Please check the JSON syntax.`, | ||||
|         ) | ||||
| @@ -137,29 +131,25 @@ export class HTMLValidatorCommand extends Command { | ||||
|       await Promise.all( | ||||
|         dataToValidate.map(async ({ data, type }) => { | ||||
|           try { | ||||
|             const options = { | ||||
|               format: "json" as "json" | undefined, | ||||
|             } | ||||
|             let result: ParsedJsonAsValidationResults | undefined | ||||
|             let result: ValidateHTMLResult | undefined | ||||
|             if (type === "url") { | ||||
|               result = await validateHTML({ | ||||
|                 url: data, | ||||
|                 isLocal: true, | ||||
|                 ...options, | ||||
|                 htmlData: await getHTMLFromURL(data), | ||||
|               }) | ||||
|             } else if (type === "file") { | ||||
|               const htmlPath = path.resolve(this.currentWorkingDirectory, data) | ||||
|               if (!(await isExistingPath(htmlPath))) { | ||||
|               let html: string | ||||
|               try { | ||||
|                 html = await fs.promises.readFile(htmlPath, { | ||||
|                   encoding: "utf-8", | ||||
|                 }) | ||||
|               } catch (error) { | ||||
|                 throw new Error( | ||||
|                   `No file found at "${htmlPath}". Please check the path.`, | ||||
|                 ) | ||||
|               } | ||||
|               const html = await fs.promises.readFile(htmlPath, { | ||||
|                 encoding: "utf-8", | ||||
|               }) | ||||
|               result = await validateHTML({ | ||||
|                 data: html, | ||||
|                 ...options, | ||||
|                 htmlData: html, | ||||
|               }) | ||||
|             } else { | ||||
|               throw new Error("Invalid type") | ||||
| @@ -194,10 +184,9 @@ export class HTMLValidatorCommand extends Command { | ||||
|                   row.push(chalk.red(message.type)) | ||||
|                 } | ||||
|                 row.push(message.message) | ||||
|                 const violation = message as ValidationMessageLocationObject | ||||
|                 if (violation.extract != null) { | ||||
|                 if (message.extract != null) { | ||||
|                   row.push( | ||||
|                     `line: ${violation.lastLine}, column: ${violation.firstColumn}-${violation.lastColumn}`, | ||||
|                     `line: ${message.lastLine}, column: ${message.firstColumn}-${message.lastColumn}`, | ||||
|                   ) | ||||
|                 } | ||||
|                 messagesTable.push(row) | ||||
|   | ||||
| @@ -1,7 +1,9 @@ | ||||
| import test from "node:test" | ||||
| import assert from "node:assert/strict" | ||||
| import path from "node:path" | ||||
| import fs from "node:fs" | ||||
| import { PassThrough } from "node:stream" | ||||
| import { fileURLToPath } from "node:url" | ||||
|  | ||||
| import sinon from "sinon" | ||||
| import { execa } from "execa" | ||||
| @@ -33,7 +35,10 @@ await test("html-w3c-validator", async (t) => { | ||||
|     async () => { | ||||
|       const exampleURL = new URL("../../example", import.meta.url) | ||||
|       process.chdir(exampleURL.pathname) | ||||
|       await execa("rimraf", ["node_modules"]) | ||||
|       await fs.promises.rm( | ||||
|         path.join(fileURLToPath(exampleURL), "node_modules"), | ||||
|         { recursive: true, force: true }, | ||||
|       ) | ||||
|       await execa("npm", ["install"]) | ||||
|       const { exitCode } = await execa("npm", [ | ||||
|         "run", | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| { | ||||
|   "$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/develop/schema/html-w3c-validatorrc-schema.json", | ||||
|   "urls": [], | ||||
|   "files": [] | ||||
| } | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| { | ||||
|   "$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/develop/schema/html-w3c-validatorrc-schema.json", | ||||
|   "files": "Invalid" | ||||
| } | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| { | ||||
|   "$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/develop/schema/html-w3c-validatorrc-schema.json", | ||||
|   "files": ["./index.html"], | ||||
|   "severities": [] | ||||
| } | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| { | ||||
|   "$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/develop/schema/html-w3c-validatorrc-schema.json", | ||||
|   "files": ["./index.html"], | ||||
|   "severities": ["errors-invalid"] | ||||
| } | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| { | ||||
|   "$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/develop/schema/html-w3c-validatorrc-schema.json", | ||||
|   "urls": "Invalid" | ||||
| } | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| { | ||||
|   "$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/develop/schema/html-w3c-validatorrc-schema.json", | ||||
|   "files": ["./index.html"] | ||||
| } | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| { | ||||
|   "$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/develop/schema/html-w3c-validatorrc-schema.json", | ||||
|   "files": ["./build/index.html", "./build/about.html"] | ||||
| } | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| { | ||||
|   "$schema": "https://raw.githubusercontent.com/theoludwig/html-w3c-validator/develop/schema/html-w3c-validatorrc-schema.json", | ||||
|   "files": ["./build/index.html", "./build/about.html"] | ||||
| } | ||||
|   | ||||
| @@ -1,26 +0,0 @@ | ||||
| import test from "node:test" | ||||
| import assert from "node:assert/strict" | ||||
|  | ||||
| import fsMock from "mock-fs" | ||||
|  | ||||
| import { isExistingPath } from "../isExistingPath.js" | ||||
|  | ||||
| await test("utils/isExistingPath", async (t) => { | ||||
|   t.afterEach(() => { | ||||
|     fsMock.restore() | ||||
|   }) | ||||
|  | ||||
|   await t.test("should return true if the file exists", async () => { | ||||
|     fsMock({ | ||||
|       "/file.txt": "", | ||||
|     }) | ||||
|     assert.strictEqual(await isExistingPath("/file.txt"), true) | ||||
|   }) | ||||
|  | ||||
|   await t.test("should return false if the file doesn't exists", async () => { | ||||
|     fsMock({ | ||||
|       "/file.txt": "", | ||||
|     }) | ||||
|     assert.strictEqual(await isExistingPath("/randomfile.txt"), false) | ||||
|   }) | ||||
| }) | ||||
| @@ -1,10 +0,0 @@ | ||||
| import fs from "node:fs" | ||||
|  | ||||
| export const isExistingPath = async (path: string): Promise<boolean> => { | ||||
|   try { | ||||
|     await fs.promises.access(path, fs.constants.F_OK) | ||||
|     return true | ||||
|   } catch { | ||||
|     return false | ||||
|   } | ||||
| } | ||||
							
								
								
									
										46
									
								
								src/validateHTML.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/validateHTML.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| export interface ValidateHTMLOptions { | ||||
|   htmlData: string | ||||
| } | ||||
|  | ||||
| export interface ValidationMessage { | ||||
|   type: "error" | "info" | "non-document-error" | ||||
|   subType?: "warning" | "fatal" | "internal" | "io" | "schema" | ||||
|   message: string | ||||
|   extract?: string | ||||
|   lastLine: number | ||||
|   firstColumn: number | ||||
|   lastColumn: number | ||||
| } | ||||
|  | ||||
| export interface ValidateHTMLResult { | ||||
|   messages: ValidationMessage[] | ||||
| } | ||||
|  | ||||
| export const validateHTML = async ( | ||||
|   options: ValidateHTMLOptions, | ||||
| ): Promise<ValidateHTMLResult> => { | ||||
|   const { htmlData } = options | ||||
|   const url = new URL("https://validator.w3.org/nu/") | ||||
|   url.searchParams.set("out", "json") | ||||
|   const response = await fetch(url, { | ||||
|     method: "POST", | ||||
|     body: htmlData, | ||||
|     headers: { | ||||
|       "Content-Type": "text/html", | ||||
|     }, | ||||
|   }) | ||||
|   if (!response.ok) { | ||||
|     throw new Error(`Failed to validate HTML`) | ||||
|   } | ||||
|   const result = (await response.json()) as ValidateHTMLResult | ||||
|   return result | ||||
| } | ||||
|  | ||||
| export const getHTMLFromURL = async (url: string): Promise<string> => { | ||||
|   const response = await fetch(url) | ||||
|   if (!response.ok) { | ||||
|     throw new Error(`Failed to fetch HTML from "${url}"`) | ||||
|   } | ||||
|   const html = await response.text() | ||||
|   return html | ||||
| } | ||||
		Reference in New Issue
	
	Block a user