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> `;
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的更多相关文章
- node.js一行一行的获取txt文件内容
node.js一行一行获取text文件代码: const readline = require('readline');//Readline是Node.js里实现标准输入输出的封装好的模块,通过这个模 ...
- Node.js是什么?提供了哪些内容?
什么是Node.js? Node.js是基于Chrome V8 引擎的 JavaScript运行时(运行环境). Node.js提供了哪些内容? Node.js运行时,JavaScript代码运行时的 ...
- Node.js中的express框架,修改内容后自动更新(免重启),express热更新
个人网站 https://iiter.cn 程序员导航站 开业啦,欢迎各位观众姥爷赏脸参观,如有意见或建议希望能够不吝赐教! 以前node中的express框架,每次修改代码之后,都需要重新npm s ...
- node.js学习4--------------------- 根据不同路径来响应内容,以及中文乱码的解决
/** * http服务器的搭建,相当于php中的Apache或者java中的tomcat服务器 */ // 导包 const http=require("http"); //创建 ...
- node.js小工具--修改Xcode 'Create by'作者名称
简介 用Xcode创建源文件时会自动在文件开始位置加入如下注释: // // ISSImageCycleScrollView.m // SoftTravel // // Created by iss1 ...
- Node.js快速入门
Node.js是什么? Node.js是建立在谷歌Chrome的JavaScript引擎(V8引擎)的Web应用程序框架. 它的最新版本是:v0.12.7(在编写本教程时的版本).Node.js在官方 ...
- Node.js记录
在智能社上听了一些关于node.js的视频,总结一小部分内容,都是总结老师讲的知识点,并且也是在不断学习的过程,所以会不断更新.也是为了怕自己遗忘一些知识点,同时现今没有什么项目可以让我去真正实践,这 ...
- vue2.0+node.js+mongodb全栈打造商城
Github地址:https://github.com/ccyinghua/vue-node-mongodb-project 一.构建项目所用: vue init webpack vue-node-m ...
- node.js module初步理解
在开发一个复杂的应用程序的时候,我们需要把各个功能拆分.封装到不同的文件,在需要的时候引用该文件.没人会写一个几万行代码的文件,这样在可读性.复用性和维护性上都很差,几乎所有的编程语言都有自己的模块组 ...
随机推荐
- javascript数据结构与算法——栈
前言: 栈就是和列表类似的一种数据结构,不过栈的特点是'后人先出'.栈是一种高效的数据结构,因为数据只能在栈顶添加或删除,所以这样操作很快,而且容易实现. 1. 栈的介绍: 栈是一种特殊的列表,栈内的 ...
- python笔记39-unittest框架如何将上个接口的返回结果给下个接口适用(面试必问)
前言 面试必问:如何将上个接口的返回结果,作为下个接口的请求入参?使用unittest框架写用例时,如何将用例a的结果,给用例b使用. unittest框架的每个用例都是独立的,测试数据共享的话,需设 ...
- Centos7服务器搭建部署显卡计算环境以及常用软件的安装使用
安装好anaconda的服务器上会more你已经安装好jupyter notebook,执行下面的命令可以提供链接地址允许远程浏览器打开并访问: jupyter notebook --no-brows ...
- Redis.Memcache和MongoDB区别?
Memcached的优势: Memcached可以利用多核优势,单吞吐量极高,可以达到几十万QPS(取决于Key.value的字节大小以及服务器硬件性能,日常环境中QPS高峰大约在4-6w左右.)适用 ...
- angular和datatables一起使用的时候,出现TypeError: Cannot Read Property "childNodes" Of Undefined In AngularJS
在anguglar中注入'$timeout' 然后: $timeout(function{},0,false);
- javascript慕课入门
1.javascript引用 html里引用: <script type="text/javascript"> ... ... </script> 引用外部 ...
- go 学习 (一):环境配置
Go 下载地址:https://golang.google.cn/dl/ 右键我的电脑 --> 左上方 “高级系统设置” ---> 环境变量 --> 第二个菜单栏 “系统变 ...
- codeforcesC - Berry Jam(折半枚举+1-1序列前后缀和)
Educational Codeforces Round 78 (Rated for Div. 2) C - Berry Jam C. Berry Jam time limit per test 2 ...
- ipcs
用于报告Linux中进程间通信设施的状态,显示的信息包括消息列表.共享内存和信号量的信息
- Generating a Random Sample from discrete probability distribution
If is a discrete random variable taking on values , then we can write . Implementation of this formu ...