Rewrite webview urls to be more url-ish

## Problem

Webview uris currently have the form:

```
https://uuid.vscode-webview.net/vscode-resource/scheme/authority/path...
```

We have this syntax because we need to be able to recover the original scheme and authority of the resource in order to load it from disk

However this syntax means that absolute urls don't behave as you'd expect. For example, if you have a webview that sets a `<base>` to a document in the workspace, an absolute url `/abs/path.png` ends up being resolved to:

```
https://uuid.vscode-webview.net/abs/path.png
```

This drops the original scheme and authority, which prevents us from loading the resource

## Fix
With this change, I've moved the original scheme and authority into the authority of the webview uri instead of the path:

```
https://scheme+authority.vscode-resource.uuid.vscode-webview.net/path...
```

With this change, absolute paths should correctly be resolved
This commit is contained in:
Matt Bierner
2021-05-20 22:34:35 -07:00
parent 70c87f0db9
commit 79dea51e79
8 changed files with 88 additions and 81 deletions

View File

@@ -269,14 +269,12 @@ export class MarkdownEngine {
if (uri.path[0] === '/') {
const root = vscode.workspace.getWorkspaceFolder(this.currentDocument!);
if (root) {
const fileUri = vscode.Uri.joinPath(root.uri, uri.fsPath).with({
uri = vscode.Uri.joinPath(root.uri, uri.fsPath).with({
fragment: uri.fragment,
query: uri.query,
}).with({
scheme: 'markdown-link'
});
// Ensure fileUri is relative by prepending `/` so that it uses the <base> element URI
// when resolving the absolute URL
uri = vscode.Uri.parse('markdown-link:' + '/' + fileUri.toString(true).replace(/^\S+?:/, fileUri.scheme));
}
}