autoLinks.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. showdown.subParser('autoLinks', function (text, options, globals) {
  2. 'use strict';
  3. text = globals.converter._dispatch('autoLinks.before', text, options, globals);
  4. var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)()(?=\s|$)(?!["<>])/gi,
  5. simpleURLRegex2 = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+?)([.!?()]?)(?=\s|$)(?!["<>])/gi,
  6. delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)>/gi,
  7. simpleMailRegex = /(?:^|\s)([A-Za-z0-9!#$%&'*+-/=?^_`{|}~.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?:$|\s)/gi,
  8. delimMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;
  9. text = text.replace(delimUrlRegex, replaceLink);
  10. text = text.replace(delimMailRegex, replaceMail);
  11. // simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[-.+~:?#@!$&'()*,;=[\]\w]+)\b/gi,
  12. // Email addresses: <address@domain.foo>
  13. if (options.simplifiedAutoLink) {
  14. if (options.excludeTrailingPunctuationFromURLs) {
  15. text = text.replace(simpleURLRegex2, replaceLink);
  16. } else {
  17. text = text.replace(simpleURLRegex, replaceLink);
  18. }
  19. text = text.replace(simpleMailRegex, replaceMail);
  20. }
  21. function replaceLink(wm, link, m2, m3, trailingPunctuation) {
  22. var lnkTxt = link,
  23. append = '';
  24. if (/^www\./i.test(link)) {
  25. link = link.replace(/^www\./i, 'http://www.');
  26. }
  27. if (options.excludeTrailingPunctuationFromURLs && trailingPunctuation) {
  28. append = trailingPunctuation;
  29. }
  30. return '<a href="' + link + '">' + lnkTxt + '</a>' + append;
  31. }
  32. function replaceMail(wholeMatch, mail) {
  33. var href = 'mailto:';
  34. mail = showdown.subParser('unescapeSpecialChars')(mail);
  35. if (options.encodeEmails) {
  36. mail = showdown.helper.encodeEmailAddress(mail);
  37. href = showdown.helper.encodeEmailAddress(href + mail);
  38. } else {
  39. href = href + mail;
  40. }
  41. return '<a href="' + href + '">' + mail + '</a>';
  42. }
  43. text = globals.converter._dispatch('autoLinks.after', text, options, globals);
  44. return text;
  45. });