Merge pull request #1279 from txdv/entire-words-as-links

convert entire words as urls or do nothing
This commit is contained in:
Lorenz Hübschle 2024-04-26 18:16:22 +02:00 committed by GitHub
commit f39fe804b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 15 deletions

View File

@ -68,20 +68,30 @@ weechat.filter('conditionalLinkify', ['$filter', function($filter) {
return text;
}
return linkifyStr(text, {
className: '',
attributes: {
rel: 'noopener noreferrer'
},
target: {
url: '_blank'
},
validate: {
email: function () {
return false; //Do not linkify emails
return text.replaceAll(/\S+/g, function (match, p2) {
const result = linkifyStr(match, {
className: '',
attributes: {
rel: 'noopener noreferrer'
},
target: {
url: '_blank'
},
validate: {
email: function () {
return false; //Do not linkify emails
}
}
});
if (result.endsWith("</a>")) {
return result;
} else {
return match
}
});
});
};
}]);

View File

@ -21,15 +21,30 @@ describe('Filters', function() {
var url = 'asdf https://a.example.com/wiki/asdf_qwer_(rivi%C3%A8re) Some text.',
link = 'asdf <a href="https://a.example.com/wiki/asdf_qwer_(rivi%C3%A8re)" target="_blank" rel="noopener noreferrer">https://a.example.com/wiki/asdf_qwer_(rivi%C3%A8re)</a> Some text.',
result = $filter('conditionalLinkify')(url);
expect(result).toEqual(link);
expect(result).toEqual(link);
}));
it('should not make emails into links', angular.mock.inject(function($filter) {
var url = 'asdf@gmail.com',
link = 'asdf@gmail.com',
result = $filter('conditionalLinkify')(url);
expect(result).toEqual(link);
expect(result).toEqual(link);
}));
it('convert the entire words to links', angular.mock.inject(function($filter) {
var text = 'weechat.network.connection_timeout',
link = 'weechat.network.connection_timeout',
result = $filter('conditionalLinkify')(text);
expect(result).toEqual(link);
}));
it('linkify parenthesis at the end of an url', angular.mock.inject(function($filter) {
var text = 'http://test.com/(test)',
link = '<a href="http://test.com/(test)" target="_blank" rel="noopener noreferrer">http://test.com/(test)</a>',
result = $filter('conditionalLinkify')(text);
expect(result).toEqual(link);
}));
});
describe('irclinky', function() {
@ -153,6 +168,6 @@ describe('Filters', function() {
expect(codifyFilter('Weird`ness`')).toEqual('Weird`ness`');
}));
});
});