The monitoring MD file changes, the change is changed to take the MD file, and the library is used to convert it to HTML;
- Supervisor: FS.Watchfile (FILENAME, (CUR, Pre) = <{})
- Read: fs.readfile;
- Distening to whether it changes: Cur.mtime /Pre.mtime; // Cur and Pre are all instances of fs.stats; both have stats attributes;
- conversion: Use the Marked library to install Install Marked – Save
- Converted successfully, spelling HTML code
- Write into the HTML file;
- Add style;
-
automatic refresh
const fs = require("fs"); const path = require("path"); const marked = require("marked"); const filepath= path.join(__dirname, process.argv[2]); const template = `<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div>{ { {content}}} </div> </body> </html> ` FS.Watchfile (FilePath, (Cur, Pre) => {// Options: Surveillance failure, etc. if (Cur.mtime! == Pre.mtime) { // Read the file and convert to write fs.readfile (Filepath, "UTF8", (ERR, DATA) => { if (err) {console.log (err); Return;} let html = marked (data); // Because the md file is converted to HTML without body and other labels, you need to replace the addition template = html.replace ("{{{{{ {content}}} ", html); // Write in html fs.writefile (path.join (__diRNAME, "Watch.html"), template, "utf8"); }) } })
can also download the MD style table file, replace it to the page; it is best to add the style label instead of link, because the link method file path dynamic changes will make an error;
automatic refresh
Use Broswer-Sync Library
1. Install Browser-Sync-Save
2. Watch.js starts the Browser service at the beginning
Var Browsersync = Require ("Browser-Sync");
browsersync ({server: __dirname});
3. After writing the file, execute the Browser Relaod command;
After the completion of the MD file, the browser automatically opens the localhost page; enter the name of the HTML file; you can dynamically monitor the modification