Skip to content

Top voted solution webview #193

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Enrich content in webview
  • Loading branch information
Vigilans committed Mar 8, 2019
commit eaba0610c69f3997350befc8b7fca69f3f9632c5
2 changes: 1 addition & 1 deletion src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function showSolution(node?: LeetCodeNode): Promise<void> {
let solution: string = await leetCodeExecutor.showSolution(node, language);
// remove slash in espaced \'...\'(generated by leetcode's database)
solution = solution.replace(/\\'/g, "'");
await leetCodeSolutionProvider.show(solution);
await leetCodeSolutionProvider.show(solution, node);
} catch (error) {
await promptForOpenOutputChannel("Failed to fetch the top voted solution. Please open the output channel for details.", DialogType.error);
}
Expand Down
16 changes: 13 additions & 3 deletions src/leetCodeSolutionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as MarkdownIt from "markdown-it";
import * as path from "path";
import * as vscode from "vscode";
import { Disposable, ExtensionContext, ViewColumn, WebviewPanel, window } from "vscode";
import { Solution } from "./shared";
import { IProblem, Solution } from "./shared";

class LeetCodeSolutionProvider implements Disposable {

Expand Down Expand Up @@ -37,7 +37,7 @@ class LeetCodeSolutionProvider implements Disposable {
};
}

public async show(solutionString: string): Promise<void> {
public async show(solutionString: string, problem: IProblem): Promise<void> {
if (!this.panel) {
this.panel = window.createWebviewPanel("leetCode", "Top voted solution", ViewColumn.Active, {
retainContextWhenHidden: true,
Expand All @@ -51,7 +51,7 @@ class LeetCodeSolutionProvider implements Disposable {
}

this.solution = this.parseSolution(solutionString);
this.panel.title = this.solution.title;
this.panel.title = problem.name;
this.panel.webview.html = this.getWebViewContent(this.solution);
this.panel.reveal(ViewColumn.Active);
}
Expand Down Expand Up @@ -96,6 +96,14 @@ class LeetCodeSolutionProvider implements Disposable {
const styles: string = this.getMarkdownStyles()
.map((style: vscode.Uri) => `<link rel="stylesheet" type="text/css" href="${style.toString()}">`)
.join("\n");
const { title, url, lang, author, votes } = solution;
const head: string = this.markdown.render(`# [${title}](${url})`);
const auth: string = `[${author}](https://leetcode.com/${author}/)`;
const info: string = this.markdown.render([
`| Language | Author | Votes |`,
`| :------: | :------: | :------: |`,
`| ${lang} | ${auth} | ${votes} |`,
].join("\n"));
const body: string = this.markdown.render(solution.body);
return `
<!DOCTYPE html>
Expand All @@ -104,6 +112,8 @@ class LeetCodeSolutionProvider implements Disposable {
${styles}
</head>
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
${head}
${info}
${body}
</body>
</html>
Expand Down