Skip to content

Commit e79e94e

Browse files
committed
refactor panel.webview.html content
1 parent 4a91431 commit e79e94e

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

src/ConfluenceContentProvider.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Import the module and reference it with the alias vscode in your code below
44
import * as vscode from 'vscode';
55

6-
import { parseMarkup, cssUri } from './markupParser';
6+
import {parseMarkup} from './markupParser';
77

88
export function packConfluenceUri(uri: vscode.Uri) {
99
// Temporarily change the URI scheme
@@ -40,24 +40,10 @@ export class ConfluenceContentProvider implements vscode.TextDocumentContentProv
4040
}
4141

4242
public async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
43-
let document = await vscode.workspace.openTextDocument(unpackConfluenceUri(uri));
44-
let body = await parseMarkup(unpackConfluenceUri(uri), document.getText());
45-
let cssLink = cssUri("confluence.css");
43+
const document = await vscode.workspace.openTextDocument(unpackConfluenceUri(uri));
44+
const body = await parseMarkup(unpackConfluenceUri(uri), document.getText());
4645

47-
48-
// Security
49-
// https://code.visualstudio.com/api/extension-guides/webview#security
50-
return `<!DOCTYPE html>
51-
<html>
52-
<head>
53-
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
54-
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'self' 'unsafe-inline'; img-src vscode-resource: https:; script-src vscode-resource:; style-src vscode-resource:;"/>
55-
<link rel="stylesheet" href="${cssLink}">
56-
</head>
57-
<body>
58-
${body}
59-
</body>
60-
</html>`;
46+
return body
6147
}
6248

6349
get onDidChange(): vscode.Event<vscode.Uri> {

src/extension.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,37 @@ import { packConfluenceUri, unpackConfluenceUri, ConfluenceContentProvider } fro
77

88
const path = require('path');
99

10+
import {cssUri} from './markupParser';
11+
12+
1013
function getRenderedContent(contentProvider: ConfluenceContentProvider, uri: vscode.Uri, panel: vscode.WebviewPanel) {
1114
contentProvider.provideTextDocumentContent(packConfluenceUri(uri)).then((renderedContent) => {
12-
panel.webview.html = renderedContent;
15+
// Security
16+
// https://code.visualstudio.com/api/extension-guides/webview#security
17+
18+
const cssFile = cssUri('confluence.css')
19+
let cssLink = ""
20+
if (cssFile) {
21+
const cssUrl = panel.webview.asWebviewUri(cssFile)
22+
cssLink = `<link rel="stylesheet" href="${cssUrl}">`
23+
}
24+
25+
panel.webview.html = `<!DOCTYPE html>
26+
<html>
27+
<head>
28+
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
29+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
30+
<meta http-equiv="Content-Security-Policy"
31+
content="default-src 'none';
32+
img-src self vscode-resource: https:;
33+
script-src self vscode-resource:;
34+
style-src 'unsafe-inline' self vscode-resource:;"/>
35+
${cssLink}
36+
</head>
37+
<body>
38+
${renderedContent}
39+
</body>
40+
</html>`;
1341
}, (reason) => {
1442
vscode.window.showErrorMessage(reason);
1543
});

0 commit comments

Comments
 (0)