I am encountering the same problem trying to create a renderer that produces Commonmark(down). The use case is quite simple (but there are many), to produce a README document from multiple markdown documents occasionally including file contents or the output of executing programs. I use this to keep the examples in my README in sync with the state of the repository.
Whilst I can certainly create a Renderer that does all the basics, if I want to output the document as close as possible to the original (I imagine only changes to whitespace might occur) then the Parser would need to be modified. Some examples:
- Determining the type of hard line break used (backslash or double spaces)
- Rendering link references (
[link]: http://example.com) - Determining the type of character used for emph, strong etc.
I am sure there are more I haven’t run into but as far as I can tell the AST does not expose enough information for me to achieve a lot of these tasks.
My personal feeling is that a round-trip back to Markdown should be possible. I ran into the same issue with marked previously with my initial implementation (which works) and had to fork the source.
I would really like not to fork commonmark.js and contribute to a Parser and Renderer design that allows rendering back to Commonmark (as well as HTML, XML, PDF, TXT, MAN etc).
So I just wanted to see if @jgm thought this problem was worth solving and to continue this discussion on what I think is an important topic.