Fix flattening of visually-nested DOCX lists that use a separate numbering definition - #2332
Open
changsheng (changsheng0804-blip) wants to merge 1 commit into
Conversation
…ering definition Word often renders sub-lists (e.g. 'a) ... b) ...' below a numbered item) as an independent top-level numbering definition whose visual nesting comes only from its indentation in numbering.xml. mammoth flattens such lists because the paragraphs reference their own numbering definition at ilvl 0, so the converted Markdown loses the nesting and renumbers the sub-items at the top level. The new _pre_process_nested_lists step rewrites such runs to continue the parent list at a deeper ilvl when a list at ilvl 0 with a different numId directly follows another list paragraph and its level-0 indent is strictly greater than the preceding level's indent. Fixes microsoft#2323
|
changsheng (@changsheng0804-blip) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
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.
Description
Fixes #2323.
When a Word document contains a visually-nested sub-list such as
the current output flattens the
a) / b)sub-list into the top level and renumbers its items (4.,5.).Root cause: Word frequently stores such sub-lists as an independent top-level numbering definition (their own
numIdatilvl0); the visual nesting comes only from the level's left indent innumbering.xml. Since mammoth resolves nesting fromilvl, it emits the sub-items as siblings of the parent list, and the Markdown conversion faithfully flattens them. (Verified by inspecting the intermediate mammoth HTML and the document/numbering XML of the repro file from the issue: the sub-list paragraphs referencenumId=3, ilvl=0with left indent 1080, while the parent list level uses 720.)Fix: a new
_pre_process_nested_listsstep inpre_process_docxrewrites such runs to continue the parent list at a deeperilvl. A run is rewritten only when all of the following hold, keeping the heuristic conservative:ilvl0 with anumIddifferent from the immediately preceding numbered paragraph (any non-list paragraph in between prevents the rewrite);numbering.xml,w:left/w:start) is strictly greater than the indent of the preceding paragraph's list level;Documents without a
numbering.xmlpart, and any file in which preprocessing raises, are passed through unchanged (same fallback behavior as the existing math preprocessing step).Test evidence
All run locally (Windows, Python 3.12 venv,
pip install -e packages/markitdown[docx]):4. Item 3.1/5. Item 3.2at top level) to a properly nested list after this change.packages/markitdown/tests/test_docx_nested_lists.py(self-contained; they build minimal DOCX files in memory instead of adding binary fixtures):[xlsx]/[pptx], unrelated to DOCX); thetest.docxconversion vector passes.black --check(repo-pinned 23.7.0) passes on both changed files.