Skip to content

Commit

Permalink
fix: Line wrap on lines right after a hard break (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin authored Nov 8, 2024
1 parent 8c573e4 commit 8d1712d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/users/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Note that there is currently no guarantee for a stable Markdown formatting style
- Deprecated
- Plugin interface: `mdformat.plugins.ParserExtensionInterface.add_cli_options`.
The replacing interface is `mdformat.plugins.ParserExtensionInterface.add_cli_argument_group`.
- Fixed
- Incorrect line wrap on lines right after a hard break.
Thank you, [MDW](https://github.com/mdeweerd), for the issue.
- Added
- Plugin interface: `mdformat.plugins.ParserExtensionInterface.add_cli_argument_group`.
With this plugins can now read CLI arguments merged with values from `.mdformat.toml`.
Expand Down
9 changes: 8 additions & 1 deletion src/mdformat/renderer/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,14 @@ def paragraph(node: RenderTreeNode, context: RenderContext) -> str: # noqa: C90
if isinstance(wrap_mode, int):
wrap_mode -= context.env["indent_width"]
wrap_mode = max(1, wrap_mode)
text = _wrap(text, width=wrap_mode)
# Newlines should be mostly WRAP_POINTs by now, but there are
# exceptional newlines that need to be preserved:
# - hard breaks: newline defines the hard break
# - html inline: newline vs space can be the difference between
# html block and html inline
# Split the text and word wrap each section separately.
sections = text.split("\n")
text = "\n".join(_wrap(s, width=wrap_mode) for s in sections)

# A paragraph can start or end in whitespace e.g. if the whitespace was
# in decimal representation form. We need to re-decimalify it, one reason being
Expand Down
46 changes: 46 additions & 0 deletions tests/data/wrap_width_50.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,49 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa   No-wrap-points-until-
Unicode whitespace shouldnt act as wrap point, the
normal space should
.


Hard break (issue #326)
.
Loremiiinoeffefe\
ipsum dolor sit amet, consectetur adip elitismisun.

Here's 50 chars including hardbreak backslashhhhh\
ipsum dolor sit amet, consectetur adip elitismisun.

Here's 50 chars before hardbreak backslash ggggggg\
ipsum dolor sit amet, consectetur adip elitismisun.
.
Loremiiinoeffefe\
ipsum dolor sit amet, consectetur adip
elitismisun.

Here's 50 chars including hardbreak backslashhhhh\
ipsum dolor sit amet, consectetur adip
elitismisun.

Here's 50 chars before hardbreak backslash
ggggggg\
ipsum dolor sit amet, consectetur adip
elitismisun.
.


Newline in HTML inline
.
There is no hard break here, only HTML inlineee <a href="foo\
bar"> ipsum dolor sit amet, consectetur adip elitismisun.

sssssssssssssssssssssssssssssssssssss backslash <a href="foo
bar"> ipsum dolor sit amet, consectetur adip elitismisun.
.
There is no hard break here, only HTML inlineee
<a href="foo\
bar"> ipsum dolor sit amet, consectetur adip
elitismisun.

sssssssssssssssssssssssssssssssssssss backslash
<a href="foo
bar"> ipsum dolor sit amet, consectetur adip
elitismisun.
.
1 change: 0 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ def test_consecutive_wrap_width_lines(tmp_path):
assert file_path.read_text() == text


@pytest.mark.xfail(reason="https://github.com/executablebooks/mdformat/issues/326")
def test_wrap__hard_break(tmp_path):
file_path = tmp_path / "test_markdown.md"
file_path.write_text(
Expand Down

0 comments on commit 8d1712d

Please sign in to comment.