https://github.com/reshape/plugin-util
a little helper for building reshape plugins
https://github.com/reshape/plugin-util
Last synced: 4 months ago
JSON representation
a little helper for building reshape plugins
- Host: GitHub
- URL: https://github.com/reshape/plugin-util
- Owner: reshape
- License: other
- Created: 2016-08-10T14:12:00.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2019-02-05T12:43:30.000Z (over 7 years ago)
- Last Synced: 2025-03-24T05:27:03.964Z (about 1 year ago)
- Language: JavaScript
- Size: 29.3 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Contributing: contributing.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Reshape Plugin Util
[](https://npmjs.com/package/reshape-plugin-util)
[](https://travis-ci.org/reshape/plugin-util?branch=master)
[](https://david-dm.org/reshape/plugin-util)
[](https://coveralls.io/r/reshape/plugin-util?branch=master)
A little set of utilities for reshape plugins
> **Note:** This project is in early development, and versioning is a little different. [Read this](http://markup.im/#q4_cRZ1Q) 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
```js
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) => Object.assign(n, { content: n.content.toUpperCase() }))
return node
})
}
```
Input:
```html
hello world!
```
Output:
```html
HELLO WORLD!
```
Example: Using `modifyNodes` to remove a node
```js
const util = require('reshape-plugin-util')
module.exports = function removeNodePlugin (tree) {
return util.modifyNodes(tree, (node) => node.name === 'remove', (node) => {
return null
})
}
```
Input:
```html
before
hello world!
after
```
Output:
```html
before
after
```
Example: Using `modifyNodes` to add extra nodes
```js
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:
```html
before
echo
after
```
Output:
```html
before
echo
echo
after
```
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:
```js
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.
```js
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
- Details on the license [can be found here](LICENSE.md)
- Details on running tests and contributing [can be found here](contributing.md)