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
|
|
|
|
2016-12-01 23:25:46 +08:00
|
|
|
var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)()(?=\s|$)(?!["<>])/gi,
|
|
|
|
simpleURLRegex2 = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+?)([.!?()]?)(?=\s|$)(?!["<>])/gi,
|
2015-07-11 09:42:53 +08:00
|
|
|
delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)>/gi,
|
2017-01-28 03:25:46 +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;
|
2015-07-11 09:42:53 +08:00
|
|
|
|
2016-08-20 02:12:25 +08:00
|
|
|
text = text.replace(delimUrlRegex, replaceLink);
|
2015-07-11 09:42:53 +08:00
|
|
|
text = text.replace(delimMailRegex, replaceMail);
|
2016-08-20 02:12:25 +08:00
|
|
|
// simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi,
|
2015-01-19 19:37:21 +08:00
|
|
|
// Email addresses: <address@domain.foo>
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-07-11 09:42:53 +08:00
|
|
|
if (options.simplifiedAutoLink) {
|
2016-12-01 23:25:46 +08:00
|
|
|
if (options.excludeTrailingPunctuationFromURLs) {
|
|
|
|
text = text.replace(simpleURLRegex2, replaceLink);
|
|
|
|
} else {
|
|
|
|
text = text.replace(simpleURLRegex, replaceLink);
|
|
|
|
}
|
2015-08-11 12:57:52 +08:00
|
|
|
text = text.replace(simpleMailRegex, replaceMail);
|
2015-07-11 09:42:53 +08:00
|
|
|
}
|
|
|
|
|
2016-12-01 23:25:46 +08:00
|
|
|
function replaceLink(wm, link, m2, m3, trailingPunctuation) {
|
|
|
|
var lnkTxt = link,
|
|
|
|
append = '';
|
2016-08-20 02:12:25 +08:00
|
|
|
if (/^www\./i.test(link)) {
|
|
|
|
link = link.replace(/^www\./i, 'http://www.');
|
|
|
|
}
|
2016-12-01 23:25:46 +08:00
|
|
|
if (options.excludeTrailingPunctuationFromURLs && trailingPunctuation) {
|
|
|
|
append = trailingPunctuation;
|
|
|
|
}
|
|
|
|
return '<a href="' + link + '">' + lnkTxt + '</a>' + append;
|
2016-08-20 02:12:25 +08:00
|
|
|
}
|
|
|
|
|
2017-01-28 03:25:46 +08:00
|
|
|
function replaceMail(wholeMatch, b, mail) {
|
2017-01-28 03:03:37 +08:00
|
|
|
var href = 'mailto:';
|
2017-01-28 03:25:46 +08:00
|
|
|
b = b || '';
|
2017-01-28 03:03:37 +08:00
|
|
|
mail = showdown.subParser('unescapeSpecialChars')(mail);
|
|
|
|
if (options.encodeEmails) {
|
|
|
|
mail = showdown.helper.encodeEmailAddress(mail);
|
|
|
|
href = showdown.helper.encodeEmailAddress(href + mail);
|
|
|
|
} else {
|
|
|
|
href = href + mail;
|
|
|
|
}
|
2017-01-28 03:25:46 +08:00
|
|
|
return b + '<a href="' + href + '">' + mail + '</a>';
|
2015-07-11 09:42:53 +08:00
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2016-03-21 01:08:44 +08:00
|
|
|
text = globals.converter._dispatch('autoLinks.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
|
|
|
});
|