Skip to content

fix(markdown): preserve non-line fragments in preview links - #332661

Merged
Aaron Munger (amunger) merged 3 commits into
microsoft:mainfrom
shoemoney:fix/markdown-pdf-fragment
Oct 2, 2026
Merged

Aaron Munger (amunger) merged 3 commits into
microsoft:mainfrom
shoemoney:fix/markdown-pdf-fragment

Conversation

@shoemoney

@shoemoney Jeremy Schoemaker (shoemoney) commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Markdown preview clicks on [pdf](file.pdf#page=3) lose their non-line fragment. This change forwards the percent-decoded fragment in MdLinkOpener, allowing a fragment-aware viewer to open the requested page. Existing line and heading resolution paths are unchanged.

This PR covers Markdown preview links only. Relative-link Ctrl+click in the Markdown source editor remains tracked in #338920, including the encoded #page%3D3 case. This PR does not close that issue.

Maintainer validation at bea9c5f used mathematic.vscode-pdf 0.2.6 in Code OSS Dev: the patched preview opened page 3, while the baseline opened page 1. The same report confirms that the relative-link source-editor case is still unfixed.

Fix verified RED->GREEN. Markdown file links drop non-line fragments (#page=3) - PDFs always open on page 1 at openDocumentLink.ts:43
Copilot AI balanced review requested due to automatic review settings August 26, 2026 00:06

Copilot AI 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.

Pull request overview

This PR fixes a bug in the built-in markdown language features extension where file links with non-line fragments (e.g. [pdf](file.pdf#page=3)) lost their fragment when opened, causing PDF viewers to always land on page 1. It adds a fallback in MdLinkOpener.openDocumentLink so that, when the fragment is not a recognized line/location fragment, the raw fragment is still attached to the target URI.

Changes:

  • Add an else branch after the location-fragment handling to extract the raw fragment (#...) from the link text and set it on the URI when no line fragment matches.
  • Preserves fragments like #page=3 / #nameddest for PDF and other custom viewers while keeping line-navigation behavior intact.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@shoemoney

Copy link
Copy Markdown
Contributor Author

Done, fixed in bea9c5f, the raw fragment is now decoded with decodeURIComponent (wrapped in try/catch) before Uri.with, matching getLocationFragmentFromLinkText.

@amunger

Copy link
Copy Markdown
Collaborator

please open an issue for this with steps to reproduce the problem

@shoemoney

Copy link
Copy Markdown
Contributor Author

Opened #338920 with repro steps.

@amunger

Aaron Munger (amunger) commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the fix. I tested the exact changed opener from bea9c5f in an isolated Markdown extension copy, running in a real Code OSS Dev window with mathematic.vscode-pdf 0.2.6. Preview clicks on [pdf](file.pdf#page=3) now reach page 3, while the baseline opens page 1.

However, Ctrl+clicking the same relative link in the Markdown source editor still opens page 1 with the patch. The encoded variant file.pdf#page%3D3 has the same result. Absolute file:// links in the source editor already work on the baseline, so this finding is specifically about the relative Markdown-link path.

The preview path uses resolveLinkTarget and then the patched MdLinkOpener; the editor path uses resolveDocumentLink and bypasses that opener. Both resolution methods use the language service's shared internal-link resolver, where the unconsumed fragment is lost.

Could fragment preservation be handled at that shared boundary, or otherwise cover both opening paths, with regression tests for fragment forwarding/decoding and existing line/heading navigation? Both interactions are included in #338920. I understand the shared-boundary fix may require an upstream language-service change and dependency update; if this PR is intentionally preview-only, please make that scope explicit and keep the editor case tracked separately.

[comment authored by copilot]

@shoemoney Jeremy Schoemaker (shoemoney) changed the title fix(markdown): preserve non-line fragments like #page=3 for PDF links fix(markdown): preserve non-line fragments in preview links Oct 1, 2026
@shoemoney

Copy link
Copy Markdown
Contributor Author

Thanks for checking both paths. I've narrowed the title and description to Markdown preview links. The relative-link Ctrl+click case, including #page%3D3, remains tracked in #338920, so this PR does not close that issue. The code is unchanged at bea9c5f; the description now cites your preview reproduction and states the remaining editor limitation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants