Skip to content

Markdown previewScripts new rendering behavior #136255

@mjbvz

Description

@mjbvz

This issue documents the new rendering behavior of the markdown preview, specifically how it may effect previewScripts

Existing Behavior (Pre 1.63)

VS Code's markdown preview updates lives as the user types. To support this, we currently re-render the entire webview.

This means that any scripts on the page are restarted every time the preview contents are updated. These scripts could always count on running in a fresh environment with freshly rendered markdown content

This approach has a number of problems:

  • It is difficult to correctly maintain the original scroll position
  • Resources on the page are reloaded on every update
  • Scripts need to re-initialize themselves on every update

New behavior

Starting in 1.63, we'd like to migrate to use incremental dom updates for updating the markdown preview.

For preview scripts, this means that they are initialized once when the page first loads, but then are expected to handle any future markdown content updates that happen in the existing document. This change in behavior isn't a change for some scripts, but may cause unexpected behavior for others

Updating your preview scripts to support 1.63

The most common issue you may see after updating is that your script runs for the initial page render, but does not kick in for subsequent page updates. This is because preview scripts were written assuming the markdown on the page would only ever be rendered once, which is no longer true

To workaround this, add a new listener in your preview for the 'vscode.markdown.updateContent' custom event. This event is fired when the markdown content on the page has been updated:

window.addEventListener('vscode.markdown.updateContent', () => {
    // Update based on the new markdown content in the file
});

Metadata

Metadata

Assignees

Labels

apimarkdownMarkdown support issues

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions