commonmark-java是一个Markdown解析器,一个基于 CommonMark 规范解析和渲染Markdown文本的Java库。具有以下特性:
小(最小化的依赖)
快(比pegdown快10-20倍,在仓库中可查看benchmarks)
灵活(解析后可操作AST,自定义HTML渲染)
可扩展(表格,删除线,自动链接等等)
Maven:
<dependency> <groupId>com.atlassian.commonmark</groupId> <artifactId>commonmark</artifactId> <version>0.9.0</version></dependency>解析和渲染成HTML
import org.commonmark.node.*;import org.commonmark.parser.Parser;import org.commonmark.renderer.html.HtmlRenderer;Parser parser = Parser.builder().build();Node document = parser.parse("This is *Sparta*");HtmlRenderer renderer = HtmlRenderer.builder().build();renderer.render(document); // "<p>This is <em>Sparta</em></p>\n"你也可以对输出HTML进行属性定制:
Parser parser = Parser.builder().build();HtmlRenderer renderer = HtmlRenderer.builder() .attributeProviderFactory(new AttributeProviderFactory() { public AttributeProvider create(AttributeProviderContext context) { return new ImageAttributeProvider(); } }) .build();Node document = parser.parse("![text](/url.png)");renderer.render(document);// "<p><img src=\"/url.png\" alt=\"text\" class=\"border\" /></p>\n"class ImageAttributeProvider implements AttributeProvider { @Override public void setAttributes(Node node, String tagName, Map<String, String> attributes) { if (node instanceof Image) { attributes.put("class", "border"); } }}对输出的HTML进行渲染定制:
Parser parser = Parser.builder().build();HtmlRenderer renderer = HtmlRenderer.builder() .nodeRendererFactory(new HtmlNodeRendererFactory() { public NodeRenderer create(HtmlNodeRendererContext context) { return new IndentedCodeBlockNodeRenderer(context); } }) .build();Node document = parser.parse("Example:\n\n code");renderer.render(document);// "<p>Example:</p>\n<pre>code\n</pre>\n"class IndentedCodeBlockNodeRenderer implements NodeRenderer { private final HtmlWriter html; IndentedCodeBlockNodeRenderer(HtmlNodeRendererContext context) { this.html = context.getWriter(); } @Override public Set<Class<? extends Node>> getNodeTypes() { // Return the node types we want to use this renderer for. return Collections.<Class<? extends Node>>singleton(IndentedCodeBlock.class); } @Override public void render(Node node) { // We only handle one type as per getNodeTypes, so we can just cast it here. IndentedCodeBlock codeBlock = (IndentedCodeBlock) node; html.line(); html.tag("pre"); html.text(codeBlock.getLiteral()); html.tag("/pre"); html.line(); }}
评论