Links in rendered preview will now open in a new tab by default.

pull/11/head
Jeroen Akkerman 6 years ago
parent 31310fc4dd
commit e50bffba36

File diff suppressed because one or more lines are too long

@ -16,6 +16,7 @@ var marked = require('marked');
// Some variables
var isMac = /Mac/.test(navigator.platform);
var anchorToExternalRegex = new RegExp(/(<a.*?https?:\/\/.*?[^a]>)+?/g);
// Mapping of actions that can be bound to keyboard shortcuts or toolbar buttons
var bindings = {
@ -77,6 +78,25 @@ var isMobile = function () {
return check;
};
/**
* Modify HTML to add 'target="_blank"' to links so they open in new tabs by default.
* @param {string} htmlText - HTML to be modified.
* @return {string} The modified HTML text.
*/
function addAnchorTargetBlank(htmlText) {
var match;
while ((match = anchorToExternalRegex.exec(htmlText)) !== null) {
// With only one capture group in the RegExp, we can safely take the first index from the match.
var linkString = match[0];
if (linkString.indexOf('target=') === -1) {
var fixedLinkString = linkString.replace(/>$/, ' target="_blank">');
htmlText = htmlText.replace(linkString, fixedLinkString);
}
}
return htmlText;
}
/**
* Fix shortcut. Mac use Command, others use Ctrl.
@ -1477,13 +1497,16 @@ EasyMDE.prototype.markdown = function (text) {
}
}
// Set options
marked.setOptions(markedOptions);
// Convert the markdown to HTML
var htmlText = marked(text);
// Edit the HTML anchors to add 'target="_blank"' by default.
htmlText = addAnchorTargetBlank(htmlText);
// Return
return marked(text);
return htmlText;
}
};

Loading…
Cancel
Save