Skip to content

Communication between an extension and markdown preview script #174080

Closed

Description

@eronnen

Hi,

I would like to be able to have communication between my extension and a markdown preview script, in the same way that you can communicate with a regular webview with the postMessage and onDidReceiveMessage functions.

Please let me know if you're willing to consider this feature, I will be glad to work on it.

Why is it useful?

in my vscode-markdown-chess extension, when having a chess game moves in the editor and the game itself rendered in the markdown preview, I would like to be able to go to a specific move in the rendered game by selecting the corresponding move text in the editor.

Proposal

Extension side

In order to be able to send and receive messages, I suggest to add another callback onMarkdownPreview that the extension will return from activate along with extendMarkdownIt. The onMarkdownPreview will be called upon creating a new markdown preview panel, with some MarkdownPreviewSubscription object, that will allow the extension to register a handler to onDidReceiveMessage through a vscode.EventEmitter and to call postMessage to that webview.

Markdown preview side

The script will need access to the postMessage function from vscode. according to the Webview API documentation, acquireVsCodeApi can be only called once per view, and it's already called from the builtin markdown extension.

So in order to export this API to all the scripts, I suggest to add something like:

window.vscode = { postMessage: vscode.postMessage };

in the built in preview script (https://github.com/microsoft/vscode/blob/main/extensions/markdown-language-features/preview-src/index.ts).

Activity

  1. added
    markdown-ext
    on Feb 20, 2023
  2. mjbvz commented on Feb 20, 2023

    @mjbvz
    Contributor

    Closing as out of scope. As you already noted, this functionality already exists for webviews. However the markdown preview is not intended to be as extensible as webviews

  3. added
    *out-of-scope
    feature-request
    on Feb 20, 2023
  4. riccardoferretti commented on Oct 30, 2023

    @riccardoferretti

    Hi Matt Bierner (@mjbvz) - could you share the reason behind such restriction on the markdown preview webview?

    For reference, we are trying to migrate Foam to be a Web Extension.
    One feature we need to port is the embed of a note into another, which requires reading a file from the file system. Currently we use node's readFileSync, but using workspace.fs there is understandably no sync way to do it.

    Given the markdown-it extension functions are sync, we were hoping to use postMessage to asynchronously update the preview, but I see from this issue that this is not supported.

    Why don't you like the idea of exposing a lower level API to the markdown preview's webview?
    Alternatively, do you have any suggestion on how we might be able to integrate async code into the markdown preview rendering?

    References:

  5. alex-netkachov commented on Dec 18, 2023

    @alex-netkachov

    Riccardo (@riccardoferretti), I'm also working on a similar issue, and the ability to communicate with the Markdown preview would be immensely helpful. Unfortunately, the issue is closed, and I doubt a closed issue will garner much attention. I've started a discussion at microsoft/vscode-discussions#981 and would appreciate your input there.

  6. dstengle commented on Jan 10, 2024

    @dstengle

    Putting this here for people who might be interested in solving dynamic content in markdown previews in vscode. The key is doing an MVC-style split between the extension and the preview scripts. I made an extension that extends markdown-it with a new highlight plugin (after following what the graphviz plugin does) and just put all of my code there instead of in a preview script. I would put actual view rendering scripts into the preview but otherwise dynamic content can be generated in extension.ts.

    You can see a demo of this here: https://github.com/dstengle/markdown-queries

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

Metadata

Metadata

Labels

*out-of-scopefeature-requestmarkdown-ext

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions