Marked: What would be the preferred method to add target="_blank" to links?

Created on 3 Sep 2015  路  15Comments  路  Source: markedjs/marked

Most helpful comment

@julmot :

var marked   = require('marked');
var renderer = new marked.Renderer();

marked.setOptions({
  ...
});

renderer.link = function( href, title, text ) {
  return '<a target="_blank" href="'+ href +'" title="' + title + '">' + text + '</a>';
}

marked(message, { renderer:renderer });

All 15 comments

@chjj

lol, I missed this issue wehn searching. I have apull request in that does this: See #657

:+1: When will it be available @chjj?

Push!? @chjj @drmuey

@julmot in the meantime, just use a custom renderer with your own link method.

Render links to external hosts with target="_blank": https://github.com/romanpitak/pfm

Just a suggestion...

@adam-lynch Could you please describe how?

@romanpitak A URL to a different subdomain would be also a different host but should be handled like an internal link. The same for a different top-level-domain like domain.es and domain.de.

@julmot you are right and that's why it was just a suggestion. I wrote the extension just to suite my own needs.

@julmot :

var marked   = require('marked');
var renderer = new marked.Renderer();

marked.setOptions({
  ...
});

renderer.link = function( href, title, text ) {
  return '<a target="_blank" href="'+ href +'" title="' + title + '">' + text + '</a>';
}

marked(message, { renderer:renderer });

And for the es6-y (and lazy):

const renderer = new marked.Renderer();
renderer.link = ( href, title, text ) => `<a target="_blank" href="${ href }" title="${ title }">${ text }</a>`;

I just want to suggest the following: {:target="_blank"} at the end of the link in markdown (Reference)

+1 for the @julmot is suggesting!
I'm surprised {:target="_blank"} doesn't already work!

984

I would be careful when overriding the renderer.link() method. There's some sanitization and encoding in the original method which is useful in preventing XSS attacks.

Here's an alternative method which modifies the HTML produced from the existing method:

const renderer = new marked.Renderer();
const linkRenderer = renderer.link;
renderer.link = (href, title, text) => {
    const html = linkRenderer.call(renderer, href, title, text);
    return html.replace(/^<a /, '<a target="_blank" rel="nofollow" ');
};
const html = marked(markdown, { renderer });

Same thing as @csytan's method, but with a small tweak to only modify the anchor element when we're linking to an external host :3

const renderer = new marked.Renderer();
const linkRenderer = renderer.link;
renderer.link = (href, title, text) => {
  const localLink = href.startsWith(`${location.protocol}//${location.hostname}`);
  const html = linkRenderer.call(renderer, href, title, text);
  return localLink ? html : html.replace(/^<a /, `<a target="_blank" rel="noreferrer noopener nofollow" `);
};
const html = marked(markdown, { renderer });
Was this page helpful?
0 / 5 - 0 ratings

Related issues

priyesh-diukar picture priyesh-diukar  路  3Comments

bennycode picture bennycode  路  4Comments

camwiegert picture camwiegert  路  4Comments

FireflyAndStars picture FireflyAndStars  路  3Comments

amejiarosario picture amejiarosario  路  3Comments