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

Multiple extensions in marked.use() causing defaults being wrongly overrided #2649

Closed
hyrious opened this issue Nov 11, 2022 · 1 comment · Fixed by #2651
Closed

Multiple extensions in marked.use() causing defaults being wrongly overrided #2649

hyrious opened this issue Nov 11, 2022 · 1 comment · Fixed by #2651
Labels
L2 - annoying Similar to L1 - broken but there is a known workaround available for the issue

Comments

@hyrious
Copy link

hyrious commented Nov 11, 2022

Marked version:
4.2.2

Describe the bug
As described in the pro doc:

  • The renderer and tokenizer options are objects with functions that will be merged into the built-in renderer and tokenizer respectively.

Which means people can use multiple extensions written in this way:

marked.use({ renderer }, { tokenizer })

However this way will cause errors, see reproduction below.

To Reproduce
Install marked locally and run this simple script:

import { marked } from "marked";

marked.use({ tokenizer: {} }, { renderer: {} });
marked.parse(`# hello`);

yields:

TypeError: renderer.text is not a function

Workarounds
You can call marked.use() multiple times to avoid this error.

Expected behavior
No error occurs.

Cause
The last line of marked.use()

marked.setOptions(opts);

causes an unexpected renderer or tokenizer object being merged into marked.defaults.

Maybe related(?) #2082

@UziTech
Copy link
Member

UziTech commented Nov 11, 2022

It looks like the issue is this line

const opts = merge({}, ...args);

currently it merges all of the extensions together to get the options. It should be smarter about how it merges the options.

For example, if one extension has async: true set and another one after it has async: false the last extension loaded will override the previous which is what we want for most options but not all. in the case of async if any of the extensions are async: true than async should be true.

It should also disregard the renderer, tokenizer, extensions, and walkTokens options in this merge. They are merged intelligently below this line.

There might be some other options that I am missing.

@UziTech UziTech added the L2 - annoying Similar to L1 - broken but there is a known workaround available for the issue label Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L2 - annoying Similar to L1 - broken but there is a known workaround available for the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants