ensure \n\n are kept as \n\n

When splitting `'foo\n\nbar'` by `\n`, you will get `['foo', '', 'bar']`.

The `''` value will result in `[]` after the word wrapping. This
information gets lost when we `flatMap`, so let's keep the newline using
`['']` as the fallback.
This commit is contained in:
Robin Malfait 2025-08-27 19:04:05 +02:00
parent be10cf4474
commit 1d1535af90
No known key found for this signature in database

View file

@ -44,7 +44,7 @@ export function wordWrap(text: string, width: number): string[] {
// Handle text with newlines by maintaining the newlines, then splitting
// each line separately.
if (text.includes('\n')) {
return text.split('\n').flatMap((line) => wordWrap(line, width))
return text.split('\n').flatMap((line) => (line ? wordWrap(line, width) : ['']))
}
let words = text.split(' ')