Skip to content
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

parseDescription using regex #752

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
55 changes: 29 additions & 26 deletions src/webview/leetCodePreviewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,37 @@ class LeetCodePreviewProvider extends LeetCodeWebview {
// await commands.executeCommand("workbench.action.toggleSidebarVisibility");
// }

private parseDescription(descString: string, problem: IProblem): IDescription {
const [
/* title */, ,
url, ,
/* tags */, ,
/* langs */, ,
category,
difficulty,
likes,
dislikes,
/* accepted */,
/* submissions */,
/* testcase */, ,
...body
] = descString.split("\n");
private parseDescription(descString, problem) {
// Parse body by looking for the first html tag
const bodyStartIdx = descString.search(/<.*>/);
const bodyRaw = descString.substring(bodyStartIdx);

const { name: title, tags, companies } = problem;
return {
title: problem.name,
url,
tags: problem.tags,
companies: problem.companies,
category: category.slice(2),
difficulty: difficulty.slice(2),
likes: likes.split(": ")[1].trim(),
dislikes: dislikes.split(": ")[1].trim(),
body: body.join("\n").replace(/<pre>[\r\n]*([^]+?)[\r\n]*<\/pre>/g, "<pre><code>$1</code></pre>"),
title,
tags,
companies,
url: descString.match(/https:.*leetcode.*/)?.[0] || "??",
// Category is the first element in list
category: descString.match(/\*.*/)?.[0]?.slice(2) || "??",
// Difficulty is the first element in list with a percentage sign
difficulty: descString.match(/.*\%.*/)?.[0]?.slice(2) || "??",
likes:
descString
.match(/Likes.*?\n/)?.[0]
?.split(": ")[1]
?.trim() || "0",
dislikes:
descString
.match(/Dislikes.*?\n/)?.[0]
?.split(": ")[1]
?.trim() || "0",
body: bodyRaw.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) {
Expand Down