|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import * as vscode from 'vscode'; |
| 4 | +import { fullPathToRelative } from '../../utils'; |
| 5 | + |
| 6 | +export class ExportNotesWebview implements vscode.WebviewViewProvider { |
| 7 | + public static readonly viewType = 'export-notes-view'; |
| 8 | + |
| 9 | + private _view?: vscode.WebviewView; |
| 10 | + private noteMap: Map<string, vscode.CommentThread>; |
| 11 | + |
| 12 | + constructor( |
| 13 | + private readonly _extensionUri: vscode.Uri, |
| 14 | + noteMap: Map<string, vscode.CommentThread>, |
| 15 | + ) { |
| 16 | + this.noteMap = noteMap; |
| 17 | + } |
| 18 | + |
| 19 | + public resolveWebviewView( |
| 20 | + webviewView: vscode.WebviewView, |
| 21 | + _context: vscode.WebviewViewResolveContext, |
| 22 | + _token: vscode.CancellationToken, |
| 23 | + ) { |
| 24 | + this._view = webviewView; |
| 25 | + |
| 26 | + webviewView.webview.options = { |
| 27 | + // Allow scripts in the webview |
| 28 | + enableScripts: true, |
| 29 | + localResourceRoots: [this._extensionUri], |
| 30 | + }; |
| 31 | + |
| 32 | + webviewView.webview.html = this._getHtmlForWebview(webviewView.webview); |
| 33 | + |
| 34 | + webviewView.webview.onDidReceiveMessage((data) => { |
| 35 | + switch (data.type) { |
| 36 | + case 'exportNotes': { |
| 37 | + exportNotes(data.status, data.options, data.format, this.noteMap); |
| 38 | + } |
| 39 | + } |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + private _getHtmlForWebview(webview: vscode.Webview) { |
| 44 | + const scriptUri = webview.asWebviewUri( |
| 45 | + vscode.Uri.joinPath( |
| 46 | + this._extensionUri, |
| 47 | + 'src', |
| 48 | + 'webviews', |
| 49 | + 'assets', |
| 50 | + 'exportNotes.js', |
| 51 | + ), |
| 52 | + ); |
| 53 | + const styleResetUri = webview.asWebviewUri( |
| 54 | + vscode.Uri.joinPath(this._extensionUri, 'src', 'webviews', 'assets', 'reset.css'), |
| 55 | + ); |
| 56 | + const styleVSCodeUri = webview.asWebviewUri( |
| 57 | + vscode.Uri.joinPath( |
| 58 | + this._extensionUri, |
| 59 | + 'src', |
| 60 | + 'webviews', |
| 61 | + 'assets', |
| 62 | + 'vscode.css', |
| 63 | + ), |
| 64 | + ); |
| 65 | + const styleMainUri = webview.asWebviewUri( |
| 66 | + vscode.Uri.joinPath(this._extensionUri, 'src', 'webviews', 'assets', 'main.css'), |
| 67 | + ); |
| 68 | + |
| 69 | + return `<!DOCTYPE html> |
| 70 | + <html lang="en"> |
| 71 | + <head> |
| 72 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 73 | + <link href="${styleResetUri}" rel="stylesheet"> |
| 74 | + <link href="${styleVSCodeUri}" rel="stylesheet"> |
| 75 | + <link href="${styleMainUri}" rel="stylesheet"> |
| 76 | + </head> |
| 77 | + <body> |
| 78 | + <p>Select the notes you want to export:</p> |
| 79 | + <p> |
| 80 | + <input type="checkbox" id="vulnerable-notes" value="vulnerable-notes" checked> |
| 81 | + <label for="vulnerable-notes"> Vulnerable</label></br> |
| 82 | + <input type="checkbox" id="not-vulnerable-notes" value="not-vulnerable-notes" checked> |
| 83 | + <label for="not-vulnerable-notes"> Not Vulnerable</label></br> |
| 84 | + <input type="checkbox" id="todo-notes" value="todo-notes" checked> |
| 85 | + <label for="todo-notes"> TODO</label></br> |
| 86 | + <input type="checkbox" id="no-status-notes" value="no-status-notes" checked> |
| 87 | + <label for="no-status-notes"> No Status</label></br> |
| 88 | + </p> |
| 89 | + </br> |
| 90 | +
|
| 91 | + <p>Select additional options:</p> |
| 92 | + <p> |
| 93 | + <input type="checkbox" id="include-code-snippet" name="include-code-snippet" value="include-code-snippet" checked> |
| 94 | + <label for="include-code-snippet"> Include code snippet</label></br> |
| 95 | + </p> |
| 96 | + <p> |
| 97 | + <input type="checkbox" id="include-note-replies" name="include-note-replies" value="include-note-replies" checked> |
| 98 | + <label for="include-note-replies"> Include note replies</label></br> |
| 99 | + </p> |
| 100 | + <p> |
| 101 | + <input type="checkbox" id="include-authors" name="include-authors" value="include-authors" checked> |
| 102 | + <label for="include-authors"> Include authors</label></br> |
| 103 | + </p> |
| 104 | + </br> |
| 105 | +
|
| 106 | + <p>Select export format:</p> |
| 107 | + <p> |
| 108 | + <select id="format-select"> |
| 109 | + <option value="markdown">Markdown</option> |
| 110 | + </select> |
| 111 | + </p> |
| 112 | + </br> |
| 113 | +
|
| 114 | + <p> |
| 115 | + <button class="export-notes-button">Export</button> |
| 116 | + </p> |
| 117 | +
|
| 118 | + <script src="${scriptUri}"></script> |
| 119 | + </body> |
| 120 | + </html>`; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +async function exportNotes( |
| 125 | + status: any, |
| 126 | + options: any, |
| 127 | + format: string, |
| 128 | + noteMap: Map<string, vscode.CommentThread>, |
| 129 | +) { |
| 130 | + // filter notes based on selected status |
| 131 | + const selectedNotes = [...noteMap] |
| 132 | + .map(([_id, note]) => { |
| 133 | + const firstComment = note.comments[0].body.toString(); |
| 134 | + if ( |
| 135 | + (status.vulnerable && firstComment.startsWith('[Vulnerable] ')) || |
| 136 | + (status.notVulnerable && firstComment.startsWith('[Not Vulnerable] ')) || |
| 137 | + (status.todo && firstComment.startsWith('[TODO] ')) || |
| 138 | + status.noStatus |
| 139 | + ) { |
| 140 | + return note; |
| 141 | + } |
| 142 | + }) |
| 143 | + .filter((element) => element !== undefined); |
| 144 | + |
| 145 | + if (!selectedNotes.length) { |
| 146 | + vscode.window.showInformationMessage('[Export] No notes met the criteria.'); |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + switch (format) { |
| 151 | + case 'markdown': { |
| 152 | + const outputs = await Promise.all( |
| 153 | + selectedNotes.map(async (note: any) => { |
| 154 | + // include code snippet |
| 155 | + if (options.includeCodeSnippet) { |
| 156 | + const codeSnippet = await exportCodeSnippet(note.uri, note.range); |
| 157 | + return codeSnippet + exportComments(note, options); |
| 158 | + } |
| 159 | + return exportComments(note, options); |
| 160 | + }), |
| 161 | + ); |
| 162 | + |
| 163 | + const document = await vscode.workspace.openTextDocument({ |
| 164 | + content: outputs.join(''), |
| 165 | + language: 'markdown', |
| 166 | + }); |
| 167 | + vscode.window.showTextDocument(document); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +function exportComments(note: vscode.CommentThread, options: any) { |
| 173 | + // export first comment |
| 174 | + let output = ''; |
| 175 | + output += exportComment( |
| 176 | + note.comments[0].body.toString(), |
| 177 | + options.includeAuthors ? note.comments[0].author.name : undefined, |
| 178 | + ); |
| 179 | + |
| 180 | + // include replies |
| 181 | + if (options.includeReplies) { |
| 182 | + note.comments.slice(1).forEach((comment: vscode.Comment) => { |
| 183 | + output += exportComment( |
| 184 | + comment.body.toString(), |
| 185 | + options.includeAuthors ? comment.author.name : undefined, |
| 186 | + ); |
| 187 | + }); |
| 188 | + } |
| 189 | + |
| 190 | + output += `\n-----\n`; |
| 191 | + return output; |
| 192 | +} |
| 193 | + |
| 194 | +function exportComment(body: string, author: string | undefined) { |
| 195 | + if (author) { |
| 196 | + return `\n**${author}** - ${body}\n`; |
| 197 | + } |
| 198 | + return `\n${body}\n`; |
| 199 | +} |
| 200 | + |
| 201 | +async function exportCodeSnippet(uri: vscode.Uri, range: vscode.Range) { |
| 202 | + const output = await vscode.workspace.openTextDocument(uri).then(async (document) => { |
| 203 | + const newRange = new vscode.Range(range.start.line, 0, range.end.line + 1, 0); |
| 204 | + const codeSnippet = await document.getText(newRange).trimEnd(); |
| 205 | + return `\nCode snippet \`${fullPathToRelative( |
| 206 | + uri.fsPath, |
| 207 | + )}\`:\n\n\`\`\`\n${codeSnippet}\n\`\`\`\n`; |
| 208 | + }); |
| 209 | + return output; |
| 210 | +} |
0 commit comments