1. Version One
-1.1 ' npm install marked--save ' installs Markdwon to HTML package.
Use watchfile monitoring markdown files
/** * Created by Liyinghao on 2016/11/8. */const fs = require (' FS ') const marked = require (' marked ')//1. Real-time monitoring of note.md file Changes Fs.watchfile ('./note.md ', (curr,prev) = >{ //2. Read the contents of the Note.md file fs.readfile ('./note.md ', ' Utf-8 ', (err,data) =>{ if (err) { throw err }else{ //3. Using the Marked method, convert the MD format file into HTML format let htmlstr = marked (Data.tostring ()); 4. Write the converted HTML-formatted string to a new file in fs.writefile ('./new.html ', htmlstr,err=>{ if (err) { throw err } else{ Console.log ("Success");}}) })
2. Version two: Use a pre-prepared HTML template that contains some styles
/** * Created by Liyinghao on 2016/11/8. */const fs = require (' FS ') const marked = require (' Marked ') fs.watchfile ('./note.md ', (curr,prev) =>{//Read prepared HTML template file Fs.readfile ('./template.html ', ' UTF8 ', (err,template) =>{if (err) {Throw Err}else{ Fs.readfile ('./note.md ', ' UTF8 ', (err,markcontent) =>{if (err) {Throw err }else{//converted HTML string Let Htmlstr = Marked (Markcontent.tostring ()); Replace ' @markdown ' in HTML template file with HTML string template.replace (' @markdown ', htmlstr)//Will The newly generated string template is re-written to the file Fs.writefile ('./template.html ', template,err=>{if (err {Throw err}else{console.log ("Success"); } }) } }) } })});
A real-time markdown to HTML tool applet based on Nodejs