commonmark / cmark

CommonMark parsing and rendering library and program in C
Other
1.64k stars 546 forks source link

Introduce a mechanism to duplicate/clone a node and all it's children? #560

Open ioquatix opened 2 months ago

ioquatix commented 2 months ago

When manipulating ASTs using nodes, sometimes you need to copy parts of the AST to a different place or part of the document.

The most naive approach I can think of is converting a document to a markdown string and parsing it again, but I'm sure there are subtle differences. It might be good for an initial implementation.

It might be nice to introduce a clone function which takes a node and returns a copy, including all children.

jgm commented 1 month ago

This seems reasonable to me. @nwellnhof what do you think?

nwellnhof commented 1 month ago

The most naive approach I can think of is converting a document to a markdown string and parsing it again, but I'm sure there are subtle differences.

This should result in a perfect copy (except for source positions), but it's inefficient. A clone function certainly seems useful.

ioquatix commented 1 month ago

Okay can I submit a PR?

jgm commented 1 month ago

Yes!