### Examples Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=readme Practical examples demonstrating the usage of the CLI with various flags. ```APIDOC ## Examples * `html-validator` Validates all HTML files in the project. * `html-validator docs --exclude=build,tmp` Validates all HTML files in the **docs** folder except files which have "build" or "tmp" anywhere in their pathname or filename. * `html-validator docs '--ignore=Trailing slash on void elements'` Allows the ugly slashes of self-closing tags despite XHTML being a hideous scourge on the web. * `html-validator docs '--ignore=/^Duplicate ID/'` Uses a regex (regular expression) to skip all HTML validation messages that start with "Duplicate ID". * `html-validator docs '--ignore=/^Duplicate ID|^Section lacks|^Element .blockquote. not allowed/'` Uses a regex with "or" operators (`|`) to skip multiple HTML validation messages. * `html-validator docs --ignore-config=spec/ignore-config.txt` Similar to the pervious command but strings and regexes are stored in a configuration file (see the _Configuration File for Ignore Patterns_ section below). * `html-validator --default-rules --quiet` Skip all HTML validation messages in the built-in opinionated ignore list and also suppresses all the "pass" status messages. * `html-validator docs --delay=200` Validates all HTML files in the **docs** folder at a rate of 1 file per 200 ms (default is 500 ms). * `html-validator docs --trim=30 --continue` Truncates validation messages to 30 characters and does not abort CI if validation fails. * `html-validator docs --check-url=http://localhost/nu/ --delay=0` Uses a locally hosted W3C validator, such as the docker-validator-w3c server. > [!NOTE] _Single quotes in commands are normalized so they work cross-platform and avoid the errors often encountered on Microsoft Windows._ ``` -------------------------------- ### Ignore Configuration File Example Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=code Example content for an ignore configuration file, where each line specifies a string or regex for messages to be skipped. Lines starting with '#' are comments. ```text # Example configuration file with 3 regexes: ``` -------------------------------- ### Run Example Validation Source: https://www.npmjs.com/package/w3c-html-validator Commands to execute the provided example script. ```bash $ cd w3c-html-validator $ node examples.js ``` -------------------------------- ### Installation Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=code Install the w3c-html-validator package as a development dependency. ```APIDOC ## Installation Install package for node: ```bash $ npm install --save-dev w3c-html-validator ``` ``` -------------------------------- ### Examples Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=code Practical examples demonstrating various ways to use the html-validator CLI. ```APIDOC ## Examples * `html-validator` Validates all HTML files in the project. * `html-validator docs --exclude=build,tmp` Validates all HTML files in the **docs** folder except files which have "build" or "tmp" anywhere in their pathname or filename. * `html-validator docs '--ignore=Trailing slash on void elements'` Allows the ugly slashes of self-closing tags despite XHTML being a hideous scourge on the web. * `html-validator docs '--ignore=/^Duplicate ID/'` Uses a regex (regular expression) to skip all HTML validation messages that start with "Duplicate ID". * `html-validator docs '--ignore=/^Duplicate ID|^Section lacks|^Element .blockquote. not allowed/'` Uses a regex with "or" operators (`|`) to skip multiple HTML validation messages. * `html-validator docs --ignore-config=spec/ignore-config.txt` Similar to the pervious command but strings and regexes are stored in a configuration file (see the _Configuration File for Ignore Patterns_ section below). * `html-validator --default-rules --quiet` Skip all HTML validation messages in the built-in opinionated ignore list and also suppresses all the "pass" status messages. * `html-validator docs --delay=200` Validates all HTML files in the **docs** folder at a rate of 1 file per 200 ms (default is 500 ms). * `html-validator docs --trim=30 --continue` Truncates validation messages to 30 characters and does not abort CI if validation fails. * `html-validator docs --check-url=http://localhost/nu/ --delay=0` Uses a locally hosted W3C validator, such as the docker-validator-w3c server. > [!NOTE] _Single quotes in commands are normalized so they work cross-platform and avoid the errors often encountered on Microsoft Windows._ ``` -------------------------------- ### Examples Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=dependencies Practical examples demonstrating various ways to use the html-validator command with different flags and parameters. ```APIDOC ### 5. Examples * `html-validator` Validates all HTML files in the project. * `html-validator docs --exclude=build,tmp` Validates all HTML files in the **docs** folder except files which have "build" or "tmp" anywhere in their pathname or filename. * `html-validator docs '--ignore=Trailing slash on void elements'` Allows the ugly slashes of self-closing tags despite XHTML being a hideous scourge on the web. * `html-validator docs '--ignore=/^Duplicate ID/'` Uses a regex (regular expression) to skip all HTML validation messages that start with "Duplicate ID". * `html-validator docs '--ignore=/^Duplicate ID|^Section lacks|^Element .blockquote. not allowed/'` Uses a regex with "or" operators (`|`) to skip multiple HTML validation messages. * `html-validator docs --ignore-config=spec/ignore-config.txt` Similar to the pervious command but strings and regexes are stored in a configuration file (see the _Configuration File for Ignore Patterns_ section below). * `html-validator --default-rules --quiet` Skip all HTML validation messages in the built-in opinionated ignore list and also suppresses all the "pass" status messages. * `html-validator docs --delay=200` Validates all HTML files in the **docs** folder at a rate of 1 file per 200 ms (default is 500 ms). * `html-validator docs --trim=30 --continue` Truncates validation messages to 30 characters and does not abort CI if validation fails. * `html-validator docs --check-url=http://localhost/nu/ --delay=0` Uses a locally hosted W3C validator, such as the docker-validator-w3c server. > [!NOTE] _Single quotes in commands are normalized so they work cross-platform and avoid the errors often encountered on Microsoft Windows._ ``` -------------------------------- ### Install w3c-html-validator Source: https://www.npmjs.com/package/w3c-html-validator Install the package as a development dependency in your Node.js project. ```bash $ npm install --save-dev w3c-html-validator ``` -------------------------------- ### Mocha Example for w3c-html-validator Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=dependencies This example demonstrates how to use the w3c-html-validator in a Mocha test to validate an HTML file. Ensure the 'w3c-html-validator' and 'assert' modules are installed. ```javascript import assert from 'assert'; import { w3cHtmlValidator } from 'w3c-html-validator'; describe('Home page', () => { it('validates', (done) => { const handleResults = (results) => { assert(results.status === 200, 'Request succeeded'); assert(results.validates, 'Home page validates'); done(); }; const options = { filename: 'docs/index.html' }; w3cHtmlValidator.validate(options).then(handleResults); }); }); ``` -------------------------------- ### Install w3c-html-validator Source: https://www.npmjs.com/package/w3c-html-validator Install the package via npm to access the validator functionality. ```bash npm i w3c-html-validator ``` -------------------------------- ### Command-line Execution with npx Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=readme Execute the validator using npx for direct command-line use after installation. ```APIDOC ## Command-line Execution with npx Example terminal commands: ```bash $ npm install --save-dev w3c-html-validator $ npx html-validator docs ``` The above `npx` line validates all the HTML files in the **docs** folder. You can also install **w3c-html-validator** globally (`--global`) and then run it anywhere directly from the terminal. ``` -------------------------------- ### Run Validator via npx Source: https://www.npmjs.com/package/w3c-html-validator Execute the validator directly using npx without global installation. ```bash $ npm install --save-dev w3c-html-validator $ npx html-validator docs ``` -------------------------------- ### Usage - Command Line Interface Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=dependencies Learn how to use the html-validator command-line tool, including basic usage, package.json scripts, and npx. ```APIDOC ## Usage ### 1. Synopsis ```bash html-validator [INPUT1] [INPUT2] [INPUT3] [...] ``` Parameters: Each parameter is a folder or file to be sent to the HTML validator. ### 2. npm package.json scripts Run `html-validator` from the `"scripts"` section of your **package.json** file. Example **package.json** scripts: ```json "scripts": { "validate": "html-validator docs flyer.html", "one-folder": "html-validator docs", "all": "html-validator --quiet" }, ``` Passing no parameters defaults to validating all HTML files in the project (skipping the **node_modules** folder). ### 3. Command-line npx Example terminal commands: ```bash $ npm install --save-dev w3c-html-validator $ npx html-validator docs ``` The above `npx` line validates all the HTML files in the **docs** folder. You can also install **w3c-html-validator** globally (`--global`) and then run it anywhere directly from the terminal. ``` -------------------------------- ### Import and Validate HTML Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=versions Demonstrates how to import the validator and use the `validate` function with options. It also shows how to use the `reporter` for formatted output. ```APIDOC ## Import and Validate HTML ### Description This section shows how to import the `w3cHtmlValidator` and use its `validate` function. You can provide options such as `filename` to specify the HTML file to validate. The results can be logged directly or formatted using the `reporter` function. ### Method `import { w3cHtmlValidator } from 'w3c-html-validator';` ### Endpoint N/A (Programmatic Usage) ### Parameters #### Request Body (for `validate` function) - **options** (object) - Required - Configuration options for validation. - **filename** (string) - Optional - HTML file to validate. - **html** (string) - Optional - HTML string to validate. - **checkUrl** (string) - Optional - W3C validation API endpoint. Defaults to 'https://validator.w3.org/nu/'. - **defaultRules** (boolean) - Optional - Apply additional built-in opinionated ignore list. Defaults to `false`. - **dryRun** (boolean) - Optional - Bypass validation. Defaults to `false`. - **ignoreLevel** (string) - Optional - Skip unwanted messages. Can be `'info'` or `'warning'`. Defaults to `null`. - **ignoreMessages** (array) - Optional - Skip messages containing a string or matching a regex. Defaults to `[]`. - **output** (string) - Optional - Get results as `'json'` or `'html'`. Defaults to `'json'`. - **website** (string) - Optional - URL of website to validate. #### Parameters for `reporter` function - **results** (object) - Required - The results object returned by `validate`. - **options** (object) - Optional - Configuration options for the reporter. - **continueOnFail** (boolean) - Optional - Report messages but do not throw an error if validation failed. Defaults to `false`. - **maxMessageLen** (number) - Optional - Trim validation messages to not exceed a maximum length. Defaults to `null`. - **quiet** (boolean) - Optional - Suppress status messages for successful validations. Defaults to `false`. - **title** (string) - Optional - Override display title. Defaults to `null`. ### Request Example ```javascript import { w3cHtmlValidator } from 'w3c-html-validator'; const options = { filename: 'docs/index.html' }; w3cHtmlValidator.validate(options).then(console.log); // outputs the results object // To display formatted output: w3cHtmlValidator.validate(options).then(w3cHtmlValidator.reporter); ``` ### Response #### Success Response (Promise resolving to ValidatorResults) - **validates** (boolean) - Indicates if the HTML is valid. - **mode** (string) - The validation mode used ('html', 'filename', or 'website'). - **html** (string | null) - The validated HTML content if mode is 'html'. - **filename** (string | null) - The validated filename if mode is 'filename'. - **website** (string | null) - The validated website URL if mode is 'website'. - **output** (string) - The format of the output ('json' or 'html'). - **status** (number) - The HTTP status code of the validation response. - **messages** (array | null) - An array of validation messages if output is 'json'. - **display** (string | null) - The formatted HTML output if output is 'html'. - **dryRun** (boolean) - Indicates if the validation was a dry run. ``` -------------------------------- ### Import and Validate HTML Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=code Demonstrates how to import the validator and use the `validate` function with options. It also shows how to use the `reporter` for formatted output. ```APIDOC ## Import and Validate HTML ### Description This section shows how to import the `w3cHtmlValidator` and use its `validate` function. You can provide options such as `filename` to specify the HTML file to validate. The results can be logged directly or formatted using the `reporter` function. ### Method `import { w3cHtmlValidator } from 'w3c-html-validator';` ### Endpoint N/A (Programmatic Usage) ### Parameters #### Request Body (for `validate` function) - **options** (object) - Required - Configuration options for validation. - **filename** (string) - Optional - Path to the HTML file to validate. - **html** (string) - Optional - HTML string to validate. - **checkUrl** (string) - Optional - W3C validation API endpoint. Defaults to 'https://validator.w3.org/nu/'. - **defaultRules** (boolean) - Optional - Apply an additional built-in ignore list. Defaults to `false`. - **dryRun** (boolean) - Optional - Bypass validation. Defaults to `false`. - **ignoreLevel** (string) - Optional - Skip messages with a specific level ('info' or 'warning'). Only works for 'json' output. Defaults to `null`. - **ignoreMessages** (array) - Optional - Skip messages containing a string or matching a regex. Only works for 'json' output. Defaults to `[]`. - **output** (string) - Optional - Format of the results ('json' or 'html'). Defaults to 'json'. - **website** (string) - Optional - URL of the website to validate. #### Parameters for `reporter` function - **results** (object) - Required - The validation results object returned by `validate()`. - **options** (object) - Optional - Configuration options for the reporter. - **continueOnFail** (boolean) - Optional - Report messages but do not throw an error if validation failed. Defaults to `false`. - **maxMessageLen** (number) - Optional - Trim validation messages to not exceed a maximum length. Defaults to `null`. - **quiet** (boolean) - Optional - Suppress status messages for successful validations. Defaults to `false`. - **title** (string) - Optional - Override display title. Defaults to `null`. ### Request Example ```javascript import { w3cHtmlValidator } from 'w3c-html-validator'; const options = { filename: 'docs/index.html' }; w3cHtmlValidator.validate(options).then(console.log); // outputs the results object // To display formatted output: w3cHtmlValidator.validate(options).then(w3cHtmlValidator.reporter); ``` ### Response #### Success Response (Promise resolves with ValidatorResults object) - **validates** (boolean) - Indicates if the HTML is valid. - **mode** (string) - The validation mode ('html', 'filename', or 'website'). - **html** (string | null) - The validated HTML content if mode is 'html'. - **filename** (string | null) - The validated filename if mode is 'filename'. - **website** (string | null) - The validated website URL if mode is 'website'. - **output** (string) - The format of the output ('json' or 'html'). - **status** (number) - The HTTP status code of the validation response. - **messages** (array | null) - An array of validation messages if output is 'json'. - **display** (string | null) - The formatted HTML output if output is 'html'. - **dryRun** (boolean) - Indicates if the validation was a dry run. #### Response Example (for `console.log`) ```json { "validates": true, "mode": "filename", "html": null, "filename": "docs/index.html", "website": null, "output": "json", "status": 200, "messages": [], "display": null, "dryRun": false } ``` ``` -------------------------------- ### HTML Validator Synopsis Source: https://www.npmjs.com/package/w3c-html-validator General syntax for running the validator from the command line. ```bash html-validator [INPUT1] [INPUT2] [INPUT3] [...] ``` -------------------------------- ### CLI Usage and Flags Source: https://www.npmjs.com/package/w3c-html-validator Details on how to use the html-validator command-line tool and the available configuration flags. ```APIDOC ## CLI Command: html-validator ### Description Validates HTML files or folders using the W3C validator. If no parameters are provided, it defaults to validating all HTML files in the project (excluding node_modules). ### Usage `html-validator [INPUT1] [INPUT2] [...] [FLAGS]` ### Parameters #### Path Parameters - **INPUT** (string) - Optional - A folder or file path to be sent to the HTML validator. ### CLI Flags - **--check-url** (string) - Optional - W3C validation API endpoint (e.g., http://localhost/nu/). - **--continue** (boolean) - Optional - Report messages but do not throw an error if validation failed. - **--default-rules** (boolean) - Optional - Apply additional built-in opinionated ignore list. - **--delay** (number) - Optional - Debounce pause in milliseconds between each file validation. - **--dry-run** (boolean) - Optional - Bypass validation. - **--exclude** (string) - Optional - Comma separated list of strings to match in paths to skip. - **--ignore-config** (string) - Optional - File containing strings and regexes of validation messages to skip. - **--ignore** (string) - Optional - Skip validation messages containing a string or matching a regex. - **--note** (string) - Optional - Comment for human reference. - **--quiet** (boolean) - Optional - Suppress messages for successful validations. - **--trim** (number) - Optional - Truncate validation messages to a maximum length. ``` -------------------------------- ### Import and Validate HTML Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=readme Demonstrates how to import the w3cHtmlValidator and use the validate function with options to check an HTML file. It shows how to log the raw results or use a formatted reporter. ```APIDOC ## Import and Validate HTML ### Description This section shows how to import the `w3cHtmlValidator` module and use its `validate` function to check an HTML file. It includes examples for logging raw results and using the `reporter` for formatted output. ### Method `w3cHtmlValidator.validate(options)` ### Parameters #### Request Body - **options** (object) - Required - Configuration options for validation. - **filename** (string) - Optional - Path to the HTML file to validate. - **html** (string) - Optional - HTML string to validate. - **website** (string) - Optional - URL of the website to validate. - **checkUrl** (string) - Optional - Defaults to 'https://validator.w3.org/nu/'. The W3C validation API endpoint. - **defaultRules** (boolean) - Optional - Defaults to `false`. Apply an additional built-in ignore list. - **dryRun** (boolean) - Optional - Defaults to `false`. Bypass validation. - **ignoreLevel** (string) - Optional - `'info'` or `'warning'`. Skip messages with this level or lower. Only works for JSON output. - **ignoreMessages** (array) - Optional - Array of strings or regex patterns to ignore in validation messages. Only works for JSON output. - **output** (string) - Optional - Defaults to `'json'`. `'json'` for results array, `'html'` for web page output. ### Request Example ```javascript import { w3cHtmlValidator } from 'w3c-html-validator'; const options = { filename: 'docs/index.html' }; w3cHtmlValidator.validate(options).then(console.log); // Outputs the results object w3cHtmlValidator.validate(options).then(w3cHtmlValidator.reporter); // Outputs formatted results ``` ### Response #### Success Response (Promise resolves with ValidatorResults object) - **validates** (boolean) - Indicates if the HTML is valid. - **mode** (string) - The validation mode used ('html', 'filename', 'website'). - **html** (string | null) - The validated HTML content if mode is 'html'. - **filename** (string | null) - The validated filename if mode is 'filename'. - **website** (string | null) - The validated website URL if mode is 'website'. - **output** (string) - The format of the output ('json' or 'html'). - **status** (number) - The HTTP status code of the validation response. - **messages** (array | null) - An array of validation messages if output is 'json'. - **display** (string | null) - Formatted output string if output is 'html'. - **dryRun** (boolean) - Indicates if the validation was a dry run. ``` -------------------------------- ### Basic Usage Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=code Validate HTML files or folders using the command-line interface. ```APIDOC ## Basic Usage ### Synopsis ```bash html-validator [INPUT1] [INPUT2] [INPUT3] [...] ``` Parameters: Each parameter is a folder or file to be sent to the HTML validator. ### npm package.json scripts Run `html-validator` from the `"scripts"` section of your **package.json** file. Example **package.json** scripts: ```json "scripts": { "validate": "html-validator docs flyer.html", "one-folder": "html-validator docs", "all": "html-validator --quiet" }, ``` Passing no parameters defaults to validating all HTML files in the project (skipping the **node_modules** folder). ### Command-line npx Example terminal commands: ```bash $ npm install --save-dev w3c-html-validator $ npx html-validator docs ``` The above `npx` line validates all the HTML files in the **docs** folder. You can also install **w3c-html-validator** globally (`--global`) and then run it anywhere directly from the terminal. ``` -------------------------------- ### Basic Usage Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=readme Validate HTML files by providing them as arguments or by using npm scripts. ```APIDOC ## Basic Usage ### 1. Synopsis Validate HTML files by listing them as arguments: ```bash html-validator [INPUT1] [INPUT2] [INPUT3] [...] ``` Parameters: Each parameter is a folder or file to be sent to the HTML validator. ### 2. npm package.json scripts Run `html-validator` from the `"scripts"` section of your **package.json** file. Example **package.json** scripts: ```json "scripts": { "validate": "html-validator docs flyer.html", "one-folder": "html-validator docs", "all": "html-validator --quiet" } ``` Passing no parameters defaults to validating all HTML files in the project (skipping the **node_modules** folder). ``` -------------------------------- ### Configure package.json Scripts Source: https://www.npmjs.com/package/w3c-html-validator Define validation commands within the scripts section of your package.json file. ```json "scripts": { "validate": "html-validator docs flyer.html", "one-folder": "html-validator docs", "all": "html-validator --quiet" }, ``` -------------------------------- ### Configuration File for Ignore Patterns Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=code Details on how to use a configuration file to specify patterns for ignoring validation messages. ```APIDOC ## Configuration File for Ignore Patterns The optional `--ignore-config=FILENAME` flag specifies a configuration file with one string or regex per line. HTML validation messages containing any of the strings or matching any of the regexes will be skipped. Empty lines and lines starting with a hash sign (`#`) are treated as comments and do nothing. Example configuration file with 3 regexes: ``` ``` -------------------------------- ### Apply Default Rules and Quiet Mode Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=code Use --default-rules to apply an opinionated ignore list and --quiet to suppress success messages. ```bash html-validator --default-rules --quiet ``` -------------------------------- ### Ignore Configuration Source: https://www.npmjs.com/package/w3c-html-validator?activeTab=code Details on how to configure message ignoring using regex patterns and the default ignore list. ```APIDOC ## Ignore Configuration ### Description This section explains how to configure the validator to ignore specific validation messages using regular expressions or a default list of common, often unhelpful, messages. ### Method Configuration is done via the `ignoreMessages` and `ignoreLevel` options when calling `w3cHtmlValidator.validate()`. ### Parameters #### `ignoreMessages` (array) - Type: `array` - Default: `[]` - Description: An array of strings or regular expressions. Messages containing a string or matching a regex in this list will be ignored. This option only works for `'json'` output. #### `ignoreLevel` (string) - Type: `'info'` or `'warning'` - Default: `null` - Description: Skips messages with a level equal to or less than the specified level. Setting `ignoreLevel` to `'warning'` skips both `'warning'` and `'info'` level messages. This option only works for `'json'` output. ### Default Ignore List The `--default-rules` flag (or `defaultRules: true` option) applies an opinionated pre-defined list of messages that are often considered unhelpful. | Pattern | Level | Explanation | |------------------------|---------|------------------------------------------------------------------------------------------------------------| | `with computed level` | error | Ridiculous that adding an `