Skip to content

Instantly share code, notes, and snippets.

@dmadisetti
Forked from jakemmarsh/filters.js
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save dmadisetti/20c17e0ec8d1e834010f to your computer and use it in GitHub Desktop.

Select an option

Save dmadisetti/20c17e0ec8d1e834010f to your computer and use it in GitHub Desktop.

Revisions

  1. dmadisetti revised this gist Dec 21, 2014. 1 changed file with 15 additions and 19 deletions.
    34 changes: 15 additions & 19 deletions filters.js
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,18 @@
    app.filter('parseUrl', function() {
    var //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim,
    //Change email addresses to mailto:: links.
    replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;

    return function(text, target, otherProp) {
    angular.forEach(text.match(replacePattern1), function(url) {
    text = text.replace(replacePattern1, "<a href=\"$1\" target=\"_blank\">$1</a>");
    });
    angular.forEach(text.match(replacePattern2), function(url) {
    text = text.replace(replacePattern2, "$1<a href=\"http://$2\" target=\"_blank\">$2</a>");
    });
    angular.forEach(text.match(replacePattern3), function(url) {
    text = text.replace(replacePattern3, "<a href=\"mailto:$1\">$1</a>");
    });

    return text;
    };
    });
    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim,
    //Change email addresses to mailto:: links.
    replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;

    return function(text, target, otherProp) {
    if(!text) return;

    text = text.replace(replacePattern1, "<a href=\"$1\" target=\"_blank\">$1</a>");
    text = text.replace(replacePattern2, "$1<a href=\"http://$2\" target=\"_blank\">$2</a>");
    text = text.replace(replacePattern3, "<a href=\"mailto:$1\">$1</a>");

    return text;
    };
    });
  2. @jakemmarsh jakemmarsh created this gist Jul 16, 2013.
    22 changes: 22 additions & 0 deletions filters.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    app.filter('parseUrl', function() {
    var //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim,
    //Change email addresses to mailto:: links.
    replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;

    return function(text, target, otherProp) {
    angular.forEach(text.match(replacePattern1), function(url) {
    text = text.replace(replacePattern1, "<a href=\"$1\" target=\"_blank\">$1</a>");
    });
    angular.forEach(text.match(replacePattern2), function(url) {
    text = text.replace(replacePattern2, "$1<a href=\"http://$2\" target=\"_blank\">$2</a>");
    });
    angular.forEach(text.match(replacePattern3), function(url) {
    text = text.replace(replacePattern3, "<a href=\"mailto:$1\">$1</a>");
    });

    return text;
    };
    });