{"id":21540952,"url":"https://github.com/reshape/plugin-util","last_synced_at":"2026-03-05T16:38:15.718Z","repository":{"id":57141931,"uuid":"65388022","full_name":"reshape/plugin-util","owner":"reshape","description":"a little helper for building reshape plugins","archived":false,"fork":false,"pushed_at":"2019-02-05T12:43:30.000Z","size":30,"stargazers_count":4,"open_issues_count":7,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T05:27:03.964Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/reshape.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"contributing.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-10T14:12:00.000Z","updated_at":"2018-07-25T18:00:08.000Z","dependencies_parsed_at":"2022-09-05T18:50:53.895Z","dependency_job_id":null,"html_url":"https://github.com/reshape/plugin-util","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reshape%2Fplugin-util","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reshape%2Fplugin-util/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reshape%2Fplugin-util/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reshape%2Fplugin-util/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reshape","download_url":"https://codeload.github.com/reshape/plugin-util/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247721955,"owners_count":20985083,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T05:00:37.533Z","updated_at":"2026-03-05T16:38:15.582Z","avatar_url":"https://github.com/reshape.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Reshape Plugin Util\n\n[![npm](https://img.shields.io/npm/v/reshape-plugin-util.svg?style=flat-square)](https://npmjs.com/package/reshape-plugin-util)\n[![tests](https://img.shields.io/travis/reshape/plugin-util.svg?style=flat-square)](https://travis-ci.org/reshape/plugin-util?branch=master)\n[![dependencies](https://img.shields.io/david/reshape/plugin-util.svg?style=flat-square)](https://david-dm.org/reshape/plugin-util)\n[![coverage](https://img.shields.io/coveralls/reshape/plugin-util.svg?style=flat-square)](https://coveralls.io/r/reshape/plugin-util?branch=master)\n\nA little set of utilities for reshape plugins\n\n\u003e **Note:** This project is in early development, and versioning is a little different. [Read this](http://markup.im/#q4_cRZ1Q) for more details.\n\n### Installation\n\n`npm i reshape-plugin-util --save`\n\n\u003e **Note:** This project is compatible with node v6+ only\n\n### Usage\n\nThis is just a small utility that contains a couple a useful functions when developing reshape plugins.\n\n#### `modifyNodes(tree, match, transform)`\n\nGiven 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.\n\nExample: Using `modifyNodes` to modify a node's content\n\n```js\nconst util = require('reshape-plugin-util')\n\nmodule.exports = function yellPlugin (tree) {\n  return util.modifyNodes(tree, (node) =\u003e node.name === 'p', (node) =\u003e {\n    node.content = node.content.map((n) =\u003e Object.assign(n, { content: n.content.toUpperCase() }))\n    return node\n  })\n}\n```\n\nInput:\n\n```html\n\u003cp\u003ehello world!\u003c/p\u003e\n```\n\nOutput:\n\n```html\n\u003cp\u003eHELLO WORLD!\u003c/p\u003e\n```\n\nExample: Using `modifyNodes` to remove a node\n\n```js\nconst util = require('reshape-plugin-util')\n\nmodule.exports = function removeNodePlugin (tree) {\n  return util.modifyNodes(tree, (node) =\u003e node.name === 'remove', (node) =\u003e {\n    return null\n  })\n}\n```\n\nInput:\n\n```html\n\u003cp\u003ebefore\u003c/p\u003e\n\u003cremove\u003ehello world!\u003c/remove\u003e\n\u003cp\u003eafter\u003c/p\u003e\n```\n\nOutput:\n\n```html\n\u003cp\u003ebefore\u003c/p\u003e\n\u003cp\u003eafter\u003c/p\u003e\n```\n\nExample: Using `modifyNodes` to add extra nodes\n\n```js\nconst util = require('reshape-plugin-util')\n\nmodule.exports = function echoPlugin (tree) {\n  return util.modifyNodes(tree, (node) =\u003e node.name === 'echo', (node) =\u003e {\n    if (!node.attrs) node.attrs = {}\n    if (!node.attrs.class) node.attrs.class = []\n    node.attrs.class.push('echo')\n    node.name = 'div'\n    return [node, node]\n  })\n}\n```\n\nInput:\n\n```html\n\u003cp\u003ebefore\u003c/p\u003e\n\u003cecho\u003eecho\u003c/echo\u003e\n\u003cp\u003eafter\u003c/p\u003e\n```\n\nOutput:\n\n```html\n\u003cp\u003ebefore\u003c/p\u003e\n\u003cdiv class='echo'\u003eecho\u003c/div\u003e\n\u003cdiv class='echo'\u003eecho\u003c/div\u003e\n\u003cp\u003eafter\u003c/p\u003e\n```\n\nYou can also return a promise from either function and it will work fine.\n\n#### `validateNode(node)`\n\nGiven a single reshape AST node, checks it for formatting errors. Example:\n\n```js\nconst util = require('reshape-plugin-util')\n\nutil.validateNode({\n  type: 'text',\n  content: ['foo', 'bar'],\n  location: { line: 1, col: 1 }\n})\n\n// =\u003e Error: text node content must be a string\n//    From: plugin-util\n//    Node: {\n//      type: 'text',\n//      content: ['foo', 'bar'],\n//      location: { line: 1, col: 1 }\n//    }\n```\n\n#### `validateTree(tree)`\n\nRecursively validates each node in a given reshape AST tree.\n\n```js\nconst util = require('reshape-plugin-util')\n\nutil.validateNode({\n  type: 'tag',\n  name: 'div'\n  content: [\n    {\n      content: 'foo',\n      location: { line: 1, col: 4 }\n    }\n  ],\n  location: { line: 1, col: 1 }\n})\n\n// =\u003e Error: node missing \"type\" attribute\n//    From: plugin-util\n//    Node: {\n//      content: 'foo',\n//      location: { line: 1, col: 4}\n//    }\n```\n\n### License \u0026 Contributing\n\n- Details on the license [can be found here](LICENSE.md)\n- Details on running tests and contributing [can be found here](contributing.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freshape%2Fplugin-util","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freshape%2Fplugin-util","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freshape%2Fplugin-util/lists"}