I am currently using a LimitTo filter to make excerpts of the description of an article to appear on the homepage of a website as a snippet in the feature section.
LimitTo is currently working for say "100 characters" but it gives abruptly ending words in excerpt sometimes, since the 100 character limit ends there.
I want to somehow end the excerpt at the occurrence of the last word (under 100 character limit) or at the occurrence of last space .
{{content.description | limitTo: 100}}{{content.description.length > 100 ? '...' : ''}}
I am using this currently, what can I add or do to make sure the words are complete under the 100 character limit and it doesn't end abruptly before showing the "..."
E.g. When I put "LimitTo: 12" here,
if content.description = "A Terrible Day", the output would be: "A Terrible D..."
but I want this to happen
if content.description = "A Terrible Day", the output would be: "A Terrible..." That is before or at the occurrence of the space.
How can i get the above mentioned output?
CodePudding user response:
I am giving the implementation of pipe transform method here :-
function transform(input: string, limitTo: number) {
let output = input.substr(0, limitTo);
const customLimit = limitTo;
if(input.length > customLimit && input[customLimit] !== ' ') {
output = output.split(' ');
output = output.slice(0, output.length - 1).join(' ');
}
return output;
}
CodePudding user response:
let trimmedString = "A Terrible Day".substring(0, 12);
trimmedString = trimmedString.substring(0, trimmedString.lastIndexOf(" ")) "..."