From b51a0c85f83903c2f59e46d408fe68d8d7b4828d Mon Sep 17 00:00:00 2001 From: Amolith Date: Wed, 12 Nov 2025 15:16:23 -0700 Subject: [PATCH] fix: unwrap and rewrap body text correctly Previously, formatBody processed each line independently. When input text was already wrapped (but incorrectly), each line got rewrapped separately, making the wrapping worse. Now consecutive plain text lines are collected in a buffer, joined with spaces to unwrap them, then passed to wordWrap once as a single paragraph. Blank lines, bullet points, and numbered lists interrupt the buffer to preserve paragraph boundaries and list formatting. Fixes: ed58135 Assisted-by: Claude Sonnet 4.5 via Crush --- wrapBody.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/wrapBody.go b/wrapBody.go index 9514c57e75b1eb59d3ba6fdeda7df4f2133dc4df..3c0ac3291f5e99736d6890bc6114e173745549ef 100644 --- a/wrapBody.go +++ b/wrapBody.go @@ -14,15 +14,27 @@ var numberedListRegex = regexp.MustCompile(`^\d+\.\s`) func formatBody(body string) (string, error) { lines := strings.Split(body, "\n") var result []string + var plainTextBuffer []string + + flushPlainText := func() { + if len(plainTextBuffer) > 0 { + joined := strings.Join(plainTextBuffer, " ") + wrapped := wordWrap(joined, 72) + result = append(result, wrapped) + plainTextBuffer = nil + } + } for _, line := range lines { trimmed := strings.TrimSpace(line) if trimmed == "" { + flushPlainText() result = append(result, "") continue } if strings.HasPrefix(trimmed, "- ") || strings.HasPrefix(trimmed, "* ") { + flushPlainText() marker := trimmed[:2] content := trimmed[2:] wrapped := wrapWithHangingIndent(marker, " ", content, 72) @@ -31,6 +43,7 @@ func formatBody(body string) (string, error) { } if numberedListRegex.MatchString(trimmed) { + flushPlainText() parts := strings.SplitN(trimmed, " ", 2) marker := parts[0] + " " content := "" @@ -43,9 +56,11 @@ func formatBody(body string) (string, error) { continue } - result = append(result, wordWrap(trimmed, 72)) + plainTextBuffer = append(plainTextBuffer, trimmed) } + flushPlainText() + return strings.Join(result, "\n"), nil }