w3c-html-validator 1.1.3 → 1.2.1

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,91 @@ _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
- ## A) 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.
13
+
14
+ <img src=https://raw.githubusercontent.com/center-key/w3c-html-validator/main/examples.png
15
+ width=800 alt=screenshot>
12
16
 
13
- ### Install
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
- ## B) 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
+ | `--note` | Place to add a comment only for humans. | **string** |
56
+ | `--quiet` | Suppress messages for successful validations. | N/A |
57
+ | `--trim` | Truncate validation messages to not exceed a maximum length. | **number** |
58
+
59
+ ### 4. Example CLI Usage
60
+ Examples:
61
+ - `html-validator`<br>
62
+ Validate all HTML files in the project.
63
+ - `html-validator --exclude=build,tmp`<br>
64
+ Slip all files which have "build" or "tmp" anywhere in their pathname or filename.
65
+ - `html-validator --quiet`<br>
66
+ Suppress "pass" messages.
67
+ - `html-validator docs`<br>
68
+ Validate HTML files in a folder.
69
+ - `html-validator docs --trim=30`<br>
70
+ Truncate messages to 30 characters.
71
+
72
+ ## D) Application Code and Testing Frameworks
73
+ In addition to the CLI interface, the **w3c-html-validator** package can also be imported and called directly in ESM and TypeScript projects.
74
+
75
+ ### 1. Import
76
+ Example call to the `validate()` function:
77
+ ```typescript
78
+ import { w3cHtmlValidator } from 'w3c-html-validator';
79
+
29
80
  const options = { filename: 'docs/index.html' };
30
81
  w3cHtmlValidator.validate(options).then(console.log);
31
82
  ```
32
83
  To display formatted output, replace `console.log` with `w3cHtmlValidator.reporter`:
33
- ```javascript
84
+ ```typescript
34
85
  w3cHtmlValidator.validate(options).then(w3cHtmlValidator.reporter);
35
86
  ```
87
+
36
88
  To see some example validation results, run the commands:
37
89
  ```shell
38
90
  $ cd w3c-html-validator
39
91
  $ node examples.js
40
92
  ```
41
- <img src=https://raw.githubusercontent.com/center-key/w3c-html-validator/main/examples.png
42
- width=800 alt=screenshot>
43
93
 
44
- ## C) Options
45
- ### w3cHtmlValidator.validate(options)
94
+ ### 2. Options
95
+ #### w3cHtmlValidator.validate(options)
46
96
  | Name (key) | Type | Default | Description |
47
97
  | :--------------- | :---------------------- | :------------------------------- | :------------------------------------------------------------------- |
48
98
  | `html` | **string** | `null` | HTML string to validate. |
@@ -56,14 +106,14 @@ width=800 alt=screenshot>
56
106
  *The `ignoreMessages` and `ignoreLevel` options only work for `'json'` output.&nbsp;
57
107
  Option value `'warning'` also skips `'info'`.
58
108
 
59
- ### w3cHtmlValidator.reporter(options)
109
+ #### w3cHtmlValidator.reporter(options)
60
110
  | Name (key) | Type | Default | Description |
61
111
  | :-------------- | :---------- | :------ | :------------------------------------------------------------- |
62
112
  | `maxMessageLen` | **number** | `null` | Trim validation messages to not exceed a maximum length. |
63
113
  | `quiet` | **boolean** | `false` | Suppress messages for successful validations. |
64
114
  | `title` | **string** | `null` | Override display title (useful for naming HTML string inputs). |
65
115
 
66
- ## D) TypeScript Declarations
116
+ ### 3. TypeScript Declarations
67
117
  The **TypeScript Declaration File** file is [w3c-html-validator.d.ts](dist/w3c-html-validator.d.ts)
68
118
  in the **dist** folder.
69
119
 
@@ -83,12 +133,13 @@ type ValidatorResults = {
83
133
  };
84
134
  ```
85
135
 
86
- ## E) Mocha Example
136
+ ### 4. Mocha Example
87
137
  ```javascript
88
138
  import assert from 'assert';
89
139
  import { w3cHtmlValidator } from 'w3c-html-validator';
90
140
 
91
141
  describe('Home page', () => {
142
+
92
143
  it('validates', (done) => {
93
144
  const handleResults = (results) => {
94
145
  assert(results.status === 200, 'Request succeeded');
@@ -98,39 +149,20 @@ describe('Home page', () => {
98
149
  const options = { filename: 'docs/index.html' };
99
150
  w3cHtmlValidator.validate(options).then(handleResults);
100
151
  });
101
- });
102
- ```
103
-
104
- ## F) Command Line
105
- You can install **w3c-html-validator** globally and then run it anywhere directly from the terminal.
106
152
 
