jquery.linky.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * jquery.linky.js v0.1.8
  3. * https://github.com/AnSavvides/jquery.linky
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013 - 2015 Andreas Savvides
  7. */
  8. (function($) {
  9. "use strict";
  10. $.fn.linky = function(options) {
  11. return this.each((idx, elm) => {
  12. var $el = $(this),
  13. linkifiedContent = _linkify($el, options);
  14. $el.html(linkifiedContent);
  15. });
  16. };
  17. function _linkify($el, options) {
  18. var links = {
  19. twitter: {
  20. baseUrl: "https://twitter.com/",
  21. hashtagSearchUrl: "hashtag/"
  22. },
  23. instagram: {
  24. baseUrl: "http://instagram.com/",
  25. hashtagSearchUrl: null // Doesn't look like there is one?
  26. },
  27. github: {
  28. baseUrl: "https://github.com/",
  29. hashtagSearchUrl: null
  30. }
  31. },
  32. defaultOptions = {
  33. mentions: false,
  34. hashtags: false,
  35. urls: true,
  36. linkTo: "twitter" // Let's default to Twitter
  37. },
  38. extendedOptions = $.extend(defaultOptions, options),
  39. elContent = $el.html(),
  40. // Regular expression courtesy of Matthew O'Riordan, see: http://goo.gl/3syEKK
  41. urlRegEx = /((?:[A-Za-z]{3,9}:(?:\/\/?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*)?)/,
  42. matches;
  43. // Linkifying URLs
  44. if (extendedOptions.urls) {
  45. elContent = _linkifyUrls(elContent, urlRegEx);
  46. }
  47. // Linkifying mentions
  48. if (extendedOptions.mentions) {
  49. elContent = _linkifyMentions(elContent, links[extendedOptions.linkTo].baseUrl);
  50. }
  51. // Linkifying hashtags
  52. if (extendedOptions.hashtags) {
  53. elContent = _linkifyHashtags(elContent, links[extendedOptions.linkTo]);
  54. }
  55. return elContent;
  56. }
  57. function _linkifyUrls(elContent, re) {
  58. let result = "";
  59. for (let fragment of elContent.split(re)) {
  60. if (re.test(fragment)) {
  61. let addr = fragment.includes("@")?"mailto:" + fragment:fragment;
  62. result += `<a href="${addr}" target="_blank">${fragment}</a>`;
  63. } else {
  64. result += fragment;
  65. }
  66. }
  67. return result;
  68. }
  69. // Find any mentions (e.g. @andrs) and turn them into links that
  70. // refer to the appropriate social profile (e.g. twitter or instagram).
  71. function _linkifyMentions(text, baseUrl) {
  72. return text.replace(/(^|\s|\(|>)@(\w+)/g, "$1<a href='" + baseUrl + "$2' target='_blank'>@$2</a>");
  73. }
  74. // Find any hashtags (e.g. #linkyrocks) and turn them into links that refer
  75. // to the appropriate social profile.
  76. function _linkifyHashtags(text, links) {
  77. // If there is no search URL for a hashtag, there isn't much we can do
  78. if (links.hashtagSearchUrl === null) return text;
  79. return text.replace(/(^|\s|\(|>)#((\w|[\u00A1-\uFFFF])+)/g, "$1<a href='" + links.baseUrl + links.hashtagSearchUrl + "$2' target='_blank'>#$2</a>");
  80. }
  81. }(jQuery));