Showdown: Add option to only open external links in new window

Created on 6 Oct 2018  路  4Comments  路  Source: showdownjs/showdown

I was thinking it might be nice to add an option like openExternalLinksInNewWindow

and then use a filter something like this (hopefully better, just whipped this up)

    converter.addExtension(function () {
        return [{
            type: 'output',
            regex: /<a\shref.+">/g,
            replace : function (text) {
                var url = text.match(/"(.*?)"/)[1]
                if(url.includes(window.location.hostname)){
                    return text
                }
                return '<a href="' + url + '" target="_blank">'
            }
        }]
    }, 'externalLink')
help wanted

All 4 comments

@oscarmorrison thank you for your suggestion but... There's already an option to open links in external windows 馃槄 馃ぃ openLinksInNewWindow

or... do you mean opening ONLY links that point to external URLs in a new window?

If it's the second, unfortunately that cannot be put into showdown since it doesn't make sense in nodejs environment (and the 'window' object is not available in Node).

But as you rightly pointed out, that can easily be achieved with an output extension (with the side bonus of said extension being able to also change tags that the user wrote in HTML in the original document).

@oscarmorrison Did you manage to find a solution for your problem?

I found that openLinksInNewWindow: true does not work along with simplifiedAutoLink: true The latter overrides the former, and links are shown in _current_ window itself

Was this page helpful?
0 / 5 - 0 ratings