Skip to content

Commit ef4bcbd

Browse files
Vigilansjdneo
authored andcommitted
fix: Bring markdown engine into submission provider (#398)
1 parent 5e51132 commit ef4bcbd

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

src/webview/leetCodeSubmissionProvider.ts

+59-9
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { markdownEngine } from "./markdownEngine";
99
class LeetCodeSubmissionProvider extends LeetCodeWebview {
1010

1111
protected readonly viewType: string = "leetcode.submission";
12-
private result: string;
12+
private result: IResult;
1313

14-
public show(result: string): void {
15-
this.result = result;
14+
public show(resultString: string): void {
15+
this.result = this.parseResult(resultString);
1616
this.showWebviewInternal();
1717
this.showKeybindingsHint();
1818
}
@@ -25,18 +25,36 @@ class LeetCodeSubmissionProvider extends LeetCodeWebview {
2525
}
2626

2727
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>
3047
<head>
3148
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource:; style-src vscode-resource:;"/>
3249
<meta charset="UTF-8">
3350
<meta name="viewport" content="width=device-width, initial-scale=1.0">
34-
${markdownEngine.getStyles()}
51+
${styles}
3552
</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}
3855
</body>
39-
</html>`;
56+
</html>
57+
`;
4058
}
4159

4260
protected onDidDisposeWebview(): void {
@@ -52,6 +70,38 @@ class LeetCodeSubmissionProvider extends LeetCodeWebview {
5270
(): Promise<any> => openKeybindingsEditor("leetcode solution"),
5371
);
5472
}
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[];
55105
}
56106

57107
export const leetCodeSubmissionProvider: LeetCodeSubmissionProvider = new LeetCodeSubmissionProvider();

0 commit comments

Comments
 (0)