Fri, 24 Jan 2025 13:27:45 +0100
Fix directory creation, as lfs doesn't do all leaves like the old approach.
#!/usr/bin/env node /*eslint no-console:0*/ var fs = require('fs'); var hljs = require('highlight.js'); var katexOptions = { throwOnError: false, macros: { "\\iprod": "{\\langle #1, #2\\rangle}", "\\grad": "\\nabla", "\\isect": "\\cap", "\\union": "\\cup", "\\Isect": "\\bigcap", "\\Union": "\\bigcup", "\\supp": "\\mathop{\\mathrm{supp}}", "\\sign": "\\mathop{\\mathrm{sign}}", "\\Id": "\\mathop{\\mathrm{Id}}", "\\linear": "\\mathbb{L}" } }; // process.argv[0] seems to be the node executable itself if(process.argv.length<=2 || process.argv.length>4){ console.error(`Usage: ${process.argv[1]} input_file [katex_options.json]`) process.exit(1); } if(process.argv.length>3){ katexOptions = JSON.parse(fs.readFileSync(process.argv[3])); } 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{ // Replace local links to .md by .html. var re_nonlocal=/^[a-zA-Z0-9]*:\/\//; var re_md_to_html=/^(.*)\.md$/; 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(str, {language: lang}).value; } catch (__){} } return ''; }, replaceLink: function (link, env) { if(link.match(re_nonlocal)){ return link; }else{ return link.replace(re_md_to_html, "$1.html"); } } }) var mk = require('@neilsustc/markdown-it-katex'); var mm = require('markdown-it-mark'); var mr = require('markdown-it-replace-link'); var mc = require('markdown-it-checkbox'); var mcc = require('markdown-it-container'); md.use(mk, katexOptions) .use(mm) .use(mr) .use(mc) .use(mcc, 'NOTE') .use(mcc, 'IMPORTANT'); output = md.render(input); } catch(e) { console.error(e.stack || e.message || String(e)); process.exit(1); } process.stdout.write(output); });