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.
132 lines
3.1 KiB
132 lines
3.1 KiB
10 years ago
|
#!/usr/bin/env node
|
||
|
/*eslint no-console:0*/
|
||
|
|
||
10 years ago
|
// Fixtures generator from commonmark specs. Split spec to working / not working
|
||
10 years ago
|
// examples, or show total stat.
|
||
|
|
||
1 year ago
|
import fs from 'node:fs';
|
||
|
import argparse from 'argparse';
|
||
|
import markdownit from '../index.mjs';
|
||
10 years ago
|
|
||
|
|
||
|
var cli = new argparse.ArgumentParser({
|
||
4 years ago
|
add_help: true
|
||
10 years ago
|
});
|
||
|
|
||
4 years ago
|
cli.add_argument('type', {
|
||
10 years ago
|
help: 'type of examples to filter',
|
||
|
nargs: '?',
|
||
|
choices: [ 'good', 'bad' ]
|
||
|
});
|
||
|
|
||
4 years ago
|
cli.add_argument('-s', '--spec', {
|
||
|
help: 'spec file to read',
|
||
1 year ago
|
default: new URL('../test/fixtures/commonmark/spec.txt', import.meta.url)
|
||
4 years ago
|
});
|
||
|
|
||
|
cli.add_argument('-o', '--output', {
|
||
|
help: 'output file, stdout if not set',
|
||
|
default: '-'
|
||
10 years ago
|
});
|
||
|
|
||
4 years ago
|
var options = cli.parse_args();
|
||
10 years ago
|
|
||
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
|
||
10 years ago
|
function normalize(text) {
|
||
|
return text.replace(/<blockquote>\n<\/blockquote>/g, '<blockquote></blockquote>');
|
||
|
}
|
||
|
|
||
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
|
||
10 years ago
|
function readFile(filename, encoding, callback) {
|
||
|
if (options.file === '-') {
|
||
|
// read from stdin
|
||
|
|
||
|
var chunks = [];
|
||
|
|
||
9 years ago
|
process.stdin.on('data', function (chunk) {
|
||
10 years ago
|
chunks.push(chunk);
|
||
|
});
|
||
|
|
||
9 years ago
|
process.stdin.on('end', function () {
|
||
10 years ago
|
return callback(null, Buffer.concat(chunks).toString(encoding));
|
||
|
});
|
||
|
} else {
|
||
|
fs.readFile(filename, encoding, callback);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
readFile(options.spec, 'utf8', function (error, input) {
|
||
|
var good = [], bad = [],
|
||
1 year ago
|
markdown = markdownit('commonmark');
|
||
10 years ago
|
|
||
|
if (error) {
|
||
|
if (error.code === 'ENOENT') {
|
||
|
process.stderr.write('File not found: ' + options.spec);
|
||
|
process.exit(2);
|
||
|
}
|
||
|
|
||
|
process.stderr.write(error.stack || error.message || String(error));
|
||
|
process.exit(1);
|
||
|
}
|
||
|
|
||
|
input = input.replace(/→/g, '\t');
|
||
|
|
||
9 years ago
|
markdown.parse(input, {})
|
||
5 years ago
|
.filter(function (token) {
|
||
|
return token.tag === 'code' &&
|
||
|
token.info.trim() === 'example';
|
||
|
})
|
||
|
.forEach(function (token) {
|
||
|
var arr = token.content.split(/^\.\s*?$/m, 2);
|
||
|
var md = arr[0];
|
||
|
var html = arr[1].replace(/^\n/, '');
|
||
|
|
||
|
var result = {
|
||
|
md: md,
|
||
|
html: html,
|
||
|
line: token.map[0],
|
||
|
err: ''
|
||
|
};
|
||
|
|
||
|
try {
|
||
|
if (markdown.render(md) === normalize(html)) {
|
||
|
good.push(result);
|
||
|
} else {
|
||
|
result.err = markdown.render(md);
|
||
|
bad.push(result);
|
||
|
}
|
||
|
} catch (___) {
|
||
|
// bad.push(result);
|
||
|
throw ___;
|
||
|
}
|
||
|
});
|
||
10 years ago
|
|
||
4 years ago
|
var out = [];
|
||
|
|
||
10 years ago
|
if (!options.type) {
|
||
1 year ago
|
out.push(`CM spec stat: passed samples - ${good.length}, failed samples - ${bad.length}`);
|
||
10 years ago
|
} else {
|
||
|
var data = options.type === 'good' ? good : bad;
|
||
|
|
||
|
data.forEach(function (sample) {
|
||
1 year ago
|
out.push(
|
||
10 years ago
|
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' +
|
||
1 year ago
|
`src line: ${sample.line}\n\n.\n${sample.md}.\n${sample.html}.\n`
|
||
|
);
|
||
10 years ago
|
if (sample.err) {
|
||
1 year ago
|
out.push(`error:\n\n${sample.err}\n`);
|
||
10 years ago
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
4 years ago
|
if (options.output !== '-') fs.writeFileSync(options.output, out.join('\n'));
|
||
4 years ago
|
else console.log(out.join('\n'));
|
||
|
|
||
10 years ago
|
process.exit(0);
|
||
|
});
|