markdown_it.js

Mon, 06 Jul 2020 12:51:11 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 06 Jul 2020 12:51:11 -0500
changeset 27
8f40424fb02e
parent 21
3c71c525cec2
permissions
-rwxr-xr-x

Attempt markdown-it-texmath instead of markdown-it-katex.

#!/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-texmath', {
                engine: require('katex'),
                delimiters:'dollars'
            }),
            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