2017-02-27 03:13:52 +08:00
|
|
|
// url allowed chars [a-z\d_.~:/?#[]@!$&'()*+,;=-]
|
|
|
|
|
|
|
|
var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)()(?=\s|$)(?!["<>])/gi,
|
2017-03-15 10:49:20 +08:00
|
|
|
simpleURLRegex2 = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+?)([.!?,()\[\]]?)(?=\s|$)(?!["<>])/gi,
|
2017-02-27 03:13:52 +08:00
|
|
|
//simpleURLRegex3 = /\b(((https?|ftp):\/\/|www\.)[a-z\d.-]+\.[a-z\d_.~:/?#\[\]@!$&'()*+,;=-]+?)([.!?()]?)(?=\s|$)(?!["<>])/gi,
|
2017-04-23 09:14:56 +08:00
|
|
|
delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)()>/gi,
|
2017-02-27 03:13:52 +08:00
|
|
|
simpleMailRegex = /(^|\s)(?:mailto:)?([A-Za-z0-9!#$%&'*+-/=?^_`{|}~.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?=$|\s)/gmi,
|
|
|
|
delimMailRegex = /<()(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi,
|
|
|
|
|
|
|
|
replaceLink = function (options) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
return function (wm, link, m2, m3, trailingPunctuation) {
|
|
|
|
var lnkTxt = link,
|
2017-03-31 07:25:40 +08:00
|
|
|
append = '',
|
|
|
|
target = '';
|
2017-02-27 03:13:52 +08:00
|
|
|
if (/^www\./i.test(link)) {
|
|
|
|
link = link.replace(/^www\./i, 'http://www.');
|
|
|
|
}
|
|
|
|
if (options.excludeTrailingPunctuationFromURLs && trailingPunctuation) {
|
|
|
|
append = trailingPunctuation;
|
|
|
|
}
|
2017-03-31 07:25:40 +08:00
|
|
|
if (options.openLinksInNewWindow) {
|
|
|
|
target = ' target="_blank"';
|
|
|
|
}
|
|
|
|
return '<a href="' + link + '"' + target + '>' + lnkTxt + '</a>' + append;
|
2017-02-27 03:13:52 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
replaceMail = function (options, globals) {
|
|
|
|
'use strict';
|
|
|
|
return function (wholeMatch, b, mail) {
|
|
|
|
var href = 'mailto:';
|
|
|
|
b = b || '';
|
|
|
|
mail = showdown.subParser('unescapeSpecialChars')(mail, options, globals);
|
|
|
|
if (options.encodeEmails) {
|
|
|
|
href = showdown.helper.encodeEmailAddress(href + mail);
|
|
|
|
mail = showdown.helper.encodeEmailAddress(mail);
|
|
|
|
} else {
|
|
|
|
href = href + mail;
|
|
|
|
}
|
|
|
|
return b + '<a href="' + href + '">' + mail + '</a>';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-08-03 10:47:49 +08:00
|
|
|
showdown.subParser('autoLinks', function (text, options, globals) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('autoLinks.before', text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2017-02-27 03:13:52 +08:00
|
|
|
text = text.replace(delimUrlRegex, replaceLink(options));
|
|
|
|
text = text.replace(delimMailRegex, replaceMail(options, globals));
|
|
|
|
|
|
|
|
text = globals.converter._dispatch('autoLinks.after', text, options, globals);
|
2015-07-11 09:42:53 +08:00
|
|
|
|
2017-02-27 03:13:52 +08:00
|
|
|
return text;
|
|
|
|
});
|
|
|
|
|
|
|
|
showdown.subParser('simplifiedAutoLinks', function (text, options, globals) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
if (!options.simplifiedAutoLink) {
|
|
|
|
return text;
|
2016-08-20 02:12:25 +08:00
|
|
|
}
|
|
|
|
|
2017-02-27 03:13:52 +08:00
|
|
|
text = globals.converter._dispatch('simplifiedAutoLinks.before', text, options, globals);
|
|
|
|
|
|
|
|
if (options.excludeTrailingPunctuationFromURLs) {
|
|
|
|
text = text.replace(simpleURLRegex2, replaceLink(options));
|
|
|
|
} else {
|
|
|
|
text = text.replace(simpleURLRegex, replaceLink(options));
|
2015-07-11 09:42:53 +08:00
|
|
|
}
|
2017-02-27 03:13:52 +08:00
|
|
|
text = text.replace(simpleMailRegex, replaceMail(options, globals));
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2017-02-27 03:13:52 +08:00
|
|
|
text = globals.converter._dispatch('simplifiedAutoLinks.after', text, options, globals);
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|