Skip to content

Drop whitespace-only text nodes between block elements - #52

Closed
trsdn wants to merge 1 commit into
fix/34-code-block-languagefrom
fix/33-whitespace-between-blocks
Closed

trsdn wants to merge 1 commit into
fix/34-code-block-languagefrom
fix/33-whitespace-between-blocks

Conversation

@trsdn

@trsdn trsdn commented Aug 28, 2026

Copy link
Copy Markdown
Owner

Fixes #33

Pretty-printed HTML puts a newline between block elements, and that newline is its own text node. It was collapsed to a single space and appended, so <p>one</p>\n<p>two</p> converted to one\n\n two with a stray leading space on the second block. Anything that pastes formatted HTML hits this, since almost every source indents its markup.

The part that makes this non-trivial

The obvious fix, discarding whitespace-only text nodes, is wrong: between inline elements the very same node is a real word separator, so <em>a</em> <em>b</em> would become *a**b*.

The distinguishing signal turns out to already be in the output buffer. Every block element finishes by appending a newline, so if the buffer ends with a newline the whitespace is layout and can be dropped; otherwise it is inline and is kept as a single space. That also handles leading whitespace at the very start of a document, where the buffer is still empty.

Verification

Full suite: 86 passed, 0 failed, 1 skipped. The remaining skip is #32, which is still open. The inline-spacing cases are covered by the existing tests, which is what pinned down that the naive fix would have regressed them.

Base branch

Stacked on fix/34-code-block-language (PR #51), which is itself stacked on the test project in PR #41. Merge order is #41, then #51, then this.

Pretty-printed HTML puts a newline between block elements, and that newline is
its own text node. It was collapsed to a single space and appended, so
"<p>one</p>\n<p>two</p>" converted to "one\n\n two" with a stray leading space
on the second block.

The same whitespace-only node between inline elements is a real word separator,
so it cannot simply be discarded. A block always ends by appending a newline,
which makes the buffer a reliable signal: if the output so far ends with a
newline the whitespace is layout and is dropped, otherwise it is kept as a
single space.

Fixes #33

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b721ad5f-9add-4f55-b9c8-7e6d7b0fb3e7
@trsdn
trsdn changed the base branch from fix/34-code-block-language to test/23-converter-tests August 28, 2026 20:58
@trsdn
trsdn changed the base branch from test/23-converter-tests to fix/34-code-block-language August 28, 2026 20:59
@trsdn

trsdn commented Aug 28, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #53. These commits are already contained in that branch, so this PR would merge nothing on its own. Collapsing the stacked converter PRs into one avoids the base-rewriting problem that made the stack unmergeable.

@trsdn trsdn closed this Aug 28, 2026
@trsdn
trsdn deleted the fix/33-whitespace-between-blocks branch August 28, 2026 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant