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

John Otander
InstructorJohn Otander
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 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.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today