Skip to content

fix(md): decode character references in link destinations - #5250

Open
jaeminbird wants to merge 2 commits into
docling-project:mainfrom
jaeminbird:fix/md-decode-link-destination-references
Open

jaeminbird wants to merge 2 commits into
docling-project:mainfrom
jaeminbird:fix/md-decode-link-destination-references

Conversation

@jaeminbird

@jaeminbird jaeminbird commented Nov 9, 2026 •

Copy link
Copy Markdown

Markdown that writes HTML-safe URLs gets a broken hyperlink. [q](https://example.com/?a=1&b=2) converts to the hyperlink https://example.com/?a=1&b=2, so the second query parameter becomes amp;b. & and named references such as ö in a path are also kept as written. CommonMark decodes character references in destinations (spec examples 32, 33 and 503), and the HTML backend already gets decoded hrefs from BeautifulSoup. With fetch_images, ![](a&b.png) also tried to load a&b.png.

Cause: MarkdownDocumentBackend._resolve_link_dest() and _load_image_ref() used marko's raw dest. Titles were already decoded with _decode_char_references() (#5231).

Change: decode the destination with the same helper in both places. Inline, reference and table-cell links and image sources are decoded. Autolinks and GFM bare URLs keep their URI literal, as CommonMark and cmark-gfm do (both keep &amp; in the href of <http://a.com/?x=1&amp;y=2> and of the bare URL).

Checklist:

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

Review evidence:

Before/after and checks (main 336e9dd, Windows, Python 3.12)
  • test_link_destination_character_references_are_decoded (inline, reference, table cell): 3/3 fail on main, pass on the branch.
  • test_autolink_character_references_are_kept (autolink and bare URL, each in prose and in a table cell): 4/4 pass on main and on the branch.
  • test_image_destination_character_references_are_decoded (local a&b.png with fetch_images and enable_local_fetch): fails with the image line reverted, passes with it.
  • Link targets of the CommonMark 0.31.2 spec examples: of 118 examples with links, 22 differ from the spec on main and 19 on the branch. 32, 33 and 503 are fixed; no new differences.
  • pytest tests/test_backend_markdown.py tests/test_backend_markdown_images.py tests/test_markdown_math.py tests/test_backend_email.py tests/test_inline_whitespace_contract.py: 197 passed; 1 failure (test_html_pre_keeps_interior_whitespace) fails on main too. No reference data changes.
  • ruff check and ruff format --check (0.17.2) on the two changed files: pass.
  • ty check docling/backend/md_backend.py: the same 4 warnings as main. tach check: pass.
  • Not run: remote image fetch, Linux.

@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 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit b2f491f8e6. 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.

CI safety: not assessed.

Possibly related pull requests

PR State Signal Assessment
#5231 merged referenced as #5231 not assessed
#38 merged referenced as #38 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.

@mergify

mergify Bot commented Nov 9, 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 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 cb457cbb49. It read the code and did not run it.

The fix is small and correct for inline and reference links and for image sources. The risk is that autolinks (<…>) and GFM bare URLs also go through _resolve_link_dest(), and CommonMark does not decode character references there. The PR cites only spec examples 32, 33 and 503, and none of them is an autolink.

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

Comment thread docling/backend/md_backend.py Outdated
# marko keeps the character references of a destination; CommonMark
# decodes them, so `?a=1&amp;b=2` links to `?a=1&b=2`.
return TypeAdapter(AnyUrl | Path | None).validate_python(
_decode_char_references(dest)

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.

question: Autolink and bare-URL destinations are now decoded; CommonMark does not decode them

_resolve_link_dest() is also called for marko.inline.AutoLink, which includes the GFM bare-URL subclass (docling/backend/md_backend.py:1001 and docling/backend/md_backend.py:645). In marko, AutoLink.dest is the raw text between < and >, with no escape processing (marko/inline.py:237). My understanding is that CommonMark treats an autolink URI literally and does not decode character references in it. Spec examples 32, 33 and 503 cover only inline and reference links and titles. The new autolink test case asserts the decoded result, so it fixes this behavior without a spec reference. A literal &amp; or &#38; in a URL is rare, but it is valid input.

Please cite the spec example or the reference-implementation output that supports decoding autolinks. If none exists, decode only for Link, or pass a flag from the Link call sites, and drop the autolink case from the test. The question is also open for GFM bare URLs. The review has no test for them.

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.

@jaeminbird can you answer this question?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Right: CommonMark keeps autolink URIs literal (commonmark.js 0.31.2 renders <http://a.com/?x=1&amp;y=2> with &amp; kept in the href). a9ed911 skips decoding for autolinks and GFM bare URLs, and test_autolink_character_references_are_kept covers both, in prose and in a table cell. Inline, reference and image destinations still decode. Thanks @PeterStaar-IBM.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

6e2b8c8 adds the bare-URL cases to test_autolink_character_references_are_kept (prose and table cell); cmark-gfm keeps &amp; in the href there too.

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

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread docling/backend/md_backend.py Outdated
# marko keeps the character references of a destination; CommonMark
# decodes them, so `?a=1&amp;b=2` links to `?a=1&b=2`.
return TypeAdapter(AnyUrl | Path | None).validate_python(
_decode_char_references(dest)

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.

@jaeminbird can you answer this question?

@github-actions github-actions Bot removed the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Nov 10, 2026
@PeterStaar-IBM

Copy link
Copy Markdown
Member

Hi @jaeminbird, thank you for this pull request.

It has a merge conflict with main. Please rebase your branch on the latest main and resolve the conflict:

git fetch upstream
git rebase upstream/main
# resolve the conflicts, then:
git push --force-with-lease

We can continue with the review and CI after that.

@jaeminbird
jaeminbird force-pushed the fix/md-decode-link-destination-references branch from a9ed911 to 1bb5b8c Compare November 13, 2026 05:38
@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) labels Nov 13, 2026
@jaeminbird
jaeminbird force-pushed the fix/md-decode-link-destination-references branch from 1bb5b8c to 6e2b8c8 Compare November 13, 2026 05:40
@github-actions github-actions Bot removed the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 13, 2026
@jaeminbird

Copy link
Copy Markdown
Author

Rebased onto main (336e9dd), conflict resolved: _resolve_link_dest() now uses the shared _LINK_DEST adapter from #5241. Head is 6e2b8c8. Ready for another look when you have time.

@github-actions github-actions Bot added the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 13, 2026
@jaeminbird
jaeminbird force-pushed the fix/md-decode-link-destination-references branch from 6e2b8c8 to 39e41fc Compare November 28, 2026 04:44
@jaeminbird

Copy link
Copy Markdown
Author

Rebased on main again (39e41fc) and resolved the conflict with #5462: _resolve_link_dest() now goes through parse_hyperlink(). Markdown tests still pass. Anything else needed here?

@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) labels Nov 28, 2026
marko keeps the character references of a link destination, and the
Markdown backend passed them on as is, so
[q](https://example.com/?a=1&amp;b=2) linked to ?a=1&amp;b=2, and with
fetch_images ![](a&amp;b.png) loaded a&amp;b.png. CommonMark decodes
them (spec examples 32, 33 and 503). Decode link and image destinations
with the same helper the backend uses for text and titles.

Signed-off-by: jaeminbird <jaeminbird@gmail.com>
Signed-off-by: jaeminbird <jaeminbird@gmail.com>
@jaeminbird
jaeminbird force-pushed the fix/md-decode-link-destination-references branch from 39e41fc to b2f491f Compare December 6, 2026 07:03
@jaeminbird

Copy link
Copy Markdown
Author

Rebased on a0bd015 (b2f491f), no conflicts this time; markdown tests and ruff pass. The changes-requested review is what still blocks the merge, and the autolink question it pointed to was answered in a9ed911 and 6e2b8c8. Could you re-review or dismiss it when you get a chance?

@github-actions github-actions Bot removed the ai:ci-safe AI triage found no CI-safety concern (advisory) label Dec 6, 2026

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

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