diff --git a/resources/snippetview/snippetview.html b/resources/snippetview/snippetview.html
index 425a9406c..d2a1a61ba 100644
--- a/resources/snippetview/snippetview.html
+++ b/resources/snippetview/snippetview.html
@@ -5,15 +5,15 @@
-
+
-
-
-
+
+
+
diff --git a/src/extras/snippet-view.ts b/src/extras/snippet-view.ts
index 46b20b71c..d0b55038f 100644
--- a/src/extras/snippet-view.ts
+++ b/src/extras/snippet-view.ts
@@ -68,7 +68,7 @@ function receive(message: SnippetViewResult) {
class SnippetViewProvider implements vscode.WebviewViewProvider {
private serverHandlerInserted = false
- public resolveWebviewView(webviewView: vscode.WebviewView) {
+ public async resolveWebviewView(webviewView: vscode.WebviewView) {
if (this.serverHandlerInserted === false) {
lw.server.setHandler((url: string) => {
if (url.startsWith('/snippetview/')) {
@@ -91,7 +91,7 @@ class SnippetViewProvider implements vscode.WebviewViewProvider {
const webviewSourcePath = path.join(lw.extensionRoot, 'resources', 'snippetview', 'snippetview.html')
webviewView.webview.html = readFileSync(webviewSourcePath, { encoding: 'utf8' })
- .replaceAll('%PORT%', lw.server.getPort().toString())
+ .replaceAll('%SRC%', (await lw.server.getUrl()).url)
.replaceAll('%CSP%', webviewView.webview.cspSource + ' http://127.0.0.1:*')
webviewView.webview.onDidReceiveMessage((e: SnippetViewResult) => {
diff --git a/src/preview/server.ts b/src/preview/server.ts
index d28f203bd..657cecd14 100644
--- a/src/preview/server.ts
+++ b/src/preview/server.ts
@@ -72,10 +72,12 @@ function getPort(): number {
return portNum
}
-async function getUrl(pdfUri: vscode.Uri): Promise<{url: string, uri: vscode.Uri}> {
+async function getUrl(pdfUri?: vscode.Uri): Promise<{url: string, uri: vscode.Uri}> {
// viewer/viewer.js automatically requests the file to server.ts, and server.ts decodes the encoded path of PDF file.
const origUrl = await vscode.env.asExternalUri(vscode.Uri.parse(`http://127.0.0.1:${lw.server.getPort()}`, true))
- const url = origUrl.toString() + (origUrl.toString().endsWith('/') ? '' : '/' ) + `viewer.html?file=${encodePathWithPrefix(pdfUri)}`
+ const url =
+ (origUrl.toString().endsWith('/') ? origUrl.toString().slice(0, -1) : origUrl.toString()) +
+ (pdfUri ? ('/viewer.html?file=' + encodePathWithPrefix(pdfUri)) : '')
return { url, uri: vscode.Uri.parse(url, true) }
}