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初步理解
在开发一个复杂的应用程序的时候,我们需要把各个功能拆分.封装到不同的文件,在需要的时候引用该文件.没人会写一个几万行代码的文件,这样在可读性.复用性和维护性上都很差,几乎所有的编程语言都有自己的模块组 ...
随机推荐
- test201909027 老Z
30+100+40=170.数据出锅*2,也是没谁了. 装饰 快要到 Mope 的生日了,Mope 希望举行一场盛大的生日派对. 派对的准备工作中当然有装饰房间啦.Mope 的房间里有按从左到右的顺序 ...
- LightOJ - 1326 - Race(DP)
链接: https://vjudge.net/problem/LightOJ-1326 题意: Disky and Sooma, two of the biggest mega minds of Ba ...
- for循环:从键盘输入一个正整数n,
#include<stdio.h>void main(){ int i,n,sum=0; //声明三个整型变量,并为变量sum初始化赋值为0// printf("Please e ...
- LeetCode 1026. Maximum Difference Between Node and Ancestor
原题链接在这里:https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/ 题目: Given the ro ...
- Windbg命令的语法规则系列(三)
五.源文件行语法 可以将源文件行号指定为MASM表达式的全部或部分.这些数字计算出与该源代码行对应的可执行代码的偏移量.不能使用源代码行作为C++表达式的一部分.必须用重音符(`)将源文件和行号表达式 ...
- ROM
ROM 是 read only memory的简称,表示只读存储器,是一种半导体存储器.只读存储器(ROM)是一种在正常工作时其存储的数据固定不变,其中的数据只能读出,不能写入,即使断电也能够保留数据 ...
- tox 试用
安装 pip install tox tox 使用 tox 包含一个tox.ini 文件,此文件可以自动,或者手工编写 tox.ini 文件格式 # content of: tox.ini , put ...
- 【loj3119】【CTS2019】随机立方体
题目 一个 $ n m l $ 的立方体等概率填入 $ 1-nml $ ; 定义一个位置是极大的当且仅当这个位置比三位坐标的至少一维与之相等的位置的值都大. 询问极大值恰好有\(k\)个的 ...
- 利用Xilinx ROM仿真时注意包括.mif文件
利用Xilinx ROM仿真时,注意包括.mif文件.一般是将.v文件和.mif文件放在同一个目录下,以便.v文件读取.mif数据.如不注意,就不会读出有效数据.
- spring boot 防止重复提交
服务器端实现方案:同一客户端在2秒内对同一URL的提交视为重复提交 上代码吧 pom.xml <?xml version="1.0" encoding="UTF-8 ...