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

reshape-plugin-util

v0.2.1

Published

utilities for reshape plugins

Downloads

33

Readme

Reshape Plugin Util

npm tests dependencies coverage

A little set of utilities for reshape plugins

Note: This project is in early development, and versioning is a little different. Read this for more details.

Installation

npm i reshape-plugin-util --save

Note: This project is compatible with node v6+ only

Usage

This is just a small utility that contains a couple a useful functions when developing reshape plugins.

modifyNodes(tree, match, transform)

Given a reshape AST, a function that will return any node that matches given criteria, and a function that receives matched nodes and returns one or more modified nodes, returns a modified AST.

Example: Using modifyNodes to modify a node's content

const util = require('reshape-plugin-util')

module.exports = function yellPlugin (tree) {
  return util.modifyNodes(tree, (node) => node.name === 'p', (node) => {
    node.content = node.content.map((n) => n.content.toUpperCase())
    return node
  })
}

Input:

<p>hello world!</p>

Output:

<p>HELLO WORLD!</p>

Example: Using modifyNodes to remove a node

const util = require('reshape-plugin-util')

module.exports = function removeNodePlugin (tree) {
  return util.modifyNodes(tree, (node) => node.name === 'remove', (node) => {
    return null
  })
}

Input:

<p>before</p>
<remove>hello world!</remove>
<p>after</p>

Output:

<p>before</p>
<p>after</p>

Example: Using modifyNodes to add extra nodes

const util = require('reshape-plugin-util')

module.exports = function echoPlugin (tree) {
  return util.modifyNodes(tree, (node) => node.name === 'echo', (node) => {
    if (!node.attrs) node.attrs = {}
    if (!node.attrs.class) node.attrs.class = []
    node.attrs.class.push('echo')
    node.name = 'div'
    return [node, node]
  })
}

Input:

<p>before</p>
<echo>echo</echo>
<p>after</p>

Output:

<p>before</p>
<div class='echo'>echo</div>
<div class='echo'>echo</div>
<p>after</p>

You can also return a promise from either function and it will work fine.

validateNode(node)

Given a single reshape AST node, checks it for formatting errors. Example:

const util = require('reshape-plugin-util')

util.validateNode({
  type: 'text',
  content: ['foo', 'bar'],
  location: { line: 1, col: 1 }
})

// => Error: text node content must be a string
//    From: plugin-util
//    Node: {
//      type: 'text',
//      content: ['foo', 'bar'],
//      location: { line: 1, col: 1 }
//    }

validateTree(tree)

Recursively validates each node in a given reshape AST tree.

const util = require('reshape-plugin-util')

util.validateNode({
  type: 'tag',
  name: 'div'
  content: [
    {
      content: 'foo',
      location: { line: 1, col: 4 }
    }
  ],
  location: { line: 1, col: 1 }
})

// => Error: node missing "type" attribute
//    From: plugin-util
//    Node: {
//      content: 'foo',
//      location: { line: 1, col: 4}
//    }

License & Contributing