normalizeWhitespace collapses runs of blank lines and trims trailing spaces per line, so the parsed view is compact without losing paragraph structure.
(s string)
| 274 | // normalizeWhitespace collapses runs of blank lines and trims trailing spaces |
| 275 | // per line, so the parsed view is compact without losing paragraph structure. |
| 276 | func normalizeWhitespace(s string) string { |
| 277 | lines := strings.Split(s, "\n") |
| 278 | out := make([]string, 0, len(lines)) |
| 279 | blank := 0 |
| 280 | for _, ln := range lines { |
| 281 | ln = strings.TrimRight(ln, " \t\r") |
| 282 | if ln == "" { |
| 283 | blank++ |
| 284 | if blank > 1 { |
| 285 | continue // collapse 2+ blank lines into one |
| 286 | } |
| 287 | } else { |
| 288 | blank = 0 |
| 289 | } |
| 290 | out = append(out, ln) |
| 291 | } |
| 292 | return strings.TrimSpace(strings.Join(out, "\n")) |
| 293 | } |