npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-mutate

v0.2.1

Published

Manipulate HTML on the fly and inject data using simple CSS selectors

Downloads

5

Readme

HTML Mutate

Manipulate HTML on the fly and inject data using simple CSS selectors

Install

$ npm i html-mutate

Module API

module(path)

Return a template function.

path is the path to the HTML file that will be used as a template.

var mutate = require('html-mutate')

var template = mutate('myFile.html')

If path isn't a valid path to a file, it will be considered being an HTML string and will be used as such. This let you choose if you prefer to load some HTML from a file or to craft your own.

var mutate = require('html-mutate')

var template = mutate('<!DOCTYPE html><title></title><main></main>')

template(data)

Return a readable stream of the resulting HTML file

data are the data that will be injected into the HTML, see below to learn more about the expected data format

const fs     = require('fs')
const mutate = require('html-mutate')
const data   = require('./data.json')

const template = mutate('./base.html')

template(data).pipe(fs.createWriteStream('./index.html'))

template.callback(data, fn)

fn is a callback function that will be called once the data would have been injected into the HTML flow. It follows node conventions for callback with the following signature: fn(err, html) where err is a possible Error object and html the successful transformed HTML

const fs     = require('fs')
const mutate = require('html-mutate')
const data   = require('./data.json')

const template = mutate('./base.html')

template.callback(data, (err, html) => {
  if (err) { throw err }

  fs.writeFile('./index.html', html)
})

template.promise(data)

Return a Promise where the success callback will get the transformed HTML

const fs     = require('fs')
const mutate = require('html-mutate')
const data   = require('./data.json')

const template = mutate('./base.html')

template.promise(data).then((html) => {
  fs.writeFile('./index.html', html)
}, (err) => {
  throw err
})

template.inject(data)

Return a transform stream that will inject data into any HTML read stream. This is especially handy to chain data injection or to output several file out of one template.

const fs     = require('fs')
const mutate = require('html-mutate')
const data1  = require('./data1.json')
const data2  = require('./data2.json')

const template = mutate('./base.html')
const stream   = template({})

stream
  .pipe(template.inject(data1))
  .pipe(fs.createWriteStream('./index.html'))

stream
  .pipe(template.inject(data2))
  .pipe(fs.createWriteStream('./page.html'))

Data magic

The template magic is based on the data format used. Data are a collection of key/value pair where the key is a valid CSS selector and the value is an object (or array of object) defining the data to be injected and how to inject them.

Replacing element content

{
  "main": {
    "replace": "<p>Hello world!</p>"
  }
}

For a straight replacement, you can use a compact syntax:

{
  "main": "<p>Hello world!</p>"
}

NOTE: Using the value null will remove the element if it exists

Append or prepend content to element

{
  "main": {
    "prepend": "<p>Hi!</p>",
    "append" : "<p>Bye!</p>"
  }
}

Selector matching more than one element

If a selector match more than one element, the object value will be used for all of them. However, if the value is an Array, each value of the Array will be successively used to alter the elements.

If there are more elements than values then the extra elements will be altered using the last available value.

{
  ".page": [
    {"replace": 1},
    {"replace": 2},
    {"replace": 3},
    {"replace": 4},
    {"replace": 5},
  ]
}

Again, for a straight replacement, you can use a more compact syntax:

{
  ".page": [1, 2, 3, 4, 5]
}

Replacing attributes content

It is possible to replace attribute content rather than element content. To do this, use the key attr:* in the value object where * is the name of the attribute to change.

{
  "meta[name=description]": {
    "attr:content": "Hello World"
  }
}

NOTE: Using the value null will remove the attribute if it exists

Duplicating and replacing elements

Elements can be duplicate using the key clone. Its value is a number indicating the number of clones to produce

const mutate = require('html-mutate')

var html = mutate('<span id="wtf">Hi!</span>\n')

html({node
  "span": { clone: 2 }
}).pipe(process.stdout)

// Output: <span>Hi!</span><span>Hi!</span><span>Hi!</span>\n

NOTE: When using clone, if the target element has an id attribute, this attribute is removed. Duplicate ids can lead to some tricky issues with CSS or JS. If you are duplicating an element with an id there is a high chance you are doing something very nasty, and you shouldn't.

Rather than duplicating an element, it is possible to replace it using clone:0 with replace to define the new element (or to remove it if you use replace:null):

const mutate = require('html-mutate')

var html = mutate('<span>Hi!</span>\n')

html({node
  "span": {
    clone: 0
    replace: "<strong>Hello</strong>"
  }
}).pipe(process.stdout)

// Output: <strong>Hello</strong>\n

More examples?

Just look at the tests ;)