JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1702543
  • Score
    100M100P100Q209672F
  • License MIT

Remove nodes from Unist tree

Package Exports

  • unist-util-remove

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (unist-util-remove) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm

unist-util-remove

Build Status Dependency Status

Remove one or more nodes from Unist tree, mutating it.

Example

var u = require('unist-builder');
var remove = require('unist-util-remove');
var inspect = require('unist-util-inspect');

var ast = u('root', [
  u('leaf', 1),
  u('node', [
    u('leaf', 2),
    u('node', [
      u('leaf', 3),
      u('other', 4)
    ]),
    // this node will be removed as well because of `opts.cascade`.
    u('node', [
      u('leaf', 5),
    ])
  ]),
  u('leaf', 6)
]);

// Remove all nodes of type `leaf`.
remove(ast, 'leaf')
//=> root[1]
//   └─ node[1]
//      └─ node[1]
//         └─ other: "4"

If the root node gets removed, the entire tree is destroyed and remove returns null. That's the only case in which remove doesn't return the original root node.

remove(ast, ast)
//=> null

API

remove(ast, [opts], predicate)

Iterates over ast in preorder traversal and removes all nodes matching predicate from ast. Returns a modified Unist tree.

Note: unist-util-is used to remove a node when given as test. This no longer works. To migrate, pass a function as a predicate, like so: remove(ast, function (node) { return node === predicate; }).

opts.cascade

Type: Boolean
Default: true

If true, removing of the last child triggers removal of its parent node.

Install

npm install unist-util-remove

License

MIT