scottcorgan / angular-linkify

Angular filter, directive, and service to linkify text.
MIT License
82 stars 29 forks source link

Problem escaping special characters to url entities #15

Open luccasmaso opened 9 years ago

luccasmaso commented 9 years ago

Given the url to linkify:

https://www.site.com/someimage.jpg?var=1&option=1

The href attribute for the <a> tag has some characters (&) escaped to html entities.

https://www.site.com/someimage.jpg?var=1&amp;option=1

This way the url cannot be achieved. I suggest unescaping it back when adding the attribute do the tag.

scottcorgan commented 9 years ago

Want to submit a PR?

luccasmaso commented 9 years ago

Actually I realize a simpler solution for my case, and created own directive. The regex is kind of different and more like the facebook one's

appDirectives.directive('linkify', ['$timeout', function($timeout){
  return {
    restrict: 'A',
    replace: true,
    link: function(scope, element, attrs){
      var linkify = function(text){
        if (text) {
          text = text.replace(
            /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,
            function(url){
              var full_url = url;
              if (!full_url.match('^https?:\/\/')) {
                full_url = 'http://' + full_url;
              }
              return '<a target="_blank" href="' + full_url + '">' + url + '</a>';
            }
          );
        }
        return text;
      };

      $timeout(function() {
        element.html(linkify(element.text()));
      });
    }
  };
}])
scottcorgan commented 9 years ago

Would this be useful in this directive? Should we use that regex?