From 4533c5efb6c7a1b8c5c39544e53deeca48dc9f97 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Thu, 26 Nov 2026 13:44:08 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20a=20hard=20break=20in?= =?UTF-8?q?=20a=20parsed=20literal=20is=20the=20line=20ending?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- myst_parser/mdit_to_docutils/base.py | 7 ++++ tests/test_docutils.py | 34 +++++++++++++++++++ .../fixtures/docutil_directives.md | 19 +++++++++++ 3 files changed, 60 insertions(+) diff --git a/myst_parser/mdit_to_docutils/base.py b/myst_parser/mdit_to_docutils/base.py index 7b1808af..013c9aca 100644 --- a/myst_parser/mdit_to_docutils/base.py +++ b/myst_parser/mdit_to_docutils/base.py @@ -49,6 +49,7 @@ from myst_parser._compat import findall from myst_parser.config.main import MdParserConfig, UrlSchemeType from myst_parser.mocking import ( + _PRESERVE_LINE_EDGE_WHITESPACE, MockIncludeDirective, MockingError, MockInliner, @@ -611,6 +612,12 @@ def render_softbreak(self, token: SyntaxTreeNode) -> None: self.current_node.append(nodes.Text("\n")) def render_hardbreak(self, token: SyntaxTreeNode) -> None: + if self.md_env.get(_PRESERVE_LINE_EDGE_WHITESPACE): + # inside a literal block (a parsed-literal), the line ending is + # already a line break in every writer, so a raw break beside it + # would draw a second one: an empty line in the listing + self.current_node.append(nodes.Text("\n")) + return self.current_node.append(nodes.raw("", "
\n", format="html")) self.current_node.append(nodes.raw("", "\\\\\n", format="latex")) diff --git a/tests/test_docutils.py b/tests/test_docutils.py index cabb2c03..d8b88304 100644 --- a/tests/test_docutils.py +++ b/tests/test_docutils.py @@ -82,6 +82,40 @@ def test_parsed_literal_whitespace_preserves_delimiter_boundaries(): assert not list(literal.findall(nodes.emphasis)) +def test_parsed_literal_hard_break_is_a_line_ending(): + """A hard break in a parsed literal draws one line ending, not two. + + The literal block's own newline is already a line break in every writer, + so a raw ``
`` beside it would add an empty line to the listing. + """ + source = "\n".join(["```{parsed-literal}", "alpha *em*\\", "beta", "```", ""]) + output = publish_string( + source, + parser=Parser(), + writer="html5", + settings_overrides={"output_encoding": "unicode"}, + ) + assert "alpha em\nbeta" in output + assert "``.""" + source = "\n".join(["```{parsed-literal}", "a", "```", "", "alpha\\", "beta", ""]) + output = publish_string( + source, + parser=Parser(), + writer="html5", + settings_overrides={"output_encoding": "unicode"}, + ) + assert "

alpha
\nbeta

" in output + + def test_cli_html(monkeypatch, capsys): monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"text"))) cli_html([]) diff --git a/tests/test_renderers/fixtures/docutil_directives.md b/tests/test_renderers/fixtures/docutil_directives.md index a5713664..3f77da0b 100644 --- a/tests/test_renderers/fixtures/docutil_directives.md +++ b/tests/test_renderers/fixtures/docutil_directives.md @@ -237,6 +237,25 @@ bar bar . +[parsed-literal-hard-break] (a hard break is the line ending, with no raw `
` node): +. +```{parsed-literal} +foo\ +bar *em\ +baz* +``` +. + + + foo + + bar + + em + + baz +. + [parsed-literal-following-paragraph] (the block keeps its indent; a nested paragraph parsed after it still collapses): . ```{parsed-literal} From 75d8296df8fd81b612ef609f14f08d15df88ce56 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Thu, 26 Nov 2026 13:47:03 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A7=AA=20TEST:=20pass=20an=20html5=20?= =?UTF-8?q?writer=20instance,=20as=20docutils=20<=200.22=20requires?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_docutils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_docutils.py b/tests/test_docutils.py index d8b88304..bcc81436 100644 --- a/tests/test_docutils.py +++ b/tests/test_docutils.py @@ -12,6 +12,7 @@ from docutils import __version_info__ as docutils_version from docutils import nodes from docutils.core import publish_doctree, publish_string +from docutils.writers.html5_polyglot import Writer as HTML5Writer from docutils.writers.latex2e import Writer as LatexWriter from markdown_it.token import Token @@ -92,7 +93,7 @@ def test_parsed_literal_hard_break_is_a_line_ending(): output = publish_string( source, parser=Parser(), - writer="html5", + writer=HTML5Writer(), settings_overrides={"output_encoding": "unicode"}, ) assert "alpha em\nbeta" in output @@ -110,7 +111,7 @@ def test_hard_break_outside_a_parsed_literal_is_unchanged(): output = publish_string( source, parser=Parser(), - writer="html5", + writer=HTML5Writer(), settings_overrides={"output_encoding": "unicode"}, ) assert "

alpha
\nbeta

" in output