markdown_it.js

changeset 21
3c71c525cec2
child 27
8f40424fb02e
child 28
9b75d0f5e042
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/markdown_it.js	Mon Jul 06 10:56:23 2020 -0500
@@ -0,0 +1,48 @@
+#!/usr/bin/env node
+/*eslint no-console:0*/
+
+var fs = require('fs');
+var hljs = require('highlight.js');
+
+// process.argv[0] seems to be the node executable itself
+if(process.argv.length<=2){
+    console.error(`Usage: ${process.argv[1]} input_file`)
+    process.exit(1);
+}
+
+fs.readFile(process.argv[2], 'utf8', function (err, input) {
+    var output, md;
+
+    if(err){
+        console.error(err.stack || err.message || String(err));
+        process.exit(1);
+    }
+
+    try{
+        var md = require('markdown-it')({
+                html: true,
+                xhtmlOut: true,
+                breaks: false,
+                linkify: true,
+                highlight: function (str, lang) {
+                    if (lang && hljs.getLanguage(lang)) {
+                        try{
+                            return hljs.highlight(lang, str).value;
+                        } catch (__){}
+                    }
+                    return '';
+                }
+            }),
+            mk = require('markdown-it-katex'),
+            mm = require('markdown-it-mark');
+        md.use(mk).use(mm);
+        output = md.render(input);
+    } catch(e) {
+        console.error(e.stack || e.message || String(e));
+        process.exit(1);
+    }
+
+    process.stdout.write(output);
+});
+
+

mercurial