Last active
September 23, 2015 15:17
-
-
Save jack-pallot/be77272290190d95bba3 to your computer and use it in GitHub Desktop.
Creates hyperlinks for elements returned from the Twitter API in plain text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// call function | |
$(document).ready(function() { | |
var twitterFeed = TWITTER_PARSE_LINKS("#js-twitterFeed"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var TWITTER_PARSE_LINKS = function(el) { | |
var stripSpecialChars = function stripSpecialChars(text) { | |
return text.replace(/[^A-z\s\d][\\\^]?/, ""); | |
}; | |
var findTwitterUrl = function findTwitterUrl(text) { | |
var regexPattern = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; | |
return text.replace(regexPattern, function(url) { | |
return '<a target="_blank" href="' + url + '">' + url + '</a>'; | |
}); | |
}; | |
var findTwitterMentions = function findTwitterUrl(text) { | |
var regexPattern = /\B@(?!\-)[a-zA-Z0-9\-]*(?![^<]*<\/a>)/ig; | |
return text.replace(regexPattern, function(url) { | |
var urlNoCharacters = stripSpecialChars(url); | |
return '<a target="_blank" href="http://twitter.com/' + urlNoCharacters + '">' + url + '</a>'; | |
}); | |
}; | |
var findTwitterHashtags = function findTwitterHashtags(text) { | |
var regexPattern = /\B#([a-z0-9]{2,})(?![~!@#$%^&*?=+_`\-\|\/'\[\]\{\}]*\w)/ig; | |
return text.replace(regexPattern, function(url) { | |
var urlNoCharacters = stripSpecialChars(url); | |
return '<a target="_blank" href="http://twitter.com/hashtag/' + urlNoCharacters + '">' + url + '</a>'; | |
}); | |
}; | |
$(el).each(function() { | |
this.innerHTML = findTwitterUrl(this.innerHTML); | |
this.innerHTML = findTwitterMentions(this.innerHTML); | |
this.innerHTML = findTwitterHashtags(this.innerHTML); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var TWITTER_PARSE_LINKS=function(t){var n=function(t){return t.replace(/[^A-z\s\d][\\\^]?/,"")},r=function(t){var n=/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;return t.replace(n,function(t){return'<a target="_blank" href="'+t+'">'+t+"</a>"})},e=function(t){var r=/\B@(?!\-)[a-zA-Z0-9\-]*(?![^<]*<\/a>)/gi;return t.replace(r,function(t){var r=n(t);return'<a target="_blank" href="http://twitter.com/'+r+'">'+t+"</a>"})},a=function(t){var r=/\B#([a-z0-9]{2,})(?![~!@#$%^&*?=+_`\-\|\/'\[\]\{\}]*\w)/gi;return t.replace(r,function(t){var r=n(t);return'<a target="_blank" href="http://twitter.com/hashtag/'+r+'">'+t+"</a>"})};$(t).each(function(){this.innerHTML=r(this.innerHTML),this.innerHTML=e(this.innerHTML),this.innerHTML=a(this.innerHTML)})}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment