引入依赖

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.atlassian.commonmark/commonmark -->
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.12.1</version>
</dependency>

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.ledao.util;

import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

/**
* @author LeDao
* @company
* @create 2021-12-18 21:40
*/
public class MarkdownToHtmlUtil {

public static String convert(String md){
Parser parser=Parser.builder().build();
Node document = parser.parse(md);
HtmlRenderer htmlRenderer=HtmlRenderer.builder().build();
return htmlRenderer.render(document);
}

public static void main(String[] args) {
System.out.println(convert("### 111"));
}
}

结果