Last active
July 1, 2022 02:59
-
-
Save paulohfev/94022ef5932cc713a386fe53349aaa9e to your computer and use it in GitHub Desktop.
Create a content excerpt
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
const createContentExcerpt = (content, maxNumberOfWords, trailingIndicator = '...') => { | |
const listOfWords = content.trim().split(' '); | |
const truncatedContent = listOfWords.slice(0, maxNumberOfWords).join(' '); | |
const excerpt = truncatedContent + trailingIndicator; | |
const result = listOfWords.length > maxNumberOfWords ? excerpt : content; | |
return result; | |
}; | |
// Example: | |
// createContentExcerpt('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 3); | |
// Result: Lorem ipsum dolor... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment