Skip to content

Commit

Permalink
fix(markdown-renderer): preserve list margin spacing
Browse files Browse the repository at this point in the history
The extra spacing can be before of after leader, so try to preserve it
on both positions.

Fixes miyuchina#213
  • Loading branch information
nijel committed Apr 22, 2024
1 parent 06f2a93 commit 6921bf2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
17 changes: 11 additions & 6 deletions mistletoe/markdown_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,22 @@ def render_list(
def render_list_item(
self, token: block_token.ListItem, max_line_length: int
) -> Iterable[str]:
indentation = len(token.leader) + 1 if self.normalize_whitespace else token.prepend - token.indentation
if self.normalize_whitespace:
prepend = len(token.leader) + 1
indentation = 0
else:
prepend = token.prepend
indentation = token.indentation
max_child_line_length = (
max_line_length - indentation if max_line_length else None
)
lines = self.blocks_to_lines(
lines = list(self.blocks_to_lines(
token.children, max_line_length=max_child_line_length
)
))
return self.prefix_lines(
list(lines) or [""],
token.leader + " " * (indentation - len(token.leader)),
" " * indentation
lines or [""],
" " * indentation + token.leader + " " * (prepend - len(token.leader) - indentation),
" " * prepend,
)

def render_table(
Expand Down
31 changes: 15 additions & 16 deletions test/test_markdown_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ def test_numbered_list(self):
]
output = self.roundtrip(input)
expected = [
"22) *emphasized list item*\n",
"96) \n",
"128) here begins a nested list.\n",
" + apples\n",
" + bananas\n",
" 22) *emphasized list item*\n",
" 96) \n",
" 128) here begins a nested list.\n",
" + apples\n",
" + bananas\n",
]
self.assertEqual(output, "".join(expected))

Expand All @@ -157,7 +157,6 @@ def test_bulleted_list(self):
output = self.roundtrip(input)
self.assertEqual(output, "".join(input))

# we don't currently support keeping margin indentation:
def test_list_item_margin_indentation_not_preserved(self):
# 0 to 4 spaces of indentation from the margin
input = [
Expand All @@ -176,14 +175,14 @@ def test_list_item_margin_indentation_not_preserved(self):
expected = [
"- 0 space: ok.\n",
" subsequent line.\n",
"- 1 space: ok.\n",
" subsequent line.\n",
"- 2 spaces: ok.\n",
" subsequent line.\n",
"- 3 spaces: ok.\n",
" subsequent line.\n",
" - 4 spaces: in the paragraph of the above list item.\n",
" subsequent line.\n",
" - 1 space: ok.\n",
" subsequent line.\n",
" - 2 spaces: ok.\n",
" subsequent line.\n",
" - 3 spaces: ok.\n",
" subsequent line.\n",
" - 4 spaces: in the paragraph of the above list item.\n",
" subsequent line.\n",
]
self.assertEqual(output, "".join(expected))

Expand Down Expand Up @@ -577,8 +576,8 @@ def test_wordwrap_paragraph_in_list(self):
"1. List item\n"
"2. A second list item\n"
" including:\n"
" * Nested list. This is\n"
" a continuation line\n"
" * Nested list. This is a\n"
" continuation line\n"
)

def test_wordwrap_paragraph_in_block_quote(self):
Expand Down

0 comments on commit 6921bf2

Please sign in to comment.