From 207b67582600bdada839df093423ab3074383ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 22 Apr 2024 12:17:07 +0200 Subject: [PATCH] fix(markdown-renderer): preserve list margin spacing The extra spacing can be before of after leader, so try to preserve it on both positions. Fixes #213 --- mistletoe/markdown_renderer.py | 13 ++++++---- test/test_markdown_renderer.py | 43 ++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/mistletoe/markdown_renderer.py b/mistletoe/markdown_renderer.py index 6ebaca6..4b2d8e4 100644 --- a/mistletoe/markdown_renderer.py +++ b/mistletoe/markdown_renderer.py @@ -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( diff --git a/test/test_markdown_renderer.py b/test/test_markdown_renderer.py index 25eb76d..8e9873e 100644 --- a/test/test_markdown_renderer.py +++ b/test/test_markdown_renderer.py @@ -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)) @@ -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", @@ -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",