Renderer not merged when used in marked() options
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
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. 😁👍
I would like to create a PR fixing this issue(to update the documentation), may you please guide me. :)
@madhavanand-github You will have to create a PR that updates the files in the /docs folder.
May you tell me the content to be updated. I am a beginner, not much experienced with JavaScript, but want to contribute and learn.
This is just a documentation change so no JavaScript experience is needed. I don't know what exactly needs to change.
@foxbunny I would like to fix this issue. Thank you.
@YashasviChaurasia sounds good 👍 you can take it on.
@UziTech Could you specify exactly where should I do the documentation changes? Thank you
https://github.com/markedjs/marked/blob/master/docs/USING_PRO.md#markeduse
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
rendererin the options object passed tomarked.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.
@stephenhutchings PRs are appreciated 😁👍