Java 實現 markdown轉Html

weixin_34116110發表於2018-11-30

以來github的開源專案flexmark-java

maven依賴:

<dependency>
    <groupId>com.vladsch.flexmark</groupId>
    <artifactId>flexmark-all</artifactId>
    <version>0.34.48</version>
</dependency>

精簡依賴:

<!--markdown to html-->
<dependency>
    <groupId>com.vladsch.flexmark</groupId>
    <artifactId>flexmark</artifactId>
    <version>0.34.48</version>
</dependency>
<dependency>
    <groupId>com.vladsch.flexmark</groupId>
    <artifactId>flexmark-util</artifactId>
    <version>0.34.48</version>
</dependency>
<!--表格渲染外掛-->
<dependency>
    <groupId>com.vladsch.flexmark</groupId>
    <artifactId>flexmark-ext-tables</artifactId>
    <version>0.34.48</version>
</dependency>

API簡單實用

package com.pzy;

import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.pdf.converter.PdfConverterExtension;
import com.vladsch.flexmark.profiles.pegdown.Extensions;
import com.vladsch.flexmark.profiles.pegdown.PegdownOptionsAdapter;
import com.vladsch.flexmark.util.options.DataHolder;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;

/**
 * Unit test for simple App.
 */
public class AppTest {

    @Test
    public void simpleTest() throws IOException {
        String html = markdown2Html();
        StringBuilder htmlStructBuilder = new StringBuilder();
        htmlStructBuilder.append("<html>");
        htmlStructBuilder.append("<head>");
        htmlStructBuilder.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
        InputStream stream = this.getClass().getClassLoader().getResourceAsStream("markdown.css");
        String styleContent = IOUtils.toString(stream, "UTF-8");
        htmlStructBuilder.append(String.format("<style type=\"text/css\"> %s </style>", styleContent));
        htmlStructBuilder.append("</head>");
        htmlStructBuilder.append(String.format("<body class='markdown-body'>%s</body>", html));
        htmlStructBuilder.append("<html>");
        PrintStream printStream = new PrintStream(new FileOutputStream(new File("test.html")), true);
        printStream.println(htmlStructBuilder.toString());
        printStream.close();
        DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(true,
                Extensions.ALL
        );
        PdfConverterExtension.exportToPdf("test.pdf", htmlStructBuilder.toString(), "", OPTIONS);
    }

    /**
     * markdown文件轉html內容
     *
     * @return
     * @throws IOException
     */
    private String markdown2Html() throws IOException {
        DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(true,
                Extensions.ALL
        );

        InputStream stream = this.getClass().getClassLoader().getResourceAsStream("test.md");
        String htmlContent = IOUtils.toString(stream, "UTF-8");

        Parser parser = Parser.builder(OPTIONS).build();
        HtmlRenderer renderer = HtmlRenderer.builder(OPTIONS).build();

        Node document = parser.parse(htmlContent);
        return renderer.render(document);
    }
}

相關文章