miyuchina / mistletoe

A fast, extensible and spec-compliant Markdown parser in pure Python.
MIT License
791 stars 110 forks source link

Delete a Document node. Recommended way? #209

Closed 4l1fe closed 6 months ago

4l1fe commented 6 months ago

Sadly, after searching the subject i didn't find a way of how i could drop a few nodes while modifying Document for further rendering it back to markdown.

Pls suggest an idea. I see the example of editing node content, but deleting tree nodes is somewhat more complex and i'm not sure that it can be done by mistletoe

pbodnar commented 6 months ago

Hi @4l1fe, in mistletoe, deleting tree nodes before rendering should be easy - you just need to update the children attribute (possibly a property soon - see #206) of the token you want to modify, see e.g. here for inspiration. Mistletoe currently doesn't have any API for handling operations like these explicitly.

4l1fe commented 6 months ago

Do you think deleting nodes while traversing a tree with utils.traverse is safe?

pbodnar commented 6 months ago

Well, I think that if you manage to modify the children of a yielded TraverseResult.node, nothing bad will happen - the newly assigned child nodes will just be iterated thereafter by the traverse() function. Right?

4l1fe commented 6 months ago

I think that if you manage to modify the children of a yielded TraverseResult.node

I'm doing exactly this way, all is ok at the moment,but it is somewhat like modifing a list while iterating it that is unsafe in general.

Anyway, thx for your opinion.