Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

diffable-html

rayrutjes4mMIT5.0.0TypeScript support: definitely-typed

Opinionated HTML formatter focused towards making HTML diffs readable.

html, formatter, diff, diffable, jest, snapshot, serializer, snapshotSerializer, string, vue, vue.js, test, beautifier, js-beautify

readme

Opinionated HTML formatter focused towards making HTML diffs readable.

NPM version Build Status

This formatter will normalize your HTML in a way that when you diff it, you get a clear sense of what changed.

This is a "zero-config" and opinionated HTML formatter. Default rules might change in future releases in which case we will push a major release.

Feel free to open issues to discuss better defaults.

Formatting consists of:

  • indenting every level with 2 spaces
  • align attributes
  • put every opening and closing tag on its own line
  • trimming text nodes

Be aware that this plugin is intended for making HTML diffs more readable. We took the compromise of not dealing with white-spaces like the browsers do.

Install

Add the package as a dev-dependency:

# With npm
npm install --save-dev diffable-html

# With yarn
yarn add --dev diffable-html

Example

import toDiffableHtml from 'diffable-html';

const html = `
<div id="header">
  <h1>Hello World!</h1>
  <ul id="main-list" class="list"><li><a href="#">My HTML</a></li></ul>
</div>
`

console.log(toDiffableHtml(html));

Will output:

<div id="header">
  <h1>
    Hello World!
  </h1>
  <ul
    id="main-list"
    class="list"
  >
    <li>
      <a href="#">
        My HTML
      </a>
    </li>
  </ul>
</div>

Yet another HTML formatting plugin?

This formatter was initially developed to address the lack of some features in js-beautifier:

  • Put the inner content of each tag on its own line (beautify-web/js-beautify#980)
  • Put closing bracket on its own line (beautify-web/js-beautify#937)
  • Indent every text node

These features are needed to improve readability of HTML diffs.

Usage with Jest

Development of this plugin was motivated by making testing of Vue.js components easier by the use of Jest with snapshot tests.

You can find a serializer for formatting your HTML here Jest serializer.

changelog

5.0.0 (2021-10-19)

Features

4.1.0 (2021-07-13)

Features

4.0.0 (2018-12-31)

Bug Fixes

  • trim space characters but not Unicode whitespace (bade75d), closes #14

3.0.0 (2018-01-03)

Features

2.1.0 (2017-05-27)

Features

  • xml: handle self closing tags as real tags (e9f80d0)

2.0.1 (2017-05-18)

Bug Fixes

  • build: add the missing build (eee0b47)

2.0.0 (2017-05-18)

Features

  • directives: add support for directives (c86f99e)

Bug Fixes

  • attributes: do not display right side of empty attributes (c86f99e)
  • tags: do not automatically lower case tag names (c86f99e)

1.0.0 (2017-04-18)

0.2.0 (2017-04-18)

Bug Fixes

  • build: export dist folder (0841b9c)

0.1.0 (2017-04-18)