Skip to content

Commit 9470e8f

Browse files
authored
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
1 parent 84beefb commit 9470e8f

3 files changed

Lines changed: 76 additions & 3 deletions

File tree

‎bin/main.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function main(nodeProcess) {
5959
*/
6060
async function start(argv) {
6161
const files = [];
62-
const options = {};
62+
const cliOptions = {};
6363
let input;
6464
let output;
6565
let string;
@@ -137,11 +137,11 @@ export async function main(nodeProcess) {
137137
continue;
138138
}
139139
if (arg.indexOf('--no-') === 0) {
140-
options[opt] = typeof marked.defaults[opt] !== 'boolean'
140+
cliOptions[opt] = typeof marked.defaults[opt] !== 'boolean'
141141
? null
142142
: false;
143143
} else {
144-
options[opt] = typeof marked.defaults[opt] !== 'boolean'
144+
cliOptions[opt] = typeof marked.defaults[opt] !== 'boolean'
145145
? argv.shift()
146146
: true;
147147
}
@@ -221,6 +221,7 @@ export async function main(nodeProcess) {
221221
}
222222
}
223223

224+
const options = { ...marked.defaults, ...cliOptions };
224225
const html = tokens
225226
? JSON.stringify(marked.lexer(data, options), null, 2)
226227
: await marked.parse(data, options);

‎test/unit/bin.test.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ function fixturePath(filePath) {
7171
return resolve(import.meta.dirname, './fixtures', filePath);
7272
}
7373

74+
async function execMarkedString(args, markdown) {
75+
const { stdout, stderr } = await execFileAsync(process.execPath, [
76+
resolve(import.meta.dirname, '../../bin/marked.js'),
77+
...args, '--string', markdown,
78+
]);
79+
assert.strictEqual(stderr, '');
80+
return stdout;
81+
}
82+
7483
function execMarked({ args = '', stdin = '', stdout = '', stderr = '' }) {
7584
const execPath = resolve(import.meta.dirname, '../../bin/marked');
7685
return async() => {
@@ -170,6 +179,26 @@ describe('bin/marked', () => {
170179
});
171180

172181
describe('config', () => {
182+
it('uses GFM by default', async() => {
183+
const result = await execMarkedString([], '~~test~~');
184+
assert.strictEqual(result, '<p><del>test</del></p>\n\n');
185+
});
186+
187+
it('uses options from the config file in a separate process', async() => {
188+
const result = await execMarkedString(['--config', fixturePath('bin-config.js')], 'line1\nline2');
189+
assert.strictEqual(result, '<p>line1<br>line2</p>\n\n');
190+
});
191+
192+
it('uses a tokenizer from the config file', async() => {
193+
const result = await execMarkedString(['--config', fixturePath('bin-config-tokenizer.js')], '`test`');
194+
assert.strictEqual(result, '<p><code>TEST</code></p>\n\n');
195+
});
196+
197+
it('lets command-line options override config options', async() => {
198+
const result = await execMarkedString(['--config', fixturePath('bin-config.js'), '--no-breaks'], 'line1\nline2');
199+
assert.strictEqual(result, '<p>line1\nline2</p>\n\n');
200+
});
201+
173202
it('-c', testInput({
174203
args: ['-c', fixturePath('bin-config.js'), '-s', 'line1\nline2'],
175204
stdout: '<p>line1<br>line2</p>',
@@ -225,6 +254,34 @@ describe('bin/marked', () => {
225254
}));
226255
});
227256

257+
describe('tokens', () => {
258+
async function tokens(args, markdown) {
259+
const stdout = await execMarkedString(['--tokens', ...args], markdown);
260+
return JSON.parse(stdout);
261+
}
262+
263+
it('uses GFM by default', async() => {
264+
const result = await tokens([], '~~test~~');
265+
assert.strictEqual(result[0].tokens[0].type, 'del');
266+
});
267+
268+
it('uses options from the config file', async() => {
269+
const result = await tokens(['--config', fixturePath('bin-config.js')], 'line1\nline2');
270+
assert.deepStrictEqual(result[0].tokens.map(token => token.type), ['text', 'br', 'text']);
271+
});
272+
273+
it('uses a tokenizer from the config file', async() => {
274+
const result = await tokens(['--config', fixturePath('bin-config-tokenizer.js')], '`test`');
275+
assert.strictEqual(result[0].tokens[0].type, 'codespan');
276+
assert.strictEqual(result[0].tokens[0].text, 'TEST');
277+
});
278+
279+
it('lets command-line options override config options', async() => {
280+
const result = await tokens(['--config', fixturePath('bin-config.js'), '--no-breaks'], 'line1\nline2');
281+
assert.deepStrictEqual(result[0].tokens.map(token => token.type), ['text']);
282+
});
283+
});
284+
228285
describe('input', () => {
229286
it('input file positional', testInput({
230287
args: [fixturePath('bin-input.md')],
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default {
2+
tokenizer: {
3+
codespan(src) {
4+
const match = /^`([^`]+)`/.exec(src);
5+
if (match) {
6+
return {
7+
type: 'codespan',
8+
raw: match[0],
9+
text: match[1].toUpperCase(),
10+
};
11+
}
12+
return false;
13+
},
14+
},
15+
};

0 commit comments

Comments
 (0)