Honor the value attribute on <li> in ordered lists#263
Open
gaoflow wants to merge 1 commit into
Open
Conversation
markdownify numbered <ol> items purely by position, so an explicit value attribute on an <li> was ignored. Per the HTML spec, value sets that item's ordinal and the following items continue counting from it, e.g. '<ol><li>a<li value="5">b<li>c' is 1, 5, 6 (not 1, 2, 3). Walk from the item backwards for the nearest numeric value and offset by the number of items that follow it. Non-numeric or negative values fall back to positional numbering, mirroring the existing start guard.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
markdownifynumbers<ol>items purely by position, so an explicitvalueattribute on an<li>is silently ignored and the converted Markdown renders different numbering than the source HTML:Per the HTML spec,
valuesets that item's ordinal and the following items continue counting from it. The library already honors<ol start>, but never read<li value>.Fix
In
convert_li, before falling back to the positional number, walk from the current item backwards (nearest first, including the item itself) for the closest numericvalue; the ordinal isint(value) + offset, whereoffsetis the number of items between it and the current one. Non-numeric or negativevaluefalls back to positional numbering, mirroring the existingstartguard (.isnumeric()).This composes correctly with
<ol start>and with multiplevalueattributes:<ol><li>a<li value="5">b<li>c<ol start="2"><li>a<li value="10">b<li value="4">c<li>d<ol><li value="foo">a<li>bVerification
tests/test_lists.py::test_ol(the bug case, value-on-first-item, the compoundstart+ multiplevaluecase, and non-numeric/negative fallback). They fail before the fix and pass after.flake8 --ignore=E501,W503 markdownify testsclean.This pull request was prepared with the assistance of AI, under my direction and review.