When it comes to Markdown processor, I feel that Perl (the head family) / Python / Ruby / JavaScript has relatively well-known libraries, but when it comes to Java, I haven't heard that much (or rather standard). I don't have anything).
Current status (2017-03-02) Looking through the Java Markdown processor found in open-sourced, it is still under active development and seems to be satisfactory in terms of functionality and speed [flexmark-java]( I touched https://github.com/vsch/flexmark-java) a little, so it's a light introduction.
As mentioned above, Markdown is famous for having many dialects, but it's almost the same, and I'd like to cover it with a single implementation if possible. So flexmark-java covers many dialects by making it extended (plugin type?).
// https://github.com/vsch/flexmark-java/wiki/Modified Usage example
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.parser.ParserEmulationProfile;
import com.vladsch.flexmark.util.options.MutableDataHolder;
import com.vladsch.flexmark.util.options.MutableDataSet;
import com.vladsch.flexmark.ext.abbreviation.AbbreviationExtension;
import com.vladsch.flexmark.ext.definition.DefinitionExtension;
import com.vladsch.flexmark.ext.footnotes.FootnoteExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.ext.typographic.TypographicExtension;
public class RenderingExamples {
//Set options for each Markdown dialect
private MutableHolder getOptions(String type) {
MutableDataHolder options = new MutableDataSet();
switch (type) {
case "markdown":
options.setFrom(ParserEmulationProfile.MARKDOWN);
break;
case "multimarkdown":
options.setFrom(ParserEmulationProfile.MULTI_MARKDOWN);
break;
case "kramdown":
options.setFrom(ParserEmulationProfile.KRAMDOWN);
options.set(Parser.EXTENSIONS, Arrays.asList(
AbbreviationExtension.create(),
DefinitionExtension.create(),
FootnoteExtension.create(),
TablesExtension.create(),
TypographicExtension.create()
));
break;
}
return options;
}
//Generate Parser for each dialect
private Parser getParser(String type) {
switch (type) {
case "commonmark":
return Parser.builder().build();
default:
return Parser.builder(getOptions(type)).build();
}
}
//Generate Renderer for each dialect
private HtmlRenderer getHtmlRenderer(String type) {
switch (type) {
case "commonmark":
return HtmlRenderer.builder().build();
default:
return HtmlRenderer.builder(getOptions(type)).build();
}
}
//Render HTML!
public String render(String markdownText, String type) {
Node document = getParser(type).parse(markdownText);
return getHtmlRenderer(type).render(document);
}
}
It seems that there is also a way for the older brothers to play with the parsed Node by themselves.
// https://github.com/vsch/flexmark-java/wiki/Modified Usage example
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.ast.NodeVisitor;
import com.vladsch.flexmark.ast.Text;
import com.vladsch.flexmark.ast.VisitHandler;
public class NodeVisitingExamples {
public NodeVisitingExamples() {
this.visitor = new NodeVisitor(
new VisitHandler<>(Text.class, NodeVisitingExamples.this::visit)
);
}
public void traverse(String markdownText) {
Parser parser = Parser.builder().build();
this.visitor.visit(parser.parse(markdownText));
}
private void visit(Text text) {
System.out.println(text);
}
private final NodeVisitor visitor;
}
(Addition if you feel like this)
Explanation of migration method: https://github.com/vsch/flexmark-java/wiki/Pegdown-Migration
Official: https://github.com/vsch/flexmark-java/wiki/Usage
https://gist.github.com/dmikurube/630b82f437977501a415155e3df3d68c
Recommended Posts