|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as vscode from "vscode"; |
| 5 | +import * as fse from "fs-extra"; |
| 6 | +import * as path from "path"; |
| 7 | +import { getExtensionContext } from "../utils"; |
| 8 | + |
| 9 | +class MarkdownPreviewProvider implements vscode.Disposable { |
| 10 | + private panel: vscode.WebviewPanel | undefined; |
| 11 | + // a cache maps document path to rendered html |
| 12 | + private documentCache: Map<string, string> = new Map<string, string>(); |
| 13 | + private disposables: vscode.Disposable[] = []; |
| 14 | + |
| 15 | + public async show(markdownFilePath: string, title: string, context: vscode.ExtensionContext, webviewPanel?: vscode.WebviewPanel): Promise<void> { |
| 16 | + if (webviewPanel) { |
| 17 | + this.panel = webviewPanel; |
| 18 | + } |
| 19 | + |
| 20 | + if (!this.panel) { |
| 21 | + this.panel = vscode.window.createWebviewPanel("java.markdownPreview", title, vscode.ViewColumn.Beside, { |
| 22 | + localResourceRoots: [ |
| 23 | + vscode.Uri.file(path.join(context.extensionPath, "webview-resources")), |
| 24 | + vscode.Uri.file(path.dirname(markdownFilePath)), |
| 25 | + ], |
| 26 | + retainContextWhenHidden: true, |
| 27 | + enableFindWidget: true, |
| 28 | + enableCommandUris: true, |
| 29 | + enableScripts: true, |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + this.disposables.push(this.panel.onDidDispose(() => { |
| 34 | + this.panel = undefined; |
| 35 | + })); |
| 36 | + |
| 37 | + this.panel.iconPath = { |
| 38 | + light: vscode.Uri.file(path.join(context.extensionPath, "caption.light.svg")), |
| 39 | + dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg")) |
| 40 | + }; |
| 41 | + this.panel.webview.html = await this.getHtmlContent(this.panel.webview, markdownFilePath, title, context); |
| 42 | + this.panel.title = title; |
| 43 | + this.panel.reveal(this.panel.viewColumn); |
| 44 | + } |
| 45 | + |
| 46 | + public dispose(): void { |
| 47 | + if (this.panel) { |
| 48 | + this.panel.dispose(); |
| 49 | + } |
| 50 | + for (const disposable of this.disposables) { |
| 51 | + disposable.dispose(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + protected async getHtmlContent(webview: vscode.Webview, markdownFilePath: string, title: string, context: vscode.ExtensionContext): Promise<string> { |
| 56 | + const nonce: string = this.getNonce(); |
| 57 | + const styles: string = this.getStyles(webview, context); |
| 58 | + let body: string | undefined = this.documentCache.get(markdownFilePath); |
| 59 | + if (!body) { |
| 60 | + let markdownString: string = await fse.readFile(markdownFilePath, "utf8"); |
| 61 | + body = await vscode.commands.executeCommand("markdown.api.render", markdownString); |
| 62 | + this.documentCache.set(markdownFilePath, body as string); |
| 63 | + } |
| 64 | + return ` |
| 65 | + <!DOCTYPE html> |
| 66 | + <html> |
| 67 | + <head> |
| 68 | + <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${webview.cspSource}; img-src 'self' ${webview.cspSource} https: data:; script-src 'nonce-${nonce}';"/> |
| 69 | + <meta charset="UTF-8"> |
| 70 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 71 | + ${styles} |
| 72 | + <base href="${webview.asWebviewUri(vscode.Uri.file(markdownFilePath))}"> |
| 73 | + </head> |
| 74 | + <body class="vscode-body scrollBeyondLastLine wordWrap showEditorSelection"> |
| 75 | + ${body} |
| 76 | + <script nonce="${nonce}"> |
| 77 | + (function() { |
| 78 | + const vscode = acquireVsCodeApi(); |
| 79 | + vscode.setState({ |
| 80 | + markdownUri: "${vscode.Uri.file(markdownFilePath).toString()}", |
| 81 | + title: "${title}", |
| 82 | + }); |
| 83 | + })(); |
| 84 | + </script> |
| 85 | + </body> |
| 86 | + </html> |
| 87 | + `; |
| 88 | + } |
| 89 | + |
| 90 | + protected getStyles(webview: vscode.Webview, context: vscode.ExtensionContext): string { |
| 91 | + const styles: vscode.Uri[] = [ |
| 92 | + vscode.Uri.file(path.join(context.extensionPath, "webview-resources", "highlight.css")), |
| 93 | + vscode.Uri.file(path.join(context.extensionPath, "webview-resources", "markdown.css")), |
| 94 | + ]; |
| 95 | + return styles.map((styleUri: vscode.Uri) => `<link rel="stylesheet" type="text/css" href="${webview.asWebviewUri(styleUri).toString()}">`).join("\n"); |
| 96 | + } |
| 97 | + |
| 98 | + private getNonce(): string { |
| 99 | + let text = ""; |
| 100 | + const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 101 | + for (let i = 0; i < 32; i++) { |
| 102 | + text += possible.charAt(Math.floor(Math.random() * possible.length)); |
| 103 | + } |
| 104 | + return text; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +export const markdownPreviewProvider: MarkdownPreviewProvider = new MarkdownPreviewProvider(); |
| 109 | + |
| 110 | +export class MarkdownPreviewSerializer implements vscode.WebviewPanelSerializer { |
| 111 | + async deserializeWebviewPanel(webviewPanel: vscode.WebviewPanel, state: any) { |
| 112 | + if (state.markdownUri && state.title) { |
| 113 | + markdownPreviewProvider.show(vscode.Uri.parse(state.markdownUri).fsPath, state.title, getExtensionContext(), webviewPanel); |
| 114 | + } else { |
| 115 | + webviewPanel.dispose(); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments