12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /**
- * jquery.linky.js v0.1.8
- * https://github.com/AnSavvides/jquery.linky
- * The MIT License (MIT)
- *
- * Copyright (c) 2013 - 2015 Andreas Savvides
- */
- (function($) {
- "use strict";
- $.fn.linky = function(options) {
- return this.each((idx, elm) => {
- var $el = $(this),
- linkifiedContent = _linkify($el, options);
- $el.html(linkifiedContent);
- });
- };
- function _linkify($el, options) {
- var links = {
- twitter: {
- baseUrl: "https://twitter.com/",
- hashtagSearchUrl: "hashtag/"
- },
- instagram: {
- baseUrl: "http://instagram.com/",
- hashtagSearchUrl: null // Doesn't look like there is one?
- },
- github: {
- baseUrl: "https://github.com/",
- hashtagSearchUrl: null
- }
- },
- defaultOptions = {
- mentions: false,
- hashtags: false,
- urls: true,
- linkTo: "twitter" // Let's default to Twitter
- },
- extendedOptions = $.extend(defaultOptions, options),
- elContent = $el.html(),
- // Regular expression courtesy of Matthew O'Riordan, see: http://goo.gl/3syEKK
- urlRegEx = /((?:[A-Za-z]{3,9}:(?:\/\/?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*)?)/,
- matches;
- // Linkifying URLs
- if (extendedOptions.urls) {
- elContent = _linkifyUrls(elContent, urlRegEx);
- }
- // Linkifying mentions
- if (extendedOptions.mentions) {
- elContent = _linkifyMentions(elContent, links[extendedOptions.linkTo].baseUrl);
- }
- // Linkifying hashtags
- if (extendedOptions.hashtags) {
- elContent = _linkifyHashtags(elContent, links[extendedOptions.linkTo]);
- }
- return elContent;
- }
- function _linkifyUrls(elContent, re) {
- let result = "";
- for (let fragment of elContent.split(re)) {
- if (re.test(fragment)) {
- let addr = fragment.includes("@")?"mailto:" + fragment:fragment;
- result += `<a href="${addr}" target="_blank">${fragment}</a>`;
- } else {
- result += fragment;
- }
- }
- return result;
- }
- // Find any mentions (e.g. @andrs) and turn them into links that
- // refer to the appropriate social profile (e.g. twitter or instagram).
- function _linkifyMentions(text, baseUrl) {
- return text.replace(/(^|\s|\(|>)@(\w+)/g, "$1<a href='" + baseUrl + "$2' target='_blank'>@$2</a>");
- }
- // Find any hashtags (e.g. #linkyrocks) and turn them into links that refer
- // to the appropriate social profile.
- function _linkifyHashtags(text, links) {
- // If there is no search URL for a hashtag, there isn't much we can do
- if (links.hashtagSearchUrl === null) return text;
- return text.replace(/(^|\s|\(|>)#((\w|[\u00A1-\uFFFF])+)/g, "$1<a href='" + links.baseUrl + links.hashtagSearchUrl + "$2' target='_blank'>#$2</a>");
- }
- }(jQuery));
|