-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Option to make links open in new tab #337
Comments
Adding extra syntax to the link element is not a good idea as it diverts from the markdown spec and breks compatibility. However, there are 2 easy ways to add this to links: Using embeded htmlsome text with a link <a href="http://www.google.com" target="blank">google</a> Create an extensionSyntax
Test onlineCodeshowdown.extension('targetlink', function() {
return [{
type: 'lang',
regex: /\[((?:\[[^\]]*]|[^\[\]])*)]\([ \t]*<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\4[ \t]*)?\)\{\:target=(["'])(.*)\6}/g,
replace: function(wholematch, linkText, url, a, b, title, c, target) {
var result = '<a href="' + url + '"';
if (typeof title != 'undefined' && title !== '' && title !== null) {
title = title.replace(/"/g, '"');
title = showdown.helper.escapeCharacters(title, '*_', false);
result += ' title="' + title + '"';
}
if (typeof target != 'undefined' && target !== '' && target !== null) {
result += ' target="' + target + '"';
}
result += '>' + linkText + '</a>';
return result;
}
}];
}); |
Would that extension work with the auto-parsed simple-url (i.e., not |
this specific extension no. But this one will make all links in the page open as Code: showdown.extension('targetlink', function() {
return [{
type: 'html',
regex: /(<a [^>]+?)(>.*<\/a>)/g,
replace: '$1 target="_blank"$2'
}];
}); |
I have installed both (I hope) extensions (named one 'targetlink2' to keep them unique - is that the right thing to do?) and neither seem to be actually doing anything. I assume I have to just call those showdown.extension(...) bits during my startup? Here is how I use it: showdown.setFlavor('github');
showdown.extension('targetlink', function() {
return [{
type: 'html',
regex: /(<a [^>]+?)(>.*<\/a>)/g,
replace: '$1 target="_blank"$2'
}];
});
showdown.extension('targetlink2', function() {
return [{
type: 'lang',
regex: /\[((?:\[[^\]]*]|[^\[\]])*)]\([ \t]*<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\4[ \t]*)?\)\{\:target=(["'])(.*)\6}/g,
replace: function(wholematch, linkText, url, a, b, title, c, target) {
var result = '<a href="' + url + '"';
if (typeof title != 'undefined' && title !== '' && title !== null) {
title = title.replace(/"/g, '"');
title = showdown.helper.escapeCharacters(title, '*_', false);
result += ' title="' + title + '"';
}
if (typeof target != 'undefined' && target !== '' && target !== null) {
result += ' target="' + target + '"';
}
result += '>' + linkText + '</a>';
return result;
}
}];
});
this.converter = new showdown.Converter({
simplifiedAutoLink: true,
excludeTrailingPunctuationFromURLs: true,
strikethrough: true,
tables: true,
tasklists: true,
encodeEmails: true
}); My test block: Link test:
* www.google.com
* http://www.google.com
* [Google](http://www.google.com)
* [Google][1]
[1]: http://www.google.com |
You SHOULD NOT enable both, enable just one since they will colide. You also have to activate them:
|
Hmmm... I was hoping the activating was all I was missing, but it seems not. This is now all I have to initialize: showdown.setFlavor('github');
showdown.extension('targetlink', function() {
return [{
type: 'html',
regex: /(<a [^>]+?)(>.*<\/a>)/g,
replace: '$1 target="_blank"$2'
}];
});
this.converter = new showdown.Converter({
simplifiedAutoLink: true,
excludeTrailingPunctuationFromURLs: true,
strikethrough: true,
tables: true,
tasklists: true,
encodeEmails: true,
extension: ['targetlink']
}); and it's not doing anything. I guess it's no biggie - I can write a recursive function to traverse the DOM tree and find any A tags adding the |
you're missing an s in extensions. it's:
Also you don't need to set all those options if you use
and |
The above solution does not work for more than 1 link in the markup. Just in case anyone stumbles upon this issue, here's how I got it to work for ALL hyperlinks -
|
…n the target attribute. Also documented here - https://github.com/showdownjs/showdown/issues/337\#issuecomment-286647733
- This has been done in the way recommended by showdown showdownjs/showdown#337
* Format privacy-policy & cookies headers - Make main heading be a h1 - Make the text a large heading. This has not been done for all h1s because h1s in other pages have a different styling * Declaration page: privacy-policy link opens new tab - This has been done in the way recommended by showdown showdownjs/showdown#337
I know that this issue has been resolved a long time ago, but since I found this result on google prior to the documentation, I think it's good to mention here that this feature is now implemented in showdown itself since
|
links in user-provided markdown content should be opened in a new tab. we achieve this by creating an extension for the showdown markdown rendering library, `targetlinks`. Source: showdownjs/showdown#337 Signed-off-by: fidiego <[email protected]>
links in user-provided markdown content should be opened in a new tab. we achieve this by creating an extension for the showdown markdown rendering library, `targetlinks`. Source: showdownjs/showdown#337 Signed-off-by: fidiego <[email protected]>
links in user-provided markdown content should be opened in a new tab. we achieve this by creating an extension for the showdown markdown rendering library, `targetlinks`. Source: showdownjs/showdown#337 Signed-off-by: fidiego <[email protected]>
When using the "simple links" option it'd be nice if there was another option that would add
target="_blank"
(or whatever target of your choice) to all links.The text was updated successfully, but these errors were encountered: