将Markdown转变成Html的插件
1.先导入Markdown的依赖
com.atlassian.commonmark
commonmark
0.13.1
2.编写Markdown的Utils
import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
/**
* Makerdwon转变成Html
* @author fereli
*/
public class MarkdownUtils {
/**
* markdown格式装换成Html格式
* @param markdown
* @return
*/
public static String markdownToHtml(String markdown){
Parser parser = Parser.builder().build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder().build();
return renderer.render(document);
}
}
衍生功能
增加扩展 标题锚点,表格生成
1.依赖
com.atlassian.commonmark
commonmark-ext-heading-anchor
0.13.1
com.atlassian.commonmark
commonmark-ext-gfm-tables
0.13.1
2.Utils
/**
* 增加扩展[标题锚点,表格生成]
* Markdown转换成HTML
* @param markdown
* @return
*/
public static String markdownToHtmlExtensions(String markdown) {
//h标题生成id
Set headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
//转换table的HTML
List tableExtension = Arrays.asList(TablesExtension.create());
Parser parser = Parser.builder()
.extensions(tableExtension)
.build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder()
.extensions(headingAnchorExtensions)
.extensions(tableExtension)
.attributeProviderFactory(new AttributeProviderFactory() {
public AttributeProvider create(AttributeProviderContext context) {
return new CustomAttributeProvider();
}
})
.build();
return renderer.render(document);
}
/**
* 处理标签的属性
*/
static class CustomAttributeProvider implements AttributeProvider {
@Override
public void setAttributes(Node node, String tagName, Map attributes) {
//改变a标签的target属性为_blank
if (node instanceof Link) {
attributes.put("target", "_blank");
}
if (node instanceof TableBlock) {
attributes.put("class", "ui celled table");
}
}
}
懒人复制:(233)
依赖:
com.atlassian.commonmark
commonmark
0.13.1
com.atlassian.commonmark
commonmark-ext-heading-anchor
0.13.1
com.atlassian.commonmark
commonmark-ext-gfm-tables
0.13.1
Utils:
import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TableBlock;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;
import java.util.*;
/**
* Makerdwon转变成Html
* @author fereli
*/
public class MarkdownUtils {
/**
* markdown格式装换成Html格式
* @param markdown
* @return
*/
public static String markdownToHtml(String markdown){
Parser parser = Parser.builder().build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder().build();
return renderer.render(document);
}
/**
* 增加扩展[标题锚点,表格生成]
* Markdown转换成HTML
* @param markdown
* @return
*/
public static String markdownToHtmlExtensions(String markdown) {
//h标题生成id
Set headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
//转换table的HTML
List tableExtension = Arrays.asList(TablesExtension.create());
Parser parser = Parser.builder()
.extensions(tableExtension)
.build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder()
.extensions(headingAnchorExtensions)
.extensions(tableExtension)
.attributeProviderFactory(new AttributeProviderFactory() {
@Override
public AttributeProvider create(AttributeProviderContext context) {
return new CustomAttributeProvider();
}
})
.build();
return renderer.render(document);
}
/**
* 处理标签的属性
*/
static class CustomAttributeProvider implements AttributeProvider {
@Override
public void setAttributes(Node node, String tagName, Map attributes) {
//改变a标签的target属性为_blank
if (node instanceof Link) {
attributes.put("target", "_blank");
}
//给table标签的class属性值为 ui celled table
if (node instanceof TableBlock) {
attributes.put("class", "ui celled table");
}
}
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
String table = "| 1 | 2 | 3 |\n" +
"| ----- | ---- | ----- |\n" +
"| 22 | 22 | 22 |\n" +
"| 阿什顿发 | 非固定杆 | 撒阿什顿发 |\n" +
"\n";
String a = "[fereli 爱编程](http://www.lirenmi.cn)";
System.out.println(markdownToHtmlExtensions(a));
}
}