From 081a99a5595a2a3368dd60beccaaaf5a4caf5a53 Mon Sep 17 00:00:00 2001 From: Dylan Pulver Date: Wed, 2 Sep 2026 14:42:02 +0300 Subject: [PATCH] Do not duplicate a non-ASCII space when wrapping In convert_p's wrap branch, `trailing` is captured with a bare rstrip() and then appended after fill(). Bare rstrip() removes every character str.isspace() calls whitespace, but fill() only strips the ASCII ones, so a trailing U+00A0 (or U+202F, U+2007, U+2003, U+2009, U+3000) is left in place by fill() and then appended a second time. `


c

` with wrap=True gives 'a\xa0\xa0 \nc' where wrap=False gives 'a\xa0 \nc'. Restricting the rstrip to the same ' \t\r\n' set fill() uses makes the two agree. --- markdownify/__init__.py | 2 +- tests/test_conversions.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 28cdaf6..627d46d 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -675,7 +675,7 @@ def convert_p(self, el, text, parent_tags): new_lines = [] for line in lines: line = line.lstrip(' \t\r\n') - line_no_trailing = line.rstrip() + line_no_trailing = line.rstrip(' \t\r\n') trailing = line[len(line_no_trailing):] line = fill(line, width=self.options['wrap_width'], diff --git a/tests/test_conversions.py b/tests/test_conversions.py index c95483c..5baaf17 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -285,6 +285,7 @@ def test_p(): assert md('

1234 5678 9012
67890

', wrap=True, wrap_width=10, newline_style=SPACES) == '\n\n1234 5678\n9012 \n67890\n\n' assert md('First

Second

Third

Fourth') == 'First\n\nSecond\n\nThird\n\nFourth' assert md('

 x y

', wrap=True, wrap_width=80) == '\n\n\u00a0x y\n\n' + assert md('

x y 
z

', wrap=True, wrap_width=80, newline_style=SPACES) == md('

x y 
z

', newline_style=SPACES) == '\n\nx y\u00a0 \nz\n\n' def test_pre():