From 2317184fe716fad78345f40537ba852116e11be7 Mon Sep 17 00:00:00 2001 From: the-good-boy Date: Fri, 15 Apr 2022 17:04:29 +0530 Subject: [PATCH 1/5] remove redundant https --- src/client/helpers/utils.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/helpers/utils.tsx b/src/client/helpers/utils.tsx index 28f3f2ef08..65e06f4526 100644 --- a/src/client/helpers/utils.tsx +++ b/src/client/helpers/utils.tsx @@ -180,7 +180,7 @@ export function stringToHTMLWithLinks(string: string) { const addHttpRegex = /(^|\b)www\./ig; // eslint-disable-next-line max-len, no-useless-escape const urlRegex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%~*@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/g; - let content = string.replace(addHttpRegex, '$1https://www.'); + let content = string.replace(addHttpRegex, '$1www.'); content = content.replace( urlRegex, '$1' From ec5e0c9ffa044a8af062ef03ac019eac926671ba Mon Sep 17 00:00:00 2001 From: the-good-boy Date: Fri, 15 Apr 2022 19:16:23 +0530 Subject: [PATCH 2/5] add http only if not already present --- src/client/helpers/utils.tsx | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/client/helpers/utils.tsx b/src/client/helpers/utils.tsx index 65e06f4526..89f812185a 100644 --- a/src/client/helpers/utils.tsx +++ b/src/client/helpers/utils.tsx @@ -177,15 +177,16 @@ export function dateObjectToISOString(value: DateObject) { * @returns {JSX} returns a JSX Element */ export function stringToHTMLWithLinks(string: string) { - const addHttpRegex = /(^|\b)www\./ig; - // eslint-disable-next-line max-len, no-useless-escape - const urlRegex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%~*@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/g; - let content = string.replace(addHttpRegex, '$1www.'); - content = content.replace( - urlRegex, - '$1' - ); - const sanitizedHtml = DOMPurify.sanitize(content); + let urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g; + let contentString = string.replace(urlRegex, (url) => { + let hyperlink = url; + if (!hyperlink.match('^https?:')) { + hyperlink = `http://${hyperlink}`; + } + return `${url}`; + }); + + const sanitizedHtml = DOMPurify.sanitize(contentString); // eslint-disable-next-line react/no-danger return ; } From dc13bb7c9b12830e8fad1a176b21e246ece32933 Mon Sep 17 00:00:00 2001 From: the-good-boy Date: Mon, 2 May 2022 19:30:58 +0530 Subject: [PATCH 3/5] improved regex --- src/client/helpers/utils.tsx | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/client/helpers/utils.tsx b/src/client/helpers/utils.tsx index 89f812185a..1f50290782 100644 --- a/src/client/helpers/utils.tsx +++ b/src/client/helpers/utils.tsx @@ -173,20 +173,25 @@ export function dateObjectToISOString(value: DateObject) { * Convert any string url that has a prefix http|https|ftp|ftps to a clickable link * and then rendered the HTML string as real HTML. * @function stringToHTMLWithLinks - * @param {string} string - Can be either revision notes or annotation content etc... + * @param {string} text - Can be either revision notes or annotation content etc... * @returns {JSX} returns a JSX Element */ -export function stringToHTMLWithLinks(string: string) { - let urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g; - let contentString = string.replace(urlRegex, (url) => { - let hyperlink = url; - if (!hyperlink.match('^https?:')) { - hyperlink = `http://${hyperlink}`; - } - return `${url}`; - }); - - const sanitizedHtml = DOMPurify.sanitize(contentString); +export function stringToHTMLWithLinks(text: string) { + let urlRegex = /(?:http|\S+\.\S).+?(?=[.,;:?!-]?(?:\s|$))/g; + const matches = text.match(urlRegex) + if (!matches) return text; + const contentString = [] + matches.forEach(x => { + const [t1, ...t2] = text.split(x) + contentString.push(t1) + text = t2.join(x) + const y = (!(x.match(/(http(s?)):\/\//)) ? 'https://' : '') + x; + contentString.push('' + y + ''); + }) + contentString.push(text); + const finalText = contentString.join(''); + + const sanitizedHtml = DOMPurify.sanitize(finalText); // eslint-disable-next-line react/no-danger return ; } From 11b936b5fdc74674d46d292c177c971d2cd110ec Mon Sep 17 00:00:00 2001 From: the-good-boy Date: Sat, 11 Feb 2023 23:23:01 +0530 Subject: [PATCH 4/5] improve the original addHttpRegex --- src/client/helpers/utils.tsx | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/client/helpers/utils.tsx b/src/client/helpers/utils.tsx index 1f50290782..73fc593925 100644 --- a/src/client/helpers/utils.tsx +++ b/src/client/helpers/utils.tsx @@ -173,25 +173,19 @@ export function dateObjectToISOString(value: DateObject) { * Convert any string url that has a prefix http|https|ftp|ftps to a clickable link * and then rendered the HTML string as real HTML. * @function stringToHTMLWithLinks - * @param {string} text - Can be either revision notes or annotation content etc... + * @param {string} string - Can be either revision notes or annotation content etc... * @returns {JSX} returns a JSX Element */ -export function stringToHTMLWithLinks(text: string) { - let urlRegex = /(?:http|\S+\.\S).+?(?=[.,;:?!-]?(?:\s|$))/g; - const matches = text.match(urlRegex) - if (!matches) return text; - const contentString = [] - matches.forEach(x => { - const [t1, ...t2] = text.split(x) - contentString.push(t1) - text = t2.join(x) - const y = (!(x.match(/(http(s?)):\/\//)) ? 'https://' : '') + x; - contentString.push('' + y + ''); - }) - contentString.push(text); - const finalText = contentString.join(''); - - const sanitizedHtml = DOMPurify.sanitize(finalText); +export function stringToHTMLWithLinks(string: string) { + const addHttpRegex = /(^(|\b))www\./igm; + // eslint-disable-next-line max-len, no-useless-escape + const urlRegex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%~*@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/g; + let content = string.replace(addHttpRegex, '$1https://www.'); + content = content.replace( + urlRegex, + '$1' + ); + const sanitizedHtml = DOMPurify.sanitize(content); // eslint-disable-next-line react/no-danger return ; } From 191a684a4f0b3721c50be97a2f6db8cf0b56aada Mon Sep 17 00:00:00 2001 From: the-good-boy Date: Wed, 15 Feb 2023 15:47:21 +0530 Subject: [PATCH 5/5] improve regex --- src/client/helpers/utils.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/helpers/utils.tsx b/src/client/helpers/utils.tsx index 73fc593925..239ae6fa74 100644 --- a/src/client/helpers/utils.tsx +++ b/src/client/helpers/utils.tsx @@ -177,10 +177,10 @@ export function dateObjectToISOString(value: DateObject) { * @returns {JSX} returns a JSX Element */ export function stringToHTMLWithLinks(string: string) { - const addHttpRegex = /(^(|\b))www\./igm; + const addHttpRegex = /(\b(?$1'