0.8.0 • Published 8 years ago

posthtml-tidy v0.8.0

Weekly downloads
818
License
MIT
Repository
-
Last release
8 years ago

HTML Tidy for PostHTML

HTML Tidy corrects and cleans up HTML and XML documents by fixing markup errors and upgrading legacy code to modern standards.

Install

(sudo) npm i -D posthtml-tidy

Usage

Options

log Boolean

Boolean option which logs tidied html to the console. By default no output is logged.

rules Object

If no rules set, tidy will use it's default setup. For rules take a look at the Quick Reference. Multi-word rules separated with a hyphen should be used with camelCase.

const tidy = require('posthtml-tidy')({
  log: true,
  rules: {
    doctype: 'omit',
    hideComments: true,
    dropEmptyElements: true
    // more options...
  }
})

Input

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>PostHTML Tidy</title>
</head>

<body>
  <!-- Bad format -->
  <h1>Well formatted</h1>
  <h1>Bad formatted</h4>
  <h6>Even worse formatted</h1

  <div></div>
</body>

</html>

Output

<html>
<head>

<meta charset="utf-8">
<title>PostHTML Tidy</title>

</head>

<body>

<h1>Well formatted</h1>
<h1>Bad formatted</h1>
<h6>Even worse formatted</h6>

</body>
</html>

Example using Node API

For general usage and build process integration see PostHTML Docs

const fs = require('fs')

const posthtml = require('posthtml')

const tidy = require('posthtml-tidy')(/* options */)

let html = fs.readFileSync('./index.html', 'utf8')

posthtml([ tidy ])
  .process(html)
  .then(result => console.log(result.html))

Input

<h1>Well formatted</h1>
<h1>Bad formatted</h4>
<h6>Even worser formatted</h1

Output

<h1>Well formatted</h1>
<h1>Bad formatted</h1>
<h6>Even worser formatted</h6>
0.8.0

8 years ago