w3c-html-validator 1.1.2 → 1.2.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 CHANGED
@@ -8,41 +8,90 @@ _Check the markup validity of HTML files using the W3C validator_
8
8
  [![Vulnerabilities](https://snyk.io/test/github/center-key/w3c-html-validator/badge.svg)](https://snyk.io/test/github/center-key/w3c-html-validator)
9
9
  [![Build](https://github.com/center-key/w3c-html-validator/workflows/build/badge.svg)](https://github.com/center-key/w3c-html-validator/actions/workflows/run-spec-on-push.yaml)
10
10
 
11
- ## 1) Setup
11
+ **w3c-html-validator** takes HTML files and returns detailed validation results. 
12
+ The reporter produces formatted output indended for use in build scripts and test suites.
12
13
 
13
- ### Install
14
+ <img src=https://raw.githubusercontent.com/center-key/w3c-html-validator/main/examples.png
15
+ width=800 alt=screenshot>
16
+
17
+ ## A) Setup
14
18
  Install package for node:
15
19
  ```shell
16
20
  $ npm install --save-dev w3c-html-validator
17
21
  ```
18
22
 
19
- ### Import
20
- Import into your application:
21
- ```javascript
22
- import { w3cHtmlValidator } from 'w3c-html-validator';
23
+ ## B) Usage
24
+ ### 1. npm scripts
25
+ Run `html-validator` from the `"scripts"` section of your **package.json** file.
26
+
27
+ The parameters are files to be validated.
28
+
29
+ Example **package.json** scripts:
30
+ ```json
31
+ "scripts": {
32
+ "validate": "html-validator docs/*.html flyer.html",
33
+ "one-folder": "html-validator docs",
34
+ "all": "html-validator --quiet"
35
+ },
23
36
  ```
24
- or invoke directly [from the command line or from a **package.json** script](#6-command-line).
25
37
 
26
- ## 2) Usage
27
- Call the `validate()` function:
28
- ```javascript
38
+ Passing no parameters defaults to validating all HTML files in the project (skipping the
39
+ **node_modules** folder).
40
+
41
+ ### 2. Global
42
+ You can install **w3c-html-validator** globally and then run it anywhere directly from the terminal.
43
+
44
+ Example terminal commands:
45
+ ```shell
46
+ $ npm install --global w3c-html-validator
47
+ $ html-validator docs/*.html flyer.html
48
+ ```
49
+
50
+ ### 3. CLI Flags
51
+ Command-line flags:
52
+ | Flag | Description | Value |
53
+ | ----------- | ------------------------------------------------------------ | ---------- |
54
+ | `--exclude` | Comma separated list of strings to match in paths to skip. | **string** |
55
+ | `--quiet` | Suppress messages for successful validations. | N/A |
56
+ | `--trim` | Truncate validation messages to not exceed a maximum length. | **number** |
57
+
58
+ ### 4. Example CLI Usage
59
+ Examples:
60
+ - `html-validator`<br>
61
+ Validate all HTML files in the project.
62
+ - `html-validator --exclude=build,tmp`<br>
63
+ Slip all files which have "build" or "tmp" anywhere in their pathname or filename.
64
+ - `html-validator --quiet`<br>
65
+ Suppress "pass" messages.
66
+ - `html-validator docs`<br>
67
+ Validate HTML files in a folder.
68
+ - `html-validator docs --trim=30`<br>
69
+ Truncate messages to 30 characters.
70
+
71
+ ## D) Application Code and Testing Frameworks
72
+ In addition to the CLI interface, the **w3c-html-validator** package can also be imported and called directly in ESM and TypeScript projects.
73
+
74
+ ### 1. Import
75
+ Example call to the `validate()` function:
76
+ ```typescript
77
+ import { w3cHtmlValidator } from 'w3c-html-validator';
78
+
29
79
  const options = { filename: 'docs/index.html' };
30
80
  w3cHtmlValidator.validate(options).then(console.log);
31
81
  ```
32
82
  To display formatted output, replace `console.log` with `w3cHtmlValidator.reporter`:
33
- ```javascript
83
+ ```typescript
34
84
  w3cHtmlValidator.validate(options).then(w3cHtmlValidator.reporter);
35
85
  ```
86
+
36
87
  To see some example validation results, run the commands:
37
88
  ```shell
38
89
  $ cd w3c-html-validator
39
90
  $ node examples.js
40
91
  ```
41
- <img src=https://raw.githubusercontent.com/center-key/w3c-html-validator/main/examples.png
42
- width=800 alt=screenshot>
43
92
 
44
- ## 3) Options
45
- ### w3cHtmlValidator.validate(options)
93
+ ### 2. Options
94
+ #### w3cHtmlValidator.validate(options)
46
95
  | Name (key) | Type | Default | Description |
47
96
  | :--------------- | :---------------------- | :------------------------------- | :------------------------------------------------------------------- |
48
97
  | `html` | **string** | `null` | HTML string to validate. |
@@ -56,14 +105,14 @@ width=800 alt=screenshot>
56
105
  *The `ignoreMessages` and `ignoreLevel` options only work for `'json'` output.&nbsp;
57
106
  Option value `'warning'` also skips `'info'`.
58
107
 
59
- ### w3cHtmlValidator.reporter(options)
108
+ #### w3cHtmlValidator.reporter(options)
60
109
  | Name (key) | Type | Default | Description |
61
110
  | :-------------- | :---------- | :------ | :------------------------------------------------------------- |
62
111
  | `maxMessageLen` | **number** | `null` | Trim validation messages to not exceed a maximum length. |
63
112
  | `quiet` | **boolean** | `false` | Suppress messages for successful validations. |
64
113
  | `title` | **string** | `null` | Override display title (useful for naming HTML string inputs). |
65
114
 
66
- ## 4) TypeScript Declarations
115
+ ### 3. TypeScript Declarations
67
116
  The **TypeScript Declaration File** file is [w3c-html-validator.d.ts](dist/w3c-html-validator.d.ts)
68
117
  in the **dist** folder.
69
118
 
@@ -83,12 +132,13 @@ type ValidatorResults = {
83
132
  };
84
133
  ```
85
134
 
86
- ## 5) Mocha Example
135
+ ### 4. Mocha Example
87
136
  ```javascript
88
137
  import assert from 'assert';
89
138
  import { w3cHtmlValidator } from 'w3c-html-validator';
90
139
 
91
140
  describe('Home page', () => {
141
+
92
142
  it('validates', (done) => {
93
143
  const handleResults = (results) => {
94
144
  assert(results.status === 200, 'Request succeeded');
@@ -98,37 +148,19 @@ describe('Home page', () => {
98
148
  const options = { filename: 'docs/index.html' };
99
149
  w3cHtmlValidator.validate(options).then(handleResults);
100
150
  });
101
- });
102
- ```
103
151
 
104
- ## 6) Command Line
105
- You can install **w3c-html-validator** globally and then run it anywhere directly from the terminal.
106
-
107
- Example terminal commands:
108
- ```shell
109
- $ npm install --global w3c-html-validator
110
- $ html-validator docs/*.html flyer.html
111
- $ html-validator docs #validate html files in a folder
112
- $ html-validator #validate all html files in project
113
- ```
114
- or as an npm script in **package.json**:
115
- ```json
116
- "scripts": {
117
- "validate": "html-validator docs/*.html flyer.html",
118
- "one-folder": "html-validator docs",
119
- "all": "html-validator"
120
- },
152
+ });
121
153
  ```
122
- Passing no parameters defaults to validating all HTML files in the project (skipping the
123
- **node_modules** folder).
124
154
 
125
155
  <br>
126
156
 
127
157
  ---
128
- **Build Tools**
129
- - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):&nbsp; _Prepend a one-line header comment (with license notice) to distribution files_
130
- - 📄 [copy-file-util](https://github.com/center-key/copy-file-util):&nbsp; _Copy or rename a file (CLI tool designed for use in npm scripts)_
131
- - 📂 [copy-folder-cli](https://github.com/center-key/copy-folder-cli):&nbsp; _Recursively copy a folder (CLI tool designed for use in npm scripts)_
158
+ **CLI Build Tools**
159
+ - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):&nbsp; _Prepend a one-line banner comment (with license notice) to distribution files_
160
+ - 📄 [copy-file-util](https://github.com/center-key/copy-file-util):&nbsp; _Copy or rename a file with optional package version number_
161
+ - 📂 [copy-folder-util](https://github.com/center-key/copy-folder-util):&nbsp; _Recursively copy files from one folder to another folder_
162
+ - 🔍 [replacer-util](https://github.com/center-key/replacer-util):&nbsp; _Find and replace strings or template outputs in text files_
163
+ - 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets):&nbsp; _Revision web asset filenames with cache busting content hash fingerprints_
132
164
  - 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator):&nbsp; _Check the markup validity of HTML files using the W3C validator_
133
165
 
134
166
  Feel free to submit questions at:<br>
package/bin/cli.js CHANGED
@@ -21,38 +21,44 @@
21
21
 
22
22
  // Imports
23
23
  import { w3cHtmlValidator } from '../dist/w3c-html-validator.js';
24
- import { lstatSync } from 'fs';
25
- import chalk from 'chalk';
26
- import glob from 'glob';
27
- import log from 'fancy-log';
24
+ import chalk from 'chalk';
25
+ import fs from 'fs';
26
+ import glob from 'glob';
27
+ import log from 'fancy-log';
28
28
 
29
29
  // Parameters
30
- const validFlags = ['quiet'];
30
+ const validFlags = ['exclude', 'quiet', 'trim'];
31
31
  const args = process.argv.slice(2);
32
32
  const flags = args.filter(arg => /^--/.test(arg));
33
33
  const flagMap = Object.fromEntries(flags.map(flag => flag.replace(/^--/, '').split('=')));
34
+ const flagOn = Object.fromEntries(validFlags.map(flag => [flag, flag in flagMap]));
34
35
  const invalidFlag = Object.keys(flagMap).find(key => !validFlags.includes(key));
35
36
  const params = args.filter(arg => !/^--/.test(arg));
36
37
 
37
38
  // Data
38
39
  const files = params;
39
- const mode = { quiet: 'quiet' in flagMap };
40
+ const trim = parseInt(flagMap.trim) || null;
40
41
 
41
42
  // Validator
42
43
  const keep = (filename) => !filename.includes('node_modules/');
43
44
  const readFolder = (folder) => glob.sync(folder + '**/*.html', { ignore: '**/node_modules/**/*' });
44
- const expandFolder = (file) => lstatSync(file).isDirectory() ? readFolder(file + '/') : file;
45
+ const expandFolder = (file) => fs.lstatSync(file).isDirectory() ? readFolder(file + '/') : file;
45
46
  const getFilenames = () => [...new Set(files.map(expandFolder).flat().filter(keep))].sort();
46
- const filenames = files.length ? getFilenames() : readFolder('');
47
+ const list = files.length ? getFilenames() : readFolder('');
48
+ const excludes = flagMap.exclude?.split(',') ?? [];
49
+ const filenames = list.filter(name => !excludes.find(exclude => name.includes(exclude)));
47
50
  const error =
48
- invalidFlag ? 'Invalid flag: ' + invalidFlag :
49
- !files.length ? 'Missing file parameter.' :
50
- !filenames.length ? 'No files to validate.' :
51
+ invalidFlag ? 'Invalid flag: ' + invalidFlag :
52
+ !filenames.length ? 'No files to validate.' :
53
+ flagOn.trim && !trim ? 'Value of "trim" must be a positive whole number.' :
51
54
  null;
52
55
  if (error)
53
56
  throw Error('[w3c-html-validator] ' + error);
54
- if (filenames.length > 1 && !mode.quiet)
57
+ if (filenames.length > 1 && !flagOn.quiet)
55
58
  log(chalk.gray('w3c-html-validator'), chalk.magenta('files: ' + filenames.length));
56
- const reporterOptions = { quiet: mode.quiet };
59
+ const reporterOptions = {
60
+ quiet: flagOn.quiet,
61
+ maxMessageLen: trim,
62
+ };
57
63
  const handleReport = (report) => w3cHtmlValidator.reporter(report, reporterOptions);
58
64
  filenames.forEach(file => w3cHtmlValidator.validate({ filename: file }).then(handleReport));
@@ -1,14 +1,15 @@
1
- //! w3c-html-validator v1.1.2 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
1
+ //! w3c-html-validator v1.2.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
2
2
 
3
- export declare type ValidatorOptions = {
4
- html?: string;
5
- filename?: string;
6
- website?: string;
7
- checkUrl?: string;
8
- ignoreLevel?: 'info' | 'warning';
9
- ignoreMessages?: string | RegExp;
10
- output?: ValidatorResultsOutput;
3
+ export declare type ValidatorSettings = {
4
+ html: string;
5
+ filename: string;
6
+ website: string;
7
+ checkUrl: string;
8
+ ignoreLevel: 'info' | 'warning';
9
+ ignoreMessages: string | RegExp;
10
+ output: ValidatorResultsOutput;
11
11
  };
12
+ export declare type ValidatorOptions = Partial<ValidatorSettings>;
12
13
  export declare type ValidatorResultsMessage = {
13
14
  type: 'info' | 'error' | 'non-document-error' | 'network-error';
14
15
  subType?: 'warning' | 'fatal' | 'io' | 'schema' | 'internal';
@@ -35,11 +36,12 @@ export declare type ValidatorResults = {
35
36
  display: string | null;
36
37
  };
37
38
  export declare type ValidatorResultsOutput = ValidatorResults['output'];
38
- export declare type ReporterOptions = {
39
- maxMessageLen?: number | null;
40
- quiet?: boolean;
41
- title?: string | null;
39
+ export declare type ReporterSettings = {
40
+ maxMessageLen: number | null;
41
+ quiet: boolean;
42
+ title: string | null;
42
43
  };
44
+ export declare type ReporterOptions = Partial<ReporterSettings>;
43
45
  declare const w3cHtmlValidator: {
44
46
  version: string;
45
47
  validate(options: ValidatorOptions): Promise<ValidatorResults>;
@@ -1,11 +1,11 @@
1
- //! w3c-html-validator v1.1.2 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
1
+ //! w3c-html-validator v1.2.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
2
2
 
3
- import { readFileSync } from 'fs';
4
3
  import chalk from 'chalk';
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.1.2',
8
+ version: '1.2.0',
9
9
  validate(options) {
10
10
  var _a;
11
11
  const defaults = {
@@ -22,7 +22,7 @@ const w3cHtmlValidator = {
22
22
  if (settings.output !== 'json' && settings.output !== 'html')
23
23
  throw Error('[w3c-html-validator] Option "output" must be "json" or "html".');
24
24
  const mode = settings.html ? 'html' : settings.filename ? 'filename' : 'website';
25
- const readFile = (filename) => readFileSync(filename, 'utf8').replace(/\r/g, '');
25
+ const readFile = (filename) => fs.readFileSync(filename, 'utf-8').replace(/\r/g, '');
26
26
  const inputHtml = (_a = settings.html) !== null && _a !== void 0 ? _a : (settings.filename ? readFile(settings.filename) : null);
27
27
  const makePostRequest = () => request.post(settings.checkUrl)
28
28
  .set('Content-Type', 'text/html; encoding=utf-8')
@@ -1,4 +1,4 @@
1
- //! w3c-html-validator v1.1.2 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
1
+ //! w3c-html-validator v1.2.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 };
@@ -9,18 +9,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
9
9
  if (v !== undefined) module.exports = v;
10
10
  }
11
11
  else if (typeof define === "function" && define.amd) {
12
- define(["require", "exports", "fs", "chalk", "fancy-log", "superagent"], factory);
12
+ define(["require", "exports", "chalk", "fs", "fancy-log", "superagent"], factory);
13
13
  }
14
14
  })(function (require, exports) {
15
15
  "use strict";
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.w3cHtmlValidator = void 0;
18
- const fs_1 = require("fs");
19
18
  const chalk_1 = __importDefault(require("chalk"));
19
+ const fs_1 = __importDefault(require("fs"));
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.1.2',
23
+ version: '1.2.0',
24
24
  validate(options) {
25
25
  var _a;
26
26
  const defaults = {
@@ -37,7 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  if (settings.output !== 'json' && settings.output !== 'html')
38
38
  throw Error('[w3c-html-validator] Option "output" must be "json" or "html".');
39
39
  const mode = settings.html ? 'html' : settings.filename ? 'filename' : 'website';
40
- const readFile = (filename) => (0, fs_1.readFileSync)(filename, 'utf8').replace(/\r/g, '');
40
+ const readFile = (filename) => fs_1.default.readFileSync(filename, 'utf-8').replace(/\r/g, '');
41
41
  const inputHtml = (_a = settings.html) !== null && _a !== void 0 ? _a : (settings.filename ? readFile(settings.filename) : null);
42
42
  const makePostRequest = () => superagent_1.default.post(settings.checkUrl)
43
43
  .set('Content-Type', 'text/html; encoding=utf-8')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "w3c-html-validator",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "Check the markup validity of HTML files using the W3C validator",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -33,7 +33,7 @@
33
33
  "w3c"
34
34
  ],
35
35
  "jshintConfig": {
36
- "esversion": 9,
36
+ "esversion": 11,
37
37
  "strict": "implied",
38
38
  "eqeqeq": true,
39
39
  "undef": true,
@@ -65,16 +65,16 @@
65
65
  "step:01": "rimraf build dist **/.DS_Store",
66
66
  "step:02": "jshint . --exclude-path .gitignore",
67
67
  "step:03": "eslint --max-warnings 0 . --ext .ts",
68
- "step:04": "tsc",
69
- "step:05": "tsc --module UMD --outDir build/umd",
70
- "step:06": "copy-file build/umd/w3c-html-validator.js build/w3c-html-validator.umd.cjs",
71
- "step:07": "add-dist-header build dist",
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
72
  "pretest": "npm-run-all step:*",
73
73
  "test": "mocha spec/*.spec.js --timeout 5000",
74
74
  "examples": "node examples.js"
75
75
  },
76
76
  "dependencies": {
77
- "chalk": "~5.0",
77
+ "chalk": "~5.1",
78
78
  "fancy-log": "~2.0",
79
79
  "glob": "~8.0",
80
80
  "superagent": "~8.0"
@@ -82,15 +82,15 @@
82
82
  "devDependencies": {
83
83
  "@types/fancy-log": "~2.0",
84
84
  "@types/glob": "~8.0",
85
- "@types/node": "~18.7",
85
+ "@types/node": "~18.8",
86
86
  "@types/superagent": "~4.1",
87
- "@typescript-eslint/eslint-plugin": "~5.38",
88
- "@typescript-eslint/parser": "~5.38",
89
- "add-dist-header": "~0.2",
87
+ "@typescript-eslint/eslint-plugin": "~5.40",
88
+ "@typescript-eslint/parser": "~5.40",
89
+ "add-dist-header": "~0.3",
90
90
  "assert-deep-strict-equal": "~1.0",
91
91
  "copy-file-util": "~0.1",
92
- "copy-folder-cli": "~0.1",
93
- "eslint": "~8.23",
92
+ "copy-folder-util": "~0.2",
93
+ "eslint": "~8.25",
94
94
  "jshint": "~2.13",
95
95
  "merge-stream": "~2.0",
96
96
  "mocha": "~10.0",