使用Java对Markdown与Html内容进行互转

使用Java对Markdown与Html内容进行互转

  • 一、Markdown转Html
    • 1.pom.xml引入
    • 2.测试类MarkdownToHtmlUtils.java
  • 二、Html转Markdown
    • 1.maven的pom.xml引入如下
    • 2.测试类HtmlToMarkdownUtils.java


一、Markdown转Html

1.pom.xml引入

    <!--引入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>

2.测试类MarkdownToHtmlUtils.java

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 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));
    }
 
}

二、Html转Markdown

1.maven的pom.xml引入如下

	    <dependency>
	        <groupId>io.github.furstenheim</groupId>
	        <artifactId>copy_down</artifactId>
	        <version>1.0</version>
	    </dependency>

2.测试类HtmlToMarkdownUtils.java

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.测试

这是一段测试的文本

"
; 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; } }

你可能感兴趣的:(【Java】,java,markdown,html)