从VS Code Extension API在Markdown预览中打开文件

问题描述

所以我想在markdown预览中打开文件,所以当触发特定命令时,我想在vscode中打开文件(但在markdown预览模式下)

到目前为止我尝试过什么:

  • 这将以原始文本模式打开文件
const docs = await workspace.openTextDocument("/path/to/file.md");
await window.showtextDocument(docs);
  • 这将触发markdown命令,并且刚刚从上方打开的文件将在markdown预览中呈现
await commands.executeCommand("markdown.showPreview");

有没有更好的方法来实现此目的,因为打开文件然后触发降价预览需要花费几秒钟的时间

解决方法

markdown markdown.showPreview命令将uri作为其第一个参数。例如:

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

要控制打开编辑器所在的列,请改用markdown.showPreviewToSide命令。 Here are the arguments it takes