flexmark-java
flexmark-java copied to clipboard
Question: Render node document as markdown syntax
Question
Is it possible to render a Node document using markdown syntax ?
Parser parser = Parser.builder(options).build();
MarkdownRenderer renderer = MarkdownRenderer.builder(options).build();
// You can re-use parser and renderer instances
Node document = new TextBase("Hello World");
String markdown = renderer.render(document);
System.out.println(markdown);
@cmoulliard, yes. Use Formatter
as the renderer. A few examples are available for formatter features:
FormatConverterCommonMark.java
FormatterMergeSample.java
FormatterWithMods.java
FormatterWithMods2.java
Many thanks @vsch. Is there an example showing how we could use java AST classes to add a paragraph, bullet lists, some titles, ... instead of using a String ?
@cmoulliard, you can modify the AST before rendering with Formatter.
However, the AST is expected to be derived from a single sequence, with possible insertions/deletions of text but the start/end offsets of nodes are expected to be strictly in increasing order. Some care must be taken is creating new nodes so that their text is has the correct start/end offsets.
This can be accomplished by using the end offset of the previous node chars as the start of the inserted node text with the actual text coming from out of original character sequence.
Additionally, node chars node.getChars()
can consist of concatenation of different parts of the node. This can be updated by calling node.setCharsFromContent()
after modifying the node's parts.
SyntheticLinkSample.java shows conversion of "markdown" in paragraphs to a link to http://commonmark.org
in the AST.
TokenReplacingPostProcessorSample.java shows replacement of link nodes with nodes representing link text.
In general, the inserted nodes would need to be created similar to they are created by their corresponding parser code, in the core or extension implementation.
If you give me sample code of what you want to insert into the AST it will be easier for me to give you specific code for your solution. For example you can use methods for inserting specific nodes: Heading insertHeading(Node node, String text, int level)
to insert a heading node after the given node.
In your code you would call these methods to create and insert specific nodes into the AST.
If you also provide sample markdown for processing then I will then be able to give you code which would generate the given nodes.
The easiest way for us to do this if you provide a console app class, similar to samples, which contains your sample in one file, including the sample markdown used. I will then be able to modify/debug the sample and post it back for you.
Many thanks for your well detailed and long response. I will have a look. What I would like to populate is a report as this one
# Team weekly report: 19 Apr 2020
## Action Items
| Item | Description | Who | Status |
| ----- | -------- | ----- | ----- |
| 001 | ADD ME | ASSIGNE ME | DEFINE ME |
---
## Gytis Trikleris
- Spring on Quarkus
- Code Panache REST client - <https://github.com/cmoulliard/weekly-report/issues/1>
## Georgios Andrianakis
- Spring on Quarkus
- Test related fixes
- Make beans injected with @InjectMock unremoveable - <https://github.com/quarkusio/quarkus/pull/8491>
- Allow the use of @QuarkusTestResource with QuarkusProdModeTest - <https://github.com/quarkusio/quarkus/pull/8597>
- RELEASE SB 2.2.6
- Help Antonio and Auri
- Help team with CVE + JIRA handling
- Misc
- PR Reviews
- Community engagement