summary refs log tree commit diff stats
path: root/public_md/build.js
diff options
context:
space:
mode:
authorMenix <menix@tilde.institute>2022-07-15 18:36:10 +0200
committerMenix <menix@tilde.institute>2022-07-15 18:36:10 +0200
commitbae49528dc575cf2889b0dd4bd83d2f6467fcc36 (patch)
tree88882a108d3cbe99f941625d898d996dc46c66cc /public_md/build.js
downloadtilde-notes-bae49528dc575cf2889b0dd4bd83d2f6467fcc36.tar.gz
Basic md to html for tilde_notes
Diffstat (limited to 'public_md/build.js')
-rw-r--r--public_md/build.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/public_md/build.js b/public_md/build.js
new file mode 100644
index 0000000..542948b
--- /dev/null
+++ b/public_md/build.js
@@ -0,0 +1,41 @@
+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/', 'build/', fileHandler);
+
+
+