Skip to content

Commit 263975e

Browse files
committed
Added github support and linkify as a service.
1 parent 824fd49 commit 263975e

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# angular-linkify
22

3-
Angular filter and directive to linkify text. As of **v0.2.1**, angular-linkify works for twitter mentions, hashtags, and basic urls. Github support is COMING SOON!
3+
Angular filter, directive, and service to linkify text. As of **v0.3.0**, angular-linkify works for twitter/github mentions, twitter hashtags, and basic urls.
44

55
## Install
66

@@ -16,7 +16,7 @@ Inject module into your application
1616
angular.module('YourApp', ['linkify']);
1717
```
1818

19-
User as a normal [AngularJS Filter](http://docs.angularjs.org/guide/dev_guide.templates.filters.using_filters) or [AngularJS Directive](http://docs.angularjs.org/guide/directive)
19+
User as a [AngularJS Filter](http://docs.angularjs.org/guide/dev_guide.templates.filters.using_filters) or [AngularJS Directive](http://docs.angularjs.org/guide/directive)
2020

2121
```html
2222
<!-- As a filter -->
@@ -36,9 +36,22 @@ User as a normal [AngularJS Filter](http://docs.angularjs.org/guide/dev_guide.te
3636
<div ng-bind="someModel" linkify="twitter"></div>
3737
```
3838

39-
## Todo
39+
User as a service
4040

41-
* add Github support
41+
```javascript
42+
// Injected into controller
43+
angular.module('someModule').controller('SomeCtrl', function ($scope.linkify) {
44+
var text = "@scottcorgan and http://github.com";
45+
46+
// Twitter
47+
$scope.text = linkify.twitter(text);
48+
49+
// Github
50+
$scope.text = linkify.github(text);
51+
52+
});
53+
54+
```
4255

4356
## Build
4457

angular-linkify.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ angular.module('linkify')
55
'use strict';
66

77
function linkify (_str, type) {
8-
var tweet = _str.replace( /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+(?![^\s]*?")([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/ig, function(url) {
8+
var _text = _str.replace( /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+(?![^\s]*?")([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/ig, function(url) {
99
var wrap = document.createElement('div');
1010
var anch = document.createElement('a');
1111
anch.href = url;
@@ -17,32 +17,46 @@ angular.module('linkify')
1717

1818
// Twitter
1919
if (type === 'twitter') {
20-
tweet = tweet.replace(/(|\s)*@(\w+)/g, '$1<a href="https://www.twitter.com/$2" target="_blank">@$2</a>');
21-
tweet = tweet.replace(/(^|\s)*#(\w+)/g, '$1<a href="https://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
20+
_text = _text.replace(/(|\s)*@(\w+)/g, '$1<a href="https://twitter.com/$2" target="_blank">@$2</a>');
21+
_text = _text.replace(/(^|\s)*#(\w+)/g, '$1<a href="https://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
2222
}
2323

2424
// Github
2525
if (type === 'github') {
26-
26+
_text = _text.replace(/(|\s)*@(\w+)/g, '$1<a href="https://github.com/$2" target="_blank">@$2</a>');
2727
}
2828

29-
return tweet;
29+
return _text;
3030
}
3131

3232
//
3333
return function (text, type) {
34-
return linkify(text, type);
34+
return linkify(text, type);
3535
};
3636
})
37-
.directive('linkify', function ($filter, $timeout) {
37+
.factory('linkify', function ($filter) {
38+
'use strict';
39+
40+
function _linkifyAsType (type) {
41+
return function (str) {(type, str);
42+
return $filter('linkify')(str, type);
43+
};
44+
}
45+
46+
return {
47+
twitter: _linkifyAsType('twitter'),
48+
github: _linkifyAsType('github'),
49+
normal: _linkifyAsType()
50+
};
51+
})
52+
.directive('linkify', function ($filter, $timeout, linkify) {
53+
'use strict';
54+
3855
return {
3956
restrict: 'A',
4057
link: function (scope, element, attrs) {
41-
var type = attrs.linkify;
42-
43-
$timeout(function () {
44-
element.html($filter('linkify')(element.html(), type));
45-
});
58+
var type = attrs.linkify || 'normal';
59+
$timeout(function () { element.html(linkify[type](element.html())); });
4660
}
4761
};
4862
});

angular-linkify.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-linkify",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"main": "angular-linkify.js",
55
"description": "Angular filter to linkify urls, \"@\" usernames, and hashtags.",
66
"ignore": [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-linkify",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "Angular filter to linkify urls, \"@\" usernames, and hashtags.",
55
"main": "angular-linkify.js",
66
"directories": {

0 commit comments

Comments
 (0)