Search code examples
javascriptregexmarkdownmarkdown-it

How to support Markdown links syntax "[text](link)" inside superscript syntex "^(text)"?


So I'm using Markdown-it as my Markdown renderer, and I added some custom tags using markdownitRegexp.

I have a problem with the superscript syntax ^(text), it ends with a ")" so you can't use links [text](link) because they ends with it too.

I wanna make it able to handle multiple links without any problem:

^(foo doo [bar](https://stackoverflow.com "baz") foo [doo](https://github.com)...)

That's what I'm using for now:

window.markdownitRegexp(
    /\^\(([\s\S]+?)[\)]/,
    function (match, utils) {
        const html = inlineRenderer('supsubscript').render(match[1], env);
        return `<sup>${html.replace(/\<p\>|\<\/p\>\s/g, '')}</sup>`;
    }
)

Solution

  • I ended up with this one:

    \^\(((?:\[[^\]]*\]\([^)]*\)|[\s\S])+?)\)