From 552ef5619570a5c4a7b98e366e1d0ff91f8caada Mon Sep 17 00:00:00 2001 From: shubham21155102 Date: Sat, 24 Jan 2026 05:52:23 +0000 Subject: [PATCH] Fix: Scroll markdown preview to top when navigating to new document Issue: Fixes microsoft/vscode#164071 Problem: When clicking a relative link in a markdown preview to navigate to another markdown document, the new document would open at the same scroll position as the previous document instead of scrolling to the top. This created a confusing user experience where users would see the middle or bottom of the new document without any context. Root Cause: The markdown preview webview was preserving the scroll position when updating content for a different markdown resource. When the 'updateContent' message was received with a new resource (data.source !== documentResource), the code would replace the body content but did not reset the scroll position. Solution: Added window.scrollTo(0, 0) call immediately after replacing the document body when navigating to a different markdown resource. This ensures that when a user clicks a relative link to another markdown file, the preview scrolls to the top of the new document, providing the expected navigation behavior. Changes: - extensions/markdown-language-features/preview-src/index.ts: Added scroll reset when navigating to different documents in the 'updateContent' message handler Testing: The fix can be manually tested by: 1. Creating two markdown documents with enough content to scroll 2. Adding a relative link from one document to the other 3. Opening the first document in markdown preview 4. Scrolling down and clicking the link 5. Verifying the second document opens at the top (not at the previous scroll position) Contributor: shubham21155102 Co-Authored-By: Claude Sonnet 4.5 --- extensions/markdown-language-features/preview-src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extensions/markdown-language-features/preview-src/index.ts b/extensions/markdown-language-features/preview-src/index.ts index b6200b8ceb9ff..8091807f6ed32 100644 --- a/extensions/markdown-language-features/preview-src/index.ts +++ b/extensions/markdown-language-features/preview-src/index.ts @@ -239,6 +239,8 @@ window.addEventListener('message', async event => { const newBody = newContent.querySelector('.markdown-body')!; root.replaceWith(newBody); domEval(newBody); + // Scroll to top when navigating to a different document + window.scrollTo(0, 0); } else { const newRoot = newContent.querySelector('.markdown-body')!;