mirror of
https://github.com/Ionaru/easy-markdown-editor
synced 2025-07-02 15:44:28 -06:00
Links in rendered preview will now open in a new tab by default.
This commit is contained in:
parent
31310fc4dd
commit
e50bffba36
2
dist/easymde.min.js
vendored
2
dist/easymde.min.js
vendored
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);
|
||||
|
||||
// Return
|
||||
return marked(text);
|
||||
// Edit the HTML anchors to add 'target="_blank"' by default.
|
||||
htmlText = addAnchorTargetBlank(htmlText);
|
||||
|
||||
return htmlText;
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user