Created
February 22, 2013 17:47
-
-
Save jsoa/5015255 to your computer and use it in GitHub Desktop.
Tweetify angular filter. linkifies links, mentions and hashtags REF: https://gist.github.com/furf/2415595 Slightly changed tag regex so - (dashes) are not matched and changed the tag link to use ?q=%23
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
app = angular.module 'myApp', [] | |
tweetify = (-> | |
linkToUrl = (url) -> | |
url.link url | |
linkToUser = (match, atUser, user) -> | |
atUser.link twitterUrl + "/#!/" + user | |
linkToTag = (match, hashTag, tag) -> | |
hashTag.link twitterUrl + "/search/?q=%23" + tag | |
twitterUrl = "https://twitter.com" | |
urlRegExp = /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g | |
atUserRegExp = /(@([A-Za-z0-9-_]+))/g | |
hashTagRegExp = /(#([A-Za-z0-9_]+))/g | |
(str) -> | |
str.replace(urlRegExp, linkToUrl) | |
.replace(atUserRegExp, linkToUser) | |
.replace(hashTagRegExp, linkToTag) | |
)() | |
app.filter 'tweetify', -> | |
(text)-> | |
tweetify(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
var app = angular.module('myApp', []); | |
var tweetify = (function() { | |
var linkToUrl = function(url) { | |
return url.link(url); | |
}; | |
var linkToUser = function(match, atUser, user) { | |
return atUser.link(twitterUrl + "/#!/" + user); | |
}; | |
var linkToTag = function(match, hashTag, tag) { | |
return hashTag.link(twitterUrl + "/search/?q=%23" + tag); | |
}; | |
var twitterUrl = "https://twitter.com"; | |
var urlRegExp = /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g; | |
var atUserRegExp = /(@([A-Za-z0-9-_]+))/g; | |
var hashTagRegExp = /(#([A-Za-z0-9_]+))/g; | |
return function(str) { | |
return str.replace(urlRegExp, linkToUrl) | |
.replace(atUserRegExp, linkToUser) | |
.replace(hashTagRegExp, linkToTag); | |
}; | |
})(); | |
app.filter('tweetify', function() { | |
return function(text) { | |
return tweetify(text); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment