Skip to content

Markdown preview resets when switching tabs #147718

Description

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.66.2, 1.67.0-insider
  • OS Version: Windows_NT x64 10.0.19044

Steps to Reproduce:

  1. See gif below, or: Open markdown file
  2. Show preview
  3. Change markdown file
  4. Switch to another tab to hide preview
  5. Get back to preview

Code_-_Insiders_i6uW3MTnMI

Activity

  1. added
    bug
    help wanted
    markdown-ext
    on May 5, 2022
  2. added a commit that references this issue on Apr 1, 2026
    1fd1e14
  3. tanglinfang commented on Jul 5, 2026

    @tanglinfang

    I am also running into this while using Markdown Preview as a reading mode for long documents. After switching to another file/tab and then coming back, losing the previous scroll position means I have to manually find where I left off every time. Preserving the preview scroll position per document would make reading and cross-referencing Markdown files much smoother.

  4. tanglinfang commented on Jul 5, 2026

    @tanglinfang

    A more specific reproduction case that is different from the existing recording:

    1. Open a long Markdown file in source/edit mode and leave the editor position around a specific line, for example line 100.
      1. Open the same file in Markdown Preview / read mode.
      1. Scroll the preview further down, to content corresponding to a much later part of the document.
      1. Switch to another Markdown tab/file.
      1. Switch back to the first Markdown Preview tab.
        Expected: Markdown Preview preserves the preview's own scroll position from step 3.

    Actual: Markdown Preview jumps back to the source editor's anchored position from step 1, for example the area corresponding to line 100, instead of the later preview position where I was reading.

    This is disruptive when using Markdown Preview as a reading mode for long documents, because switching tabs makes the reader lose the actual preview reading position.

    vscode_markdown_preview_editor_anchor_vscode_display.mov
  5. bompus commented on Oct 5, 2026

    @bompus
    Contributor

    PR #334681 addresses the stale-content reset in the original report and recording. In the recording, the preview displays the edited paragraph, then falls back to the earlier "b" after another tab hides it.

    Visible updates are applied with updateContent messages, but leave webview.html unchanged. Hiding the preview discards the webview; revealing it restores that old HTML. The PR tracks when the stored HTML is behind and forces a full refresh when the panel is hidden. It covers both the dynamic preview and vscode.markdown.preview.editor, including a panel hidden during rendering and a forced refresh requested while a regular refresh is pending.

    The same content-reset sequence is reported in #271233 and in the tab-switch comments on #265277. The PR description contains before/after measurements and regression coverage.

    The newer comments here describe preserving the preview's independent scroll position. This PR does not claim to resolve that separate behavior.

  6. ermse commented on Oct 26, 2026

    @ermse

    Context first, because this issue and #317033 are two halves of one design problem.

    The markdown preview and the markdown text editor share a single remembered line number per resource, and it is used as a bidirectional sync anchor without any record of which side scrolled last. Whichever side you did not touch most recently can therefore overwrite the position of the side you did:

    Both are the same root design point seen from opposite ends, but they fail through different code, so the rest of this comment is specific to the preview side. It is also distinct from the stale-content reset that #334681 fixes; that PR explicitly does not cover the scroll position.


    Still present in 1.143.1 (e646868, Windows x64). The code-level cause:

    The preview panel is created without retainContextWhenHidden, so hiding the tab destroys the webview and revealing it reloads the HTML. On reload, preview-src/index.ts restores the reading position from the persisted scrollProgress — but throws it away whenever the host-side starting line changed since the last save:

    // extensions/markdown-language-features/preview-src/index.ts
    const hasStartingLine = typeof settings.settings.line === 'number' && !isNaN(settings.settings.line);
    if (typeof originalState.scrollProgress !== 'undefined'
        && (originalState?.resource !== state.resource || (hasStartingLine && originalState.line !== settings.settings.line))) {
        state.scrollProgress = hasStartingLine ? undefined : 0;
    }

    settings.line is MarkdownPreview.#line on the host side. That field gets overwritten with the source editor's topmost visible line by two paths in src/preview/preview.ts, neither of which is gated on the user having actually scrolled the editor:

    • DynamicMarkdownPreview's vscode.window.onDidChangeActiveTextEditor handler → this.update(editor.document.uri, new StartingScrollLine(getVisibleLine(editor))) → MarkdownPreview.scrollTo(line) → #line = topLine
    • TopmostLineMonitor.onDidChanged (fired from onDidChangeTextEditorVisibleRanges) → scrollTo(line) → #line = topLine

    So the sequence that loses the position is:

    1. Scroll the preview down.
    2. The source .md editor is open in a background tab, so it is not in visibleTextEditors and scrollEditorWithPreview cannot move it — it stays at line 0.
    3. Activate that editor tab. onDidChangeActiveTextEditor fires, #line is reset to the editor's topmost visible line.
    4. Return to the preview. The webview reloads, sees originalState.line !== settings.settings.line, discards scrollProgress, and scrolls to settings.line — the top of the file.

    This explains why the repro is flaky, and why it does not happen when switching between the preview and a non-markdown tab: neither path fires, #line is unchanged, and scrollProgress restores correctly. That is very likely why #318532 could not be reproduced on request and was closed. It also matches tanglinfang's variant above, where the preview jumps back to the source editor's anchored line rather than to the very top — same mechanism, different editor line.

    Worth noting for anyone looking for a workaround: "markdown.preview.scrollPreviewWithEditor": false does not help. It is only checked on the webview side, so the host still clobbers #line, scrollProgress is still discarded on reload, and the if (settings.settings.scrollPreviewWithEditor) branch that would scroll to settings.line is then skipped — leaving the preview at the top regardless. What does help is keeping the source editor closed, or using a locked preview, since the !this.#locked guard suppresses the onDidChangeActiveTextEditor path.

    Two defects look separable here:

    1. MarkdownPreview.scrollTo mutates #line — the anchor used to restore the webview after a reload — even when the webview will ignore the resulting updateView message because scrollPreviewWithEditor is disabled.
    2. A preview the user has scrolled independently is re-anchored to the editor merely because that editor became active, without any editor scroll having occurred. onDidChangeActiveTextEditor arguably should not re-anchor a preview whose own scroll position is newer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bughelp wantedmarkdown-ext

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions