Skip to content

🐛 Parsed literal: a hard break is the line ending, not <br /> - #1247

Merged
chrisjsewell merged 2 commits into
masterfrom
maint/parsed-literal-hardbreak
Nov 26, 2026
Merged

chrisjsewell merged 2 commits into
masterfrom
maint/parsed-literal-hardbreak

Conversation

@chrisjsewell

Copy link
Copy Markdown
Member

A Markdown hard break (a backslash at the end of a line) inside a parsed-literal
is rendered as raw("<br />\n") plus raw("\\\\\n"), the same nodes as in a paragraph.
Inside a literal block the line ending is already a line break in every writer,
so the HTML <pre> draws two: the listing gains an empty line its author never wrote
(measured in Chromium on 5.2.0: two lines take 45px, against 30px without the <br />).
The raw LaTeX \\ beside it is replaced by the same line ending.

```{parsed-literal}
alpha *em*\
beta
```

renders as <pre class="literal-block">alpha <em>em</em><br />\nbeta</pre>.

Change

render_hardbreak checks the flag the parsed-literal inline parse already sets
(_PRESERVE_LINE_EDGE_WHITESPACE, from the parsed-literal line-edge whitespace change),
and inside it appends Text("\n"), the node the preserved newline rule writes for a soft break there.
Everywhere else the raw HTML and LaTeX breaks are unchanged.

The two trailing spaces form of a hard break is not affected: inside a parsed literal
the preserved newline rule already keeps them as text, so no hard break arises from them.

Tests

  • docutil_directives.md: [parsed-literal-hard-break], a break after text and one nested in emphasis,
    with no raw node in the doctree.
  • test_docutils.py: the html5 output of a parsed literal holds no <br,
    and a paragraph's hard break still renders as <br />.

Both new tests fail before the change and pass after; the rest of the suite passes unchanged.

Not changed

The line after a backslash hard break still loses its leading spaces, as markdown-it's escape rule consumes them;
a parsed literal otherwise keeps every line's indentation. That could be a follow-up.

@chrisjsewell
chrisjsewell merged commit 93f5030 into master Nov 26, 2026
24 checks passed
@chrisjsewell
chrisjsewell deleted the maint/parsed-literal-hardbreak branch November 26, 2026 12:49
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.

1 participant