fix(cli): await config functions before parsing (#4138)

This commit is contained in:
YIOYIO
2026-10-25 23:39:11 -06:00
committed by GitHub
parent 379be9d656
commit 3a11ad1483
6 changed files with 46 additions and 2 deletions
+1 -1
View File
@@ -192,7 +192,7 @@ export async function main(nodeProcess) {
}
if (typeof markedConfig === 'function') {
markedConfig(marked);
await markedConfig(marked);
} else {
marked.use(markedConfig);
}
+1
View File
@@ -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
View File
@@ -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')}'`,
+6
View File
@@ -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 });
};
+5
View File
@@ -0,0 +1,5 @@
module.exports = async() => {
const { setImmediate } = await import('node:timers/promises');
await setImmediate();
throw new Error('Async config failed');
};