代码如下:

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. NLP从词袋到Word2Vec的文本表示

    在NLP(自然语言处理)领域,文本表示是第一步,也是很重要的一步,通俗来说就是把人类的语言符号转化为机器能够进行计算的数字,因为普通的文本语言机器是看不懂的,必须通过转化来表征对应文本.早期是基于规则 ...

  2. uiautomator2+python自动化测试1-环境准备

    前言 uiautomator是Google提供的用来做安卓自动化测试的一个Java库.功能很强,可以对第三方App进行测试,获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作,但有两个缺点 ...

  3. Python 推送RabbitMQ

    username = 'xxxxxxxx' pwd = 'xxxxxxxx' user_pwd = pika.PlainCredentials(username, pwd) s_conn = pika ...

  4. JSON—去除JSON数据中的所有HTML标…

    package com.linoer.utils; import java.util.ArrayList; import java.util.List; import java.util.regex. ...

  5. Go语言在国产CPU平台上应用前景的探索与思考

    http://www.chinaaet.com/article/3000087559 0 引言 CPU是电子产品的核心,代表着信息产业的发展水平.CPU发展至今已经有四十多年的历史了,实际就是Inte ...

  6. 101 More Security Best Practices for Kubernetes

    https://rancher.com/blog/2019/2019-01-17-101-more-kubernetes-security-best-practices/ The CNCF recen ...

  7. 开源项目(4-2)手势识别-Keras/Theano/OpenCV实现的CNN手势识别

    https://github.com/asingh33/CNNGestureRecognizer 我提供了两种捕获模式: 二进制模式:在这里我首先将图像转换为灰度,然后应用高斯模糊效果和自适应阈值滤波 ...

  8. (尚021)Vue_eslint编码规范检查

    1.eslint 1.1说明 1)ESLint是一个代码规范检查工具 2)它定义了很多特定的规则,一旦你的代码违背了某一规则,eslint会做出非常有用的提示 3)官网:http://eslint.o ...

  9. springboot的HTTPS配置

  10. learning scala list.collect

    collect will apply a partial function to all elements of a Traversable and return a different collec ...