Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Apply markdown engine to result provider#232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
jdneo merged 8 commits intoLeetCode-OpenSource:masterfromVigilans:webview-md-result
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionspackage.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -321,6 +321,7 @@
"dependencies": {
"fs-extra": "^6.0.1",
"highlight.js": "^9.15.6",
"lodash": "^4.17.11",
"lodash.kebabcase": "^4.1.1",
"markdown-it": "^8.4.2",
"require-from-string": "^2.0.2",
Expand Down
9 changes: 3 additions & 6 deletionssrc/explorer/LeetCodeTreeDataProvider.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.

import * as _ from "lodash";
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
Expand DownExpand Up@@ -201,10 +202,10 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
private addProblemToTreeData(problem: IProblem): void {
this.putProblemToMap(this.treeData.Difficulty, problem.difficulty, problem);
for (const tag of problem.tags) {
this.putProblemToMap(this.treeData.Tag,this.beautifyCategoryName(tag), problem);
this.putProblemToMap(this.treeData.Tag,_.startCase(tag), problem);
}
for (const company of problem.companies) {
this.putProblemToMap(this.treeData.Company,this.beautifyCategoryName(company), problem);
this.putProblemToMap(this.treeData.Company,_.startCase(company), problem);
}
}

Expand All@@ -217,10 +218,6 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
}
}

private beautifyCategoryName(name: string): string {
return name.split("-").map((c: string) => c[0].toUpperCase() + c.slice(1)).join(" ");
}

private getSubCategoryNodes(map: Map<string, IProblem[]>, category: Category): LeetCodeNode[] {
const subCategoryNodes: LeetCodeNode[] = Array.from(map.keys()).map((subCategory: string) => {
return new LeetCodeNode(Object.assign({}, defaultProblem, {
Expand Down
2 changes: 1 addition & 1 deletionsrc/webview/leetCodePreviewProvider.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -39,7 +39,7 @@ class LeetCodePreviewProvider implements Disposable {
}

this.panel.webview.html = await this.provideHtmlContent(node);
this.panel.title = node.name;
this.panel.title =`${node.name}: Preview`;
this.panel.reveal();
}

Expand Down
76 changes: 64 additions & 12 deletionssrc/webview/leetCodeResultProvider.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
// 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 {

Expand All@@ -12,19 +14,21 @@ class LeetCodeResultProvider implements Disposable {
this.context = context;
}

public async show(result: string): Promise<void> {
public async show(resultString: string): Promise<void> {
if (!this.panel) {
this.panel = window.createWebviewPanel("leetcode.result", "LeetCode Results", ViewColumn.Two, {
this.panel = window.createWebviewPanel("leetcode.result", "Submission Result", ViewColumn.Two, {
retainContextWhenHidden: true,
enableFindWidget: true,
localResourceRoots: markdownEngine.localResourceRoots,
});

this.panel.onDidDispose(() => {
this.panel = undefined;
}, null, this.context.subscriptions);
}

this.panel.webview.html = await this.provideHtmlContent(result);
const result: IResult = this.parseResult(resultString);
this.panel.webview.html = this.getWebViewContent(result);
this.panel.reveal(ViewColumn.Two);
}

Expand All@@ -34,19 +38,67 @@ class LeetCodeResultProvider implements Disposable {
}
}

private async provideHtmlContent(result: string): Promise<string> {
return `<!DOCTYPE html>
<html lang="en">
private parseResult(raw: string): IResult {
raw = raw.concat(" √ "); // Append a dummy sentinel to the end of raw string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

What will happen if we don't append?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The RegEx captures anything in between two. If we don't append it, the last section (normallystdout) won't be captured.

Worth to note that, with this trick, messages like[WARN] Failed to get memory percentile will be captured as one part ofYour runtime beats 81.3 % of python submissions, and will also be printed out even it does not starts with.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Hmm... still feeling the logic here is too tricky for maintaince.

It's ok to merge for now.

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 `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LeetCode Results</title>
${styles}
</head>
<body>
<pre>${result.trim()}</pre>
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
${body}
</body>
</html>`;
</html>
`;
}
}

interface IResult {
[key: string]: string[];
messages: string[];
}

export const leetCodeResultProvider: LeetCodeResultProvider = new LeetCodeResultProvider();
23 changes: 10 additions & 13 deletionssrc/webview/leetCodeSolutionProvider.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,36 +3,33 @@

import { Disposable, ExtensionContext, ViewColumn, WebviewPanel, window } from "vscode";
import { IProblem } from "../shared";
import {MarkdownEngine } from "./MarkdownEngine";
import {markdownEngine } from "./markdownEngine";

class LeetCodeSolutionProvider implements Disposable {

private context: ExtensionContext;
private panel: WebviewPanel | undefined;
private mdEngine: MarkdownEngine;
private solution: Solution;

public initialize(context: ExtensionContext): void {
this.context = context;
this.mdEngine = new MarkdownEngine();
}

public async show(solutionString: string, problem: IProblem): Promise<void> {
if (!this.panel) {
this.panel = window.createWebviewPanel("leetCode.solution", "Top Voted Solution", ViewColumn.Active, {
retainContextWhenHidden: true,
enableFindWidget: true,
localResourceRoots:this.mdEngine.localResourceRoots,
localResourceRoots:markdownEngine.localResourceRoots,
});

this.panel.onDidDispose(() => {
this.panel = undefined;
}, null, this.context.subscriptions);
}

this.solution = this.parseSolution(solutionString);
this.panel.title = problem.name;
this.panel.webview.html = this.getWebViewContent(this.solution);
constsolution: Solution = this.parseSolution(solutionString);
this.panel.title =`${problem.name}: Solution`;
this.panel.webview.html = this.getWebViewContent(solution);
this.panel.reveal(ViewColumn.Active);
}

Expand All@@ -56,17 +53,17 @@ class LeetCodeSolutionProvider implements Disposable {
}

private getWebViewContent(solution: Solution): string {
const styles: string =this.mdEngine.getStylesHTML();
const styles: string =markdownEngine.getStylesHTML();
const { title, url, lang, author, votes } = solution;
const head: string =this.mdEngine.render(`# [${title}](${url})`);
const head: string =markdownEngine.render(`# [${title}](${url})`);
const auth: string = `[${author}](https://leetcode.com/${author}/)`;
const info: string =this.mdEngine.render([
const info: string =markdownEngine.render([
`| Language | Author | Votes |`,
`| :------: | :------: | :------: |`,
`| ${lang} | ${auth} | ${votes} |`,
].join("\n"));
const body: string =this.mdEngine.render(solution.body, {
lang:this.solution.lang,
const body: string =markdownEngine.render(solution.body, {
lang: solution.lang,
host: "https://discuss.leetcode.com/",
});
return `
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ import * as path from "path";
import * as vscode from "vscode";
import { leetCodeChannel } from "../leetCodeChannel";

exportclass MarkdownEngine {
class MarkdownEngine {

private readonly engine: MarkdownIt;
private readonly extRoot: string; // root path of vscode built-in markdown extension
Expand DownExpand Up@@ -106,3 +106,5 @@ export class MarkdownEngine {
};
}
}

export const markdownEngine: MarkdownEngine = new MarkdownEngine();

[8]ページ先頭

©2009-2025 Movatter.jp