美文网首页
Java 实现 markdown转Html

Java 实现 markdown转Html

作者: 码农梦醒 | 来源:发表于2018-11-30 11:15 被阅读70次

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

    相关文章

      网友评论

          本文标题:Java 实现 markdown转Html

          本文链接:https://www.haomeiwen.com/subject/invccqtx.html