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

Renderer not merged when used in marked() options #2082

Closed
foxbunny opened this issue May 31, 2021 · 11 comments · Fixed by #2783
Closed

Renderer not merged when used in marked() options #2082

foxbunny opened this issue May 31, 2021 · 11 comments · Fixed by #2783
Labels
category: docs Documentation changes good first issue Something easy to get started with

Comments

@foxbunny
Copy link

foxbunny commented May 31, 2021

Marked version: 2.0.6

Markdown flavor: n/a

Expectation

Based on the documentation for marked.use(), which states that:

The renderer and tokenizer options can be an object with functions that will be merged into the renderer and tokenizer respectively.

and also based on the fact that description of renderer option in the Advanced section I would expect that the option passed to marked() would behave the same way as the option passed to marked.use().

I therefore passed in a renderer with just image() implemented.

Result

I got an exception that says renderer.paragraph() is not a function.

After looking at the source code, it does not seem like the renderer is "merged into" anything when passed directly to marked(). Documentation should clarify this, preferably before linking to the section on extending.

What was attempted

let renderer = attachments => ({
  image (href, title) {
    if (!attachments) return false
    return `<img src="${attachments.get(href)?.url || href}" alt="${title}>`
  },
})

// ....

let render = (text, attachments) => marked(text, { renderer: renderer(attachments) })

Call stack & console log

TypeError: this.renderer.paragraph is not a function

@UziTech
Copy link
Member

UziTech commented May 31, 2021

marked.use is for using extensions. marked(md, options) is the same as setting options with marked.setOptions.

If you would like to create a PR to make the documentation better it would be much appreciated. 😁👍

@UziTech UziTech added the category: docs Documentation changes label May 31, 2021
@madhavanand-github
Copy link

I would like to create a PR fixing this issue(to update the documentation), may you please guide me. :)

@UziTech
Copy link
Member

UziTech commented Oct 25, 2021

@madhavanand-github You will have to create a PR that updates the files in the /docs folder.

@madhavanand-github
Copy link

May you tell me the content to be updated.
I am a beginner, not much experienced with JavaScript, but want to contribute and learn.

@UziTech
Copy link
Member

UziTech commented Oct 26, 2021

This is just a documentation change so no JavaScript experience is needed. I don't know what exactly needs to change.

@YashasviChaurasia
Copy link

@foxbunny I would like to fix this issue.
Thank you.

@UziTech
Copy link
Member

UziTech commented Oct 23, 2022

@YashasviChaurasia sounds good 👍 you can take it on.

@YashasviChaurasia
Copy link

@UziTech Could you specify exactly where should I do the documentation changes?
Thank you

@UziTech
Copy link
Member

UziTech commented Oct 25, 2022

https://github.com/markedjs/marked/blob/master/docs/USING_PRO.md#markeduse

@stephenhutchings
Copy link
Contributor

I was also tripped up by the wording here, specifically the bit about merging defaults.

Something like the following text would help make it clear that new renderers are not merged with the default methods. Changes are in bold.

The renderer defines the HTML output of a given token. If you supply a
renderer in the options object passed to marked.use(), any functions in the
object will override the default handling of that token type.

Calling marked.use() to override the same function multiple times will give
priority to the version that was assigned last. Overriding functions can return
false to fall back to the previous override in the sequence, or resume default
behavior if all overrides return false. Returning any other value (including
nothing) will prevent fallback behavior.

Note that a renderer created with new Renderer() will not be created with the
default token handlers.

@UziTech
Copy link
Member

UziTech commented Apr 22, 2023

@stephenhutchings PRs are appreciated 😁👍

stephenhutchings added a commit to stephenhutchings/marked that referenced this issue Apr 22, 2023
Update text in line with the [issue comments](markedjs#2082 (comment))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: docs Documentation changes good first issue Something easy to get started with
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants