Skip to content
Draft
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
5 changes: 3 additions & 2 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')))
Expand Down
16 changes: 16 additions & 0 deletions tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ def test_bullets():

def test_li_text():
assert md('<ul><li>foo <a href="#">bar</a></li><li>foo bar </li><li>foo <b>bar</b> <i>space</i>.</ul>') == '\n\n* foo [bar](#)\n* foo bar\n* foo **bar** *space*.\n'


def test_ordered_list_start_allows_surrounding_whitespace():
html = '<ol start=" 9 "><li>Apple</li><li>Banana</li></ol>'
assert md(html).strip() == '9. Apple\n10. Banana'


def test_ordered_list_invalid_numeric_start_uses_default():
html = '<ol start="\u00b2"><li>Apple</li><li>Banana</li></ol>'
assert md(html).strip() == '1. Apple\n2. Banana'


def test_ordered_list_start_keeps_zero_and_sign_behavior():
assert md('<ol start="0"><li>Apple</li></ol>').strip() == '0. Apple'
assert md('<ol start="-2"><li>Apple</li></ol>').strip() == '1. Apple'
assert md('<ol start="+2"><li>Apple</li></ol>').strip() == '1. Apple'