A real-time markdown to HTML tool applet based on Nodejs

Source: Internet
Author: User
Tags readfile

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.