summary refs log blame commit diff stats
path: root/public_md/build.js
blob: 2dddfc0bec4d46e8d05b14710e714fd97b2274fb (plain) (tree)




































                                                                      
                                                          


 
var MarkdownIt = require('markdown-it');
const fs = require("fs");
const path = require("path");

md = new MarkdownIt();

const walkSync = (inputdir, outputdir, callback) => {
  const files = fs.readdirSync(inputdir);
  files.forEach((file) => {
    var filepath = path.join(inputdir, file);
    const stats = fs.statSync(filepath);
    if (stats.isDirectory()) {
      walkSync(filepath, outputdir, callback);
    } else if (stats.isFile()) {
      callback(filepath, outputdir, stats);
    }
  });
};


function fileHandler (filepath, outputdir, stats) {
  fs.readFile(filepath, function(err, buf) {
    let outputpath = filepath.replace(/^.*?\//i, outputdir);
    fs.mkdir(path.dirname(outputpath), { recursive: true }, (err) => {
      if (err) throw err;
    });
  
    outputpath = outputpath.replace(/\.md$/i, '.html');
    const html = md.render(buf.toString());
    fs.writeFile(outputpath, html, (err) => {
      if (err) console.log(err);
    console.log("Successfully Written to: " + outputpath);
    });
  }); 
}

fs.rmSync('build/', { recursive: true, force: true });
walkSync('src/', '/home/menix/public_html/', fileHandler);