Skip to content

Fix #249: preserve whitespace around stripped block-level elements#265

Open
apoorvdarshan wants to merge 1 commit into
matthewwithanm:developfrom
apoorvdarshan:fix-249-stripped-block-whitespace
Open

Fix #249: preserve whitespace around stripped block-level elements#265
apoorvdarshan wants to merge 1 commit into
matthewwithanm:developfrom
apoorvdarshan:fix-249-stripped-block-whitespace

Conversation

@apoorvdarshan

Copy link
Copy Markdown

Summary

Fixes #249. When a block-level element such as <div> is stripped (via strip=[...]) or excluded from a convert=[...] whitelist, the whitespace separating it from its neighbouring text was dropped, running the words together.

from markdownify import markdownify as md

md('<span>Ignored <div>Still ignored</div> tag.</span>', strip=['div'])
# before: 'IgnoredStill ignoredtag.'
# after:  'Ignored Still ignored tag.'

Root cause

process_text() removes whitespace immediately outside a block-level sibling (should_remove_whitespace_outside). That is correct for a converted block, which emits its own \n\n separation. But a stripped block is emitted inline, so stripping the surrounding whitespace leaves no separator at all.

Fix

Only treat a block-level sibling as a whitespace boundary when it is actually being converted. A new helper _removes_adjacent_whitespace() combines the existing block-level check with should_convert_tag(), so a stripped (inline-rendered) sibling now keeps the separating whitespace. Converted blocks are unaffected.

The reporter's own workaround (overriding convert_div to pad with spaces) left a trailing space; this approach avoids that because the stripped element's own leading/trailing whitespace is still normalised — only the inter-element separator is preserved.

Verification

  • Added test_strip_block_element_preserves_surrounding_whitespace covering both strip=['div'] and convert=['span']. It fails on develop and passes with this change.
  • Full suite: 84 passed (was 83).
  • flake8 --ignore=E501,W503 markdownify tests clean.
  • Manually confirmed no change for converted blocks (<p>a</p><p>b</p>a\n\nb) and for stripped inline elements (strip=['b'] still keeps spaces).

Disclosure: prepared with AI assistance; reviewed and verified locally against the test suite.

…el elements

Whitespace immediately outside a block-level element is collapsed because a
converted block already provides its own line separation. But when a block
element such as <div> is stripped (or excluded via convert=[...]), it is
emitted inline, so removing the surrounding whitespace runs the neighbouring
words together (e.g. '<span>a <div>b</div> c</span>' with strip=['div']
became 'ab c' -> 'abc').

Only treat a block-level sibling as a whitespace boundary when it is actually
being converted; a stripped sibling now keeps the separating whitespace.
Added a regression test.
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.

Ignored div tag removes spaces.

1 participant