Last active
June 2, 2016 23:11
-
-
Save SeanPlusPlus/dfa9629eaacf75c2235561dd0b436dca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 'use strict'; | |
| // <p ng-bind-html="mytext | linkify | trustHTML"></p> | |
| angular.module('myApp.filters', []). | |
| filter('trustHTML', ['$sce', function ($sce) { | |
| return function (text) { | |
| return $sce.trustAsHtml(text) | |
| } | |
| }]). | |
| filter('linkify', [function () { | |
| var GRUBERS_URL_RE = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i | |
| var HAS_PROTOCOL = /^[a-z][\w-]+:/ | |
| function wordToURL (word, index, array) { | |
| var m = word.match(GRUBERS_URL_RE) | |
| var result = word, url, escapedURL, text | |
| if (m) { | |
| text = m[1] | |
| url = HAS_PROTOCOL.test(text) ? text : 'http://' + text | |
| result = result.replace(text, '<a href="' + url + '">' + text + '</a>') | |
| } | |
| return result | |
| } | |
| var map = Array.prototype.map ? function (arr, callback) { | |
| return arr.map(callback) | |
| } : function (arr, callback) { | |
| var arr2 = [], i, l | |
| for (i = 0, l = arr.length; i < l; i = i + 1) { | |
| arr2[i] = callback(arr[i], i, arr) | |
| } | |
| return arr2 | |
| } | |
| return function (text) { | |
| if (!text) return '' | |
| return map(text.split(/\s+/), wordToURL).join(' ') | |
| } | |
| }]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment