You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Communication between an extension and markdown preview script #174080
*out-of-scopePosted issue is not in scope of VS Codefeature-requestRequest for new features or functionalitymarkdown-extThe Markdown extension: language features and preview
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:
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
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?
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.
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.
*out-of-scopePosted issue is not in scope of VS Codefeature-requestRequest for new features or functionalitymarkdown-extThe Markdown extension: language features and preview
Activity
mjbvz commentedon Feb 20, 2023
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
riccardoferretti commentedon Oct 30, 2023
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 usingworkspace.fsthere is understandably no sync way to do it.Given the
markdown-itextension functions are sync, we were hoping to usepostMessageto 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:
alex-netkachov commentedon Dec 18, 2023
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.
dstengle commentedon Jan 10, 2024
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
feat(share-mode): per-page bd-share opt-in with pre-fetch + plan gating
fix(preview): hide bd-share via previewScript instead of renderer ove…
Add read-aloud support for the Markdown preview