zoukankan      html  css  js  c++  java
  • 使用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);
           }
      }
  • 相关阅读:
    react: redux-devTools
    react: menuService
    react: navigator
    react style: 二级菜单
    spark复习笔记(5):API分析
    spark复习笔记(4):spark脚本分析
    maven 打包Scala代码到jar包
    spark复习笔记(3)
    mongoDB学习笔记(2)
    sparkStreaming复习笔记(1)
  • 原文地址:https://www.cnblogs.com/fdzang/p/9556874.html
Copyright © 2011-2022 走看看