Skip to content

Grunt task for checking and fixing HTML files using HTML Tidy (tidy-html5)

License

Notifications You must be signed in to change notification settings

prantlf/grunt-tidy-html5

Repository files navigation

grunt-tidy-html5

NPM version Build Status Coverage Status Dependency Status devDependency Status devDependency Status Code Climate Codacy Badge Built with Grunt semantic-release

NPM Downloads

This module provides a grunt multi-task for checking and fixing HTML files using tidy-html5, wrapped for Node.js by node-libtidy. See the official HTML Tidy pages for more information.

Installation

You need node >= 4, npm and grunt >= 1 installed and your project build managed by a Gruntfile with the necessary modules listed in package.json. If you haven't used Grunt before, be sure to check out the [Getting Started] guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins.

Install the Grunt task:

$ npm install grunt-tidy-html5 --save-dev

Configuration

Add the tidy-html5 entry with the HTML Tidy task configuration to the options of the grunt.initConfig method:

grunt.initConfig({
  'tidy-html5': {
    test: {
      src: ['*.html']
    }
  }
});

Then, load the plugin:

grunt.loadNpmTasks('grunt-tidy-html5');

Build

Call the HTML Tidy task:

$ grunt tidy-html5

or integrate it to your build sequence in Gruntfile.js:

grunt.registerTask('default', ['tidy-html5', ...]);

Warnings about invalid parts of the HTML markup will be logged on the console, if the quiet mode is not enabled. If there are any, unless the task execution is forced, the task will make Grunt fail.

Customizing

Default behaviour of the task can be tweaked by the task options; these are the defaults:

grunt.initConfig({
  tidy-html5: {
    task: {
      options: {
        force: false,
        quiet: false,
        report: '',
        ignoreMissing: false,
        tidyOptions: {}
      },
      src: ...
    }
  }
});

Options

force

Type: Boolean Default: false

Suppresses reporting the failure to Grunt and thus stopping execution of further tasks, if set to true.

quiet

Type: Boolean Default: false

Suppresses printing of errors and warnings about problems found in input files on the console. if set to true.

report

Type: String Default: ''

Path to the file, where the report will be written. If specified, the file will contain an array of objects describing every problem found, for example:

[
  {
    "type": "warning",
    "firstColumn": 1,
    "lastColumn": 1,
    "lastLine": 1,
    "message": "missing <!DOCTYPE> declaration",
    "extract": "<html></html>",
    "hiliteStart": 0,
    "hiliteLength": 0,
    "file":"test/work/incomplete.html"
  }
]

The file will have the JSON format compatible with the JSON report produced by grunt-html, which is backed up by gthe Nu Html Checker (v.Nu). Marking the invalid code excerps is not supported.

The JSON report file can be converted to a HTML report file using grunt-html-html-report-converter.

ignoreMissing

Type: Boolean Default: false

If the src property does not point to any files, or if it is missing, the task will make the Grunt run fail. If you set the ignoreMissing option to true, Grunt will continue executing other tasks.

tidyOptions

Type: Object Default: {}

Object passed to the TidyDoc.options property to set libtidy options. Any of all HTML Tidy options can be used as a property key and its value as a property value in the object.

More Usage Examples

'tidy-html5': {
  accessibility: {
    options: {
      report: 'output/report.json',
      tidyOptions: {
        'accessibility-check': 2
      }
    },
    src: ['input/*.html']
  }
}

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 2018-04-27 v1.0.0 Dropped support of Node.js 4
  • 2018-03-06 v0.1.0 Write report to a file as JSON
  • 2018-02-26 v0.0.1 Initial release

License

Copyright (c) 2018-2019 Ferdinand Prantl

Licensed under the MIT license.