Skip to content

fix(md): apply the formatting of inline HTML tags such as <b> and <sup> - #5204

Open
chrikrah wants to merge 2 commits into
docling-project:mainfrom
chrikrah:fix/md-inline-html-formatting
Open

chrikrah wants to merge 2 commits into
docling-project:mainfrom
chrikrah:fix/md-inline-html-formatting

Conversation

@chrikrah

@chrikrah chrikrah commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

_iterate_elements (docling/backend/md_backend.py:1052 on 07154cd) drops marko.inline.InlineHTML, and with it the formatting of <b>, <em> or <sup> in a paragraph, heading or list item. CommonMark (0.31, section 6.6) passes them through as raw HTML, so a reader sees that formatting, and the HTML backend already keeps it.

Before, on 07154cd:

$ python repro.py   # "a <b>bold</b> and <em>italic</em>, H<sub>2</sub>O"
a bold and italic , H 2 O
[('a', None), ('bold', None), ('and', None), ('italic', None), (', H', None), ('2', None), ('O', None)]

After, on this branch:

$ python repro.py
a **bold** and *italic* , H 2 O
[('a', None), ('bold', {'bold': True}), ('and', None), ('italic', {'italic': True}), (', H', None), ('2', {'script': 'sub'}), ('O', None)]

Marko gives the tags as siblings of the text they wrap, so the children loop now carries the formatting from the opening tag to the closing one, using the HTML backend's _FORMAT_TAG_MAP. Other inline HTML is still dropped, and table cells (_iterate_cell_inline) do not change. No issue tracks this.

Checklist:

  • Documentation has been updated, if necessary.
    • Not applicable: no option or documented behaviour changes.
  • Examples have been added, if necessary.
    • Not applicable: the change adds no option or API.
  • Tests have been added, if necessary.

Review evidence:

# docling-core 2.105.0, ruff 0.17.2, ty 0.0.33, tach 0.35.3
$ python -m pytest -q tests/test_backend_markdown.py tests/test_backend_markdown_images.py tests/test_markdown_math.py tests/test_backend_html.py
205 passed, 1 skipped   # this branch; base 07154cd: 203 passed, 1 skipped
$ python -m pytest -q <same files>   # md_backend.py reverted, tests kept
2 failed, 203 passed, 1 skipped   # test_inline_html_formatting_tags: assert {} == {'bold': True}
$ python -m pytest -q <the 11 test files that touch the Markdown backend>
5 failed, 394 passed, 1 skipped   # base: 5 failed, 392 passed; the same 5 CLI and optional-dependency tests fail on both
$ ruff check <2 files> && ruff format --check <2 files>
All checks passed!
$ ty check docling/backend/md_backend.py
Found 4 diagnostics   # the same 4 warnings as on base
$ tach check
All modules validated!
# not run: the full test suite

No source in tests/data/md holds such a tag, so no ground truth changes.

@PeterStaar-IBM, an unclosed <b> now runs to the end of its paragraph, where a browser ends it too. I'd keep that, since it matches the rendered Markdown. Does that work for you?

Rebased onto 0fa0918.

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@mergify

mergify Bot commented Nov 6, 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 ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working markdown issue related to markdown backend labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

The PR makes the Markdown backend apply the formatting of inline HTML tags such as <b>, <em>, <sup> and <sub>. It carries the formatting from the opening tag to the closing tag, and reuses _FORMAT_TAG_MAP from the HTML backend. The PR adds two tests.

CI safety: no concern found. A maintainer must still approve CI.

2 related pull request(s) in the same code
PR State Signal Assessment
#5231 open changes the same code related: Both PRs change md_backend.py, but this PR handles inline HTML formatting tags. PR 5231 changes how character references are decoded. Both can be merged.
#5225 open changes the same code related: Both PRs change the inline loop in _iterate_elements, but PR 5225 adds LaTeX formula support. This PR adds inline HTML formatting. The changes are different and may need a small merge fix.

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 599280eeb2. It read the code and did not run it.

I found no defect that decides the merge. The change in docling/backend/md_backend.py is limited to marko.inline.InlineHTML children of one element. It applies only the tags in _FORMAT_TAG_MAP that carry a formatting, and an unclosed tag ends with its block. I checked this against the existing emphasis, heading, list-item and line-break paths and found no regression.

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

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Nov 6, 2026
Inline HTML in a Markdown paragraph, heading or list item was dropped
together with its formatting, so 'a <b>bold</b> c' gave plain text.
Track the tags that the HTML backend maps to a formatting across the
sibling nodes they wrap, and end them at their closing tag or with
their block.

Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com>
@chrikrah
chrikrah force-pushed the fix/md-inline-html-formatting branch from 599280e to 0103644 Compare November 6, 2026 01:26
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Nov 6, 2026

@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 0103644319. It read the code and did not run it.

