Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
https://markdown-it.github.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.5 KiB
83 lines
1.5 KiB
10 years ago
|
#!/usr/bin/env node
|
||
10 years ago
|
/*eslint no-console:0*/
|
||
10 years ago
|
|
||
|
'use strict';
|
||
|
|
||
|
|
||
|
var fs = require('fs');
|
||
|
var argparse = require('argparse');
|
||
|
|
||
|
|
||
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
var cli = new argparse.ArgumentParser({
|
||
10 years ago
|
prog: 'markdown-it.js',
|
||
10 years ago
|
version: require('../package.json').version,
|
||
|
addHelp: true
|
||
|
});
|
||
|
|
||
10 years ago
|
cli.addArgument([ 'file' ], {
|
||
10 years ago
|
help: 'File to read',
|
||
|
nargs: '?',
|
||
|
defaultValue: '-'
|
||
|
});
|
||
|
|
||
|
var options = cli.parseArgs();
|
||
|
|
||
|
|
||
|
function readFile(filename, encoding, callback) {
|
||
|
if (options.file === '-') {
|
||
|
// read from stdin
|
||
|
|
||
|
var chunks = [];
|
||
|
|
||
|
process.stdin.on('data', function(chunk) {
|
||
|
chunks.push(chunk);
|
||
|
});
|
||
|
|
||
|
process.stdin.on('end', function() {
|
||
|
return callback(null, Buffer.concat(chunks).toString(encoding));
|
||
|
});
|
||
|
} else {
|
||
|
fs.readFile(filename, encoding, callback);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
|
||
10 years ago
|
readFile(options.file, 'utf8', function (err, input) {
|
||
10 years ago
|
var output, md;
|
||
|
|
||
10 years ago
|
if (err) {
|
||
|
if (err.code === 'ENOENT') {
|
||
10 years ago
|
console.error('File not found: ' + options.file);
|
||
|
process.exit(2);
|
||
|
}
|
||
|
|
||
10 years ago
|
console.error(err.stack || err.message || String(err));
|
||
10 years ago
|
|
||
|
process.exit(1);
|
||
|
}
|
||
|
|
||
10 years ago
|
md = require('..')({
|
||
10 years ago
|
html: true,
|
||
10 years ago
|
xhtmlOut: true,
|
||
|
typographer: true,
|
||
|
linkify: true
|
||
10 years ago
|
});
|
||
10 years ago
|
|
||
|
try {
|
||
|
output = md.render(input);
|
||
|
|
||
10 years ago
|
} catch (e) {
|
||
|
console.error(e.stack || e.message || String(e));
|
||
10 years ago
|
|
||
|
process.exit(1);
|
||
|
}
|
||
|
|
||
|
process.stdout.write(output);
|
||
|
|
||
|
process.exit(0);
|
||
|
});
|