Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What would be the preferred method to add target="_blank" to links? #655

Closed
julmot opened this issue Sep 3, 2015 · 14 comments
Closed

What would be the preferred method to add target="_blank" to links? #655

julmot opened this issue Sep 3, 2015 · 14 comments

Comments

@julmot
Copy link

@julmot julmot commented Sep 3, 2015

No description provided.

@julmot

This comment has been minimized.

Copy link
Author

@julmot julmot commented Sep 8, 2015

@drmuey

This comment has been minimized.

Copy link

@drmuey drmuey commented Sep 8, 2015

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

@julmot

This comment has been minimized.

Copy link
Author

@julmot julmot commented Sep 11, 2015

👍 When will it be available @chjj?

@julmot

This comment has been minimized.

Copy link
Author

@julmot julmot commented Sep 17, 2015

Push!? @chjj @drmuey

@adam-lynch

This comment has been minimized.

Copy link

@adam-lynch adam-lynch commented Sep 18, 2015

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

@romanpitak

This comment has been minimized.

Copy link

@romanpitak romanpitak commented Sep 23, 2015

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

Just a suggestion...

@julmot

This comment has been minimized.

Copy link
Author

@julmot julmot commented Sep 23, 2015

@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.

@romanpitak

This comment has been minimized.

Copy link

@romanpitak romanpitak commented Sep 23, 2015

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

@hardware

This comment has been minimized.

Copy link

@hardware hardware commented Sep 26, 2015

@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 });
@AndrewRayCode

This comment has been minimized.

Copy link

@AndrewRayCode AndrewRayCode commented Dec 11, 2015

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>`;
@julmot

This comment has been minimized.

Copy link
Author

@julmot julmot commented Apr 28, 2016

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

@KingScooty

This comment has been minimized.

Copy link

@KingScooty KingScooty commented May 31, 2016

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

@joshbruce

This comment has been minimized.

Copy link
Member

@joshbruce joshbruce commented Dec 25, 2017

@joshbruce joshbruce closed this Dec 25, 2017
@csytan

This comment has been minimized.

Copy link

@csytan csytan commented Apr 20, 2018

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 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.