Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(markdown-renderer): preserve list margin spacing #215

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 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
max_line_length - prepend if max_line_length else None
)
lines = 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
" " * indentation + token.leader + " " * (prepend - len(token.leader) - indentation),
" " * prepend,
)

def render_table(
Expand Down
43 changes: 36 additions & 7 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,8 +157,7 @@ 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):
def test_list_item_margin_indentation_preserved(self):
# 0 to 4 spaces of indentation from the margin
input = [
"- 0 space: ok.\n",
Expand All @@ -173,6 +172,36 @@ def test_list_item_margin_indentation_not_preserved(self):
" subsequent line.\n",
]
output = self.roundtrip(input)
expected = [
"- 0 space: ok.\n",
" subsequent line.\n",
" - 1 space: ok.\n",
" subsequent line.\n",
" - 2 spaces: ok.\n",
" subsequent line.\n",
# note: we still always normalize the indentation of all list item lines:
" - 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))

def test_list_item_margin_indentation_normalized(self):
# 0 to 4 spaces of indentation from the margin
input = [
"- 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",
]
output = self.roundtrip(input, normalize_whitespace=True)
expected = [
"- 0 space: ok.\n",
" subsequent line.\n",
Expand Down
Loading