使用 flexmark 将 MarkDown 转为 HTML

  1. 引入对应的依赖

            <!-- https://mvnrepository.com/artifact/com.vladsch.flexmark/flexmark -->
            <dependency>
                <groupId>com.vladsch.flexmark</groupId>
                <artifactId>flexmark</artifactId>
                <version>0.34.18</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.vladsch.flexmark/flexmark-util -->
            <dependency>
                <groupId>com.vladsch.flexmark</groupId>
                <artifactId>flexmark-util</artifactId>
                <version>0.34.18</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.vladsch.flexmark/flexmark-ext-tables -->
            <dependency>
                <groupId>com.vladsch.flexmark</groupId>
                <artifactId>flexmark-ext-tables</artifactId>
                <version>0.34.18</version>
            </dependency>    
    
  2. 新建工具类

    package com.fdzang.mblog.utils; import com.vladsch.flexmark.ast.Node; import com.vladsch.flexmark.ext.tables.TablesExtension; import com.vladsch.flexmark.html.HtmlRenderer; import com.vladsch.flexmark.parser.Parser; import com.vladsch.flexmark.parser.ParserEmulationProfile; import com.vladsch.flexmark.util.options.MutableDataSet; import java.util.Arrays; public class MarkDown2HtmlUtils { /** * 直接将markdown语义的文本转为html格式输出
          * @param content markdown语义文本
          * @return
          */
         public static String markdown2Html(String content) {
             String html = parse(content); return html;
         } /** * markdown to image
          * @param content markdown contents
          * @return parse html contents */
         public static String parse(String content) {
             MutableDataSet options = new MutableDataSet();
             options.setFrom(ParserEmulationProfile.MARKDOWN); //enable table parse!
     options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create()));
             Parser parser = Parser.builder(options).build();
             HtmlRenderer renderer = HtmlRenderer.builder(options).build();
             Node document = parser.parse(content); return renderer.render(document);
         }
    }