unified

Project:rehypejs/rehype-format

Package:rehype-format@3.0.0

  1. Dependencies:6·Dependents:28
  2. rehype plugin to format HTML
  1. unified 132
  2. plugin 88
  3. rehype 76
  4. html 55
  5. format 3

rehype-format

Build Coverage Downloads Size Sponsors Backers Chat

rehype plugin to format HTML.

Install

npm:

npm install rehype-format

Use

Say we have the following file, index.html:

<!doCTYPE HTML><html>
 <head>
    <title>Hello!</title>
<meta charset=utf8>
      </head>
  <body><section>    <p>hi there</p>
     </section>
 </body>
</html>

And our script, example.js, looks as follows:

var vfile = require('to-vfile')
var report = require('vfile-reporter')
var rehype = require('rehype')
var format = require('rehype-format')

rehype()
  .use(format)
  .process(vfile.readSync('index.html'), function(err, file) {
    console.error(report(err || file))
    console.log(String(file))
  })

Now, running node example yields:

index.html: no issues found
<!doctype html>
<html>
  <head>
    <title>Hello!</title>
    <meta charset="utf8">
  </head>
  <body>
    <section>
      <p>hi there</p>
    </section>
  </body>
</html>

API

rehype().use(format[, options])

Format white space in the processed tree.

All superfluous white space is removed. However, as newlines are kept (and later properly indented), your code will still line-wrap as expected.

options
options.indent

Indentation per level (number, string, default: 2). When number, uses that amount of spaces. When string, uses that per indentation level.

options.indentInitial

Whether to indent the first level (boolean, default: true). This is usually the <html>, thus not indenting head and body.

options.blanks

List of tag names to join with a blank line (Array.<string>, default: []). These tags, when next to each other, are joined by a blank line (\n\n). For example, when ['head', 'body'] is given, a blank line is added between these two.

Security

Use of rehype-format changes white space in the syntax tree. White space in <script>, <style>, <pre>, or <textarea> is not modified. If the tree is already safe, use of this plugin does not open you up for a cross-site scripting (XSS) attack. When in doubt, use rehype-sanitize.

Contribute

See contributing.md in rehypejs/.github for ways to get started. See support.md for ways to get help.

This project has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Titus Wormer