Skip to content

Custom renderer in marked.use() not applied to inline tokens #3871

@The-LukeZ

Description

@The-LukeZ

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() })

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions