fix(docx): clamp Word Heading 7-9 to h6 - #2555
Open
stanleys12 wants to merge 1 commit into
Open
stanleys12 wants to merge 1 commit into
stanleys12 wants to merge 1 commit into
Conversation
Mammoth's default style map covers Heading 1-6, so paragraphs styled Heading 7, 8 or 9 fall through as plain text and lose their heading structure. Markdown has no level past 6, so add style map entries that clamp those levels to h6, in both the style id and style name forms the default map uses.
Author
|
@microsoft-github-policy-service agree |
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.
Word defines Heading styles 1 through 9, but mammoth's default style map stops at Heading 6 and
DocxConverteradds nothing beyond it, so paragraphs styled Heading 7, 8 or 9 come through as plain text and lose their heading structure entirely.A document with one paragraph per heading level converts like this on
main:Nothing warns about it, and downstream the last three levels read as body text rather than as sections. Deep heading levels are common in legal and technical-spec documents, which is the kind of long structured input this library is usually pointed at.
Fix
Markdown has no heading level past 6, so clamp 7-9 to
h6instead of dropping them: a new_DEEP_HEADING_STYLE_MAPis joined into the style map alongside_UNDERLINE_STYLE_MAP. It uses both selector forms that mammoth's default map already uses for levels 1-6:Both are needed. mammoth matches style ids exactly, so the
p.HeadingNform is what catches a document with no stylesheet part; it matches style names case-insensitively, so thestyle-nameform is what catches the non-English style ids a localized Word writes (Titre7,Überschrift7) via<w:name w:val="heading 7"/>. The entries go after the caller-supplied and embedded maps in the existing join, so a caller passingstyle_map=still overrides the clamp, the same way it overrides theu => udefault.After:
Documents that do not use Heading 7-9 are unaffected.
Tests
New file
packages/markitdown/tests/test_docx_headings.py, over nine-heading documents built in memory:style_mapstill outranks the clampAgainst unmodified
main:With the fix:
The third test passes before and after; it is there so the ordering inside the style map join stays overridable.
Verification
packages/markitdown:python -m pytest tests -q-> 998 passed, 14 skippedpackages/markitdown-ocr:python -m pytest -q-> 108 passed (DocxConverterWithOCRreuses the core pipeline, so the clamp reaches it too)black --check packages/with black 23.7.0 -> 107 files unchanged;git diff --checkcleanRun on macOS / CPython 3.13; the CI matrix was not reproduced locally.
Fixes #2530. This is the heading part of #2531, which was closed because it was bundled with unrelated whitespace changes; rebuilt here on its own.
AI assistance (Claude) was used to draft this change; the full
packages/markitdowntest suite (998 passed) andblack --checkwere run locally.