showdown/src/subParsers/autoLinks.js

47 lines
1.8 KiB
JavaScript
Raw Normal View History

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
text = globals.converter._dispatch('autoLinks.before', text, options, globals);
2015-01-16 05:21:33 +08:00
var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)()(?=\s|$)(?!["<>])/gi,
simpleURLRegex2 = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+?)([.!?()]?)(?=\s|$)(?!["<>])/gi,
delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)>/gi,
simpleMailRegex = /(?:^|\s)([A-Za-z0-9!#$%&'*+-/=?^_`{|}~.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?:$|\s)/gi,
delimMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;
text = text.replace(delimUrlRegex, replaceLink);
text = text.replace(delimMailRegex, replaceMail);
// 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
if (options.simplifiedAutoLink) {
if (options.excludeTrailingPunctuationFromURLs) {
text = text.replace(simpleURLRegex2, replaceLink);
} else {
text = text.replace(simpleURLRegex, replaceLink);
}
text = text.replace(simpleMailRegex, replaceMail);
}
function replaceLink(wm, link, m2, m3, trailingPunctuation) {
var lnkTxt = link,
append = '';
if (/^www\./i.test(link)) {
link = link.replace(/^www\./i, 'http://www.');
}
if (options.excludeTrailingPunctuationFromURLs && trailingPunctuation) {
append = trailingPunctuation;
}
return '<a href="' + link + '">' + lnkTxt + '</a>' + append;
}
function replaceMail(wholeMatch, mail) {
var unescapedStr = showdown.subParser('unescapeSpecialChars')(mail);
2015-01-19 19:37:21 +08:00
return showdown.subParser('encodeEmailAddress')(unescapedStr);
}
2015-01-16 05:21:33 +08:00
text = globals.converter._dispatch('autoLinks.after', text, options, globals);
2015-01-19 19:37:21 +08:00
return text;
2015-01-16 05:21:33 +08:00
});