diff --git a/markdownify/__init__.py b/markdownify/__init__.py index d946d2c..5cf75d2 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -635,8 +635,9 @@ def convert_li(self, el, text, parent_tags): # determine list item bullet character to use parent = el.parent if parent is not None and parent.name == 'ol': - if parent.get("start") and str(parent.get("start")).isnumeric(): - start = int(parent.get("start")) + start_value = str(parent.get('start', '')).strip(' \t\n\r\f') + if re.fullmatch(r'[0-9]+', start_value): + start = int(start_value) else: start = 1 bullet = '%s.' % (start + len(el.find_previous_siblings('li'))) diff --git a/tests/test_lists.py b/tests/test_lists.py index e9480ab..fa01684 100644 --- a/tests/test_lists.py +++ b/tests/test_lists.py @@ -87,3 +87,19 @@ def test_bullets(): def test_li_text(): assert md('') == '\n\n* foo [bar](#)\n* foo bar\n* foo **bar** *space*.\n' + + +def test_ordered_list_start_allows_surrounding_whitespace(): + html = '
  1. Apple
  2. Banana
' + assert md(html).strip() == '9. Apple\n10. Banana' + + +def test_ordered_list_invalid_numeric_start_uses_default(): + html = '
  1. Apple
  2. Banana
' + assert md(html).strip() == '1. Apple\n2. Banana' + + +def test_ordered_list_start_keeps_zero_and_sign_behavior(): + assert md('
  1. Apple
').strip() == '0. Apple' + assert md('
  1. Apple
').strip() == '1. Apple' + assert md('
  1. Apple
').strip() == '1. Apple'