Repository navigation
fix(markdown): indent the block children of a list item to its content column - #830
Conversation
|
✅ DCO Check Passed Thanks @morten-lagabote, all your commits are properly signed off. 🎉 |
Merge Protections🟢 All 2 merge protections satisfied — ready to merge. Show 2 satisfied protections🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
🟢 Require two reviewer for test updatesWhen test data is updated, we require two reviewers
|
4137bb0 to
fb52f40
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@morten-lagabote please check my comment #844 (comment) on how to proceed with this PR |
AI triage (advisory)Checked commit Warning The AI analysis did not finish. Only the deterministic checks are shown. CI safety: not assessed. Possibly related pull requests
The |
|
@ceberam Since #693,
With #830 rebased on Which rule do you want #830 to follow? I will push the rebase when the rule is clear. |
|
@morten-lagabote I think we should stick to the contract on docling-project/docling#4837 . Please rebase your PR #830 to |
5511360 to
28eac2e
Compare
|
Rebased on |
ceberam
left a comment
There was a problem hiding this comment.
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.
-
Rebase on
main. The merge is clean and the suite passes, but the branch is 17 commits behind andmarkdown.pychanged in the meantime, so please rebase so that the CI result reflects the current state. -
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_listandkv). Please add one test with aTableItemand aPictureItemchild. It can be one small test with a literal expected string, like the others. -
Test the new behavior of
MarkdownListSerializeron its own. The change fromf"{indent_str}{c.text}"totextwrap.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 (thenested=Truecase only has single-line item text). Please add a case, or mention it explicitly in the PR description and in the commit message. -
Non-default
indent. The continuation blocks (and a sub-list that sits next to other blocks) use the content column and ignoreMarkdownParams.indent, while a sub-list that is the only child still usesindent(as before). Withindent=2and an ordered list this gives1. A/1. B, which CommonMark does not parse as nested, whereas1. A+ paragraph + sub-list gives a valid result. Theindent=2case 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_reprnow readslen(item.children) > 0 and (len(item.children) == 1 or isinstance(item, ListItem)). This is equivalent tolen(item.children) == 1 or (isinstance(item, ListItem) and len(item.children) > 0)and the second form states the intent more directly. Note thatitem.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 conditiontext_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 itemcreates an implicit contract withMarkdownListSerializer(itssep = "\n"and the finalrstrip("\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.
28eac2e to
0d253ed
Compare
The code style changes are in 91e6ae8, with no change in the output. |
…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]>
…spaces Signed-off-by: Morten Dæhli Aslesen <[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]>
0d253ed to
d9c88f7
Compare
ceberam
left a comment
There was a problem hiding this comment.
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 🚀
Resolves #844
Resolves docling-project/docling#4526
A
ListItemowns its continuation blocks: paragraphs, inline groups, code, tables and sub-lists after its text (section D in docling-project/docling#4837).MarkdownTextSerializerwrote them at column 0 without a blank line, andMarkdownListSerializerappended everyInlineGroupto the previous line. A Markdown parser then took the blocks out of the list.Changes
MarkdownTextSerializerserializes the block children of aListItemwith 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.InlineGroupis the item text if it is the first child of an item with an empty text. Any otherInlineGroupis a continuation block. The inline groups of a list item no longer reach the append inMarkdownListSerializer.test_list_item_text_stays_apart_from_its_inline_groupfrom feat: make inline serializers concatenate runs faithfully #693 now expects the group after the item text as a continuation paragraph:- Intro, a blank line, thenmore **x** y.MarkdownListSerializerindents 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:maingives- line1 \nline2(a lazy continuation line at column 0), this branch gives- line1 \n line2.tests/test_serialization.py.Two details differ from #845:
- \n\n textis an empty item and a paragraph after the list.indentspaces. 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 (
main186741a): 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:
Test fails without fix: 14 of the 15 new or changed test cases fail on
main(f347b36). The label filter case passes onmainand guards against a regression.Reference data: no change in docling-core. In docling (a7760abf), with this branch:
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 in2305.03393_main.tex.md, where the paragraph joined the item text. Inelife-56337.nxml.mdthe paragraphs became setext headings.test_list_item_text_keeps_boundary_before_rich_paragraph(BoxNote) andtest_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 oftest_latex_enumerate_item_starting_with_paragraph_breakandtest_latex_enumerate_line_of_item_starting_with_formulafrom fix(latex): number the items of enumerate lists docling#4518. With this branch, the continuation block is in the item.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 onmainf347b36.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
DoclingDocumentJSON ground-truth files in docling (a7760abf) was compared betweenmain(186741a) and this branch. It changes for four of the five files above (asciidoc_03has no JSON ground truth) and for no PDF.