1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

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

    John OtanderJohn Otander
    javascriptJavaScript

    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.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    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.