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初步理解
在开发一个复杂的应用程序的时候,我们需要把各个功能拆分.封装到不同的文件,在需要的时候引用该文件.没人会写一个几万行代码的文件,这样在可读性.复用性和维护性上都很差,几乎所有的编程语言都有自己的模块组 ...
随机推荐
- Tensorflow简单实践系列(一):安装和运行
TensorFlow 是谷歌开发的机器学习框架. 安装 TensorFlow 直接使用 pip 安装即可,添加豆瓣镜像可以加快速度: pip install tensorflow -i https:/ ...
- spring Security的自定义用户认证
首先我需要在xml文件中声明.我要进行自定义用户的认证类,也就是我要自己从数据库中进行查询 <http pattern="/*.html" security="no ...
- 4-html图片与链接
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Haskell语言学习笔记(94)Enum Bounded
Enum class Enum a where succ, pred :: a -> a toEnum :: Int -> a fromEnum :: a -> Int enumFr ...
- vue 的computed 和 watch 两者的区别
computed是计算属性,依赖其他属性计算,并且computed的值有缓存,只有当计算值发生变化才会返回内容. computed 用来监控自己定义的变量,该变量不在data里面声明,直接在compu ...
- js中数组和字符串的方法总结
一.数组方法简单总结为以下几种 1.原有: 增.删.改.截.拼.复.排.转 2.ES5扩展: 查.遍历 增: 前增 ,,,,]; console.log(arr.unshift(,,[ console ...
- “知乎杯”2018 CCF 大学生计算机系统与程序设计竞赛 绝地求生(battleground)
/* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-ts ...
- 虚拟变量和独热编码的区别(Difference of Dummy Variable & One Hot Encoding)
在<定量变量和定性变量的转换(Transform of Quantitative & Qualitative Variables)>一文中,我们可以看到虚拟变量(Dummy Var ...
- 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码
00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...
- render函数、createElement函数与vm.$slots
1.render函数.createElement函数 Vue.component('es-header', { render: function (createElement) { return cr ...