com.wl4g.component.common.md.FlexmarkUtil Maven / Gradle / Ivy
package com.wl4g.component.common.md;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension;
import com.vladsch.flexmark.ext.gitlab.GitLabExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;
import java.util.Arrays;
/**
* @author vjay
* @date 2021-01-15 11:46:00
*/
public class FlexmarkUtil {
public static String md2html(String md) {
MutableDataSet options = new MutableDataSet();
// uncomment to set optional extensions
options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create(), StrikethroughExtension.create(), GitLabExtension.create()));
// uncomment to convert soft-breaks to hard breaks
//options.set(HtmlRenderer.SOFT_BREAK, "
\n");
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
// You can re-use parser and renderer instances
Node document = parser.parse(md);
return renderer.render(document);
}
/*public static void main(String[] args){
System.out.println(md2html("# aaa\n\n| 栏目1 | 栏目2 | \n" +
"| ----- | ----- | \n" +
"| 内容1 | 内容2 | "));
}*/
}