Skip to content

Commit

Permalink
[FIX] Links being embedded inside of blockquotes (#10496)
Browse files Browse the repository at this point in the history
* Avoid embedding links inside blockquotes and remove doubles
  • Loading branch information
gdelavald authored and graywolf336 committed Apr 18, 2018
1 parent b97b223 commit f072189
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions packages/rocketchat-lib/server/functions/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,30 @@ RocketChat.sendMessage = function(user, message, room, upsert = false) {
}

if (message.parseUrls !== false) {
const urls = message.msg.match(/([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\(\)\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?/g);

const urlRegex = /([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\(\)\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?/g;
const urls = message.msg.match(urlRegex);
if (urls) {
message.urls = urls.map(function(url) {
return {
url
};
});
// ignoredUrls contain blocks of quotes with urls inside
const ignoredUrls = message.msg.match(/(?:(?:\`{1,3})(?:[\n\r]*?.*?)*?)(([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\(\)\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?)(?:(?:[\n\r]*.*?)*?(?:\`{1,3}))/gm);
if (ignoredUrls) {
ignoredUrls.forEach((url) => {
const shouldBeIgnored = url.match(urlRegex);
if (shouldBeIgnored) {
shouldBeIgnored.forEach((match) => {
const matchIndex = urls.indexOf(match);
urls.splice(matchIndex, 1);
});
}
});
}
if (urls) {
// use the Set to remove duplicity, so it doesn't embed the same link twice
message.urls = [...new Set(urls)].map(function(url) {
return {
url
};
});
}
}
}

Expand Down

0 comments on commit f072189

Please sign in to comment.