From 52fc6d1846ade282ef4aed53ed5530ee56fb7c7d Mon Sep 17 00:00:00 2001 From: Noach Magedman Date: Wed, 30 Jun 2021 18:56:48 +0300 Subject: [PATCH] Preview message URLs only once If a message lists the same URL twice, the URL would be previewed twice. This includes Markdown code explicitly using the URL as the link text. e.g. ``` [http://example.com/](http://example.com/) ``` Strip out duplicate URLs when parsing them out,so we only show a single preview. Co-authored-by: Evgeniy Kulesh --- app/lib/server/functions/insertMessage.js | 2 +- app/lib/server/functions/parseUrlsInMessage.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/lib/server/functions/insertMessage.js b/app/lib/server/functions/insertMessage.js index 5e342ad0ab7c..8117da5b548d 100644 --- a/app/lib/server/functions/insertMessage.js +++ b/app/lib/server/functions/insertMessage.js @@ -119,7 +119,7 @@ export const insertMessage = function(user, message, rid, upsert = false) { const urls = message.html.match(/([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\(\)\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?/g); if (urls) { - message.urls = urls.map((url) => ({ url })); + message.urls = [...new Set(urls)].map((url) => ({ url })); } message = Markdown.mountTokensBack(message, false); diff --git a/app/lib/server/functions/parseUrlsInMessage.js b/app/lib/server/functions/parseUrlsInMessage.js index 3c6b662f8181..f7a6c6d1a370 100644 --- a/app/lib/server/functions/parseUrlsInMessage.js +++ b/app/lib/server/functions/parseUrlsInMessage.js @@ -10,7 +10,7 @@ export const parseUrlsInMessage = (message) => { const urls = message.html.match(/([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?/g) || []; if (urls) { - message.urls = urls.map((url) => ({ url })); + message.urls = [...new Set(urls)].map((url) => ({ url })); } message = Markdown.mountTokensBack(message, false);