prune-html
Given an html string it removes elements matching CSS selector(s) and returns the pruned html string.
Last updated 7 years ago by thlorenz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install prune-html 
SYNC missed versions from official npm registry.

prune-html build status

Given an html string it removes elements matching CSS selector(s) and returns the pruned html string.

var pruneHtml = require('prune-html');

var html = [
    '<div>'
  , '  <h1 class="page-title">Global</h1>'
  , '  <header>'
  , '    <h2>Hello</h2>'
  , '  </header>'
  , '  <article>'
  , '    <div class="container-overview">'
  , '      <dl class="details"></dl>'
  , '    </div>'
  , '    <h3 class="subsection-title">Methods</h3>'
  , '  </article>'
  , '</div>'
].join('\n')

console.log(pruneHtml([ 'h1', '.details', 'h3' ], html));
/* =>
<div>
  <header>
    <h2>Hello</h2>
  </header>
  <article>
    <div class="container-overview">
    </div>
  </article>
</div> */

You can further refine the element filtering using cheerio attributes.

var pruneHtml = require('prune-html');

var html = [
    '<div class="container-overview">'
  , '  <dl data-prune="" class="details"></dl>'
  , '</div>'
].join('\n');

function filterPruneData() {
  return this.data('prune') !== undefined;
}

console.log(pruneHtml([ '*' ], html, filterPruneData));
/* =>
<div class="container-overview">
</div>
*/

Installation

npm install prune-html

API

pruneHtml(selectors, html, filter) → {string}

Prunes all elements matching the selectors from the given html and returns result.

Parameters:
Name Type Argument Description
selectors Array.<String> | String

if one of these CSS selector(s) matches, the element is pruned

html string

unpruned html

filter function <optional>

function which can be used to filter elements further. Inside the function, this refers to the current element.

Source:
Returns:

the pruned html

Type
string

generated with docme

License

MIT

Current Tags

  • 0.2.1                                ...           latest (7 years ago)

3 Versions

  • 0.2.1                                ...           7 years ago
  • 0.2.0                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 2
Dependencies (1)
Dev Dependencies (2)

Copyright 2014 - 2017 © taobao.org |