Separate contiguous paragraphs in HTML passed to the CLI

Merged
RO
5mo ago
+217-2

Problem

Basecamp rich text relies on explicit separator nodes for paragraph spacing, not CSS margins on <p>. So contiguous <p>A</p><p>B</p> renders squished — the paragraphs run together and don't look separated. This is common with agent/LLM output, which frequently emits contiguous <p> blocks.

The Markdown pipeline already handles this: a blank line between paragraphs inserts a separator (via TrixBreak). But the raw-HTML passthrough in MarkdownToHTML sent HTML through verbatim, so contiguous <p> from HTML input rendered squished.

Context: internal card "Missing new lines to separate paragraphs in rich text."

Before (main)After (this PR)
Contiguous <p> renders squished<br> separators inserted
main-squished-croppr-separated-crop

Change

MarkdownToHTML's HTML-passthrough branch now runs insertParagraphSeparators, which inserts a bare <br> between directly adjacent, non-empty <p> blocks. This brings the raw-HTML path into line with what the Markdown path already does.

Properties:

  • Idempotent — a boundary that already carries a separator (a bare <br>, or an empty <p><br></p> / <p></p> on either side) is left untouched, so already-separated content, including Basecamp editor output, is a no-op.
  • Byte-preserving apart from the inserted separators.
  • Conservative scope — only directly adjacent <p> pairs are separated. Anything between them (a heading, list, or attachment) already provides its own break and is left alone.

Why client-side, and why a bare <br>

Basecamp's editor stores a single-Enter break as contiguous <p>A</p><p>B</p> and a double-Enter break as <p>A</p><p><br></p><p>B</p>. Contiguous <p> is therefore a legitimate, intentional editor state, byte-identical to what an API client sends when it means "separated" — so the server can't safely normalize it without corrupting editor content and breaking round-trips. The CLI can: its input is never editor-authored, and its edit loop already collapses the tight/separated distinction, so treating contiguous HTML paragraphs as separate is consistent with how the CLI already behaves everywhere else.

A bare <br> is used to match what the Markdown pipeline already emits. It renders identically to the editor's canonical <p><br></p> (verified live).

Verification

  • New tests in richtext_test.go: behavior + 14 edge cases, idempotency (f(f(x)) == f(x)), and convergence with the Markdown path.
  • richtext, commands, output, presenter, tui packages pass; gofmt / go vet / golangci-lint clean; check-skill-drift passes (no CLI surface change).
  • Live main-vs-PR comparison against a test project, same input <p>one</p><p>two</p><p>three</p>:
    • main build stores <p>one</p><p>two</p><p>three</p> → renders squished
    • this build stores <p>one</p><br><p>two</p><br><p>three</p> → renders separated

Summary by cubic

Ensures HTML passed to the CLI renders separated paragraphs by inserting "<br>" between directly adjacent non-empty <p> tags, matching Markdown behavior. Now treats <p>&nbsp;</p> (and &#160;/&#xa0;) as empty separator paragraphs.

  • Bug Fixes
    • MarkdownToHTML now runs insertParagraphSeparators for HTML input.
    • Inserts "<br>" between adjacent non-empty <p> blocks; preserves existing separators (<br>, empty <p>, or <p>&nbsp;</p>/&#160;/&#xa0;).
    • Idempotent and byte-preserving; tests added for behavior, &nbsp; variants, and Markdown-path convergence.