w3c-html-validator 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -20
- package/bin/cli.js +15 -20
- package/dist/w3c-html-validator.d.ts +11 -10
- package/dist/w3c-html-validator.js +15 -7
- package/dist/w3c-html-validator.umd.cjs +15 -7
- package/package.json +22 -17
package/README.md
CHANGED
|
@@ -47,15 +47,17 @@ $ npm install --global w3c-html-validator
|
|
|
47
47
|
$ html-validator docs/*.html flyer.html
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
### 3. CLI
|
|
50
|
+
### 3. CLI flags
|
|
51
51
|
Command-line flags:
|
|
52
|
-
| Flag
|
|
53
|
-
|
|
|
54
|
-
| `--
|
|
55
|
-
| `--
|
|
56
|
-
| `--
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
| Flag | Description | Value |
|
|
53
|
+
| ------------ | --------------------------------------------------------------- | ---------- |
|
|
54
|
+
| `--continue` | Report messages but do not throw an error if validation failed. | N/A |
|
|
55
|
+
| `--exclude` | Comma separated list of strings to match in paths to skip. | **string** |
|
|
56
|
+
| `--note` | Place to add a comment only for humans. | **string** |
|
|
57
|
+
| `--quiet` | Suppress messages for successful validations. | N/A |
|
|
58
|
+
| `--trim` | Truncate validation messages to not exceed a maximum length. | **number** |
|
|
59
|
+
|
|
60
|
+
### 4. Example CLI usage
|
|
59
61
|
Examples:
|
|
60
62
|
- `html-validator`<br>
|
|
61
63
|
Validate all HTML files in the project.
|
|
@@ -65,8 +67,8 @@ Examples:
|
|
|
65
67
|
Suppress "pass" messages.
|
|
66
68
|
- `html-validator docs`<br>
|
|
67
69
|
Validate HTML files in a folder.
|
|
68
|
-
- `html-validator docs --trim=30`<br>
|
|
69
|
-
Truncate messages to 30 characters.
|
|
70
|
+
- `html-validator docs --trim=30 --continue`<br>
|
|
71
|
+
Truncate messages to 30 characters and do not abort CI if validation fails.
|
|
70
72
|
|
|
71
73
|
## D) Application Code and Testing Frameworks
|
|
72
74
|
In addition to the CLI interface, the **w3c-html-validator** package can also be imported and called directly in ESM and TypeScript projects.
|
|
@@ -106,18 +108,19 @@ $ node examples.js
|
|
|
106
108
|
Option value `'warning'` also skips `'info'`.
|
|
107
109
|
|
|
108
110
|
#### w3cHtmlValidator.reporter(options)
|
|
109
|
-
| Name (key)
|
|
110
|
-
|
|
|
111
|
-
| `
|
|
112
|
-
| `
|
|
113
|
-
| `
|
|
111
|
+
| Name (key) | Type | Default | Description |
|
|
112
|
+
| :--------------- | :---------- | :------ | :-------------------------------------------------------------- |
|
|
113
|
+
| `continueOnFail` | **boolean** | `false` | Report messages but do not throw an error if validation failed. |
|
|
114
|
+
| `maxMessageLen` | **number** | `null` | Trim validation messages to not exceed a maximum length. |
|
|
115
|
+
| `quiet` | **boolean** | `false` | Suppress messages for successful validations. |
|
|
116
|
+
| `title` | **string** | `null` | Override display title (useful for naming HTML string inputs). |
|
|
114
117
|
|
|
115
|
-
### 3. TypeScript
|
|
116
|
-
|
|
117
|
-
|
|
118
|
+
### 3. TypeScript declarations
|
|
119
|
+
See the TypeScript declarations at the top of the
|
|
120
|
+
[w3c-html-validator.ts](w3c-html-validator.ts) file.
|
|
118
121
|
|
|
119
122
|
The output of the `w3cHtmlValidator.validate(options: ValidatorOptions)` function is a **promise**
|
|
120
|
-
for `ValidatorResults` object:
|
|
123
|
+
for a `ValidatorResults` object:
|
|
121
124
|
```typescript
|
|
122
125
|
type ValidatorResults = {
|
|
123
126
|
validates: boolean,
|
|
@@ -132,7 +135,7 @@ type ValidatorResults = {
|
|
|
132
135
|
};
|
|
133
136
|
```
|
|
134
137
|
|
|
135
|
-
### 4. Mocha
|
|
138
|
+
### 4. Mocha example
|
|
136
139
|
```javascript
|
|
137
140
|
import assert from 'assert';
|
|
138
141
|
import { w3cHtmlValidator } from 'w3c-html-validator';
|
|
@@ -161,6 +164,7 @@ describe('Home page', () => {
|
|
|
161
164
|
- 📂 [copy-folder-util](https://github.com/center-key/copy-folder-util): _Recursively copy files from one folder to another folder_
|
|
162
165
|
- 🔍 [replacer-util](https://github.com/center-key/replacer-util): _Find and replace strings or template outputs in text files_
|
|
163
166
|
- 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets): _Revision web asset filenames with cache busting content hash fingerprints_
|
|
167
|
+
- 🚆 [run-scripts-util](https://github.com/center-key/run-scripts-util): _Organize npm scripts into named groups of easy to manage commands_
|
|
164
168
|
- 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator): _Check the markup validity of HTML files using the W3C validator_
|
|
165
169
|
|
|
166
170
|
Feel free to submit questions at:<br>
|
package/bin/cli.js
CHANGED
|
@@ -17,27 +17,21 @@
|
|
|
17
17
|
//
|
|
18
18
|
// Contributors to this project:
|
|
19
19
|
// $ cd w3c-html-validator
|
|
20
|
-
// $ node bin/cli.js spec/**/*.html --
|
|
20
|
+
// $ node bin/cli.js spec/**/*.html --continue
|
|
21
21
|
|
|
22
22
|
// Imports
|
|
23
|
+
import { cliArgvUtil } from 'cli-argv-util';
|
|
23
24
|
import { w3cHtmlValidator } from '../dist/w3c-html-validator.js';
|
|
24
25
|
import chalk from 'chalk';
|
|
25
26
|
import fs from 'fs';
|
|
26
27
|
import glob from 'glob';
|
|
27
28
|
import log from 'fancy-log';
|
|
28
29
|
|
|
29
|
-
// Parameters
|
|
30
|
-
const validFlags =
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
const flagOn = Object.fromEntries(validFlags.map(flag => [flag, flag in flagMap]));
|
|
35
|
-
const invalidFlag = Object.keys(flagMap).find(key => !validFlags.includes(key));
|
|
36
|
-
const params = args.filter(arg => !/^--/.test(arg));
|
|
37
|
-
|
|
38
|
-
// Data
|
|
39
|
-
const files = params;
|
|
40
|
-
const trim = parseInt(flagMap.trim) || null;
|
|
30
|
+
// Parameters and flags
|
|
31
|
+
const validFlags = ['continue', 'exclude', 'note', 'quiet', 'trim'];
|
|
32
|
+
const cli = cliArgvUtil.parse(validFlags);
|
|
33
|
+
const files = cli.params;
|
|
34
|
+
const trim = parseInt(cli.flagMap.trim) || null;
|
|
41
35
|
|
|
42
36
|
// Validator
|
|
43
37
|
const keep = (filename) => !filename.includes('node_modules/');
|
|
@@ -45,20 +39,21 @@ const readFolder = (folder) => glob.sync(folder + '**/*.html', { ignore: '**/n
|
|
|
45
39
|
const expandFolder = (file) => fs.lstatSync(file).isDirectory() ? readFolder(file + '/') : file;
|
|
46
40
|
const getFilenames = () => [...new Set(files.map(expandFolder).flat().filter(keep))].sort();
|
|
47
41
|
const list = files.length ? getFilenames() : readFolder('');
|
|
48
|
-
const excludes = flagMap.exclude?.split(',') ?? [];
|
|
42
|
+
const excludes = cli.flagMap.exclude?.split(',') ?? [];
|
|
49
43
|
const filenames = list.filter(name => !excludes.find(exclude => name.includes(exclude)));
|
|
50
44
|
const error =
|
|
51
|
-
invalidFlag ?
|
|
52
|
-
!filenames.length ?
|
|
53
|
-
flagOn.trim && !trim ? 'Value of "trim" must be a positive whole number.' :
|
|
45
|
+
cli.invalidFlag ? cli.invalidFlagMsg :
|
|
46
|
+
!filenames.length ? 'No files to validate.' :
|
|
47
|
+
cli.flagOn.trim && !trim ? 'Value of "trim" must be a positive whole number.' :
|
|
54
48
|
null;
|
|
55
49
|
if (error)
|
|
56
50
|
throw Error('[w3c-html-validator] ' + error);
|
|
57
|
-
if (filenames.length > 1 && !flagOn.quiet)
|
|
51
|
+
if (filenames.length > 1 && !cli.flagOn.quiet)
|
|
58
52
|
log(chalk.gray('w3c-html-validator'), chalk.magenta('files: ' + filenames.length));
|
|
59
53
|
const reporterOptions = {
|
|
60
|
-
|
|
61
|
-
|
|
54
|
+
continueOnFail: cli.flagOn.continue,
|
|
55
|
+
quiet: cli.flagOn.quiet,
|
|
56
|
+
maxMessageLen: trim,
|
|
62
57
|
};
|
|
63
58
|
const handleReport = (report) => w3cHtmlValidator.reporter(report, reporterOptions);
|
|
64
59
|
filenames.forEach(file => w3cHtmlValidator.validate({ filename: file }).then(handleReport));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
//! w3c-html-validator v1.
|
|
1
|
+
//! w3c-html-validator v1.3.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
|
|
2
2
|
|
|
3
|
-
export
|
|
3
|
+
export type ValidatorSettings = {
|
|
4
4
|
html: string;
|
|
5
5
|
filename: string;
|
|
6
6
|
website: string;
|
|
@@ -9,8 +9,8 @@ export declare type ValidatorSettings = {
|
|
|
9
9
|
ignoreMessages: string | RegExp;
|
|
10
10
|
output: ValidatorResultsOutput;
|
|
11
11
|
};
|
|
12
|
-
export
|
|
13
|
-
export
|
|
12
|
+
export type ValidatorOptions = Partial<ValidatorSettings>;
|
|
13
|
+
export type ValidatorResultsMessage = {
|
|
14
14
|
type: 'info' | 'error' | 'non-document-error' | 'network-error';
|
|
15
15
|
subType?: 'warning' | 'fatal' | 'io' | 'schema' | 'internal';
|
|
16
16
|
message: string;
|
|
@@ -21,9 +21,9 @@ export declare type ValidatorResultsMessage = {
|
|
|
21
21
|
hiliteStart: number;
|
|
22
22
|
hiliteLength: number;
|
|
23
23
|
};
|
|
24
|
-
export
|
|
25
|
-
export
|
|
26
|
-
export
|
|
24
|
+
export type ValidatorResultsMessageType = ValidatorResultsMessage['type'];
|
|
25
|
+
export type ValidatorResultsMessageSubType = ValidatorResultsMessage['subType'];
|
|
26
|
+
export type ValidatorResults = {
|
|
27
27
|
validates: boolean;
|
|
28
28
|
mode: 'html' | 'filename' | 'website';
|
|
29
29
|
title: string;
|
|
@@ -35,13 +35,14 @@ export declare type ValidatorResults = {
|
|
|
35
35
|
messages: ValidatorResultsMessage[] | null;
|
|
36
36
|
display: string | null;
|
|
37
37
|
};
|
|
38
|
-
export
|
|
39
|
-
export
|
|
38
|
+
export type ValidatorResultsOutput = ValidatorResults['output'];
|
|
39
|
+
export type ReporterSettings = {
|
|
40
|
+
continueOnFail: boolean;
|
|
40
41
|
maxMessageLen: number | null;
|
|
41
42
|
quiet: boolean;
|
|
42
43
|
title: string | null;
|
|
43
44
|
};
|
|
44
|
-
export
|
|
45
|
+
export type ReporterOptions = Partial<ReporterSettings>;
|
|
45
46
|
declare const w3cHtmlValidator: {
|
|
46
47
|
version: string;
|
|
47
48
|
validate(options: ValidatorOptions): Promise<ValidatorResults>;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
//! w3c-html-validator v1.
|
|
1
|
+
//! w3c-html-validator v1.3.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
|
|
2
2
|
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import fs from 'fs';
|
|
5
5
|
import log from 'fancy-log';
|
|
6
6
|
import request from 'superagent';
|
|
7
7
|
const w3cHtmlValidator = {
|
|
8
|
-
version: '1.
|
|
8
|
+
version: '1.3.0',
|
|
9
9
|
validate(options) {
|
|
10
10
|
var _a;
|
|
11
11
|
const defaults = {
|
|
@@ -81,6 +81,7 @@ const w3cHtmlValidator = {
|
|
|
81
81
|
reporter(results, options) {
|
|
82
82
|
var _a, _b;
|
|
83
83
|
const defaults = {
|
|
84
|
+
continueOnFail: false,
|
|
84
85
|
maxMessageLen: null,
|
|
85
86
|
quiet: false,
|
|
86
87
|
title: null,
|
|
@@ -100,17 +101,24 @@ const w3cHtmlValidator = {
|
|
|
100
101
|
info: chalk.white.bold,
|
|
101
102
|
};
|
|
102
103
|
const logMessage = (message) => {
|
|
103
|
-
var _a, _b;
|
|
104
|
-
const type = message.subType
|
|
105
|
-
const typeColor = typeColorMap[type]
|
|
104
|
+
var _a, _b, _c, _d;
|
|
105
|
+
const type = (_a = message.subType) !== null && _a !== void 0 ? _a : message.type;
|
|
106
|
+
const typeColor = (_b = typeColorMap[type]) !== null && _b !== void 0 ? _b : chalk.redBright.bold;
|
|
106
107
|
const location = `line ${message.lastLine}, column ${message.firstColumn}:`;
|
|
107
|
-
const lineText = (
|
|
108
|
-
const maxLen = (
|
|
108
|
+
const lineText = (_c = message.extract) === null || _c === void 0 ? void 0 : _c.replace(/\n/g, '\\n');
|
|
109
|
+
const maxLen = (_d = settings.maxMessageLen) !== null && _d !== void 0 ? _d : undefined;
|
|
109
110
|
log(typeColor('HTML ' + type + ':'), message.message.substring(0, maxLen));
|
|
110
111
|
if (message.lastLine)
|
|
111
112
|
log(chalk.white(location), chalk.magenta(lineText));
|
|
112
113
|
};
|
|
113
114
|
messages.forEach(logMessage);
|
|
115
|
+
const failDetails = () => {
|
|
116
|
+
const toString = (message) => { var _a; return `${(_a = message.subType) !== null && _a !== void 0 ? _a : message.type} line ${message.lastLine} column ${message.firstColumn}`; };
|
|
117
|
+
const fileDetails = () => results.filename + ' -- ' + results.messages.map(toString).join(', ');
|
|
118
|
+
return !results.filename ? results.messages[0].message : fileDetails();
|
|
119
|
+
};
|
|
120
|
+
if (!settings.continueOnFail && !results.validates)
|
|
121
|
+
throw Error('[w3c-html-validator] Failed: ' + failDetails());
|
|
114
122
|
return results;
|
|
115
123
|
},
|
|
116
124
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! w3c-html-validator v1.
|
|
1
|
+
//! w3c-html-validator v1.3.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
|
|
2
2
|
|
|
3
3
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
@@ -20,7 +20,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
20
20
|
const fancy_log_1 = __importDefault(require("fancy-log"));
|
|
21
21
|
const superagent_1 = __importDefault(require("superagent"));
|
|
22
22
|
const w3cHtmlValidator = {
|
|
23
|
-
version: '1.
|
|
23
|
+
version: '1.3.0',
|
|
24
24
|
validate(options) {
|
|
25
25
|
var _a;
|
|
26
26
|
const defaults = {
|
|
@@ -96,6 +96,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
96
96
|
reporter(results, options) {
|
|
97
97
|
var _a, _b;
|
|
98
98
|
const defaults = {
|
|
99
|
+
continueOnFail: false,
|
|
99
100
|
maxMessageLen: null,
|
|
100
101
|
quiet: false,
|
|
101
102
|
title: null,
|
|
@@ -115,17 +116,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
115
116
|
info: chalk_1.default.white.bold,
|
|
116
117
|
};
|
|
117
118
|
const logMessage = (message) => {
|
|
118
|
-
var _a, _b;
|
|
119
|
-
const type = message.subType
|
|
120
|
-
const typeColor = typeColorMap[type]
|
|
119
|
+
var _a, _b, _c, _d;
|
|
120
|
+
const type = (_a = message.subType) !== null && _a !== void 0 ? _a : message.type;
|
|
121
|
+
const typeColor = (_b = typeColorMap[type]) !== null && _b !== void 0 ? _b : chalk_1.default.redBright.bold;
|
|
121
122
|
const location = `line ${message.lastLine}, column ${message.firstColumn}:`;
|
|
122
|
-
const lineText = (
|
|
123
|
-
const maxLen = (
|
|
123
|
+
const lineText = (_c = message.extract) === null || _c === void 0 ? void 0 : _c.replace(/\n/g, '\\n');
|
|
124
|
+
const maxLen = (_d = settings.maxMessageLen) !== null && _d !== void 0 ? _d : undefined;
|
|
124
125
|
(0, fancy_log_1.default)(typeColor('HTML ' + type + ':'), message.message.substring(0, maxLen));
|
|
125
126
|
if (message.lastLine)
|
|
126
127
|
(0, fancy_log_1.default)(chalk_1.default.white(location), chalk_1.default.magenta(lineText));
|
|
127
128
|
};
|
|
128
129
|
messages.forEach(logMessage);
|
|
130
|
+
const failDetails = () => {
|
|
131
|
+
const toString = (message) => { var _a; return `${(_a = message.subType) !== null && _a !== void 0 ? _a : message.type} line ${message.lastLine} column ${message.firstColumn}`; };
|
|
132
|
+
const fileDetails = () => results.filename + ' -- ' + results.messages.map(toString).join(', ');
|
|
133
|
+
return !results.filename ? results.messages[0].message : fileDetails();
|
|
134
|
+
};
|
|
135
|
+
if (!settings.continueOnFail && !results.validates)
|
|
136
|
+
throw Error('[w3c-html-validator] Failed: ' + failDetails());
|
|
129
137
|
return results;
|
|
130
138
|
},
|
|
131
139
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "w3c-html-validator",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Check the markup validity of HTML files using the W3C validator",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -61,20 +61,25 @@
|
|
|
61
61
|
"@typescript-eslint/no-non-null-assertion": "off"
|
|
62
62
|
}
|
|
63
63
|
},
|
|
64
|
+
"runScriptsConfig": {
|
|
65
|
+
"build": [
|
|
66
|
+
"rimraf build dist **/.DS_Store",
|
|
67
|
+
"jshint . --exclude-path .gitignore",
|
|
68
|
+
"eslint --max-warnings 0 . --ext .ts",
|
|
69
|
+
"tsc",
|
|
70
|
+
"tsc --module UMD --outDir build/umd",
|
|
71
|
+
"copy-file build/umd/w3c-html-validator.js build/w3c-html-validator.umd.cjs",
|
|
72
|
+
"add-dist-header build dist"
|
|
73
|
+
]
|
|
74
|
+
},
|
|
64
75
|
"scripts": {
|
|
65
|
-
"
|
|
66
|
-
"step:02": "jshint . --exclude-path .gitignore",
|
|
67
|
-
"step:03": "eslint --max-warnings 0 . --ext .ts",
|
|
68
|
-
"step:10": "tsc",
|
|
69
|
-
"step:11": "tsc --module UMD --outDir build/umd",
|
|
70
|
-
"step:12": "copy-file build/umd/w3c-html-validator.js build/w3c-html-validator.umd.cjs",
|
|
71
|
-
"step:20": "add-dist-header build dist",
|
|
72
|
-
"pretest": "npm-run-all step:*",
|
|
76
|
+
"pretest": "run-scripts build",
|
|
73
77
|
"test": "mocha spec/*.spec.js --timeout 5000",
|
|
74
78
|
"examples": "node examples.js"
|
|
75
79
|
},
|
|
76
80
|
"dependencies": {
|
|
77
|
-
"chalk": "~5.
|
|
81
|
+
"chalk": "~5.2",
|
|
82
|
+
"cli-argv-util": "~0.1",
|
|
78
83
|
"fancy-log": "~2.0",
|
|
79
84
|
"glob": "~8.0",
|
|
80
85
|
"superagent": "~8.0"
|
|
@@ -82,20 +87,20 @@
|
|
|
82
87
|
"devDependencies": {
|
|
83
88
|
"@types/fancy-log": "~2.0",
|
|
84
89
|
"@types/glob": "~8.0",
|
|
85
|
-
"@types/node": "~18.
|
|
90
|
+
"@types/node": "~18.11",
|
|
86
91
|
"@types/superagent": "~4.1",
|
|
87
|
-
"@typescript-eslint/eslint-plugin": "~5.
|
|
88
|
-
"@typescript-eslint/parser": "~5.
|
|
92
|
+
"@typescript-eslint/eslint-plugin": "~5.47",
|
|
93
|
+
"@typescript-eslint/parser": "~5.47",
|
|
89
94
|
"add-dist-header": "~0.3",
|
|
90
95
|
"assert-deep-strict-equal": "~1.0",
|
|
91
96
|
"copy-file-util": "~0.1",
|
|
92
97
|
"copy-folder-util": "~0.2",
|
|
93
|
-
"eslint": "~8.
|
|
98
|
+
"eslint": "~8.31",
|
|
94
99
|
"jshint": "~2.13",
|
|
95
100
|
"merge-stream": "~2.0",
|
|
96
|
-
"mocha": "~10.
|
|
97
|
-
"npm-run-all2": "~6.0",
|
|
101
|
+
"mocha": "~10.2",
|
|
98
102
|
"rimraf": "~3.0",
|
|
99
|
-
"
|
|
103
|
+
"run-scripts-util": "~0.1",
|
|
104
|
+
"typescript": "~4.9"
|
|
100
105
|
}
|
|
101
106
|
}
|