🔒 Linkify refacto
This commit is contained in:
parent
04e7e3720a
commit
13284f989d
@ -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 `<a href="${url}" target="_blank">${url}</a>`;
|
||||
const safeText = this.basicEscape(text);
|
||||
|
||||
const html = safeText.replace(urlRegex, (url) => {
|
||||
const href = url.startsWith("http") ? url : `https://${url}`;
|
||||
return `<a href="${href}" target="_blank">${url}</a>`;
|
||||
});
|
||||
|
||||
return this.sanitizer.bypassSecurityTrustHtml(html);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user