Skip to content

fix(markdown): indent the block children of a list item to its content column - #830

Merged
ceberam merged 7 commits into
docling-project:mainfrom
morten-lagabote:fix/markdown-inline-group-after-sub-list
Nov 27, 2026
Merged

ceberam merged 7 commits into
docling-project:mainfrom
morten-lagabote:fix/markdown-inline-group-after-sub-list

Conversation

@morten-lagabote

@morten-lagabote morten-lagabote commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #844
Resolves docling-project/docling#4526

A ListItem owns its continuation blocks: paragraphs, inline groups, code, tables and sub-lists after its text (section D in docling-project/docling#4837). MarkdownTextSerializer wrote them at column 0 without a blank line, and MarkdownListSerializer appended every InlineGroup to the previous line. A Markdown parser then took the blocks out of the list.

Changes

  • MarkdownTextSerializer serializes the block children of a ListItem with the item. Each block is indented to the content column of the item (len(marker) + 1) and has a blank line before it. A blank line follows an item with such blocks.
  • An InlineGroup is the item text if it is the first child of an item with an empty text. Any other InlineGroup is a continuation block. The inline groups of a list item no longer reach the append in MarkdownListSerializer.
  • The inline runs in the test data of this pull request carry their own spaces, as feat: make inline serializers concatenate runs faithfully #693 requires. test_list_item_text_stays_apart_from_its_inline_group from feat: make inline serializers concatenate runs faithfully #693 now expects the group after the item text as a continuation paragraph: - Intro, a blank line, then more **x** y.
  • MarkdownListSerializer indents every line of a nested item, not only the first one, so that the blocks of a nested item stay in it. This also changes a nested item with a line break in its text and no continuation block: main gives - line1 \nline2 (a lazy continuation line at column 0), this branch gives - line1 \n line2.
  • The test cases of fix(markdown): preserve loose list continuation blocks #845 (@w3lld1) are in tests/test_serialization.py.

Two details differ from #845:

  • An item with an empty text keeps its first block on the line after the marker, without a blank line. With a blank line, CommonMark ends the item (example 280): - \n\n text is an empty item and a paragraph after the list.
  • An item with only sub-lists is serialized as before, with indent spaces. A sub-list in an item that also has other blocks is indented to the content column, as the other blocks are.

Review evidence

  • Before (main 186741a): text after a sub-list, - nestedAfter the list, see [the docs](https://example.org/); a second paragraph, - Plain item Second paragraph, see [the docs](https://example.org/); the tree of MarkdownTextSerializer does not emit continuation blocks for loose list items #844 gives the continuation paragraph and the code block at column 0.

  • After: the tree of MarkdownTextSerializer does not emit continuation blocks for loose list items #844 gives the output of section D.2 in List Item Serialization Policy: Data Model & Serialization Rules docling#4837:

    1. Install the package.
    2. Set the API key.
    
       Use the key from the dashboard.
    
       ```
       export API_KEY=...
       ```
    
    3. Run the import.
    
  • Test fails without fix: 14 of the 15 new or changed test cases fail on main (f347b36). The label filter case passes on main and guards against a regression.

  • Reference data: no change in docling-core. In docling (a7760abf), with this branch:

    • Five Markdown files change: asciidoc_03.asciidoc.md, html_nested_block_in_list_item.html.md, wiki_duck.html.md, elife-56337.nxml.md, 2305.03393_main.tex.md. In each of them, the image, table, sub-list or paragraph of a list item is now in the item when marko parses the file. Before, it was outside the list, except in 2305.03393_main.tex.md, where the paragraph joined the item text. In elife-56337.nxml.md the paragraphs became setext headings.
    • Seven test cases expect the previous output: test_list_item_text_keeps_boundary_before_rich_paragraph (BoxNote) and test_chandra_list_item_text_keeps_boundary_before_inline_paragraph (Chandra) from feat: honor the InlineGroup whitespace contract in the backends docling#3891, and five cases of test_latex_enumerate_item_starting_with_paragraph_break and test_latex_enumerate_line_of_item_starting_with_formula from fix(latex): number the items of enumerate lists docling#4518. With this branch, the continuation block is in the item.
    • I can open the docling pull request that updates these files and tests after the release.
  • Checks: ruff check, ruff format --check, ty, scripts/check_compat_projectors.py, pytest tests packages/dclq/tests (1735 passed, 12 skipped, 53 xfailed) on this branch, rebased on main f347b36.

  • Not run: other Python versions than 3.13, and the docling tests that need PDF or VLM models. Instead, the Markdown export of the 226 DoclingDocument JSON ground-truth files in docling (a7760abf) was compared between main (186741a) and this branch. It changes for four of the five files above (asciidoc_03 has no JSON ground truth) and for no PDF.

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @morten-lagabote, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 All 2 merge protections satisfied — ready to merge.

Show 2 satisfied protections

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

🟢 Require two reviewer for test updates

When test data is updated, we require two reviewers

  • #approved-reviews-by >= 2

@morten-lagabote
morten-lagabote force-pushed the fix/markdown-inline-group-after-sub-list branch from 4137bb0 to fb52f40 Compare October 3, 2026 19:44
@morten-lagabote morten-lagabote changed the title fix(markdown): start a new line for an inline group after a sub-list fix(markdown): start a new line for an inline group that is not the list item text Oct 3, 2026
@codecov

codecov Bot commented Oct 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ceberam ceberam added bug Something isn't working serialization Output serializers: Markdown, HTML, LaTeX, JSON, plain-text, outline labels Oct 14, 2026
@ceberam

ceberam commented Oct 14, 2026

Copy link
Copy Markdown
Member

@morten-lagabote please check my comment #844 (comment) on how to proceed with this PR

@morten-lagabote morten-lagabote changed the title fix(markdown): start a new line for an inline group that is not the list item text fix(markdown): indent the block children of a list item to its content column Oct 19, 2026
@github-actions github-actions Bot added the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit d9c88f715d. The analysis read the diff only. It did not run code from this pull request.

Warning

The AI analysis did not finish. Only the deterministic checks are shown.

CI safety: not assessed.

Possibly related pull requests

PR State Signal Assessment
#845 closed also references issue #844; referenced as #845 not assessed
#1081 open changes the same code not assessed
#701 open changes the same code not assessed
#878 open also references issue #844 not assessed
#693 merged referenced as #693 not assessed

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

@morten-lagabote

Copy link
Copy Markdown
Contributor Author

@ceberam Since #693, main and #830 give different Markdown for one tree: a ListItem with a non-empty text and an InlineGroup child.

ListItem(text="Intro")
  InlineGroup
    TextItem("more "), TextItem("x", bold), TextItem(" y")

With #830 rebased on main, this is the only failing test. That is after one change in the test data of #830: its runs now carry their own spaces, as #693 requires.

Which rule do you want #830 to follow? I will push the rebase when the rule is clear.

@ceberam

ceberam commented Nov 10, 2026

Copy link
Copy Markdown
Member

@morten-lagabote I think we should stick to the contract on docling-project/docling#4837 . Please rebase your PR #830 to main , with the rule of your second bullet.

@morten-lagabote

Copy link
Copy Markdown
Contributor Author

Rebased on main (186741a) with the rule of the second bullet. test_list_item_text_stays_apart_from_its_inline_group from #693 now expects the continuation paragraph. In docling, seven test cases and five Markdown files expect the previous output; the description lists them. I can open the docling pull request for them after the release.

@ceberam
ceberam self-requested a review November 25, 2026 13:02

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @morten-lagabote for the work and for the very detailed description, it made the review much easier. I have a few requests (mostly about test coverage) and some non-blocking remarks.

  1. Rebase on main. The merge is clean and the suite passes, but the branch is 17 commits behind and markdown.py changed in the meantime, so please rebase so that the CI result reflects the current state.

  2. Test the other block types. The description says tables, sub-lists and pictures are handled, but the new tests only exercise paragraphs, inline groups, code and (in the inline-group test) a sub-list. A table and a picture (with a caption) as children of a list item are the most likely to regress, because they are multi-line, and they are the ones that change in the reference data (virtual_text_list and kv). Please add one test with a TableItem and a PictureItem child. It can be one small test with a literal expected string, like the others.

  3. Test the new behavior of MarkdownListSerializer on its own. The change from f"{indent_str}{c.text}" to textwrap.indent(c.text, indent_str) alters the output for every nested item whose text has more than one line (for example a hard line break "line1\nline2" in a nested item: the second line is now indented, before it was a lazy continuation at column 0), even without any continuation child. This is an improvement, but it is a separate behavior change and nothing in the tests covers it in isolation (the nested=True case only has single-line item text). Please add a case, or mention it explicitly in the PR description and in the commit message.

  4. Non-default indent. The continuation blocks (and a sub-list that sits next to other blocks) use the content column and ignore MarkdownParams.indent, while a sub-list that is the only child still uses indent (as before). With indent=2 and an ordered list this gives 1. A / 1. B, which CommonMark does not parse as nested, whereas 1. A + paragraph + sub-list gives a valid result. The indent=2 case is pre-existing and not caused by this PR, but now the same item type is indented in two different ways depending on its children.

Some suggestions about the code style:

  • has_inline_repr now reads len(item.children) > 0 and (len(item.children) == 1 or isinstance(item, ListItem)). This is equivalent to len(item.children) == 1 or (isinstance(item, ListItem) and len(item.children) > 0) and the second form states the intent more directly. Note that item.children[0] is evaluated in the same expression, so the emptiness check must stay before it.
  • The tweak texts[:2] = [f"{texts[0]}\n{texts[1]}"] works, but it is a bit hard to follow, because it mutates the list in place after the fact and depends on the condition text_part and not has_inline_repr and item.text == "" (which is only meaningful for list items). Building the join explicitly (for example computing the separator after the first part) would be easier to read and to maintain.
  • text += "\n" # blank line before the next sibling list item creates an implicit contract with MarkdownListSerializer (its sep = "\n" and the final rstrip("\n") produce the blank line and remove the trailing one for the last item). It works, and I checked the last-item, nested and orphan cases, but the comment should say that the list serializer relies on it, because somebody changing the separator there will break the blank line silently.

@morten-lagabote
morten-lagabote force-pushed the fix/markdown-inline-group-after-sub-list branch from 28eac2e to 0d253ed Compare November 26, 2026 06:06
@morten-lagabote

Copy link
Copy Markdown
Contributor Author
  1. Rebased on main (f347b36).
  2. Added test_md_list_item_continuation_table_and_picture: a table and a picture with a caption in a list item, with a literal expected string. It fails on main, where the table, the caption and the image are at column 0 after - Results.
  3. Added test_md_nested_list_item_with_line_break: a nested item with the text "line1\nline2" and no other child. main gives - line1 \nline2, this branch gives - line1 \n line2. The description and the commit message of the test state this change.
  4. The continuation blocks use the content column because it is valid CommonMark for every marker and every indent. A sub-list that is the only child keeps indent, so that the output of nested lists does not change. max(indent, content column) for the continuation blocks gives one rule: with the default indent=4, all the children of -, 1. and 10. items are at 4 spaces. But with an indent of 4 or more columns past the content column, for example indent=6 and -, a continuation paragraph becomes an indented code block. I kept the content column. Tell me if you prefer the other rule.

The code style changes are in 91e6ae8, with no change in the output.

@github-actions github-actions Bot removed the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 26, 2026
morten-lagabote and others added 7 commits November 26, 2026 17:47
…ist item text

MarkdownListSerializer appended every part whose items are in an InlineGroup
to the previous part. That is right for the text of a list item with formatting
or hyperlinks, which the backends add as an InlineGroup, the first child of a
ListItem with an empty text. But any other InlineGroup of a list item, e.g. text
after a sub-list or a second paragraph, was appended to the line of the previous
part.

Such a group is now serialized as a part of its own, like a TextItem in the
same position.

Resolves docling-project/docling#4526

Signed-off-by: Morten Dæhli Aslesen <[email protected]>
Signed-off-by: Cesar Berrospi Ramis <[email protected]>
Co-authored-by: Cesar Berrospi Ramis <[email protected]>
…t column

A list item owns its continuation blocks: paragraphs, inline groups, code,
tables and sub-lists after its text. The Markdown serializer wrote them at
column 0 without a blank line, so a Markdown parser took them out of the
list. They are now serialized with the item, indented to its content column
and separated by blank lines, as for a loose list in CommonMark.

An inline group is the item text only if it is the first child of an item
with an empty text. Any other inline group is a continuation block. The list
serializer no longer needs a special case for list items.

An item with sub-lists only is serialized as before.

Co-authored-by: w3lld1 <[email protected]>
Signed-off-by: Morten Dæhli Aslesen <[email protected]>
Signed-off-by: Cesar Berrospi Ramis <[email protected]>
Co-authored-by: Cesar Berrospi Ramis <[email protected]>
…fter the list item text

A list item with its own text and an inline-group child now gives the group as a continuation paragraph, by the rule of section B in docling-project/docling#4837. The group is the item text only when the item has no text and the group is its first child.

Signed-off-by: Morten Dæhli Aslesen <[email protected]>
The separator after the first part is now computed, instead of a merge of
the first two parts after the fact. The emptiness check in
has_inline_repr is part of the list item case. Comments in both
serializers state that the blank line between items depends on the "\n"
separator of MarkdownListSerializer.

The output does not change.

Signed-off-by: Morten Dæhli Aslesen <[email protected]>
Signed-off-by: Cesar Berrospi Ramis <[email protected]>
Co-authored-by: Cesar Berrospi Ramis <[email protected]>
…e break

A table and a picture with a caption in a list item: every line of the
block is at the content column of the item.

A nested item with a line break in its text: MarkdownListSerializer now
indents every line of the item. Before, it indented the first line only,
and the next lines were lazy continuation lines at column 0. This applies
also to an item with no continuation blocks.

Signed-off-by: Morten Dæhli Aslesen <[email protected]>
Describe what the code does now, without the history of the change and without references to a discussion.

Signed-off-by: Cesar Berrospi Ramis <[email protected]>
@ceberam
ceberam force-pushed the fix/markdown-inline-group-after-sub-list branch from 0d253ed to d9c88f7 Compare November 26, 2026 16:54

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @morten-lagabote ! Since we merged quite a few PRs today, more conflicts showed up, so I did yet another rebase and fix them directly. I also added an extra commit to reduce the inline comments to what's the really necessary for future maintainers.
All good to merge now 🚀

@ceberam
ceberam merged commit 1d55d90 into docling-project:main Nov 27, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working serialization Output serializers: Markdown, HTML, LaTeX, JSON, plain-text, outline

Projects

None yet

4 participants