| 18 | * @return {str} |
| 19 | */ |
| 20 | export const truncate = (inputContent, numWords) => { |
| 21 | if (!inputContent) { |
| 22 | return ''; |
| 23 | } |
| 24 | const limit = !numWords ? 100 : numWords; |
| 25 | |
| 26 | // Trim whitespace |
| 27 | let content = inputContent.trim(); |
| 28 | |
| 29 | // Convert the content into an array of words |
| 30 | const contentArr = content.split(' '); |
| 31 | |
| 32 | // Remove any words above the limit |
| 33 | content = contentArr.slice(0, limit); |
| 34 | |
| 35 | // Convert the array of words back into a string |
| 36 | return `${content.join(' ')}${contentArr.length > limit ? '…' : ''}`; |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * Strip any HTML from a string |