@@ -9,10 +9,10 @@ import { markdownEngine } from "./markdownEngine";
|
9 | 9 | class LeetCodeSubmissionProvider extends LeetCodeWebview {
|
10 | 10 |
|
11 | 11 | protected readonly viewType: string = "leetcode.submission";
|
12 |
| -private result: string; |
| 12 | +private result: IResult; |
13 | 13 |
|
14 |
| -public show(result: string): void { |
15 |
| -this.result = result; |
| 14 | +public show(resultString: string): void { |
| 15 | +this.result = this.parseResult(resultString); |
16 | 16 | this.showWebviewInternal();
|
17 | 17 | this.showKeybindingsHint();
|
18 | 18 | }
|
@@ -25,18 +25,36 @@ class LeetCodeSubmissionProvider extends LeetCodeWebview {
|
25 | 25 | }
|
26 | 26 |
|
27 | 27 | protected getWebviewContent(): string {
|
28 |
| -return `<!DOCTYPE html> |
29 |
| -<html lang="en"> |
| 28 | +const styles: string = markdownEngine.getStyles(); |
| 29 | +const title: string = `## ${this.result.messages[0]}`; |
| 30 | +const messages: string[] = this.result.messages.slice(1).map((m: string) => `* ${m}`); |
| 31 | +const sections: string[] = Object.keys(this.result) |
| 32 | +.filter((key: string) => key !== "messages") |
| 33 | +.map((key: string) => [ |
| 34 | +`### ${key}`, |
| 35 | +"```", |
| 36 | +this.result[key].join("\n"), |
| 37 | +"```", |
| 38 | +].join("\n")); |
| 39 | +const body: string = markdownEngine.render([ |
| 40 | +title, |
| 41 | +...messages, |
| 42 | +...sections, |
| 43 | +].join("\n")); |
| 44 | +return ` |
| 45 | +<!DOCTYPE html> |
| 46 | +<html> |
30 | 47 | <head>
|
31 | 48 | <meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource:; style-src vscode-resource:;"/>
|
32 | 49 | <meta charset="UTF-8">
|
33 | 50 | <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
34 |
| -${markdownEngine.getStyles()} |
| 51 | +${styles} |
35 | 52 | </head>
|
36 |
| -<body> |
37 |
| -<pre><code>${this.result.trim()}</code></pre> |
| 53 | +<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4"> |
| 54 | +${body} |
38 | 55 | </body>
|
39 |
| -</html>`; |
| 56 | +</html> |
| 57 | +`; |
40 | 58 | }
|
41 | 59 |
|
42 | 60 | protected onDidDisposeWebview(): void {
|
@@ -52,6 +70,38 @@ class LeetCodeSubmissionProvider extends LeetCodeWebview {
|
52 | 70 | (): Promise<any> => openKeybindingsEditor("leetcode solution"),
|
53 | 71 | );
|
54 | 72 | }
|
| 73 | + |
| 74 | +private parseResult(raw: string): IResult { |
| 75 | +raw = raw.concat(" √ "); // Append a dummy sentinel to the end of raw string |
| 76 | +const regSplit: RegExp = / [√×✔✘vx] ([^]+?)\n(?= [√×✔✘vx] )/g; |
| 77 | +const regKeyVal: RegExp = /(.+?): ([^]*)/; |
| 78 | +const result: IResult = { messages: [] }; |
| 79 | +let entry: RegExpExecArray | null; |
| 80 | +do { |
| 81 | +entry = regSplit.exec(raw); |
| 82 | +if (!entry) { |
| 83 | +continue; |
| 84 | +} |
| 85 | +const kvMatch: RegExpExecArray | null = regKeyVal.exec(entry[1]); |
| 86 | +if (kvMatch) { |
| 87 | +const [key, value] = kvMatch.slice(1); |
| 88 | +if (value) { // Do not show empty string |
| 89 | +if (!result[key]) { |
| 90 | +result[key] = []; |
| 91 | +} |
| 92 | +result[key].push(value); |
| 93 | +} |
| 94 | +} else { |
| 95 | +result.messages.push(entry[1]); |
| 96 | +} |
| 97 | +} while (entry); |
| 98 | +return result; |
| 99 | +} |
| 100 | +} |
| 101 | + |
| 102 | +interface IResult { |
| 103 | +[key: string]: string[]; |
| 104 | +messages: string[]; |
55 | 105 | }
|
56 | 106 |
|
57 | 107 | export const leetCodeSubmissionProvider: LeetCodeSubmissionProvider = new LeetCodeSubmissionProvider();
|
0 commit comments