I found no defect that decides the merge. I read the new children loop in _iterate_elements in docling/backend/md_backend.py against the existing emphasis, link, heading, list-item and line-break paths. I also read _FORMAT_TAG_MAP in docling/backend/html_backend.py. Formatting stays scoped to one element's children, and an unclosed tag ends with its block. The new import adds no dependency, because md_backend.py already imports html_backend through MarkdownHTMLDocumentBackend. The new tests cover the fixed defect and the unclosed-tag case. I could not see the test results for the 11 Markdown test files, because the PR body is cut off at "5 failed", so the maintainer must confirm that CI is green on the head commit.

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

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

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/md_backend.py 85.71% 1 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@PeterStaar-IBM

Copy link
Copy Markdown
Member

_iterate_elements (docling/backend/md_backend.py:1052 on 07154cd) drops marko.inline.InlineHTML, and with it the formatting of <b>, \<em\> or <sup> in a paragraph, heading or list item. CommonMark (0.31, section 6.6) passes them through as raw HTML, so a reader sees that formatting, and the HTML backend already keeps it.

Before, on 07154cd:

$ python repro.py   # "a <b>bold</b> and <em>italic</em>, H<sub>2</sub>O"
a bold and italic , H 2 O
[('a', None), ('bold', None), ('and', None), ('italic', None), (', H', None), ('2', None), ('O', None)]

After, on this branch:

$ python repro.py
a **bold** and *italic* , H 2 O
[('a', None), ('bold', {'bold': True}), ('and', None), ('italic', {'italic': True}), (', H', None), ('2', {'script': 'sub'}), ('O', None)]

Marko gives the tags as siblings of the text they wrap, so the children loop now carries the formatting from the opening tag to the closing one, using the HTML backend's _FORMAT_TAG_MAP. Other inline HTML is still dropped, and table cells (_iterate_cell_inline) do not change. No issue tracks this.

Checklist:

  • Documentation has been updated, if necessary.

    • Not applicable: no option or documented behaviour changes.
  • Examples have been added, if necessary.

    • Not applicable: the change adds no option or API.
  • Tests have been added, if necessary.

Review evidence:

# docling-core 2.105.0, ruff 0.17.2, ty 0.0.33, tach 0.35.3
$ python -m pytest -q tests/test_backend_markdown.py tests/test_backend_markdown_images.py tests/test_markdown_math.py tests/test_backend_html.py
205 passed, 1 skipped   # this branch; base 07154cd: 203 passed, 1 skipped
$ python -m pytest -q <same files>   # md_backend.py reverted, tests kept
2 failed, 203 passed, 1 skipped   # test_inline_html_formatting_tags: assert {} == {'bold': True}
$ python -m pytest -q <the 11 test files that touch the Markdown backend>
5 failed, 394 passed, 1 skipped   # base: 5 failed, 392 passed; the same 5 CLI and optional-dependency tests fail on both
$ ruff check <2 files> && ruff format --check <2 files>
All checks passed!
$ ty check docling/backend/md_backend.py
Found 4 diagnostics   # the same 4 warnings as on base
$ tach check
All modules validated!
# not run: the full test suite

No source in tests/data/md holds such a tag, so no ground truth changes.

@PeterStaar-IBM, an unclosed <b> now runs to the end of its paragraph, where a browser ends it too. I'd keep that, since it matches the rendered Markdown. Does that work for you?

Rebased onto 0fa0918.

@chrikrah Quicl question: if you adress the <sup>, why dont we do the <sub> too (your example for H2O would be nice for that)?

@PeterStaar-IBM
PeterStaar-IBM self-requested a review November 6, 2026 06:51

@PeterStaar-IBM PeterStaar-IBM 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.

can we do sub and sup in addition to bold and italic?

Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Nov 7, 2026

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@PeterStaar-IBM it does both. <sub> and <sup> go through the same _FORMAT_TAG_MAP lookup as <b>, so H2O already comes out with script: sub, and test_inline_html_formatting_tags asserts both scripts (tests/test_backend_markdown.py:1636). My opening line named only <sup>, which didn't help, and the body's Markdown export still prints H 2 O because docling-core's Markdown serializer has no sub/sup syntax, so the script field on the item is where it shows.

# git archive 0103644 (base 0fa0918), Python 3.12, marko installed, docling.__file__ from the export
$ python probe.py   # convert_string("H<sub>2</sub>O and E = mc<sup>2</sup>", format=InputFormat.MD), (text, script)
[('H', None), ('2', 'sub'), ('O and E = mc', None), ('2', 'super')]
$ python -m pytest -q tests/test_backend_markdown.py -k inline_html
2 passed, 62 deselected
# md_backend.py from 0fa0918, tests kept
[('H', None), ('2', None), ('O and E = mc', None), ('2', None)]
2 failed, 62 deselected
# not run: the full suite at this head

I merged main in 253042b to clear the conflict with #4991 in tests/test_backend_markdown.py, keeping both sets of tests: 80 passed there against 78 on main. Does the sub/sup answer clear your change request?

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:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working markdown issue related to markdown backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants