Repository navigation
fix(md): decode character references in link destinations - #5250
jaeminbird wants to merge 2 commits into
Conversation
AI triage (advisory)Checked commit Warning The AI analysis did not finish. Only the deterministic checks are shown. CI safety: not assessed. Possibly related pull requests
The |
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
There was a problem hiding this comment.
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.
| # marko keeps the character references of a destination; CommonMark | ||
| # decodes them, so `?a=1&b=2` links to `?a=1&b=2`. | ||
| return TypeAdapter(AnyUrl | Path | None).validate_python( | ||
| _decode_char_references(dest) |
There was a problem hiding this comment.
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 & or & 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.
There was a problem hiding this comment.
Right: CommonMark keeps autolink URIs literal (commonmark.js 0.31.2 renders <http://a.com/?x=1&y=2> with & 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.
There was a problem hiding this comment.
6e2b8c8 adds the bare-URL cases to test_autolink_character_references_are_kept (prose and table cell); cmark-gfm keeps & in the href there too.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
| # marko keeps the character references of a destination; CommonMark | ||
| # decodes them, so `?a=1&b=2` links to `?a=1&b=2`. | ||
| return TypeAdapter(AnyUrl | Path | None).validate_python( | ||
| _decode_char_references(dest) |
|
✅ DCO Check Passed Thanks @jaeminbird, all your commits are properly signed off. 🎉 |
|
Hi @jaeminbird, thank you for this pull request. It has a merge conflict with git fetch upstream
git rebase upstream/main
# resolve the conflicts, then:
git push --force-with-leaseWe can continue with the review and CI after that. |
a9ed911 to
1bb5b8c
Compare
1bb5b8c to
6e2b8c8
Compare
6e2b8c8 to
39e41fc
Compare
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&b=2) linked to ?a=1&b=2, and with fetch_images  loaded a&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>
39e41fc to
b2f491f
Compare
Markdown that writes HTML-safe URLs gets a broken hyperlink.
[q](https://example.com/?a=1&b=2)converts to the hyperlinkhttps://example.com/?a=1&b=2, so the second query parameter becomesamp;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. Withfetch_images,also tried to loada&b.png.Cause:
MarkdownDocumentBackend._resolve_link_dest()and_load_image_ref()used marko's rawdest. 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
&in the href of<http://a.com/?x=1&y=2>and of the bare URL).Checklist:
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(locala&b.pngwithfetch_imagesandenable_local_fetch): fails with the image line reverted, passes with it.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 checkandruff 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.