Socket
Socket
Sign inDemoInstall

clean-html

Package Overview
Dependencies
12
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    clean-html

HTML cleaner and beautifier


Version published
Weekly downloads
2.5K
decreased by-29.98%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

HTML cleaner and beautifier

NPM Stats

Do you have crappy HTML? I do!

<table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="31"><b>Currently we have these articles available:</b>

        <blockquote>
            <!-- List articles -->
              <p><a href="foo.html">The History of Foo</a><br />    
                An <span color="red">informative</span> piece  of <FONT FACE="ARIAL">information</FONT>.</p>
              <p><a href="bar.html">A Horse Walked Into a Bar</a><br/> The bartender said
                "Why the long face?"</p>
	</blockquote>
          </td>
        </tr>
      </table>

Just look at those blank lines and random line breaks, trailing spaces, mixed tabs, deprecated tags - it's outrageous!

Let's clean it up...

$ npm install clean-html
var cleaner = require('clean-html'),
    fs = require('fs'),
    file = process.argv[2];

fs.readFile(file, 'utf-8', function (err, data) {
    cleaner.clean(data, function (html) {
        console.log(html);
    });
});

Sanity restored!

<table>
  <tr>
    <td>
      <b>Currently we have these articles available:</b>
      <blockquote>
        <!-- List articles -->
        <p>
          <a href="foo.html">The History of Foo</a><br>
          An <span>informative</span> piece of information.
        </p>
        <p>
          <a href="bar.html">A Horse Walked Into a Bar</a><br>
          The bartender said "Why the long face?"
        </p>
      </blockquote>
    </td>
  </tr>
</table>

Options

attr-to-remove

Attributes to remove from markup.

Type: Array
Default: ['align', 'bgcolor', 'border', 'cellpadding', 'cellspacing', 'color', 'disabled', 'height', 'target', 'valign', 'width']

block-tags

Block level element tags. Line breaks are added before and after, and nested content is indented.

Type: Array
Default: ['blockquote', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'p', 'table', 'td', 'tr']

break-around-comments

Adds line breaks before and after comments.

Type: Boolean
Default: true

break-after-br

Adds line breaks after br tags.

Type: Boolean
Default: true

empty-tags

Empty element tags.

Type: Array
Default: ['br', 'hr', 'img']

indent

The string to use for indentation. e.g., a tab character or one or more spaces.

Type: String
Default: ' ' (two spaces)

remove-comments

Removes comments.

Type: Boolean
Default: false

remove-empty-paras

Removes empty paragraph tags.

Type: Boolean
Default: false

replace-nbsp

Replaces non-breaking white space entities (&nbsp;) with regular spaces.

Type: Boolean
Default: false

tags-to-remove

Tags to remove from markup.

Type: Array
Default: ['center', 'font']

Adding values to option lists

These options are added for your convenience.

add-attr-to-remove

Additional attributes to remove from markup.

Type: Array
Default: null

add-block-tags

Additional block level element tags.

Type: Array
Default: null

add-empty-tags

Additional empty element tags.

Type: Array
Default: null

add-tags-to-remove

Additional tags to remove from markup.

Type: Array
Default: null

Global installation

All of the options above are available from the command line when the package is installed globally:

$ clean-html crappy.html clean.html

The first argument is the input file and the second is the output file. If no output file is specified, the output will be piped to STDOUT.

Array options should be separated by commas. These are equivalent:

$ clean-html crappy.html clean.html --add-tags-to-remove b,i,u
$ clean-html crappy.html clean.html --add-tags-to-remove 'b,i,u'

Boolean options are parsed as true if they aren't followed by anything. These are equivalent:

$ clean-html crappy.html clean.html --remove-comments
$ clean-html crappy.html clean.html --remove-comments true
$ clean-html crappy.html clean.html --remove-comments 'true'

So are these:

$ clean-html crappy.html clean.html --break-after-br false
$ clean-html crappy.html clean.html --break-after-br 'false'

Keywords

FAQs

Last updated on 13 May 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc