marked icon indicating copy to clipboard operation
marked copied to clipboard

Renderer not merged when used in marked() options

Open foxbunny opened this issue 4 years ago • 5 comments

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

foxbunny avatar May 31 '21 19:05 foxbunny

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 avatar May 31 '21 22:05 UziTech

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

madhavanand-github avatar Oct 25 '21 12:10 madhavanand-github

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

UziTech avatar Oct 25 '21 17:10 UziTech

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

madhavanand-github avatar Oct 26 '21 11:10 madhavanand-github

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

UziTech avatar Oct 26 '21 13:10 UziTech

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

YashasviChaurasia avatar Oct 23 '22 09:10 YashasviChaurasia

@YashasviChaurasia sounds good 👍 you can take it on.

UziTech avatar Oct 23 '22 21:10 UziTech

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

YashasviChaurasia avatar Oct 24 '22 18:10 YashasviChaurasia

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

UziTech avatar Oct 25 '22 05:10 UziTech

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.

stephenhutchings avatar Apr 22 '23 03:04 stephenhutchings

@stephenhutchings PRs are appreciated 😁👍

UziTech avatar Apr 22 '23 04:04 UziTech