diff --git a/src/_filter/string/linkify.js b/src/_filter/string/linkify.js new file mode 100644 index 0000000..b9c3529 --- /dev/null +++ b/src/_filter/string/linkify.js @@ -0,0 +1,24 @@ +/** + * @ngdoc filter + * @name linkify + * @kind function + * + * @description + * takes string/html and scans for any links and convert them to links. + * Supports converting to ftp, mails and normal url links. + */ + +angular.module('a8m.linkify', []) + + .filter('linkify',[ function () { + return function (input) { + + var stringToBeReturn = ''; + + // replace for url links and ftp links + stringToBeReturn = input.replace(/((?:ht|f)tps?:\/?\/?([^\s|^\<]+))/g, "$2"); + + // replace for email links + return stringToBeReturn.replace(/(?:([^\s|^\>]+\@[0-9a-zA-Z\_]+\.[^\s|^\<]+))/g, "$1"); + } + }]); diff --git a/src/filters.js b/src/filters.js index 7d69e27..947cf66 100644 --- a/src/filters.js +++ b/src/filters.js @@ -21,6 +21,7 @@ angular.module('angular.filter', [ 'a8m.ltrim', 'a8m.rtrim', 'a8m.repeat', + 'a8m.linkify', 'a8m.to-array', 'a8m.concat', diff --git a/test/spec/filter/string/linkify.js b/test/spec/filter/string/linkify.js new file mode 100644 index 0000000..1d55fb7 --- /dev/null +++ b/test/spec/filter/string/linkify.js @@ -0,0 +1,47 @@ +'use strict'; + +describe('linkifyFilter', function () { + + var filter; + + beforeEach(module('a8m.linkify')); + + beforeEach(inject(function ($filter) { + filter = $filter('linkify'); + })); + + it('should return proper format for domain url either https or not, and either with tags or space before/after', function() { + + expect(filter('http://domain.com')).toEqual("domain.com"); + expect(filter('http://www.domain.com')).toEqual("www.domain.com"); + expect(filter('https://www.domain.com')).toEqual("www.domain.com"); + expect(filter('
some tags before
http://www.domain.com some txt')).toEqual("some tags before
www.domain.com some txt"); + expect(filter('some text http://www.domain.comsome tags after
')).toEqual("some text www.domain.comsome tags after
"); + + }); + + it('should return proper format for ftp url either https or not, and either with tags or space before/after', function() { + + expect(filter('ftp://domain.com')).toEqual("domain.com"); + expect(filter('ftp://www.domain.com')).toEqual("www.domain.com"); + expect(filter('ftps://www.domain.com')).toEqual("www.domain.com"); + expect(filter('some tags before
ftps://www.domain.com some txt')).toEqual("some tags before
www.domain.com some txt"); + expect(filter('some text ftps://www.domain.comsome tags after
')).toEqual("some text www.domain.comsome tags after
"); + + }); + + it('should return proper format for mail url', function() { + + expect(filter('email@domain.com')).toEqual("email@domain.com"); + + }); + + it('should return proper format for mail url even for space or with tags', function() { + + var textToBeScan = "someText here email@domain.comsomeTag
", + textToBeReturned = "someText here email@domain.comsomeTag
"; + + expect(filter(textToBeScan)).toEqual(textToBeReturned); + + }); +});