|  | 1 #!/usr/bin/env node | 
|  | 2 /*eslint no-console:0*/ | 
|  | 3 | 
|  | 4 var fs = require('fs'); | 
|  | 5 var hljs = require('highlight.js'); | 
|  | 6 | 
|  | 7 // process.argv[0] seems to be the node executable itself | 
|  | 8 if(process.argv.length<=2){ | 
|  | 9     console.error(`Usage: ${process.argv[1]} input_file`) | 
|  | 10     process.exit(1); | 
|  | 11 } | 
|  | 12 | 
|  | 13 fs.readFile(process.argv[2], 'utf8', function (err, input) { | 
|  | 14     var output, md; | 
|  | 15 | 
|  | 16     if(err){ | 
|  | 17         console.error(err.stack || err.message || String(err)); | 
|  | 18         process.exit(1); | 
|  | 19     } | 
|  | 20 | 
|  | 21     try{ | 
|  | 22         var md = require('markdown-it')({ | 
|  | 23                 html: true, | 
|  | 24                 xhtmlOut: true, | 
|  | 25                 breaks: false, | 
|  | 26                 linkify: true, | 
|  | 27                 highlight: function (str, lang) { | 
|  | 28                     if (lang && hljs.getLanguage(lang)) { | 
|  | 29                         try{ | 
|  | 30                             return hljs.highlight(lang, str).value; | 
|  | 31                         } catch (__){} | 
|  | 32                     } | 
|  | 33                     return ''; | 
|  | 34                 } | 
|  | 35             }), | 
|  | 36             mk = require('markdown-it-katex'), | 
|  | 37             mm = require('markdown-it-mark'); | 
|  | 38         md.use(mk).use(mm); | 
|  | 39         output = md.render(input); | 
|  | 40     } catch(e) { | 
|  | 41         console.error(e.stack || e.message || String(e)); | 
|  | 42         process.exit(1); | 
|  | 43     } | 
|  | 44 | 
|  | 45     process.stdout.write(output); | 
|  | 46 }); | 
|  | 47 | 
|  | 48 |