microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.71k stars 29.08k forks source link

MarkdownPreview should set scheme bases uri in localResourceRoots #139907

Closed ghost closed 2 years ago

ghost commented 2 years ago

Issue Type: Feature Request

MarkdownPreview now only set file based uri or workspace uri in
localResourceRoots,if you preview it in other scheme based markdown,it cannot load local resources。 for example,you have a markdown behinde a fileSystemProvier,it has image local resouce,and the image won't be loaded。

VS Code version: Code 1.63.2 (899d46d82c4c95423fb7e10e68eba52050e30ba3, 2021-12-15T09:40:02.816Z) OS version: Windows_NT x64 10.0.22000 Restricted Mode: No

mjbvz commented 2 years ago

Please provide steps to reproduce this issue

/needsMoreInfo

ghost commented 2 years ago

First I make a fileSystemProvier,It Provides md: custom schema based file access. Then I opened a markdown from my fileSystemProvier, for example md:/I:/xxx/xxx/xx.md. Then xx.md has an image , What is expected is the image can be shown well,but it doesn't. Below is code in src\features\preview.ts, a.jpg full request path is https://md+.vscode-resource.vscode-webview.net/xxxx/xxxx/a.jpg. But without a workspace it is not in localResourceRoots and it won't show。

private getLocalResourceRoots(): ReadonlyArray<vscode.Uri> {
      const baseRoots = Array.from(this._contributionProvider.contributions.previewResourceRoots);

      const folder = vscode.workspace.getWorkspaceFolder(this._resource);
      if (folder) {
          const workspaceRoots = vscode.workspace.workspaceFolders?.map(folder => folder.uri);
          if (workspaceRoots) {
              baseRoots.push(...workspaceRoots);
          }
      } else if (!this._resource.scheme || this._resource.scheme === 'file') {
          baseRoots.push(vscode.Uri.file(path.dirname(this._resource.fsPath)));
      }

      return baseRoots;
}

If I was wrong or missed something, please tell me, I will rethink about it

mjbvz commented 2 years ago

Thanks. Can you please share a minimal extension that demonstrates this issue so we can reproduce this?

ghost commented 2 years ago

demo extension

mjbvz commented 2 years ago

Thanks for the example. Was able to fix the issue using it. Please test this out in the next insiders build

One note for verification: this only enables resources under the dirname of the markdown file. For example, a markdown file: fakefs:/path/to/dir/doc.md can load images under fakefs:/path/to/dir/* but not something like fakefs:/path/to/otherdir/image.png (unless the user has opened a workspace on fakefs:/path/to or higher )

ghost commented 2 years ago

/verified