Skip to content

Commit

Permalink
fix: fix marked.use with multiple args (#2651)
Browse files Browse the repository at this point in the history
Fixes undefined
  • Loading branch information
UziTech committed Nov 20, 2022
1 parent 4aee878 commit 73a7bf5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
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

1 comment on commit 73a7bf5

@vercel
Copy link

@vercel vercel bot commented on 73a7bf5 Nov 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.