mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix(cli): honor defaults and config when outputting tokens (#4146)
* fix(cli): honor defaults and config when outputting tokens * fix(cli): share configured options between lexer and parser
This commit is contained in:
+4
-3
@@ -59,7 +59,7 @@ export async function main(nodeProcess) {
|
||||
*/
|
||||
async function start(argv) {
|
||||
const files = [];
|
||||
const options = {};
|
||||
const cliOptions = {};
|
||||
let input;
|
||||
let output;
|
||||
let string;
|
||||
@@ -137,11 +137,11 @@ export async function main(nodeProcess) {
|
||||
continue;
|
||||
}
|
||||
if (arg.indexOf('--no-') === 0) {
|
||||
options[opt] = typeof marked.defaults[opt] !== 'boolean'
|
||||
cliOptions[opt] = typeof marked.defaults[opt] !== 'boolean'
|
||||
? null
|
||||
: false;
|
||||
} else {
|
||||
options[opt] = typeof marked.defaults[opt] !== 'boolean'
|
||||
cliOptions[opt] = typeof marked.defaults[opt] !== 'boolean'
|
||||
? argv.shift()
|
||||
: true;
|
||||
}
|
||||
@@ -221,6 +221,7 @@ export async function main(nodeProcess) {
|
||||
}
|
||||
}
|
||||
|
||||
const options = { ...marked.defaults, ...cliOptions };
|
||||
const html = tokens
|
||||
? JSON.stringify(marked.lexer(data, options), null, 2)
|
||||
: await marked.parse(data, options);
|
||||
|
||||
@@ -71,6 +71,15 @@ function fixturePath(filePath) {
|
||||
return resolve(import.meta.dirname, './fixtures', filePath);
|
||||
}
|
||||
|
||||
async function execMarkedString(args, markdown) {
|
||||
const { stdout, stderr } = await execFileAsync(process.execPath, [
|
||||
resolve(import.meta.dirname, '../../bin/marked.js'),
|
||||
...args, '--string', markdown,
|
||||
]);
|
||||
assert.strictEqual(stderr, '');
|
||||
return stdout;
|
||||
}
|
||||
|
||||
function execMarked({ args = '', stdin = '', stdout = '', stderr = '' }) {
|
||||
const execPath = resolve(import.meta.dirname, '../../bin/marked');
|
||||
return async() => {
|
||||
@@ -170,6 +179,26 @@ describe('bin/marked', () => {
|
||||
});
|
||||
|
||||
describe('config', () => {
|
||||
it('uses GFM by default', async() => {
|
||||
const result = await execMarkedString([], '~~test~~');
|
||||
assert.strictEqual(result, '<p><del>test</del></p>\n\n');
|
||||
});
|
||||
|
||||
it('uses options from the config file in a separate process', async() => {
|
||||
const result = await execMarkedString(['--config', fixturePath('bin-config.js')], 'line1\nline2');
|
||||
assert.strictEqual(result, '<p>line1<br>line2</p>\n\n');
|
||||
});
|
||||
|
||||
it('uses a tokenizer from the config file', async() => {
|
||||
const result = await execMarkedString(['--config', fixturePath('bin-config-tokenizer.js')], '`test`');
|
||||
assert.strictEqual(result, '<p><code>TEST</code></p>\n\n');
|
||||
});
|
||||
|
||||
it('lets command-line options override config options', async() => {
|
||||
const result = await execMarkedString(['--config', fixturePath('bin-config.js'), '--no-breaks'], 'line1\nline2');
|
||||
assert.strictEqual(result, '<p>line1\nline2</p>\n\n');
|
||||
});
|
||||
|
||||
it('-c', testInput({
|
||||
args: ['-c', fixturePath('bin-config.js'), '-s', 'line1\nline2'],
|
||||
stdout: '<p>line1<br>line2</p>',
|
||||
@@ -225,6 +254,34 @@ describe('bin/marked', () => {
|
||||
}));
|
||||
});
|
||||
|
||||
describe('tokens', () => {
|
||||
async function tokens(args, markdown) {
|
||||
const stdout = await execMarkedString(['--tokens', ...args], markdown);
|
||||
return JSON.parse(stdout);
|
||||
}
|
||||
|
||||
it('uses GFM by default', async() => {
|
||||
const result = await tokens([], '~~test~~');
|
||||
assert.strictEqual(result[0].tokens[0].type, 'del');
|
||||
});
|
||||
|
||||
it('uses options from the config file', async() => {
|
||||
const result = await tokens(['--config', fixturePath('bin-config.js')], 'line1\nline2');
|
||||
assert.deepStrictEqual(result[0].tokens.map(token => token.type), ['text', 'br', 'text']);
|
||||
});
|
||||
|
||||
it('uses a tokenizer from the config file', async() => {
|
||||
const result = await tokens(['--config', fixturePath('bin-config-tokenizer.js')], '`test`');
|
||||
assert.strictEqual(result[0].tokens[0].type, 'codespan');
|
||||
assert.strictEqual(result[0].tokens[0].text, 'TEST');
|
||||
});
|
||||
|
||||
it('lets command-line options override config options', async() => {
|
||||
const result = await tokens(['--config', fixturePath('bin-config.js'), '--no-breaks'], 'line1\nline2');
|
||||
assert.deepStrictEqual(result[0].tokens.map(token => token.type), ['text']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('input', () => {
|
||||
it('input file positional', testInput({
|
||||
args: [fixturePath('bin-input.md')],
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
export default {
|
||||
tokenizer: {
|
||||
codespan(src) {
|
||||
const match = /^`([^`]+)`/.exec(src);
|
||||
if (match) {
|
||||
return {
|
||||
type: 'codespan',
|
||||
raw: match[0],
|
||||
text: match[1].toUpperCase(),
|
||||
};
|
||||
}
|
||||
return false;
|
||||
},
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user