Use linkifyjs to add hyperlinks.

Closes #2770
Closes #2634
This commit is contained in:
Kavin
2023-08-05 17:32:32 +01:00
parent a861a8b95d
commit 70277fe077
3 changed files with 22 additions and 14 deletions

View File

@@ -4,22 +4,10 @@ export const purifyHTML = html => {
return DOMPurify.sanitize(html);
};
export const urlify = string => {
if (!string) return "";
const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
const emailRegex = /([\w-\\.]+@(?:[\w-]+\.)+[\w-]{2,4})/g;
return string
.replace(urlRegex, url => {
if (url.endsWith("</a>") || url.endsWith("<a")) return url;
return `<a href="${url}" target="_blank">${url}</a>`;
})
.replace(emailRegex, email => {
return `<a href="mailto:${email}">${email}</a>`;
});
};
import linkifyHtml from "linkify-html";
export const rewriteDescription = text => {
return urlify(text)
return linkifyHtml(text)
.replaceAll(/(?:http(?:s)?:\/\/)?(?:www\.)?youtube\.com(\/[/a-zA-Z0-9_?=&-]*)/gm, "$1")
.replaceAll(/(?:http(?:s)?:\/\/)?(?:www\.)?youtu\.be\/(?:watch\?v=)?([/a-zA-Z0-9_?=&-]*)/gm, "/watch?v=$1")
.replaceAll("\n", "<br>");