Skip to content

Instantly share code, notes, and snippets.

@SeanPlusPlus
Last active June 2, 2016 23:11
Show Gist options
  • Select an option

  • Save SeanPlusPlus/dfa9629eaacf75c2235561dd0b436dca to your computer and use it in GitHub Desktop.

Select an option

Save SeanPlusPlus/dfa9629eaacf75c2235561dd0b436dca to your computer and use it in GitHub Desktop.
'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