Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix marked.use with multiple args #2651

Merged
merged 1 commit into from Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/marked.js
Expand Up @@ -153,20 +153,23 @@ marked.defaults = defaults;
*/

marked.use = function(...args) {
const opts = merge({}, ...args);
const extensions = marked.defaults.extensions || { renderers: {}, childTokens: {} };
let hasExtensions;

args.forEach((pack) => {
// copy options to new object
const opts = merge({}, pack);

// set async to true if it was set to true before
opts.async = marked.defaults.async || opts.async;

// ==-- Parse "addon" extensions --== //
if (pack.extensions) {
hasExtensions = true;
pack.extensions.forEach((ext) => {
if (!ext.name) {
throw new Error('extension name required');
}
if (ext.renderer) { // Renderer extensions
const prevRenderer = extensions.renderers ? extensions.renderers[ext.name] : null;
const prevRenderer = extensions.renderers[ext.name];
if (prevRenderer) {
// Replace extension with func to run new extension but fall back if false
extensions.renderers[ext.name] = function(...args) {
Expand Down Expand Up @@ -209,6 +212,7 @@ marked.use = function(...args) {
extensions.childTokens[ext.name] = ext.childTokens;
}
});
opts.extensions = extensions;
}

// ==-- Parse "overwrite" extensions --== //
Expand Down Expand Up @@ -256,10 +260,6 @@ marked.use = function(...args) {
};
}

if (hasExtensions) {
opts.extensions = extensions;
}

marked.setOptions(opts);
});
};
Expand Down
37 changes: 36 additions & 1 deletion test/unit/marked-spec.js
@@ -1,4 +1,4 @@
import { marked, Renderer, Slugger, lexer, parseInline, use, getDefaults, walkTokens as _walkTokens } from '../../src/marked.js';
import { marked, Renderer, Slugger, lexer, parseInline, use, getDefaults, walkTokens as _walkTokens, defaults, setOptions } from '../../src/marked.js';

describe('Test heading ID functionality', () => {
it('should add id attribute by default', () => {
Expand Down Expand Up @@ -612,6 +612,41 @@ used extension2 walked</p>

runTest();
});

it('should merge extensions correctly', () => {
use(
{},
{ tokenizer: {} },
{ renderer: {} },
{ walkTokens: () => {} },
{ extensions: [] }
);

expect(() => marked('# test')).not.toThrow();
});
});

it('should be async if any extension in use args is async', () => {
use(
{ async: true },
{ async: false }
);

expect(defaults.async).toBeTrue();
});

it('should be async if any extension in use is async', () => {
use({ async: true });
use({ async: false });

expect(defaults.async).toBeTrue();
});

it('should reset async with setOptions', () => {
use({ async: true });
setOptions({ async: false });

expect(defaults.async).toBeFalse();
});

it('should allow deleting/editing tokens', () => {
Expand Down