Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add renderer producing Markdown (CommonMark) #306

Merged
merged 45 commits into from
Feb 24, 2024
Merged

Conversation

robinst
Copy link
Collaborator

@robinst robinst commented Feb 9, 2024

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).

Structure mostly copied from TextContentRenderer (for extension related bits).

Started with leaf blocks (in spec order), but probably good to do some inlines
too before going too far down all the blocks.

Also, container blocks might be the most interesting.
Makes use of #303 which was split out.
@codecov-commenter
Copy link

codecov-commenter commented Feb 9, 2024

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%) ⬆️
...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%) ⬆️
...commonmark/renderer/markdown/MarkdownRenderer.java 100.00% <100.00%> (ø)
... and 3 more

... and 3 files with indirect coverage changes

@robinst robinst marked this pull request as ready for review February 24, 2024 00:24
@robinst robinst merged commit 870be81 into main Feb 24, 2024
12 checks passed
@robinst robinst deleted the markdown-renderer branch February 24, 2024 00:27
@robinst
Copy link
Collaborator Author

robinst commented Mar 15, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants