Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 915a891

Browse files
chaseflemingcaitp
authored andcommitted
fix(linky): make urls starting with www. links, like markdown
It's super cool! Closes #10290
1 parent 8df47db commit 915a891

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/ngSanitize/filter/linky.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
*/
105105
angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
106106
var LINKY_URL_REGEXP =
107-
/((ftp|https?):\/\/|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"]/,
107+
/((ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"]/,
108108
MAILTO_REGEXP = /^mailto:/;
109109

110110
return function(text, target) {
@@ -117,8 +117,10 @@ angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) {
117117
while ((match = raw.match(LINKY_URL_REGEXP))) {
118118
// We can not end in these as they are sometimes found at the end of the sentence
119119
url = match[0];
120-
// if we did not match ftp/http/mailto then assume mailto
121-
if (match[2] == match[3]) url = 'mailto:' + url;
120+
// if we did not match ftp/http/www/mailto then assume mailto
121+
if (!match[2] && !match[4]) {
122+
url = (match[3] ? 'http://' : 'mailto:') + url;
123+
}
122124
i = match.index;
123125
addText(raw.substr(0, i));
124126
addLink(url, match[0].replace(MAILTO_REGEXP, ''));

test/ngSanitize/filter/linkySpec.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ describe('linky', function() {
2020
expect(linky(undefined)).not.toBeDefined();
2121
});
2222

23+
it('should handle www.', function() {
24+
expect(linky('www.example.com')).toEqual('<a href="http://www.example.com">www.example.com</a>');
25+
});
26+
2327
it('should handle mailto:', function() {
2428
expect(linky("mailto:me@example.com")).
2529
toEqual('<a href="mailto:me@example.com">me@example.com</a>');

0 commit comments

Comments
 (0)