(string, maxLength = 300)
| 1 | import sanitizeHtml from 'sanitize-html'; |
| 2 | |
| 3 | export default function createExcerpt(string, maxLength = 300) { |
| 4 | // Stop at first code example |
| 5 | string = string.split('<table')[0]; |
| 6 | |
| 7 | let excerpt = sanitizeHtml(string, { allowedTags: [] }) |
| 8 | // Remove tabs and line breaks |
| 9 | .replace(/\t/g, '') |
| 10 | .replace(/\n/g, ' ') |
| 11 | .trim(); |
| 12 | |
| 13 | // Truncate |
| 14 | if (excerpt.length > maxLength) { |
| 15 | excerpt = excerpt.slice(0, maxLength - 1) + '…'; |
| 16 | } |
| 17 | |
| 18 | return excerpt; |
| 19 | } |
no outgoing calls
no test coverage detected