-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Comments
lol, I missed this issue wehn searching. I have apull request in that does this: See #657 |
👍 When will it be available @chjj? |
@julmot in the meantime, just use a custom renderer with your own link method. |
Render links to external hosts with 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):
|
I just want to suggest the following: |
+1 for the @julmot is suggesting! |
I would be careful when overriding the Here's an alternative method which modifies the HTML produced from the existing method:
|
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 }); |
Related #144 |
Big thanks, @csytan! |
Please support the attribute
|
No description provided.
The text was updated successfully, but these errors were encountered: