当前位置: 首页 > 工具软件 > Pegdown > 使用案例 >

Java使用PegDown将markdown文件转成html格式

宗政学
2023-12-01

maven依赖:

    <dependency>
        <groupId>org.pegdown</groupId>
        <artifactId>pegdown</artifactId>
        <version>1.6.0</version>
    </dependency>

示例代码:

package com.zifeiy.page;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.pegdown.PegDownProcessor;

public class PageGenerator {
    
    public void generateHtml(File mdFile) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(mdFile), "UTF-8"));
        String line = null;
        String mdContent = "";
        while ((line = br.readLine()) != null) {
            mdContent += line + "\r\n";
        }
        PegDownProcessor pdp = new PegDownProcessor(Integer.MAX_VALUE);
        String htmlContent = pdp.markdownToHtml(mdContent);
        System.out.println(htmlContent);
    }
    
    
    public static void main(String[] args) throws IOException {
        PageGenerator pageGenerator = new PageGenerator();
        pageGenerator.generateHtml(new File("D:\\projects\\zifeiy.github.io\\笔记\\20181125我的第一篇博客.md"));
    }
    
}

转载于:https://www.cnblogs.com/zifeiy/p/10017358.html

 类似资料: