代码如下:

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

node.js使用markdown-it批量转md内容为html的更多相关文章

  1. node.js一行一行的获取txt文件内容

    node.js一行一行获取text文件代码: const readline = require('readline');//Readline是Node.js里实现标准输入输出的封装好的模块,通过这个模 ...

  2. Node.js是什么?提供了哪些内容?

    什么是Node.js? Node.js是基于Chrome V8 引擎的 JavaScript运行时(运行环境). Node.js提供了哪些内容? Node.js运行时,JavaScript代码运行时的 ...

  3. Node.js中的express框架,修改内容后自动更新(免重启),express热更新

    个人网站 https://iiter.cn 程序员导航站 开业啦,欢迎各位观众姥爷赏脸参观,如有意见或建议希望能够不吝赐教! 以前node中的express框架,每次修改代码之后,都需要重新npm s ...

  4. node.js学习4--------------------- 根据不同路径来响应内容,以及中文乱码的解决

    /** * http服务器的搭建,相当于php中的Apache或者java中的tomcat服务器 */ // 导包 const http=require("http"); //创建 ...

  5. node.js小工具--修改Xcode 'Create by'作者名称

    简介 用Xcode创建源文件时会自动在文件开始位置加入如下注释: // // ISSImageCycleScrollView.m // SoftTravel // // Created by iss1 ...

  6. Node.js快速入门

    Node.js是什么? Node.js是建立在谷歌Chrome的JavaScript引擎(V8引擎)的Web应用程序框架. 它的最新版本是:v0.12.7(在编写本教程时的版本).Node.js在官方 ...

  7. Node.js记录

    在智能社上听了一些关于node.js的视频,总结一小部分内容,都是总结老师讲的知识点,并且也是在不断学习的过程,所以会不断更新.也是为了怕自己遗忘一些知识点,同时现今没有什么项目可以让我去真正实践,这 ...

  8. vue2.0+node.js+mongodb全栈打造商城

    Github地址:https://github.com/ccyinghua/vue-node-mongodb-project 一.构建项目所用: vue init webpack vue-node-m ...

  9. node.js module初步理解

    在开发一个复杂的应用程序的时候,我们需要把各个功能拆分.封装到不同的文件,在需要的时候引用该文件.没人会写一个几万行代码的文件,这样在可读性.复用性和维护性上都很差,几乎所有的编程语言都有自己的模块组 ...

随机推荐

  1. 从客户端(content="xxxxx")中检测到有潜在危险的 Request.Form 值——较合理解决方案

    1,修改配置文件: <httpRuntime requestValidationMode = "2.0" /> 以上修改是必须的,因为高版本的.netframework ...

  2. windows 10 下使用Navicat for oracle 数据库还原

    一.前期准备 1.安装windows 10系统 2.安装oracle 11g 数据库 3.安装PLsql(也不需要) 4.安装sqlplus(这个必须有) 5.使用下面这个东西新建数据库(不懂创建的话 ...

  3. 【Selenium-WebDriver实战篇】基于java的selenium之验证码识别内容

    ==================================================================================================== ...

  4. python 中的tile函数,shape函数,sum函数

    1.tile函数: tile函数是模板numpy.lib.shape_base中的函数.函数的形式是tile(A,reps) A的类型几乎所有类型都可以:array, list, tuple, dic ...

  5. Cocos2d-x学习小结 开始篇

    Cocos2d-x学习小结 开始篇 想要学习Cocos2d-x,是因为在高中物理课上找不到某些物理定律的证明,例如欧姆定律. 为此,我翻阅了稍高等级的物理教材,其中关于欧姆定律\(R=\frac{U} ...

  6. ImportError: cannot import name HTTPSHandler

    原因在于openssl,openssl-devel两个文件包未正确安装.用下来的命令来安装: 2 yum install openssl -y yum install openssl-devel -y ...

  7. .net core 多sdk 多版本 环境切换

    在讲述.net core多版本之前,我们先理解一下.net core sdk与.net core runtime之前的联系与区别,根据官网的解释我们可以简单地理解为:sdk是在开发过程中进行使用,而r ...

  8. 推荐一款分布式微服务框架 Surging

    surging   surging 是一个分布式微服务框架,提供高性能RPC远程服务调用,采用Zookeeper.Consul作为surging服务的注册中心,集成了哈希,随机,轮询,压力最小优先作为 ...

  9. luoguP1576 最小花费

    LOL新英雄皮肤弹丸天使点击就送 两种做法: 1.边的权值为手续费z,从b向a跑最短路,边跑边处理答案 2.边的权值为汇率,从a向b跑最短路,边跑边处理答案 #include<cstdio> ...

  10. BZOJ4241 历史研究 【回滚莫队】

    题目描述:给出一个长度为\(n\)的数组,每次询问区间 \([l,r]\),求 \(\max\limits_{x}x*cnt_x\),其中 \(cnt_x\) 表示 \(x\) 在区间 \([l,r] ...