From 91a64e3cd469f16b34ba9e8872fc8a73eee9bf00 Mon Sep 17 00:00:00 2001 From: Jiulong Wang Date: Mon, 10 May 2021 14:42:05 -0700 Subject: [PATCH] Fix missing whitespaces in
  • node --- markdownify/__init__.py | 5 ++++- tests/test_conversions.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index da04ebf..5c3b258 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -108,7 +108,10 @@ class MarkdownConverter(object): def process_text(self, el): text = six.text_type(el) - if el.parent.name == 'li': + # remove trailing whitespaces if any of the following condition is true: + # - current text node is the last node in li + # - current text node is followed by an embedded list + if el.parent.name == 'li' and (not el.next_sibling or el.next_sibling.name in ["ul", "ol"]): return escape(all_whitespace_re.sub(' ', text or '')).rstrip() return escape(whitespace_re.sub(' ', text or '')) diff --git a/tests/test_conversions.py b/tests/test_conversions.py index caac0fd..9d0cd63 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -308,6 +308,10 @@ def test_bullets(): assert md(nested_uls, bullets='-') == '\n- 1\n\t- a\n\t\t- I\n\t\t- II\n\t\t- III\n\t- b\n\t- c\n- 2\n- 3\n' +def test_li_text(): + assert md('') == '* foo [bar](#)\n* foo bar\n' + + def test_img(): assert md('Alt text') == '![Alt text](/path/to/img.jpg "Optional title")' assert md('Alt text') == '![Alt text](/path/to/img.jpg)'