43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import { Pipe, PipeTransform } from '@angular/core';
|
|
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
|
|
|
|
@Pipe({ name: 'urltext' })
|
|
export class UrlTextPipe implements PipeTransform {
|
|
|
|
httpPattern = /(\b(https?:\/\/)([-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]))/ig;
|
|
wwwPattern = /(^|[^\/>])(www\.[\S]+(\b|$))/gim;
|
|
|
|
constructor(private santisizer: DomSanitizer) { }
|
|
|
|
transform(value: string): SafeHtml {
|
|
value = value.replace(this.httpPattern, '<a href="$1" target="_blank">$3</a>');
|
|
value = value.replace(this.wwwPattern, '$1<a target="_blank" href="https://$2">$2</a>');
|
|
return this.santisizer.bypassSecurityTrustHtml(value);
|
|
}
|
|
}
|
|
|
|
@Pipe({ name: 'urlbase' })
|
|
export class UrlBasePipe implements PipeTransform {
|
|
|
|
httpPattern = /(\b(https?:\/\/)([-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]))/ig;
|
|
|
|
transform(value: string): any {
|
|
if (!value.match(this.httpPattern)) {
|
|
return '';
|
|
}
|
|
|
|
const parts = value.split('/')
|
|
|
|
if (parts.length < 3) {
|
|
return '';
|
|
}
|
|
|
|
let baseUrl = parts[2];
|
|
|
|
if (baseUrl.startsWith('www.')) {
|
|
baseUrl = baseUrl.substr(4);
|
|
}
|
|
|
|
return baseUrl;
|
|
}
|
|
} |