[FIX] Links being embedded inside of blockquotes (#10496)

* Avoid embedding links inside blockquotes and remove doubles
pull/10413/merge
Gabriel Delavald 8 years ago committed by Bradley Hilton
parent b97b2235b0
commit f072189216
  1. 30
      packages/rocketchat-lib/server/functions/sendMessage.js

@ -26,14 +26,30 @@ RocketChat.sendMessage = function(user, message, room, upsert = false) {
} }
if (message.parseUrls !== 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) { if (urls) {
message.urls = urls.map(function(url) { // ignoredUrls contain blocks of quotes with urls inside
return { 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);
url 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
};
});
}
} }
} }

Loading…
Cancel
Save