markdown转html
生活随笔
收集整理的這篇文章主要介紹了
markdown转html
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
今天臨時要寫接口文檔,然后發(fā)現(xiàn)部門給的文檔是markdown文件的,而接口文檔是要html格式的,因此想直接把markdown轉(zhuǎn)為html
這里我使用的是marked?
首先初始化一個node項目?npm init
然后安裝marked ? ? ?npm install marked --save?
先引入fs和marked?
接著在創(chuàng)建一個模板文件,命名為template.html(這里面創(chuàng)建出頁面的主體,以及需要引入的css樣式),@markdown即為之后轉(zhuǎn)換成功要替換的位置
<!DOCTYPE html> <html lang="zh-CN"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"><meta name="author" content="Yaofeng"><title>文檔</title><link rel="stylesheet" href="./markdown.min.css"><style></style></head><body>@markdown</body><script></script> </html>接著完成入口文件
const fs = require('fs'); const marked = require('marked'); fs.readFile('./template.html', 'utf8', (err, template)=>{ if(err){ throw err }else{ fs.readFile('./香港退款訂單接口.md', 'utf8', (err,markContent)=>{ if(err){ throw err }else{ // 轉(zhuǎn)化好的html字符串 let htmlStr = marked(markContent.toString()) // 將html模板文件中的 '@markdown' 替換為html字符串 template = template.replace('@markdown', htmlStr) // 將新生成的字符串template重新寫入到文件中 fs.writeFile('./香港退款訂單接口.html', template, err=>{ if(err){ throw err }else{ console.log("success") } }) } }) } })至此結(jié)束?
樣式下載:https://github.com/zhangjikai/markdown-css
轉(zhuǎn)載于:https://www.cnblogs.com/FarmanKKK/p/8005092.html
總結(jié)
以上是生活随笔為你收集整理的markdown转html的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PowerDesigner最基础的使用方
- 下一篇: PHP 用图片输出验证码 无法正常显示