Skip to content

fix(email): preserve HTML body structure - #5324

Open
krishhgg wants to merge 1 commit into
docling-project:mainfrom
krishhgg:fix/email-html-body-items
Open

krishhgg wants to merge 1 commit into
docling-project:mainfrom
krishhgg:fix/email-html-body-items

Conversation

@krishhgg

Copy link
Copy Markdown

Follow-up to #5219 after #4828 and #5228. Those fixed the sample in the issue, but an HTML body is still exported to Markdown and stored as text items. Every export of the email escapes that Markdown again, so a code span shows a < b and a table cell shows 1 & 2. Headings, lists and tables reach the JSON and text exports as Markdown text.

The email document now takes the items of the HTML conversion through DoclingDocument.concatenate(), as the XBRL backend does for its HTML text blocks. concatenate() remaps caption, footnote and rich table cell refs. add_document() does not. When the email has a subject, a body <h1> becomes a level 1 section header and the other body headings move down one level, so the subject stays the only title and comes first in chunk headings.

One change also reaches plain-text emails. The attachments heading moves from level 2 to level 1, so it sits directly under the subject instead of under the last body heading. In Markdown, ### Attachments becomes ## Attachments for every email that lists attachments.

This overlaps with two open PRs:

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

Review evidence:

Source: the HTML-only message from #5219 with a code span and a table added. On main the body items hold Markdown text, and the export shows Run `a &lt; b` first. and | a\_b | 1 &amp; 2 |. Now the tree has text, code and table items, the footer is a furniture item, and the export shows Run `a < b` first. and | a_b | 1 & 2 |.

Source: subject "Quarterly report", body <h1>Revenue</h1> and <h2>Details</h2>, one attachment listed. On main the body headings are text items such as # Revenue, so every body chunk only gets the subject as its heading. Now HierarchicalChunker gives ["Quarterly report", "Revenue", "Details"] for the body and ["Quarterly report", "Attachments"] for the attachment list.

The new tests cover escaping, heading levels and chunk headings, the attachments level, inline group parents, caption and rich table cell refs across two HTML parts, a JSON round trip and the text export.

  • uv run pytest tests/test_backend_email.py -q: 38 passed. The three new tests fail on main.
  • uv run pytest tests/test_backend_email.py tests/test_input_doc.py tests/test_backend_optional_dependencies.py -q: 147 passed.
  • make validate and make check: passed. make check prints only existing warnings.
  • Not run: the full test suite with the model and OCR tests.
  • No reference data changes. The email tests have no groundtruth files.

The email backend exported each text/html part to Markdown and stored
the result as text items. Every export of the email then escaped that
Markdown again, so a code span showed `a &lt; b` and a table cell showed
1 &amp; 2. Headings, lists and tables reached the JSON and text exports
as Markdown text.

Concatenate the HTML conversion into the email document instead, so the
body keeps its items, labels and content layers. concatenate() also
remaps caption, footnote and rich table cell references, which
add_document() does not.

When the email has a subject, turn a body <h1> into a level 1 section
header and move the other body headings down one level, so the subject
stays the only title and leads the chunk heading context. Make the
attachments heading level 1, so it ranks directly under the subject.

Signed-off-by: Krish Garg <181051779+krishhgg@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @krishhgg, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 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)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added bug Something isn't working email issue related to email (EML, MSG) backend labels Nov 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit 9fb37b747b. 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.

Topic labels added: bug, email.

CI safety: not assessed.

Possibly related pull requests

PR State Signal Assessment
#3978 open referenced as #3978; changes the same code not assessed
#5228 merged also references issue #5219; referenced as #5228 not assessed
#5275 open changes the same code not assessed
#4877 open referenced as #4877 not assessed
#4828 merged referenced as #4828 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.

@github-actions github-actions Bot 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.

AI first review (advisory): no blocker found

This review covers the full diff at 9fb37b747b. It read the code and did not run it.

I found no blocker. DoclingDocument.concatenate() remaps caption, footnote and rich-cell refs, and the new tests cover this. The one point to check is the heading-level rule in _nest_headings_under_subject(). It does not match what the docs say.

A maintainer makes the merge decision. This review never approves the pull request.


Email conversion prefers non-empty plain-text bodies. When it uses an HTML body,
text in `<footer>` elements is retained in the furniture content layer. Default
the email holds the items of the HTML conversion of that body, such as headings,

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.

suggestion: Docs say all body headings move down one level, but the code only does so when the body has an <h1>

_nest_headings_under_subject() in docling/backend/email_backend.py returns early when the HTML part has no TitleItem, so only a body with an <h1> gets shifted. An email with a subject and a body that has only <h2> or <h3> keeps those levels unchanged: <h2> stays level 1. The docs say "the other headings move one level down" without this condition. Add the condition to the docs, for example "If the body has an <h1>, ...". The behavior itself is consistent: the <h2> headings still sit directly under the subject, at the same level as the attachments heading.

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Nov 13, 2026
@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.54839% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/email_backend.py 93.54% 0 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:review-lgtm AI first review found no blocker or major issue (advisory) bug Something isn't working email issue related to email (EML, MSG) backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant