mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix(cli): await config functions before parsing (#4138)
This commit is contained in:
+1
-1
@@ -192,7 +192,7 @@ export async function main(nodeProcess) {
|
||||
}
|
||||
|
||||
if (typeof markedConfig === 'function') {
|
||||
markedConfig(marked);
|
||||
await markedConfig(marked);
|
||||
} else {
|
||||
marked.use(markedConfig);
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ If it is a `.json` file it should be a JSON object that will be passed to marked
|
||||
|
||||
If `.js` is used it should have a default export of a marked options object or a function that takes `marked` as a parameter.
|
||||
It can use the `marked` parameter to install extensions using `marked.use`.
|
||||
The CLI waits for asynchronous config functions to finish before parsing the input.
|
||||
|
||||
By default the marked cli will look for a config file in your home directory in the following order.
|
||||
|
||||
|
||||
+30
-1
@@ -3,12 +3,13 @@ import { htmlIsEqual } from '@markedjs/testutils';
|
||||
import { resolve } from 'node:path';
|
||||
import { describe, it, mock } from 'node:test';
|
||||
import assert from 'node:assert';
|
||||
import { exec } from 'node:child_process';
|
||||
import { exec, execFile } from 'node:child_process';
|
||||
import { promisify } from 'node:util';
|
||||
import { mkdtemp, readFile, rm, symlink, writeFile, access } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
function createMocks() {
|
||||
const mocks = {
|
||||
@@ -189,6 +190,34 @@ describe('bin/marked', () => {
|
||||
stdout: '<p>line1<br>line2</p>',
|
||||
}));
|
||||
|
||||
it('config with a synchronous function', testInput({
|
||||
args: ['--config', fixturePath('bin-config-function.js'), '-s', 'line1\nline2'],
|
||||
stdout: '<p>line1<br>line2</p>',
|
||||
}));
|
||||
|
||||
it('waits for an async config function', async() => {
|
||||
const { stdout, stderr } = await execFileAsync(process.execPath, [
|
||||
resolve(import.meta.dirname, '../../bin/marked.js'),
|
||||
'--config', fixturePath('bin-config-async.mjs'),
|
||||
'--string', 'line1\nline2',
|
||||
]);
|
||||
|
||||
assert.strictEqual(stdout, '<p>line1<br>line2</p>\n\n');
|
||||
assert.strictEqual(stderr, '');
|
||||
});
|
||||
|
||||
it('reports a rejected config function', async() => {
|
||||
await assert.rejects(execFileAsync(process.execPath, [
|
||||
resolve(import.meta.dirname, '../../bin/marked.js'),
|
||||
'--config', fixturePath('bin-config-reject.cjs'),
|
||||
'--string', '# test',
|
||||
]), {
|
||||
code: 1,
|
||||
stdout: '',
|
||||
stderr: 'Async config failed',
|
||||
});
|
||||
});
|
||||
|
||||
it('config not found', testInput({
|
||||
args: ['--config', fixturePath('does-not-exist.js'), '-s', 'line1\nline2'],
|
||||
stderr: `Cannot load config file '${fixturePath('does-not-exist.js')}'`,
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import { setImmediate } from 'node:timers/promises';
|
||||
|
||||
export default async marked => {
|
||||
await setImmediate();
|
||||
marked.use({ breaks: true });
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
export default marked => {
|
||||
marked.use({ breaks: true });
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
module.exports = async() => {
|
||||
const { setImmediate } = await import('node:timers/promises');
|
||||
await setImmediate();
|
||||
throw new Error('Async config failed');
|
||||
};
|
||||
Reference in New Issue
Block a user