Find links without www (or other) prefix
stinoga opened this issue · 6 comments
Hey Scott,
First off, thanks for this filter! It's just what I was looking for.
Is there a way to get this filter to find simple non-prefixed urls?
Ex:
This is a link www.rob.com, This isn't rob.com. This is though http://rob.com. Is it possible to linkify all three?
Thanks,
_Rob
Should be a simple change to the regex. Up for a PR?
I'd be game for that. Lemme see if I can figure it out. I'm not exactly a regex guru :)
Cool. Should only have to mess with this: https://github.com/scottcorgan/angular-linkify/blob/master/angular-linkify.js#L12
Actually, I got it working through updating the filter rather than the regex:
filter('linkify', function() {
var replacePattern1 = /(\b(?:http|https):\/\/)?([-a-zA-Z0-9.]{2,256}\.[a-z]{2,4})\b(?:\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
return function(text, target, otherProp) {
text = text.replace(replacePattern1, "<a href=\"http://$2\" target=\"_blank\">$2</a>");
return text;
};
});
This worked for rob.com, www.rob.com, and http://rob.com.
I needed an email pattern as well, so I ended up doing this for my url/email parsing filter. Not sure if PR helps you, since I changed most of the pattern.
filter('linkify', function() {
//URLs starting with http://, https://, or ftp://
var replacePattern1 = /(@)?(((?:http|https):\/\/)?[\da-z.-]+\.(com|net|edu|org|gov|info))\b(?:\/[-a-zA-Z0-9@:%_\+.~#?;&//=]*)?/gi,
emailPattern = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
return function(text, target, otherProp) {
// Convert email addresses to mailto links
text = text.replace(emailPattern, '<a href="mailto:$1">$1</a>');
// Convert urls to anchor tags
return text.replace(replacePattern1, function($0, $1, $2, $3) {
var fullUrl = $0;
// If the http(s) substring is missing, add it
// This ensures links are outbound
if (!$3) {
fullUrl = 'http://' + $0;
}
// If the @ substring is there, this is an email and return it
// It not, format the link
return $1 ? $0 : '<a href="' + fullUrl + '" target="_blank">' + $0 + '</a>';
});
};
});
,,,
@stinoga can you submit a pull request?