From b9f035790d63e6092760514b2fab94da5ec1cd25 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Thu, 15 Jun 2023 22:07:29 +1200 Subject: [PATCH] UI: More accurate clickable hyperlink logic in plain text messages See #125 --- server/ui-src/templates/Message.vue | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/server/ui-src/templates/Message.vue b/server/ui-src/templates/Message.vue index f212f4b..0a8c891 100644 --- a/server/ui-src/templates/Message.vue +++ b/server/ui-src/templates/Message.vue @@ -167,21 +167,26 @@ export default { // Convert plain text to HTML including anchor links textToHTML: function (s) { - // escape to HTML let html = s + + // full links with http(s) + let re = /(\b(https?|ftp):\/\/[\-\w@:%_\+.~#?,&\/\/=;]+)\b/gim + html = html.replace(re, '˱˱˱a href=ˠˠˠ$&ˠˠˠ target=_blank rel=noopener˲˲˲$&˱˱˱/a˲˲˲') + + // plain www links without https?:// prefix + let re2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim + html = html.replace(re2, '$1˱˱˱a href=ˠˠˠhttp://$2ˠˠˠ target=ˠˠˠ_blankˠˠˠ rel=ˠˠˠnoopenerˠˠˠ˲˲˲$2˱˱˱/a˲˲˲') + + // escape to HTML & convert <>" back + html = html .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'") - - // full links with http(s) - let re = /(\b(https?|ftp):\/\/[\-\w@:%_\+.~#?,&\/\/=;]+)\b/gim - html = html.replace(re, '$&') - - // plain www links without https?:// prefix - let re2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim - html = html.replace(re2, '$1$2') + .replace(/˱˱˱/g, '<') + .replace(/˲˲˲/g, '>') + .replace(/ˠˠˠ/g, '"') return html } @@ -326,7 +331,7 @@ export default { aria-labelledby="nav-html-tab" tabindex="0">