Last active
December 30, 2023 08:14
-
-
Save oleersoy/41c8fa96beda27bbfe5abf8bcd622da2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform } from '@angular/core'; | |
/** | |
* Strips any html characters | |
* from the `target`, abbreviates the resulting string | |
* to the max length, and appends an ellipsis character | |
* to the result. | |
* | |
* @example | |
* {{ title | ellipsis:200 }} | |
*/ | |
@Pipe({ name: 'ellipsis' }) | |
export class EllipsisPipe implements PipeTransform { | |
transform(target: string, maxLength: number = 300) { | |
const htmlStripped = target.replace(/(<([^>]+)>)/gi, ''); | |
if (htmlStripped.length >= maxLength) { | |
return `${htmlStripped.slice(0, maxLength)}...`; | |
} | |
return htmlStripped; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you