-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram-formatter.js
52 lines (42 loc) · 1.22 KB
/
instagram-formatter.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
/**
* Tweet Formatter
*
* Format tweet text to proper html link tag on username, hashtag, and URL
* eg: @pveyes to <a href="http://twitter.com/pveyes">@pveyes</a>
*/
(function () {
'use strict';
function tweetFormatter (tweet) {
function formatURL (tweet) {
// ref: http://stackoverflow.com/questions/19625183/js-find-urls-in-text-make-links
return tweet.replace(
/((http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?)/g,
'<a href="$1">$1</a>'
);
};
function formatUsername (tweet) {
// ref: http://stackoverflow.com/questions/5973187/parsing-twitter-name-with-regex-and-javascript
return tweet.replace(
/\B@([\w-]+)/gm,
'<a href="http://instagram.com/$1">@$1</a>'
);
};
function formatHashtag (tweet) {
return tweet.replace(
/\B#([\w-]+)/gm,
'<a href="https://twitter.com/search?q=%23$1&src=hash">#$1</a>'
);
};
// Format URL first to prevent double format
tweet = formatURL(tweet);
// then format username and hashtag
tweet = formatUsername(tweet);
tweet = formatHashtag(tweet);
return tweet;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = tweetFormatter;
} else {
window.tweetFormatter = tweetFormatter;
}
})();