Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From VS Code Extension API open file in markdown preview

So I want to open a file in markdown preview, so when the certain command is triggered I want to open a file in vscode (but in markdown preview mode)

What have I tried so far:

  • This will open the file in raw text mode
const docs = await workspace.openTextDocument("/path/to/file.md");
await window.showTextDocument(docs);
  • This will trigger markdown command and the just opened file from above will be rendered in markdown preview
await commands.executeCommand("markdown.showPreview");

Is there a better way to achieve this, since it take fraction of seconds to open the file and then trigger markdown preview

like image 450
sudo Avatar asked Oct 29 '25 04:10

sudo


1 Answers

The markdown markdown.showPreview command takes a uri as its first argument. For example:

const uri = vscode.URI.file("/path/to/file.md");
await commands.executeCommand("markdown.showPreview", uri);

To control the column the editor is opened in, use the markdown.showPreviewToSide command instead. Here are the arguments it takes

like image 134
Matt Bierner Avatar answered Oct 31 '25 12:10

Matt Bierner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!