From 13284f989da16efc31c0f7b1dba8663874172024 Mon Sep 17 00:00:00 2001 From: itskovacs Date: Wed, 6 Aug 2025 20:00:49 +0200 Subject: [PATCH] :lock: Linkify refacto --- src/src/app/shared/linkify.pipe.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/src/app/shared/linkify.pipe.ts b/src/src/app/shared/linkify.pipe.ts index 66d5452..fee2bfc 100644 --- a/src/src/app/shared/linkify.pipe.ts +++ b/src/src/app/shared/linkify.pipe.ts @@ -5,13 +5,31 @@ import { DomSanitizer, SafeHtml } from "@angular/platform-browser"; export class LinkifyPipe implements PipeTransform { constructor(private sanitizer: DomSanitizer) {} + private basicEscape(text: string): string { + return text.replace( + /[&<>"']/g, + (char) => + ({ + "&": "&", + "<": "<", + ">": ">", + '"': """, + "'": "'", + })[char]!, + ); + } + transform(text: string): SafeHtml { if (!text) return text; const urlRegex = /((https?:\/\/|www\.)[^\s]+)/g; - const html = text.replace(urlRegex, (url) => { - return `${url}`; + const safeText = this.basicEscape(text); + + const html = safeText.replace(urlRegex, (url) => { + const href = url.startsWith("http") ? url : `https://${url}`; + return `${url}`; }); + return this.sanitizer.bypassSecurityTrustHtml(html); } }