您的位置:首页 > 移动开发 > 微信开发

基于nodejs的一个实时markdown转html工具小程序

2016-11-21 15:02 369 查看
1.版本一

- 1.1`npm install marked --save` 安装markdwon转html的包.
- 1.2 使用watchFile监视 markdown文件

const fs = require('fs')
const marked = require('marked')
// 1.实时监视note.md文件的变化
fs.watchFile('./note.md', (curr, prev)=>{
// 2.读取note.md文件的内容
fs.readFile('./note.md', 'utf-8', (err, data)=>{
if(err){
throw err
}else{
// 3.使用marked方法,将md格式的文件转化为html格式
let htmlStr = marked(data.toString());
// 4.将转化的html格式的字符串,写入到新的文件中
fs.writeFile('./new.html', htmlStr, err=>{
if(err){
throw err
}else{
console.log("success")
}
})
}
})
})2.版本二:使用一个事先准备好的html模板,包含一些样式
const fs = require('fs')
const marked = require('marked')
fs.watchFile('./note.md', (curr, prev)=>{
// 读取准备好的html模板文件
fs.readFile('./template.html', 'utf8', (err, template)=>{
if(err){
throw err
}else{
fs.readFile('./note.md', 'utf8', (err,markContent)=>{
if(err){
throw err
}else{
// 转化好的html字符串
let htmlStr = marked(markContent.toString())
// 将html模板文件中的 '@markdown' 替换为html字符串
template = template.replace('@markdown', htmlStr)
// 将新生成的字符串template重新写入到文件中
fs.writeFile('./template.html', template, err=>{
if(err){
throw err
}else{
console.log("success")
}
})
}
})
}
})
})虽然是转载,但是我还是对内容进行略微的修改~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nodejs