fix(links): attach a link to every span its rect covers (#42) - #59
Open
Anai-Guo wants to merge 1 commit into
Open
fix(links): attach a link to every span its rect covers (#42)#59Anai-Guo wants to merge 1 commit into
Anai-Guo wants to merge 1 commit into
Conversation
A linked phrase is split into several spans whenever the font changes, but merge_links only kept the span with the largest overlap, so all the other words of the anchor came back with an empty url. Fixes datalab-to#42
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.
Fixes #42.
Problem
merge_linksmaps each link annotation onto exactly one span:A linked phrase is rarely a single span, though — spans get split whenever the font
changes and at the zero-width spacers pdfium emits between words. So for a link over
"Claude 3.7 Sonnet", only the span with the largest overlap keeps the url and every
other word of the same anchor comes back with
"url": "", which is what #42 reports.Fix
Attach the link to every span it substantially covers, normalizing the overlap by
min(span_area, link_area). That keeps both directions:Sonnet) → ratio 1.0, matched;→ ratio 1.0, matched, and
_reconstruct_spansthen splits it char by char, exactlyas before;
ignored, so links don't bleed across lines.
The span with the largest overlap is always kept, so nothing that resolves today
stops resolving.
_reconstruct_spansis unchanged and still decides per character,so a partially covered span is split rather than tagged wholesale.
Verification
Reproducer — one link annotation over two runs in different fonts:
before:
after:
test_link_spans_whole_anchorcovers this and also asserts the unlinked line rightbelow stays unlinked. It fails on
master(assert 'Claude' == 'Claude Sonnet') andpasses with the change. Full suite: 43 passed before, 44 passed after.
🤖 Generated with Claude Code