Skip to content
Open
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
12 changes: 11 additions & 1 deletion markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,17 @@ def convert_li(self, el, text, parent_tags):
start = int(parent.get("start"))
else:
start = 1
bullet = '%s.' % (start + len(el.find_previous_siblings('li')))
# An explicit `value` on an <li> sets that item's ordinal and the
# following items continue counting from it (per the HTML spec).
# Walk from this item backwards (nearest first) for the closest
# numeric `value`; the offset is how many items follow it.
number = start + len(el.find_previous_siblings('li'))
for offset, li in enumerate([el] + el.find_previous_siblings('li')):
value = li.get("value")
if value and str(value).isnumeric():
number = int(value) + offset
break
bullet = '%s.' % number
else:
depth = -1
while el:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def test_ol():
assert md('<ol start="foo"><li>a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'
assert md('<ol start="1.5"><li>a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'
assert md('<ol start="1234"><li><p>first para</p><p>second para</p></li><li><p>third para</p><p>fourth para</p></li></ol>') == '\n\n1234. first para\n\n second para\n1235. third para\n\n fourth para\n'
# An explicit `value` sets the item's ordinal; following items continue from it.
assert md('<ol><li>a</li><li value="5">b</li><li>c</li></ol>') == '\n\n1. a\n5. b\n6. c\n'
assert md('<ol><li value="7">a</li><li>b</li></ol>') == '\n\n7. a\n8. b\n'
assert md('<ol start="2"><li>a</li><li value="10">b</li><li value="4">c</li><li>d</li></ol>') == '\n\n2. a\n10. b\n4. c\n5. d\n'
# Non-numeric or negative `value` falls back to positional numbering.
assert md('<ol><li value="foo">a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'
assert md('<ol><li value="-1">a</li><li>b</li></ol>') == '\n\n1. a\n2. b\n'


def test_nested_ols():
Expand Down