You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Should not treat newline at end of pre/code block as blank line #218
When round-tripping between Markdown and HTML, I am seeing an extra blank line appended to every code block on every round trip because the HTML generated by markdown2 has a newline at the end of the content within the pre/code tags.
I have found that typical browsers don't display that as a blank line, so markdownify should not treat it as one either.
I think that the correct behavior should be to ignore the final character within the elements when it is a newline character.
Whitespace inside this element is displayed as written, with one exception. If one or more leading newline characters are included immediately following the opening <pre> tag, the first newline character is stripped.
I can do more testing, but it looks to me like stripping the first at the head and the last at the tail will usually do the right thing. If there are intentional newlines, there will still be 1 added at the head/tail during conversion to HTML besides the intentional ones.
@stevecj - what if we implemented a new strip_pre configuration option that behaves as follows?
strip_pre
Controls whether leading and trailing newlines are removed from content in <pre> elements. Supported values are STRIP (strip all leading/trailing newlines), STRIP_ONE (strip a single leading/trailing newline), STRIP (strip all leading/trailing newlines), and None (leave as-is). Newlines within the content are unaffected. Defaults to None.
My company could actually use the STRIP behavior, as our content pipeline has no need to preserve leading/trailing newlines in <pre> blocks.
That seems fine to me. I think that stripping 1 or stripping all should probably become the default though. From what I can see, stripping none is the least likely to give expected/desired results.
Activity
stevecj commentedon May 12, 2025
I have actually now found the same issue with leading newlines. I think that the first leading newline should also be ignored if present.
The case for this is even stronger because, from https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/pre
chrispy-snps commentedon May 16, 2025
@stevecj - I created a quick testcase and this is indeed an unfortunate round-trip behavior:
If we chomp a single newline from the start and end, is it going to nibble away valid/meaningful newlines during repeated round-trips?
How would you feel about an option that strips all leading/trailing newlines from
<pre>elements?stevecj commentedon May 17, 2025
I can do more testing, but it looks to me like stripping the first at the head and the last at the tail will usually do the right thing. If there are intentional newlines, there will still be 1 added at the head/tail during conversion to HTML besides the intentional ones.
chrispy-snps commentedon May 24, 2025
@stevecj - what if we implemented a new
strip_preconfiguration option that behaves as follows?My company could actually use the
STRIPbehavior, as our content pipeline has no need to preserve leading/trailing newlines in<pre>blocks.stevecj commentedon May 27, 2025
That seems fine to me. I think that stripping 1 or stripping all should probably become the default though. From what I can see, stripping none is the least likely to give expected/desired results.
implement a strip_pre configuration option (matthewwithanm#218)
implement a strip_pre configuration option (#218) (#222)