Remove Markdown Nodes from a Document with unist-util-remove

John Otander
InstructorJohn Otander

Share this video with your friends

Send Tweet
Published 3 years ago
Updated 2 years ago

When using remark to manipulate markdown documents, you can use unist-util-remove to remove nodes of a particular type. You can combine it with unist-util-visit to remove nodes based on their parent's context in the AST.

In this example, we remove all images contained within heading nodes.

Instructor: [0:00] Install unist-util-remove. Then we can require it. Then call the remove function by passing it the tree and the node type that we'd like to remove. As we can see now, all images have been removed from our document.

[0:16] In some cases, you might only want to remove nodes based off their parent context. For this example, we will only delete images that are contained within a heading. To achieve this, we can combine unist-util-remove with unist-util-visit.

[0:30] We can require the unist-util-visit library and visit all heading nodes in our tree before calling the remove function on all heading nodes. Now we'll see that only the image contained within our heading has been removed.