commonmark-java icon indicating copy to clipboard operation
commonmark-java copied to clipboard

Add renderer producing Markdown (CommonMark)

Open robinst opened this issue 1 year ago • 1 comments

Adds a MarkdownRenderer that renders a Node tree to CommonMark Markdown. Supports all core nodes as well as "gfm-strikethrough", "gfm-tables" and "ins" extensions. This is very useful for applications that want to produce Markdown, e.g.:

  • Parse some input Markdown, modify the nodes (AST), then render back to Markdown
  • Create a Node tree via code and then render that to Markdown

The goal is that Node tree -> render to Markdown -> parse to Node tree results in an equivalent Node tree. That is verified with all the examples from the spec and some manual test cases.

However it does not mean that a Markdown -> parse to Node tree -> render to Markdown round-trip produces the same Markdown as the original. The parse step does not preserve enough detail for that yet, e.g. things like insignificant whitespace or what characters were escaped.

See #12.


In the future, we can make changes that preserve more details when parsing and make use of that to bring the rendered Markdown closer to the original. But the Markdown renderer can't rely on those extra details to work correctly, as that would break the use case where there is no parsing (when creating Node tree via code).

robinst avatar Feb 09 '24 11:02 robinst

Codecov Report

Attention: Patch coverage is 97.97571% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 94.81%. Comparing base (85f043e) to head (94eb5ff). Report is 5 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #306      +/-   ##
============================================
+ Coverage     94.30%   94.81%   +0.50%     
- Complexity      227      252      +25     
============================================
  Files           124      130       +6     
  Lines          3633     4124     +491     
  Branches        549      606      +57     
============================================
+ Hits           3426     3910     +484     
- Misses          104      106       +2     
- Partials        103      108       +5     
Files Coverage Δ
...ommonmark/ext/gfm/strikethrough/Strikethrough.java 100.00% <100.00%> (ø)
.../ext/gfm/strikethrough/StrikethroughExtension.java 100.00% <100.00%> (ø)
...ough/internal/StrikethroughDelimiterProcessor.java 91.30% <100.00%> (+0.39%) :arrow_up:
...gh/internal/StrikethroughMarkdownNodeRenderer.java 100.00% <100.00%> (ø)
.../java/org/commonmark/ext/gfm/tables/TableCell.java 100.00% <ø> (ø)
...org/commonmark/ext/gfm/tables/TablesExtension.java 100.00% <100.00%> (ø)
...main/java/org/commonmark/ext/ins/InsExtension.java 100.00% <100.00%> (ø)
...mark/ext/ins/internal/InsMarkdownNodeRenderer.java 100.00% <100.00%> (ø)
...ava/org/commonmark/internal/util/AsciiMatcher.java 92.00% <100.00%> (+3.76%) :arrow_up:
...commonmark/renderer/markdown/MarkdownRenderer.java 100.00% <100.00%> (ø)
... and 3 more

... and 3 files with indirect coverage changes

codecov-commenter avatar Feb 09 '24 11:02 codecov-commenter

Released in 0.22.0 now: https://github.com/commonmark/commonmark-java/blob/main/CHANGELOG.md#0220---2024-03-15

robinst avatar Mar 15 '24 12:03 robinst