From b214b6e2a03cb7af9bd8339e145beefa287017ee Mon Sep 17 00:00:00 2001 From: Thomas Mathews Date: Sat, 12 Nov 2022 20:36:48 -0800 Subject: [PATCH] web: fix anchor detection This fixes an issue where text was being hyperlinked when the "http" pattern was detected in a character block. This looks that there is whitespace or such to correctly assume it's a link. This may not be the best implementation, but it seems to resolve issues for me so far. Unit tests would be good. --- web/js/damus.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/web/js/damus.js b/web/js/damus.js index 0f8856a..dbf1aa6 100644 --- a/web/js/damus.js +++ b/web/js/damus.js @@ -1033,24 +1033,28 @@ function is_video_url(path) { return VID_REGEX.test(path) } -const URL_REGEX = /(https?:\/\/[^\s]+)[,:)]?(\w|$)/g; +const URL_REGEX = /(^|\s)(https?:\/\/[^\s]+)[,:)]?(\w|$)/g; function linkify(text, show_media) { - return text.replace(URL_REGEX, function(url) { + return text.replace(URL_REGEX, function(match, p1, p2, p3) { + const url = p2 const parsed = new URL(url) - if (show_media && is_img_url(parsed.pathname)) - return ` + let html; + if (show_media && is_img_url(parsed.pathname)) { + html = ` `; - else if (show_media && is_video_url(parsed.pathname)) - return ` + } else if (show_media && is_video_url(parsed.pathname)) { + html = ` `; - else - return `${url}`; + } else { + html = `${url}`; + } + return p1+html; }) }