Skip to content

fix: links in a PDF or HTML page and files dropped outside Files no longer take the reader away - #218

Merged
bencode merged 3 commits into
mainfrom
fix/links-and-drops-stay-in-reader
Oct 26, 2026
Merged

bencode merged 3 commits into
mainfrom
fix/links-and-drops-stay-in-reader

Conversation

@bencode

@bencode bencode commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

Three natural gestures took the reader away from what they were reading: following a link in a PDF, following a link in an HTML page, and dropping a file anywhere but Files.

PDF (#189)

  • <Document> gets onItemClick, so a table of contents, "see Figure 3" or citation turns the page the way the outline does. react-pdf keeps the first onItemClick in a ref (viewer = useRef({ scrollPageIntoView })), when pageCount is still 0 and navigate returns early — so the handler reaches the current navigate through navigateRef.
  • externalLinkTarget="_blank": without a target, pdf.js's web links replaced the app in the tab.
  • A link lands on the top of its page; the destination's position within the page is not followed.

HTML page and HTML embedded in a note (#204)

  • The pass that already inlines library images (withLibraryImages in html-images.ts, now framePage in html-page.ts since it does more) gives http(s) links target="_blank", the rule MarkdownAnchor follows; both frames add allow-popups allow-popups-to-escape-sandbox, so the new tab is an ordinary page. No allow-same-origin or allow-top-navigation: the page still cannot reach the library or replace the app.
  • Links within the page (#section) never worked in the sandboxed blob frame — Chrome ignores the fragment navigation, confirmed against the old allow-scripts sandbox alone. A page that has one now gets a few lines of script that scroll to the element by id or name — looked up as written first, then decoded, as the browser does — #/#top to the top, and step aside when the page handled the click itself. Only the displayed copy changes; the file and its Source are untouched.
  • Relative links are left alone: they cannot resolve against a blob URL, so a click does nothing, as before.
  • Known limits: only links written in the page are handled. A link a page script creates later, and a relative link in a page with <base href>, still navigate the frame. The injected script has no unit test: jsdom does not run it, so it is verified in the browser, including a malformed % in an id.

Files dropped outside Files (#185)

  • main.tsx cancels dragover/drop carrying Files at the window, so a missed drop no longer opens the file in the tab. Files and the composer handle their drops first, as before; tab reordering is untouched. dropEffect is left alone so a drop into the Source editor still behaves as CodeMirror makes it.
  • Not done: the workbench as a whole importing a drop (the issue's second point).

Verification

  • pnpm check passes. The two tests that pinned sandbox="allow-scripts" now pin the new value.
  • End to end against a local server with agent-browser:
    • a generated 3-page PDF: the link on page 1 opens page 3; its URL opens example.com in a new tab while the app stays;
    • an HTML page and the same page embedded in a note: the web link opens in a new tab and the frame keeps the page; #method, a percent-encoded Chinese id, a <a name> anchor and #top each scroll to their place; a link the page handles itself is left to the page;
    • a synthetic file drag: on the reading pane dragover/drop are cancelled and nothing is imported; on Files the file is imported; on the composer it is attached; a tab drag is not cancelled.
  • Not verified: a real OS drop onto an HTML preview frame. Events there go to the frame's own document and never reach this listener.

HTML charset

The frame's blob was text/html with no charset, so a page without its own <meta charset> was read as Latin-1 and its Chinese showed as mojibake. A Blob always writes text as UTF-8, so the blob now says text/html;charset=utf-8. The file itself is untouched. Verified end to end: a Chinese page with no meta renders its headings and links correctly.

Closes #189, closes #204, closes #185

…onger take the reader away from what they are reading

- A PDF's internal links turn to the page they name; its web links open in a new tab instead of replacing the app.
- An HTML page's web links open in a new tab, and its links within the page scroll to their place, which the sandbox kept from working before.
- A file dropped where nothing takes it is ignored instead of opening in the tab.
…ter the page, and look a link's place up as written before decoding it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment