Skip to content

Commit

Permalink
update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
vsch committed May 4, 2020
1 parent eee271d commit ccdb4d7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void visit(Image node) {
private void visit(LinkNodeBase node) {
if (node.getPageRef().startsWith("/")) {
node.setUrlChars(PrefixedSubSequence.prefixOf("https:", node.getPageRef()));
node.setChars(SegmentedSequence.create(node.getChars(), Arrays.asList(node.getSegmentsForChars())));
node.setChars(node.getChars().getBuilder().addAll(Arrays.asList(node.getSegmentsForChars())).toSequence());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.vladsch.flexmark.ast.LinkNode;
import com.vladsch.flexmark.ast.Text;
import com.vladsch.flexmark.ast.TextBase;
import com.vladsch.flexmark.formatter.Formatter;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.parser.PostProcessor;
Expand Down Expand Up @@ -72,8 +73,8 @@ public void process(@NotNull NodeTracker state, @NotNull Node node) {
BasedSequence linkAddress = PrefixedSubSequence.prefixOf("http://commonmark.org", linkText.getEmptySuffix());

linkNode = new Link(BasedSequence.NULL, linkText, BasedSequence.NULL, BasedSequence.NULL, linkAddress, BasedSequence.NULL);

linkNode.setCharsFromContent();

linkNode.appendChild(contentNode);
textBase.appendChild(linkNode);
state.nodeAddedWithChildren(linkNode);
Expand Down Expand Up @@ -118,12 +119,13 @@ public Document processDocument(@NotNull Document document) {
// here you can append some markdown text but keep it based on original input by
// using PrefixedSubSequence with only prefix without any characters from input string

// here we create a based sequence of "inserted" text with offset set to end of input string
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()));

// parse using the same options as the document but remove the SyntheticLinkExtension to prevent infinite recursion
MutableDataHolder options = Parser.removeExtensions(new MutableDataSet(document), SyntheticLinkExtension.class);
Node insertedDocument = Parser.builder(options).build().parse(insertedText);

// here we create a based sequence of "inserted" text with offset set to end of input string
String insertedText = "Some inserted text with a link [flexmark-java](https://github.com/vsch/flexmark-java) in paragraph.";

Node insertedDocument = Parser.builder(options).build().parse(document.getChars().append(insertedText).toString());

// now can append nodes from inserted to document
document.takeChildren(insertedDocument);
Expand Down Expand Up @@ -184,5 +186,8 @@ public static void main(String[] args) {
Node document = parser.parse("Some markdown content");
String html = renderer.render(document);
System.out.println(html);

String markdown = Formatter.builder(options).build().render(document);
System.out.println(markdown);
}
}

0 comments on commit ccdb4d7

Please sign in to comment.