pegdown
pegdown copied to clipboard
Add parent to nodes in parsed tree
When writing a custom serializer, it's often necessary to know the parent of a given node. However, the nodes in the tree currently only provide access to their children. Please make the parent node available to the nodes...or provide a utility for looking up the parent.
Here's how we're currently working around this challenge:
https://github.com/bodiam/markdown-to-asciidoc/blob/master/src/main/java/com/laamella/markdown_to_asciidoc/ToAsciiDocSerializer.java#L629
Not too sure why you linked to line 629, your findParentNode actually starts on line 652:
https://github.com/bodiam/markdown-to-asciidoc/blob/master/src/main/java/com/laamella/markdown_to_asciidoc/ToAsciiDocSerializer.java#L652
It seems like you are running into a problem of needing the parent node when you have an ExpLinkNode as the parent of an ExpImageNode. I am guessing the Markdown you are using is this:
[](link url)
The issue is, you cannot simply render one nested in the other like HTML, but need a special syntax. Feel free to correct me if I'm wrong.
In your case, I personally think the most elegant method would be to have the logic for an image nested in a link be in the visit(ExpLinkNode node)
. That way, you can easily check the child node, instead of spending extra time finding the parent.
Another method would be to create a new Rule and/or subclass of Node for your special case. I see you have been using the Parser as-is from Pegdown, and would probably not want to implement a solution such as this.
I humbly believe that certain limitations are beneficial to the code base and the programmer, and in this case not allowing the Visitor to view the parent node inspires creativity and encourages cleaner coding.
Again, feel free to correct me if I'm wrong. Also note the date of the last release, as this project will probably not be receiving any more updates.