node.js使用markdown-it批量转md内容为html

代码如下:

var fs = require('fs');
var MarkdownIt = require('markdown-it'), md = new MarkdownIt();

/*
pages_generator:
    这个脚本用于将 resources 目录下的所有相关文件转成静态文件导入到 public 目录下。
*/

// 生成problemId对应的html文件
var generateProblem = function (problemId) {
    var file = `${__dirname}/resources/problems/${problemId}/description.md`;
    var content;
    if (fs.existsSync(file) == false) {
        var originHtmlFile = `${__dirname}/resources/problems/${problemId}/description.html`;
        if (fs.existsSync(originHtmlFile) == false) {
            console.error(`[file not exist] ${file}`);
            return;
        } 
        else {
            content = fs.readFileSync(originHtmlFile, 'utf8');
        }
    } else {
        content = md.render(fs.readFileSync(file, 'utf8'));
    }
    var htmlFile = `${__dirname}/resources/page_modules/problem.html`;
    if (fs.existsSync(htmlFile) == false) {
        console.error(`[file not exist] ${htmlFile}`);
        return;
    }
    var problemHtmlContent = fs.readFileSync(htmlFile, 'utf8');
    content = problemHtmlContent.replace(/{{content}}/g, content);
    var infoFile = `${__dirname}/resources/problems/${problemId}/info.json`;
    if (fs.existsSync(infoFile) == false) {
        console.error(`[file not exist] ${infoFile}`);
        return;
    }
    var info = JSON.parse(fs.readFileSync(infoFile, 'utf8'));
    var id = info.id;
    var title = info.title;
    // console.log(`id = ${id}\r\ntitle = ${title}`);
    if (""+id != ""+problemId) {
        console.error(`[id not match problemId] id=${id}, problemId=${problemId}`);
        return;
    }
    content = content.replace(/{{problemId}}/g, id).replace(/{{problemName}}/g, title);
    var outFile = `${__dirname}/public/problem/${problemId}.html`;
    fs.writeFileSync(outFile, content, 'utf8');
}

// 生成所有的problem对应的html文件
var generateProblems = function () {
    var files = fs.readdirSync(`${__dirname}/resources/problems`);
    files.forEach(file => {
        if (file != "0000") generateProblem(file);
    });
}

// 生成problemlist对应的html文件
var generateProblemlist = function (problemlistId, problemlistSize, problemlist) {
    var content = "";
    problemlist.forEach(problemId => {
        var infoFile = `${__dirname}/resources/problems/${problemId}/info.json`;
        var info = JSON.parse(fs.readFileSync(infoFile, 'utf8'));
        var id = info.id;
        var title = info.title;
        var row = `<tr><td>${problemId}</td><td><a href="/problem/${problemId}.html">${title}</a></td><td> -- </td><td> -- </td><td> -- </td></tr>\r\n`;
        content += row;
    });
    var htmlFile = `${__dirname}/resources/page_modules/problemlist.html`;
    var problemHtmlContent = fs.readFileSync(htmlFile, 'utf8');
    var problemlistHtmlContent = "";
    for (var i = 1; i <= problemlistSize; i ++)
        problemlistHtmlContent += `<a href="/problemlist/${i}.html"><B>${i}</B></a>&nbsp;`;
    content = problemHtmlContent.replace(/{{content}}/g, content).replace(/{{problemlistId}}/g, problemlistId).replace(/{{problemlistContent}}/, problemlistHtmlContent);
    var outFile = `${__dirname}/public/problemlist/${problemlistId}.html`;
    fs.writeFileSync(outFile, content, 'utf8');
    if (problemlistId == 1) {
        var indexFile = `${__dirname}/public/problemlist/index.html`;
        fs.copyFileSync(outFile, indexFile);
    }
}

// 生成所有problemlist对应的html文件
var generateProblemlists = function () {
    var dir = `${__dirname}/resources/problems`;
    var problemIdList = fs.readdirSync(dir);
    var infoMap = {};
    problemIdList.forEach( problemId => {
        if (problemId != "0000") {
            var problemlistId = parseInt(parseInt(problemId)/100)-9;
            if (infoMap[problemlistId] == null) 
                infoMap[problemlistId] = [];
            infoMap[problemlistId].push(problemId);
        }
    });
    var problemlistSize = 0;
    Object.keys(infoMap).forEach(problemlistId => 
        problemlistSize = Math.max(problemlistSize, problemlistId));
    Object.keys(infoMap).forEach( problemlistId => {
        generateProblemlist(problemlistId, problemlistSize, infoMap[problemlistId]);
    } );
}

generateProblems();
generateProblemlists();
posted @ 2019-05-30 19:14  zifeiy  阅读(843)  评论(0编辑  收藏  举报