|
4 | 4 | import com.vladsch.flexmark.ast.LinkNode;
|
5 | 5 | import com.vladsch.flexmark.ast.Text;
|
6 | 6 | import com.vladsch.flexmark.ast.TextBase;
|
| 7 | +import com.vladsch.flexmark.formatter.Formatter; |
7 | 8 | import com.vladsch.flexmark.html.HtmlRenderer;
|
8 | 9 | import com.vladsch.flexmark.parser.Parser;
|
9 | 10 | import com.vladsch.flexmark.parser.PostProcessor;
|
@@ -72,8 +73,8 @@ public void process(@NotNull NodeTracker state, @NotNull Node node) {
|
72 | 73 | BasedSequence linkAddress = PrefixedSubSequence.prefixOf("http://commonmark.org", linkText.getEmptySuffix());
|
73 | 74 |
|
74 | 75 | linkNode = new Link(BasedSequence.NULL, linkText, BasedSequence.NULL, BasedSequence.NULL, linkAddress, BasedSequence.NULL);
|
75 |
| - |
76 | 76 | linkNode.setCharsFromContent();
|
| 77 | + |
77 | 78 | linkNode.appendChild(contentNode);
|
78 | 79 | textBase.appendChild(linkNode);
|
79 | 80 | state.nodeAddedWithChildren(linkNode);
|
@@ -118,12 +119,13 @@ public Document processDocument(@NotNull Document document) {
|
118 | 119 | // here you can append some markdown text but keep it based on original input by
|
119 | 120 | // using PrefixedSubSequence with only prefix without any characters from input string
|
120 | 121 |
|
121 |
| - // here we create a based sequence of "inserted" text with offset set to end of input string |
122 |
| - BasedSequence insertedText = PrefixedSubSequence.prefixOf("Some inserted text with a link [flexmark-java](https://github.com/vsch/flexmark-java) in paragraph.", document.getChars().subSequence(document.getChars().length())); |
123 |
| - |
124 | 122 | // parse using the same options as the document but remove the SyntheticLinkExtension to prevent infinite recursion
|
125 | 123 | MutableDataHolder options = Parser.removeExtensions(new MutableDataSet(document), SyntheticLinkExtension.class);
|
126 |
| - Node insertedDocument = Parser.builder(options).build().parse(insertedText); |
| 124 | + |
| 125 | + // here we create a based sequence of "inserted" text with offset set to end of input string |
| 126 | + String insertedText = "Some inserted text with a link [flexmark-java](https://github.com/vsch/flexmark-java) in paragraph."; |
| 127 | + |
| 128 | + Node insertedDocument = Parser.builder(options).build().parse(document.getChars().append(insertedText).toString()); |
127 | 129 |
|
128 | 130 | // now can append nodes from inserted to document
|
129 | 131 | document.takeChildren(insertedDocument);
|
@@ -184,5 +186,8 @@ public static void main(String[] args) {
|
184 | 186 | Node document = parser.parse("Some markdown content");
|
185 | 187 | String html = renderer.render(document);
|
186 | 188 | System.out.println(html);
|
| 189 | + |
| 190 | + String markdown = Formatter.builder(options).build().render(document); |
| 191 | + System.out.println(markdown); |
187 | 192 | }
|
188 | 193 | }
|
0 commit comments