diff --git a/CHANGELOG.md b/CHANGELOG.md index 92ae26ed..352dea2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,6 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how - Add a setting `leetcode.nodePath` to specify the `Node.js` executable path [#227](https://github.com/jdneo/vscode-leetcode/issues/227) ## Changed -- Improve the submission result page - Update the activity bar icon, See: [#225](https://github.com/jdneo/vscode-leetcode/pull/225) ## Fixed diff --git a/src/webview/leetCodeResultProvider.ts b/src/webview/leetCodeResultProvider.ts index 4886fef6..189eed17 100644 --- a/src/webview/leetCodeResultProvider.ts +++ b/src/webview/leetCodeResultProvider.ts @@ -1,9 +1,7 @@ // Copyright (c) jdneo. All rights reserved. // Licensed under the MIT license. -import * as _ from "lodash"; import { Disposable, ExtensionContext, ViewColumn, WebviewPanel, window } from "vscode"; -import { markdownEngine } from "./markdownEngine"; class LeetCodeResultProvider implements Disposable { @@ -14,12 +12,11 @@ class LeetCodeResultProvider implements Disposable { this.context = context; } - public async show(resultString: string): Promise { + public async show(result: string): Promise { if (!this.panel) { - this.panel = window.createWebviewPanel("leetcode.result", "Submission Result", ViewColumn.Two, { + this.panel = window.createWebviewPanel("leetcode.result", "LeetCode Results", ViewColumn.Two, { retainContextWhenHidden: true, enableFindWidget: true, - localResourceRoots: markdownEngine.localResourceRoots, }); this.panel.onDidDispose(() => { @@ -27,8 +24,7 @@ class LeetCodeResultProvider implements Disposable { }, null, this.context.subscriptions); } - const result: IResult = this.parseResult(resultString); - this.panel.webview.html = this.getWebViewContent(result); + this.panel.webview.html = await this.provideHtmlContent(result); this.panel.reveal(ViewColumn.Two); } @@ -38,67 +34,19 @@ class LeetCodeResultProvider implements Disposable { } } - private parseResult(raw: string): IResult { - raw = raw.concat(" √ "); // Append a dummy sentinel to the end of raw string - const regSplit: RegExp = / [√×✔✘vx] ([^]+?)\n(?= [√×✔✘vx] )/g; - const regKeyVal: RegExp = /(.+?): ([^]*)/; - const result: IResult = { messages: [] }; - let entry: RegExpExecArray | null; - do { - entry = regSplit.exec(raw); - if (!entry) { - continue; - } - const kvMatch: RegExpExecArray | null = regKeyVal.exec(entry[1]); - if (kvMatch) { - const key: string = _.startCase(kvMatch[1]); - let value: string = kvMatch[2]; - if (!result[key]) { - result[key] = []; - } - if (key === "Testcase") { - value = value.slice(1, -1).replace("\\n", "\n"); - } - result[key].push(value); - } else { - result.messages.push(entry[1]); - } - } while (entry); - return result; - } - - private getWebViewContent(result: IResult): string { - const styles: string = markdownEngine.getStylesHTML(); - const title: string = `## ${result.messages[0]}`; - const messages: string[] = result.messages.slice(1).map((m: string) => `* ${m}`); - const sections: string[] = Object.keys(result).filter((k: string) => k !== "messages").map((key: string) => [ - `### ${key}`, - "```", - result[key].join("\n\n"), - "```", - ].join("\n")); - const body: string = markdownEngine.render([ - title, - ...messages, - ...sections, - ].join("\n")); - return ` - - + private async provideHtmlContent(result: string): Promise { + return ` + - ${styles} + + + LeetCode Results - - ${body} + +
${result.trim()}
- - `; + `; } } -interface IResult { - [key: string]: string[]; - messages: string[]; -} - export const leetCodeResultProvider: LeetCodeResultProvider = new LeetCodeResultProvider();