Skip to content

Commit

Permalink
feat: added solution link (LeetCode-OpenSource#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
iFun authored and jdneo committed Oct 2, 2019
1 parent 01f5a28 commit ffdccec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
4 changes: 3 additions & 1 deletion ACKNOWLEDGEMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

A big thanks to the following individuals for contributing:

- [@JIEJIAN21](https://github.com/JIEJIAN21) - thanks for logo and icon design
- [@TsFreddie](https://github.com/TsFreddie)[contributions](https://github.com/jdneo/vscode-leetcode/commits?author=TsFreddie)
- [@ntt2k](https://github.com/ntt2k)[contributions](https://github.com/jdneo/vscode-leetcode/commits?author=ntt2k)
- [@purocean](https://github.com/purocean)[contributions](https://github.com/jdneo/vscode-leetcode/commits?author=purocean)
Expand All @@ -17,4 +18,5 @@ A big thanks to the following individuals for contributing:
- [@houtianze](https://github.com/houtianze)[contributions](https://github.com/jdneo/vscode-leetcode/commits?author=houtianze)
- [@magic-akari](https://github.com/magic-akari) - [contributions](https://github.com/jdneo/vscode-leetcode/commits?author=magic-akari)
- [@SF-Zhou](https://github.com/SF-Zhou) - [contributions](https://github.com/jdneo/vscode-leetcode/commits?author=SF-Zhou)
- [@JIEJIAN21](https://github.com/JIEJIAN21) - thanks for logo and icon design
- [@fuafa](https://github.com/fuafa) - [contributions](https://github.com/jdneo/vscode-leetcode/commits?author=fuafa)
- [@iFun](https://github.com/iFun) - [contributions](https://github.com/jdneo/vscode-leetcode/commits?author=iFun)
27 changes: 23 additions & 4 deletions src/webview/leetCodePreviewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Licensed under the MIT license.

import { commands, ViewColumn } from "vscode";
import { IProblem } from "../shared";
import { getLeetCodeEndpoint } from "../commands/plugin";
import { Endpoint, IProblem } from "../shared";
import { ILeetCodeWebviewOption, LeetCodeWebview } from "./LeetCodeWebview";
import { markdownEngine } from "./markdownEngine";

Expand Down Expand Up @@ -73,9 +74,9 @@ class LeetCodePreviewProvider extends LeetCodeWebview {
const { title, url, category, difficulty, likes, dislikes, body } = this.description;
const head: string = markdownEngine.render(`# [${title}](${url})`);
const info: string = markdownEngine.render([
`| Category | Difficulty | Likes | Dislikes | [Discuss](${url.replace("/description/", "/discuss/?currentPage=1&orderBy=most_votes&query=")}) |`,
`| :------: | :--------: | :---: | :------: | :-----: |`,
`| ${category} | ${difficulty} | ${likes} | ${dislikes} | -- |`,
`| Category | Difficulty | Likes | Dislikes |`,
`| :------: | :--------: | :---: | :------: |`,
`| ${category} | ${difficulty} | ${likes} | ${dislikes} |`,
].join("\n"));
const tags: string = [
`<details>`,
Expand All @@ -97,6 +98,7 @@ class LeetCodePreviewProvider extends LeetCodeWebview {
),
`</details>`,
].join("\n");
const links: string = markdownEngine.render(`[Discussion](${this.getDiscussionLink(url)}) | [Solution](${this.getSolutionLink(url)})`);
return `
<!DOCTYPE html>
<html>
Expand All @@ -114,6 +116,8 @@ class LeetCodePreviewProvider extends LeetCodeWebview {
${tags}
${companies}
${body}
<hr />
${links}
${!this.sideMode ? button.element : ""}
<script>
const vscode = acquireVsCodeApi();
Expand Down Expand Up @@ -172,6 +176,21 @@ class LeetCodePreviewProvider extends LeetCodeWebview {
body: body.join("\n").replace(/<pre>[\r\n]*([^]+?)[\r\n]*<\/pre>/g, "<pre><code>$1</code></pre>"),
};
}

private getDiscussionLink(url: string): string {
const endPoint: string = getLeetCodeEndpoint();
if (endPoint === Endpoint.LeetCodeCN) {
return url.replace("/description/", "/comments/");
} else if (endPoint === Endpoint.LeetCode) {
return url.replace("/description/", "/discuss/?currentPage=1&orderBy=most_votes&query=");
}

return "https://leetcode.com";
}

private getSolutionLink(url: string): string {
return url.replace("/description/", "/solution/");
}
}

interface IDescription {
Expand Down

0 comments on commit ffdccec

Please sign in to comment.