How to have more instances of marked js in one project?

342 Views Asked by At

My problem, which I cannot overcome is that I cannot achieve a state where I have 2 parallel instances oh markedjs which work fully independently in terms of configuration I am passing through.

In other words I need two different markdown handlers, as in one case I want to parse markdowned text using say algorithm X, and the other using alghorithm Z. But from what I read in official docs it's... impossible...? Really?

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.

And indeed, that's how it works on my end - I use marked.parse() in two different parts of app and I see changes reflected in both places even though I try to conditionally change the configuration (using flag or something, doesn't matter, the result is always the same).

How to fix it? How can one develop such nonsensefull solution?

1

There are 1 best solutions below

0
On

A little late on this answer.

To avoid this, you can instantiate a new Marked object and pass extensions to that, e.g.:

import {Marked} from 'marked';

const renderer: RendererObject = {
  heading(text, level) {
    // turn headers into plain text
    return `<p>text</p>`;
  }
}
const marked = new Marked({renderer});

Documentation: https://marked.js.org/using_advanced#instance