fix: recognize single-block list items as ListItem
Summary
Resolves #3499.
A <li> whose only content is a single ordinary text block (<li><p>text</p></li>) now produces a ListItem. This is the HTML that Markdown loose lists (items separated by blank lines) compile to, with both markdown and markdown-it-py, so partition_md() on a loose list returned Text/NarrativeText/Title elements instead of ListItems.
This implements the approach proposed in the issue thread: when a <li> contains exactly one block element, adopt that block's content as the list item's text.
Change
ListItemBlock.iter_elements() in unstructured/partition/html/parser.py adopts the child block only when all of the following hold, otherwise it falls through to the existing Flow.iter_elements() traversal:
- the
<li>has no text of its own and exactly one child element, - the child is a plain
Flow/BlockItem(<p>,<div>,<blockquote>) — not a heading,<pre>, image, table, or nested list, - the child has no tail text and contains only phrasing content (so multi-paragraph items, including paragraphs nested inside inline markup, keep their current behavior).
The adopted element is built with the <li>'s own accumulator, so category_depth reflects list nesting, and inline annotations (links, emphasis) and data-page-number on the child are preserved.
Before / after
from unstructured.partition.md import partition_md
loose = "1. list item one.\n\n2. list item two.\n\n3. list item three.\n"
for el in partition_md(text=loose):
print(f"{el.category}: {el}")
Before:
UncategorizedText: list item one.
UncategorizedText: list item two.
UncategorizedText: list item three.
After:
ListItem: list item one.
ListItem: list item two.
ListItem: list item three.
Tight lists are unchanged.
Testing
test_unstructured/partition/html/test_parser.py::DescribeListItemBlock— adoption for<p>/<div>/<blockquote>, annotation and nested-depth preservation, page-number inheritance, and 14 cases that must keep normal traversal (multiple paragraphs, prefix/suffix text, nested list,<pre>, heading, table, image,<figure>, whitespace-only block, plain text).test_unstructured/partition/test_md.py::test_partition_md_recognizes_loose_and_tight_lists— end-to-end for ordered and unordered lists, tight and loose.- Full
test_unstructured/partition/html+test_md.pysuites pass locally; the broadertest_unstructuredrun has the same result set asmainin my environment.