Skip to content

Instantly share code, notes, and snippets.

@rootux
Forked from rishabhmhjn/tweetLinky.js
Created May 1, 2017 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rootux/93a705158f677e8cbfc35573d2742d19 to your computer and use it in GitHub Desktop.
Save rootux/93a705158f677e8cbfc35573d2742d19 to your computer and use it in GitHub Desktop.
This is an AngularJS filter to linkify #hashtags and @mention texts into respective Twitter URLsDemo - http://plnkr.co/edit/vrdgxU?p=preview
/* Based on https://gist.github.com/rishabhmhjn/7028079 */
function HashtagLinky($filter, $sce) {
'ngInject'
return function(text, target) {
if (!text) return text;
var replacedText = $filter('linky')(text, target);
var targetAttr = "";
if (angular.isDefined(target)) {
targetAttr = ' target="' + target + '"';
}
// replace #hashtags
var replacePattern1 = /(^|\s)#(\w*[a-zA-Z_]+\w*)/gim;
replacedText = replacedText.replace(replacePattern1, '$1<a href="https://twitter.com/search?q=%23$2"' + targetAttr + '>#$2</a>');
$sce.trustAsHtml(replacedText);
return replacedText;
};
}
export default HashtagLinky;
@rootux
Copy link
Author

rootux commented May 1, 2017

This is the modern Angular 1.5 version of this filter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment