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

Commit8f85142

Browse files
committed
Update parseDescription
1 parentde1dc41 commit8f85142

File tree

1 file changed

+154
-140
lines changed

1 file changed

+154
-140
lines changed

‎src/webview/leetCodePreviewProvider.ts

Lines changed: 154 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,54 @@ import { ILeetCodeWebviewOption, LeetCodeWebview } from "./LeetCodeWebview";
88
import{markdownEngine}from"./markdownEngine";
99

1010
classLeetCodePreviewProviderextendsLeetCodeWebview{
11-
12-
protectedreadonlyviewType:string="leetcode.preview";
13-
privatenode:IProblem;
14-
privatedescription:IDescription;
15-
privatesideMode:boolean=false;
16-
17-
publicisSideMode():boolean{
18-
returnthis.sideMode;
19-
}
20-
21-
publicshow(descString:string,node:IProblem,isSideMode:boolean=false):void{
22-
this.description=this.parseDescription(descString,node);
23-
this.node=node;
24-
this.sideMode=isSideMode;
25-
this.showWebviewInternal();
26-
// Comment out this operation since it sometimes may cause the webview become empty.
27-
// Waiting for the progress of the VS Code side issue: https://github.com/microsoft/vscode/issues/3742
28-
// if (this.sideMode) {
29-
// this.hideSideBar(); // For better view area
30-
// }
31-
}
32-
33-
protectedgetWebviewOption():ILeetCodeWebviewOption{
34-
if(!this.sideMode){
35-
return{
36-
title:`${this.node.name}: Preview`,
37-
viewColumn:ViewColumn.One,
38-
};
39-
}else{
40-
return{
41-
title:"Description",
42-
viewColumn:ViewColumn.Two,
43-
preserveFocus:true,
44-
};
45-
}
11+
protectedreadonlyviewType:string="leetcode.preview";
12+
privatenode:IProblem;
13+
privatedescription:IDescription;
14+
privatesideMode:boolean=false;
15+
16+
publicisSideMode():boolean{
17+
returnthis.sideMode;
18+
}
19+
20+
publicshow(
21+
descString:string,
22+
node:IProblem,
23+
isSideMode:boolean=false
24+
):void{
25+
this.description=this.parseDescription(descString,node);
26+
this.node=node;
27+
this.sideMode=isSideMode;
28+
this.showWebviewInternal();
29+
// Comment out this operation since it sometimes may cause the webview become empty.
30+
// Waiting for the progress of the VS Code side issue: https://github.com/microsoft/vscode/issues/3742
31+
// if (this.sideMode) {
32+
// this.hideSideBar(); // For better view area
33+
// }
34+
}
35+
36+
protectedgetWebviewOption():ILeetCodeWebviewOption{
37+
if(!this.sideMode){
38+
return{
39+
title:`${this.node.name}: Preview`,
40+
viewColumn:ViewColumn.One,
41+
};
42+
}else{
43+
return{
44+
title:"Description",
45+
viewColumn:ViewColumn.Two,
46+
preserveFocus:true,
47+
};
4648
}
49+
}
4750

48-
protectedgetWebviewContent():string{
49-
constbutton:{element:string,script:string,style:string}={
50-
element:`<button>Code Now</button>`,
51-
script:`const button = document.getElementById('solve');
51+
protectedgetWebviewContent():string{
52+
constbutton:{element:string;script:string;style:string}={
53+
element:`<button>Code Now</button>`,
54+
script:`const button = document.getElementById('solve');
5255
button.onclick = () => vscode.postMessage({
5356
command: 'ShowProblem',
5457
});`,
55-
style:`<style>
58+
style:`<style>
5659
#solve {
5760
position: fixed;
5861
bottom: 1rem;
@@ -70,36 +73,41 @@ class LeetCodePreviewProvider extends LeetCodeWebview {
7073
border: 0;
7174
}
7275
</style>`,
73-
};
74-
const{ title, url, category, difficulty, likes, dislikes, body}=this.description;
75-
consthead:string=markdownEngine.render(`# [${title}](${url})`);
76-
constinfo:string=markdownEngine.render([
77-
`| Category | Difficulty | Likes | Dislikes |`,
78-
`| :------: | :--------: | :---: | :------: |`,
79-
`|${category} |${difficulty} |${likes} |${dislikes} |`,
80-
].join("\n"));
81-
consttags:string=[
82-
`<details>`,
83-
`<summary><strong>Tags</strong></summary>`,
84-
markdownEngine.render(
85-
this.description.tags
86-
.map((t:string)=>`[\`${t}\`](https://leetcode.com/tag/${t})`)
87-
.join(" | "),
88-
),
89-
`</details>`,
90-
].join("\n");
91-
constcompanies:string=[
92-
`<details>`,
93-
`<summary><strong>Companies</strong></summary>`,
94-
markdownEngine.render(
95-
this.description.companies
96-
.map((c:string)=>`\`${c}\``)
97-
.join(" | "),
98-
),
99-
`</details>`,
100-
].join("\n");
101-
constlinks:string=markdownEngine.render(`[Discussion](${this.getDiscussionLink(url)}) | [Solution](${this.getSolutionLink(url)})`);
102-
return`
76+
};
77+
const{ title, url, category, difficulty, likes, dislikes, body}=
78+
this.description;
79+
consthead:string=markdownEngine.render(`# [${title}](${url})`);
80+
constinfo:string=markdownEngine.render(
81+
[
82+
`| Category | Difficulty | Likes | Dislikes |`,
83+
`| :------: | :--------: | :---: | :------: |`,
84+
`|${category} |${difficulty} |${likes} |${dislikes} |`,
85+
].join("\n")
86+
);
87+
consttags:string=[
88+
`<details>`,
89+
`<summary><strong>Tags</strong></summary>`,
90+
markdownEngine.render(
91+
this.description.tags
92+
.map((t:string)=>`[\`${t}\`](https://leetcode.com/tag/${t})`)
93+
.join(" | ")
94+
),
95+
`</details>`,
96+
].join("\n");
97+
constcompanies:string=[
98+
`<details>`,
99+
`<summary><strong>Companies</strong></summary>`,
100+
markdownEngine.render(
101+
this.description.companies.map((c:string)=>`\`${c}\``).join(" | ")
102+
),
103+
`</details>`,
104+
].join("\n");
105+
constlinks:string=markdownEngine.render(
106+
`[Discussion](${this.getDiscussionLink(
107+
url
108+
)}) | [Solution](${this.getSolutionLink(url)})`
109+
);
110+
return`
103111
<!DOCTYPE html>
104112
<html>
105113
<head>
@@ -126,85 +134,91 @@ class LeetCodePreviewProvider extends LeetCodeWebview {
126134
</body>
127135
</html>
128136
`;
137+
}
138+
139+
protectedonDidDisposeWebview():void{
140+
super.onDidDisposeWebview();
141+
this.sideMode=false;
142+
}
143+
144+
protectedasynconDidReceiveMessage(message:IWebViewMessage):Promise<void>{
145+
switch(message.command){
146+
case"ShowProblem":{
147+
awaitcommands.executeCommand("leetcode.showProblem",this.node);
148+
break;
149+
}
129150
}
130-
131-
protectedonDidDisposeWebview():void{
132-
super.onDidDisposeWebview();
133-
this.sideMode=false;
151+
}
152+
153+
// private async hideSideBar(): Promise<void> {
154+
// await commands.executeCommand("workbench.action.focusSideBar");
155+
// await commands.executeCommand("workbench.action.toggleSidebarVisibility");
156+
// }
157+
158+
privateparseDescription(descString,problem){
159+
// Parse body by looking for the first html tag
160+
constbodyStartIdx=descString.search(/<.*>/);
161+
constbodyRaw=descString.substring(bodyStartIdx);
162+
163+
const{name:title, tags, companies}=problem;
164+
return{
165+
title,
166+
tags,
167+
companies,
168+
url:descString.match(/https:.*leetcode.*/)?.[0]||"??",
169+
category:descString.match(/\*.*/)?.[0]?.slice(2)||"??",// Category is the first element in list
170+
difficulty:descString.match(/.*\%.*/)?.[0]?.slice(2)||"??",// Difficulty is the first element in list with a percentage sign
171+
likes:
172+
descString
173+
.match(/Likes.*?\n/)?.[0]
174+
?.split(": ")[1]
175+
?.trim()||"0",
176+
dislikes:
177+
descString
178+
.match(/Dislikes.*?\n/)?.[0]
179+
?.split(": ")[1]
180+
?.trim()||"0",
181+
body:bodyRaw.replace(
182+
/<pre>[\r\n]*([^]+?)[\r\n]*<\/pre>/g,
183+
"<pre><code>$1</code></pre>"
184+
),
185+
};
186+
}
187+
188+
privategetDiscussionLink(url:string):string{
189+
constendPoint:string=getLeetCodeEndpoint();
190+
if(endPoint===Endpoint.LeetCodeCN){
191+
returnurl.replace("/description/","/comments/");
192+
}elseif(endPoint===Endpoint.LeetCode){
193+
returnurl.replace(
194+
"/description/",
195+
"/discuss/?currentPage=1&orderBy=most_votes&query="
196+
);
134197
}
135198

136-
protectedasynconDidReceiveMessage(message:IWebViewMessage):Promise<void>{
137-
switch(message.command){
138-
case"ShowProblem":{
139-
awaitcommands.executeCommand("leetcode.showProblem",this.node);
140-
break;
141-
}
142-
}
143-
}
199+
return"https://leetcode.com";
200+
}
144201

145-
// private async hideSideBar(): Promise<void> {
146-
// await commands.executeCommand("workbench.action.focusSideBar");
147-
// await commands.executeCommand("workbench.action.toggleSidebarVisibility");
148-
// }
149-
150-
privateparseDescription(descString:string,problem:IProblem):IDescription{
151-
const[
152-
/* title */,,
153-
url,,
154-
/* tags */,,
155-
/* langs */,,
156-
category,
157-
difficulty,
158-
likes,
159-
dislikes,
160-
/* accepted */,
161-
/* submissions */,
162-
/* testcase */,,
163-
...body
164-
]=descString.split("\n");
165-
return{
166-
title:problem.name,
167-
url,
168-
tags:problem.tags,
169-
companies:problem.companies,
170-
category:category.slice(2),
171-
difficulty:difficulty.slice(2),
172-
likes:likes.split(": ")[1].trim(),
173-
dislikes:dislikes.split(": ")[1].trim(),
174-
body:body.join("\n").replace(/<pre>[\r\n]*([^]+?)[\r\n]*<\/pre>/g,"<pre><code>$1</code></pre>"),
175-
};
176-
}
177-
178-
privategetDiscussionLink(url:string):string{
179-
constendPoint:string=getLeetCodeEndpoint();
180-
if(endPoint===Endpoint.LeetCodeCN){
181-
returnurl.replace("/description/","/comments/");
182-
}elseif(endPoint===Endpoint.LeetCode){
183-
returnurl.replace("/description/","/discuss/?currentPage=1&orderBy=most_votes&query=");
184-
}
185-
186-
return"https://leetcode.com";
187-
}
188-
189-
privategetSolutionLink(url:string):string{
190-
returnurl.replace("/description/","/solution/");
191-
}
202+
privategetSolutionLink(url:string):string{
203+
returnurl.replace("/description/","/solution/");
204+
}
192205
}
193206

194207
interfaceIDescription{
195-
title:string;
196-
url:string;
197-
tags:string[];
198-
companies:string[];
199-
category:string;
200-
difficulty:string;
201-
likes:string;
202-
dislikes:string;
203-
body:string;
208+
title:string;
209+
url:string;
210+
tags:string[];
211+
companies:string[];
212+
category:string;
213+
difficulty:string;
214+
likes:string;
215+
dislikes:string;
216+
body:string;
204217
}
205218

206219
interfaceIWebViewMessage{
207-
command:string;
220+
command:string;
208221
}
209222

210-
exportconstleetCodePreviewProvider:LeetCodePreviewProvider=newLeetCodePreviewProvider();
223+
exportconstleetCodePreviewProvider:LeetCodePreviewProvider=
224+
newLeetCodePreviewProvider();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp