Knagis/CommonMark.NET

Remove elements from a Markdown document

Closed this issue · 2 comments

We are currently using CommonMark.NET's API to apply some pre-processing steps to markdown documents before rendering - generally by scanning for certain tokens and replacing Blocks/Inlines as necessary.

Currently our code to remove a Block looks like the following:

private static void RemoveBlock(Block block)
{
    var parent = block.Parent;
    var prev = block.Previous;
    var next = block.NextSibling;

    if (parent != null)
    {
        if (parent.FirstChild == block) { parent.FirstChild = next; }
        if (parent.LastChild == block) { parent.LastChild = prev; }
    }

    if (prev != null) { prev.NextSibling = next; }
    if (next != null) { next.Previous = prev; }
}

However the Previous property is flagged as obsolete, so this code may not work in the future.

Inline has a similar problem - In order to remove an Inline from a block I must iterate through the Inline sequence from FirstSibling, updating the NextSibling property as appropriate as I go. It looks like the process will be similar for Block after Previous is removed.

It would be great to get some guidance on the expected approach here.

Note that I don't have a specific problem with the approach outlined above, as it is fairly easy to encapsulate.

Perhaps it would be worth including such functions (Remove, Replace, etc) into the library? They look like they could be implemented as extension methods.

Yes, I agree that adding extension methods for manipulating the parse tree would be useful. I will add them some time hopefully not too far in the future.

The Previous property is marked as obsolete as the parser itself does not need it and I wanted to remove any unneeded properties in order to reduce the memory usage. Of course, until 1.0 is released, these properties will not be removed. And it does not seem that commonmark specification itself will reach 1.0 any time soon.