使用Java对Markdown与Html内容进行互转一、Markdown转Html1.pom.xml引入2.测试类MarkdownToHtmlUtils.java二、Html转Markdown1.maven的pom.xml引入如下2.测试类HtmlToMarkdownUtils.java 一、Markdown转Html 1.pom....
<a href="#MarkdownHtml_3">一、Markdown转Html</a>
<a href="#1pomxml_5">1.pom.xml引入</a> <a href="#2MarkdownToHtmlUtilsjava_29">2.测试类MarkdownToHtmlUtils.java</a> <a href="#HtmlMarkdown_117">二、Html转Markdown</a>
<a href="#1mavenpomxml_119">1.maven的pom.xml引入如下</a> <a href="#2HtmlToMarkdownUtilsjava_127">2.测试类HtmlToMarkdownUtils.java</a>
<hr color="#000000" size="1"">
<!--引入Markdown转HTML的插件-->
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.17.0</version>
</dependency>
<!--扩展 标题-->
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark-ext-heading-anchor</artifactId>
<version>0.17.0</version>
</dependency>
<!--扩展 表格-->
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark-ext-gfm-tables</artifactId>
<version>0.17.0</version>
</dependency>
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
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;
/**
*/
public class MarkdownToHtmlUtils {
/**
* 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<Extension> headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
//转换table的HTML
// List<Extension> tableExtension = Arrays.asList(TablesExtension.create());
List<Extension> tableExtension = Collections.singletonList(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<String, String> attributes) {
//改变a标签的target属性为_blank
if (node instanceof Link) {
attributes.put("target", "_blank");
}
if (node instanceof TableBlock) {
attributes.put("class", "ui celled table");
}
}
}
public static void main(String[] args) throws IOException {
String src = "# 一、标题\n ## 1.测试\n ```这是一段测试的文本```";
System.out.println(markdownToHtmlExtensions(src));
}
}
<dependency>
<groupId>io.github.furstenheim</groupId>
<artifactId>copy_down</artifactId>
<version>1.0</version>
</dependency>
import io.github.furstenheim.CopyDown;
import io.github.furstenheim.Options;
import io.github.furstenheim.OptionsBuilder;
public class HtmlToMarkdownUtils {
public static void main(String[] args) throws InterruptedException {
String htmlStr ="
# 一、标题
## 1.测试
<code>这是一段测试的文本</code>
";
System.out.println(htmlTansToMarkdown(htmlStr));
}
public static String htmlTansToMarkdown(String htmlStr) {
OptionsBuilder optionsBuilder = OptionsBuilder.anOptions();
Options options = optionsBuilder.withBr("-")
// more options
.build();
CopyDown converter = new CopyDown(options);
String markdownText = converter.convert(htmlStr);
return markdownText;
}
}
本站为非盈利网站,如果您喜欢这篇文章,欢迎支持我们继续运营!
本站主要用于日常笔记的记录和生活日志。本站不保证所有内容信息可靠!(大多数文章属于搬运!)如有版权问题,请联系我立即删除:“abcdsjx@126.com”。
QQ: 1164453243
邮箱: abcdsjx@126.com