Skip to content

Commit 058b720

Browse files
committed
Bring markdown engine into submission provider
1 parent 6e37a82 commit 058b720

File tree

1 file changed

+59
-11
lines changed

1 file changed

+59
-11
lines changed

src/webview/leetCodeSubmissionProvider.ts

Lines changed: 59 additions & 11 deletions
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,17 +25,33 @@ 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>
31-
<meta charset="UTF-8">
32-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
33-
${markdownEngine.getStyles()}
48+
${styles}
3449
</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}
3752
</body>
38-
</html>`;
53+
</html>
54+
`;
3955
}
4056

4157
protected onDidDisposeWebview(): void {
@@ -51,6 +67,38 @@ class LeetCodeSubmissionProvider extends LeetCodeWebview {
5167
(): Promise<any> => openKeybindingsEditor("leetcode solution"),
5268
);
5369
}
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 = / [×vx] ([^]+?)\n(?= [×vx] )/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[];
54102
}
55103

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

0 commit comments

Comments
 (0)