Skip to content

Instantly share code, notes, and snippets.

@oleersoy
Last active December 30, 2023 08:14
Show Gist options
  • Save oleersoy/41c8fa96beda27bbfe5abf8bcd622da2 to your computer and use it in GitHub Desktop.
Save oleersoy/41c8fa96beda27bbfe5abf8bcd622da2 to your computer and use it in GitHub Desktop.
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;
}
}
@thmarx
Copy link

thmarx commented Dec 16, 2022

thank you

@JemiloII
Copy link

thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment