What is unist-util-remove?
The unist-util-remove package is a utility for removing nodes from a Unist syntax tree. It allows you to filter out nodes based on specific criteria, making it useful for manipulating abstract syntax trees (ASTs) in various text processing tasks.
What are unist-util-remove's main functionalities?
Remove nodes by type
This feature allows you to remove all nodes of a specific type from the tree. In this example, all 'paragraph' nodes are removed from the tree.
const remove = require('unist-util-remove');
const tree = { type: 'root', children: [ { type: 'paragraph', children: [ { type: 'text', value: 'Hello' } ] }, { type: 'paragraph', children: [ { type: 'text', value: 'World' } ] } ] };
remove(tree, 'paragraph');
console.log(tree);
Remove nodes by test function
This feature allows you to remove nodes based on a test function. In this example, only the 'paragraph' node containing the text 'World' is removed.
const remove = require('unist-util-remove');
const tree = { type: 'root', children: [ { type: 'paragraph', children: [ { type: 'text', value: 'Hello' } ] }, { type: 'paragraph', children: [ { type: 'text', value: 'World' } ] } ] };
remove(tree, node => node.type === 'paragraph' && node.children[0].value === 'World');
console.log(tree);
Remove nodes by index
This feature allows you to remove nodes by their index in the parent node's children array. In this example, the second 'paragraph' node (index 1) is removed.
const remove = require('unist-util-remove');
const tree = { type: 'root', children: [ { type: 'paragraph', children: [ { type: 'text', value: 'Hello' } ] }, { type: 'paragraph', children: [ { type: 'text', value: 'World' } ] } ] };
remove(tree, { type: 'paragraph' }, 1);
console.log(tree);
Other packages similar to unist-util-remove
unist-util-filter
The unist-util-filter package allows you to filter nodes in a Unist tree based on a test function. It is similar to unist-util-remove but instead of removing nodes, it creates a new tree with only the nodes that pass the test.
unist-util-visit
The unist-util-visit package is used to recursively walk through a Unist tree and perform actions on nodes that match a specific type or test function. While it doesn't remove nodes, it can be used in conjunction with other utilities to manipulate the tree.
unist-util-map
The unist-util-map package allows you to transform nodes in a Unist tree by applying a function to each node. It is useful for modifying the tree structure or node values, but it does not directly remove nodes.
unist-util-remove
Remove one or more nodes from Unist tree, mutating it.
Example
var remove = require('unist-util-remove');
ast
remove(ast, [ast.children[1].children[0], ast.children[1].children[1]])
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)
API
remove(ast, [opts], predicate)
ast
— Unist tree.predicate
— one of:
- Unist node or array of nodes,
- node type (string),
- function invoked with arguments
(node, index?, parent?)
.
Iterates over ast
in preorder traversal and removes all nodes matching predicate
from ast
. Returns a modified Unist tree.
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