-
Notifications
You must be signed in to change notification settings - Fork 18
/
jquery.linky.js
99 lines (85 loc) · 3.51 KB
/
jquery.linky.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* 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(function() {
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]*))?)/g,
matches;
// Linkifying URLs
if (extendedOptions.urls) {
matches = elContent.match(urlRegEx);
if (matches) {
elContent = _linkifyUrls(matches, $el);
}
}
// 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;
}
// For any URLs present, unless they are already identified within
// an `a` element, linkify them.
function _linkifyUrls(matches, $el) {
var elContent = $el.html();
$.each(matches, function() {
// Only linkify URLs that are not already identified as
// `a` elements with an `href`.
if ($el.find("a[href='" + this + "']").length === 0) {
elContent = elContent.replace(this, "<a href='" + this + "' target='_blank'>" + this + "</a>");
}
});
return elContent;
}
// 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));