-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
Marked version: 17.0.1
Describe the bug
Custom renderer methods are not being applied to inline tokens in paragraphs when using marked.use() with extensions.
I don't know if that happens, when extensions are registered globally via marked.use(), the custom renderer passed to the marked() function call does not override the rendering of tokens produced by those extensions during inline parsing within block-level contexts (e.g., paragraphs).
To Reproduce
Repl Code
import { marked, Renderer } from "marked";
class CustomRenderer extends Renderer {
codespan({ text }) {
return `<custom-code>${text}</custom-code>`;
}
}
marked.use({
renderer: new CustomRenderer(),
});
const input = "**Bold** - Use `**text**`";
const output = marked(input);
console.log(output);
// Expected: <p><strong>Bold</strong> - Use <custom-code>**text**</custom-code></p>
// Actual: <p><strong>Bold</strong> - Use <code>**text**</code></p>Expected behavior
The custom renderer passed to marked.use({ renderer: new CustomRenderer() }) should be used for rendering all inline tokens, including codespan.
Additional context
I assume the issue occurs because the paragraph's inline token parsing does not receive or respect the custom renderer instance set in the marked.use() call, instead falling back to the default renderer.
The solution is to pass it with every marked() call instead of in .use(): marked(message, { renderer: new CustomRenderer() })