107
- Example terminal commands:
108
- ```shell
109
- $ npm install --global w3c-html-validator
110
- $ html-validator #validate all html files in project
111
- $ html-validator docs #validate html files in a folder
112
- $ html-validator docs/*.html flyer.html
113
- $ html-validator docs --quiet #suppress "pass" messages
114
- $ html-validator docs --trim=30 #truncate messages to 30 characters
115
- ```
116
- or as an npm script in **package.json**:
117
- ```json
118
- "scripts": {
119
- "validate": "html-validator docs/*.html flyer.html",
120
- "one-folder": "html-validator docs",
121
- "all": "html-validator --quiet"
122
- },
153
+ });
123
154
  ```
124
- Passing no parameters defaults to validating all HTML files in the project (skipping the
125
- **node_modules** folder).
126
155
 
127
156
  <br>
128
157
 
129
158
  ---
130
- **Build Tools**
131
- - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):&nbsp; _Prepend a one-line header comment (with license notice) to distribution files_
132
- - 📄 [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)_
133
- - 📂 [copy-folder-cli](https://github.com/center-key/copy-folder-cli):&nbsp; _Recursively copy a folder (CLI tool designed for use in npm scripts)_
159
+ **CLI Build Tools**
160
+ - 🎋 [add-dist-header](https://github.com/center-key/add-dist-header):&nbsp; _Prepend a one-line banner comment (with license notice) to distribution files_
161
+ - 📄 [copy-file-util](https://github.com/center-key/copy-file-util):&nbsp; _Copy or rename a file with optional package version number_
162
+ - 📂 [copy-folder-util](https://github.com/center-key/copy-folder-util):&nbsp; _Recursively copy files from one folder to another folder_
163
+ - 🔍 [replacer-util](https://github.com/center-key/replacer-util):&nbsp; _Find and replace strings or template outputs in text files_
164
+ - 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets):&nbsp; _Revision web asset filenames with cache busting content hash fingerprints_
165
+ - 🚆 [run-scripts-util](https://github.com/center-key/run-scripts-util):&nbsp; _Organize npm scripts into named groups of easy to manage commands_
134
166
  - 🚦 [w3c-html-validator](https://github.com/center-key/w3c-html-validator):&nbsp; _Check the markup validity of HTML files using the W3C validator_
135
167
 
136
168
  Feel free to submit questions at:<br>
package/bin/cli.js CHANGED
@@ -20,42 +20,38 @@
20
20
  // $ node bin/cli.js spec/**/*.html --quiet
21
21
 
22
22
  // Imports
23
+ import { cliArgvUtil } from 'cli-argv-util';
23
24
  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';
25
+ import chalk from 'chalk';
26
+ import fs from 'fs';
27
+ import glob from 'glob';
28
+ import log from 'fancy-log';
28
29
 
29
- // Parameters
30
- const validFlags = ['quiet', 'trim'];
31
- const args = process.argv.slice(2);
32
- const flags = args.filter(arg => /^--/.test(arg));
33
- const flagMap = Object.fromEntries(flags.map(flag => flag.replace(/^--/, '').split('=')));
34
- const invalidFlag = Object.keys(flagMap).find(key => !validFlags.includes(key));
35
- const params = args.filter(arg => !/^--/.test(arg));
36
-
37
- // Data
38
- const files = params;
39
- const mode = { quiet: 'quiet' in flagMap, trim: 'trim' in flagMap };
40
- const trim = parseInt(flagMap.trim) || null;
30
+ // Parameters and flags
31
+ const validFlags = ['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/');
44
38
  const readFolder = (folder) => glob.sync(folder + '**/*.html', { ignore: '**/node_modules/**/*' });
45
- const expandFolder = (file) => lstatSync(file).isDirectory() ? readFolder(file + '/') : file;
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
- const filenames = files.length ? getFilenames() : readFolder('');
41
+ const list = files.length ? getFilenames() : readFolder('');
42
+ const excludes = cli.flagMap.exclude?.split(',') ?? [];
43
+ const filenames = list.filter(name => !excludes.find(exclude => name.includes(exclude)));
48
44
  const error =
49
- invalidFlag ? 'Invalid flag: ' + invalidFlag :
50
- !filenames.length ? 'No files to validate.' :
51
- mode.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.' :
52
48
  null;
53
49
  if (error)
54
50
  throw Error('[w3c-html-validator] ' + error);
55
- if (filenames.length > 1 && !mode.quiet)
51
+ if (filenames.length > 1 && !cli.flagOn.quiet)
56
52
  log(chalk.gray('w3c-html-validator'), chalk.magenta('files: ' + filenames.length));
57
53
  const reporterOptions = {
58
- quiet: mode.quiet,
54
+ quiet: cli.flagOn.quiet,
59
55
  maxMessageLen: trim,
60
56
  };
61
57
  const handleReport = (report) => w3cHtmlValidator.reporter(report, reporterOptions);
@@ -1,15 +1,16 @@
1
- //! w3c-html-validator v1.1.3 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
1
+ //! w3c-html-validator v1.2.1 ~~ 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 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 ValidatorResultsMessage = {
12
+ export type ValidatorOptions = Partial<ValidatorSettings>;
13
+ export type ValidatorResultsMessage = {
13
14
  type: 'info' | 'error' | 'non-document-error' | 'network-error';
14
15
  subType?: 'warning' | 'fatal' | 'io' | 'schema' | 'internal';
15
16
  message: string;
@@ -20,9 +21,9 @@ export declare type ValidatorResultsMessage = {
20
21
  hiliteStart: number;
21
22
  hiliteLength: number;
22
23
  };
23
- export declare type ValidatorResultsMessageType = ValidatorResultsMessage['type'];
24
- export declare type ValidatorResultsMessageSubType = ValidatorResultsMessage['subType'];
25
- export declare type ValidatorResults = {
24
+ export type ValidatorResultsMessageType = ValidatorResultsMessage['type'];
25
+ export type ValidatorResultsMessageSubType = ValidatorResultsMessage['subType'];
26
+ export type ValidatorResults = {
26
27
  validates: boolean;
27
28
  mode: 'html' | 'filename' | 'website';
28
29
  title: string;
@@ -34,12 +35,13 @@ export declare type ValidatorResults = {
34
35
  messages: ValidatorResultsMessage[] | null;
35
36
  display: string | null;
36
37
  };
37
- export declare type ValidatorResultsOutput = ValidatorResults['output'];
38
- export declare type ReporterOptions = {
39
- maxMessageLen?: number | null;
40
- quiet?: boolean;
41
- title?: string | null;
38
+ export type ValidatorResultsOutput = ValidatorResults['output'];
39
+ export type ReporterSettings = {
40
+ maxMessageLen: number | null;
41
+ quiet: boolean;
42
+ title: string | null;
42
43
  };
44
+ export 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.3 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
1
+ //! w3c-html-validator v1.2.1 ~~ 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.3',
8
+ version: '1.2.1',
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.3 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
1
+ //! w3c-html-validator v1.2.1 ~~ 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.3',
23
+ version: '1.2.1',
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.3",
3
+ "version": "1.2.1",
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,
@@ -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
- "step:01": "rimraf build dist **/.DS_Store",
66
- "step:02": "jshint . --exclude-path .gitignore",
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",
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.0",
81
+ "chalk": "~5.1",
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.7",
90
+ "@types/node": "~18.11",
86
91
  "@types/superagent": "~4.1",
87
- "@typescript-eslint/eslint-plugin": "~5.38",
88
- "@typescript-eslint/parser": "~5.38",
89
- "add-dist-header": "~0.2",
92
+ "@typescript-eslint/eslint-plugin": "~5.43",
93
+ "@typescript-eslint/parser": "~5.43",
94
+ "add-dist-header": "~0.3",
90
95
  "assert-deep-strict-equal": "~1.0",
91
96
  "copy-file-util": "~0.1",
92
- "copy-folder-cli": "~0.1",
93
- "eslint": "~8.23",
97
+ "copy-folder-util": "~0.2",
98
+ "eslint": "~8.28",
94
99
  "jshint": "~2.13",
95
100
  "merge-stream": "~2.0",
96
- "mocha": "~10.0",
97
- "npm-run-all2": "~6.0",
101
+ "mocha": "~10.1",
98
102
  "rimraf": "~3.0",
99
- "typescript": "~4.8"
103
+ "run-scripts-util": "~0.1",
104
+ "typescript": "~4.9"
100
105
  }
101
106
  }