@@ -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,17 +25,33 @@ 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
- <meta charset="UTF-8">
32
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
33
- ${ markdownEngine . getStyles ( ) }
48
+ ${ styles }
34
49
</head>
35
- <body>
36
- <pre><code> ${ this . result . trim ( ) } </code></pre>
50
+ <body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4" >
51
+ ${ body }
37
52
</body>
38
- </html>` ;
53
+ </html>
54
+ ` ;
39
55
}
40
56
41
57
protected onDidDisposeWebview ( ) : void {
@@ -51,6 +67,38 @@ class LeetCodeSubmissionProvider extends LeetCodeWebview {
51
67
( ) : Promise < any > => openKeybindingsEditor ( "leetcode solution" ) ,
52
68
) ;
53
69
}
70
+
71
+ private parseResult ( raw : string ) : IResult {
72
+ raw = raw . concat ( " √ " ) ; // Append a dummy sentinel to the end of raw string
73
+ const regSplit : RegExp = / [ √ × ✔ ✘ v x ] ( [ ^ ] + ?) \n (? = [ √ × ✔ ✘ v x ] ) / g;
74
+ const regKeyVal : RegExp = / ( .+ ?) : ( [ ^ ] * ) / ;
75
+ const result : IResult = { messages : [ ] } ;
76
+ let entry : RegExpExecArray | null ;
77
+ do {
78
+ entry = regSplit . exec ( raw ) ;
79
+ if ( ! entry ) {
80
+ continue ;
81
+ }
82
+ const kvMatch : RegExpExecArray | null = regKeyVal . exec ( entry [ 1 ] ) ;
83
+ if ( kvMatch ) {
84
+ const [ key , value ] = kvMatch . slice ( 1 ) ;
85
+ if ( value ) { // Do not show empty string
86
+ if ( ! result [ key ] ) {
87
+ result [ key ] = [ ] ;
88
+ }
89
+ result [ key ] . push ( value ) ;
90
+ }
91
+ } else {
92
+ result . messages . push ( entry [ 1 ] ) ;
93
+ }
94
+ } while ( entry ) ;
95
+ return result ;
96
+ }
97
+ }
98
+
99
+ interface IResult {
100
+ [ key : string ] : string [ ] ;
101
+ messages : string [ ] ;
54
102
}
55
103
56
104
export const leetCodeSubmissionProvider : LeetCodeSubmissionProvider = new LeetCodeSubmissionProvider ( ) ;
0 commit comments