Skip to content

fix: recognize single-block list items as ListItem - #4484

Merged
cragwolfe merged 1 commit into
Unstructured-IO:mainfrom
huuufu:huuufu/fix-3499-list-item
Sep 14, 2026
Merged

cragwolfe merged 1 commit into
Unstructured-IO:mainfrom
huuufu:huuufu/fix-3499-list-item

Conversation

@huuufu

@huuufu huuufu commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

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.py suites pass locally; the broader test_unstructured run has the same result set as main in my environment.

Review in cubic

@cragwolfe

Copy link
Copy Markdown
Contributor

Low, non-blocking — preserve cross-page nested items during page-grouped HTML export.

This change can expose an existing exporter edge case when a nested list item is assigned to a different page from its parent. set_element_hierarchy() gives the child the parent item's ID, but default page-grouped elements_to_html() processes that child without its parent in the page subset. Because the child still has a parent_id, the exporter excludes it from the page roots and its text is omitted from the generated HTML. The partitioned element and Unstructured JSON remain intact.

This is limited to cross-page hierarchy plus page-grouped HTML export and should not block this PR. A follow-up can preserve missing-parent items as page-local roots or continuation children, with a partition-to-export regression covering same-page, cross-page, and no_group_by_page=True cases.

(authored by codex)

@cragwolfe cragwolfe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core list-item classification change looks good. The cross-page page-grouped HTML-export edge case noted separately is non-blocking and can be handled as a follow-up.

(authored by codex)

@cragwolfe
cragwolfe merged commit 6342e08 into Unstructured-IO:main Sep 14, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(html): list-item adopts single child block element as its text

2 participants