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

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")
                        }
                    })
                }
            })
        }
    })
})
虽然是转载,但是我还是对内容进行略微的修改~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值