Skip to content

Commit 4a97e5a

Browse files
committed
Fix: Preserve newlines in code blocks with github flavor
Commit 7c4daaf introduced a workaround for GitHub-flavored markdown treating newlines as <br/> in paragraphs by unconditionally replacing all \n with spaces when markdown_flavor='github'. However, this broke code blocks, doctest blocks, and math blocks where newlines are semantically significant. The visit_literal_block method already sets escape_text=False to signal verbatim contexts. This fix conditions the newline replacement on escape_text=True, so newlines are only collapsed in prose paragraphs, not in code/math/comment blocks. Impact: Multi-line argparse usage text (e.g., from sphinxcontrib-autoprogram) now renders correctly with proper line breaks instead of collapsing into a single line with multiple spaces. Added test case with multi-line code block to prevent regression. Signed-off-by: Eike Waldt <waldt@b1-systems.de> On-behalf-of: SAP <eike.waldt@sap.com>
1 parent d2dff40 commit 4a97e5a

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

sphinx_markdown_builder/translator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def visit_image(self, node):
346346
def visit_Text(self, node): # pylint: disable=invalid-name
347347
text = node.astext().replace("\r", "")
348348
# Replace line breaks with spaces to create single-line paragraphs
349-
if self.config.markdown_flavor == "github":
349+
if self.config.markdown_flavor == "github" and self.status.escape_text:
350350
text = text.replace("\n", " ")
351351
if self.status.escape_text:
352352
text = escape_markdown_chars(text)

tests/source/blocks.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ Code Example
2323
>>> print("this is a Doctest block.")
2424
this is a Doctest block.
2525

26+
Multi-line Code Block
27+
---------------------
28+
29+
.. code-block:: console
30+
31+
usage: command [-h] [--option1 VALUE1]
32+
[--option2 VALUE2]
33+
[--option3 VALUE3]
34+
argument
35+
2636
2737
==========
2838
Line Block

0 commit comments

Comments
 (0)