我正在创建一个 vscode 扩展。我想提供一个 help
命令,在调用该命令时,会打开扩展提供的 help.md
文件,因此我认为该文件作为扩展的一部分提供包。
myext/
├── .vscode/
├── test/
├── package.json
├── extension.js
├── help.md
如何获取扩展包内容的引用并在运行扩展的编辑器中打开文件 help.md
?
最佳答案
您可以使用扩展上下文获取扩展中任何文件的路径。这是我使用的一个函数 in my extension至determine the absoute path到我想要加载的文件:
/**
* Returns the absolute path to a file located in our misc folder.
*
* @param file The base file name.
* @param context The context of this extension to get its path regardless where it is installed.
* @param webview When given format the path for use in this webview.
*/
public static getMiscPath(file: string, context: ExtensionContext, webView?: Webview): string {
if (webView) {
let uri = Uri.file(context.asAbsolutePath(path.join('misc', file)));
return webView.asWebviewUri(uri).toString();
}
return context.asAbsolutePath(path.join('misc', file));
}
此函数确定路径的 2 种不同变体:
- 没有 WebView 实例:只是磁盘上的绝对文件路径。
- 带有 WebView 实例:用于 HTML/CSS 的 Web URL。
关于javascript - 如何在vscode扩展中打开本地(扩展包内)文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61222193/