From VS Code Extension API open file in markdown preview

Viewed 524

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

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

Related