|
1 | 1 | // Copyright (c) jdneo. All rights reserved.
|
2 | 2 | // Licensed under the MIT license.
|
3 | 3 |
|
4 |
| -import*ashljsfrom"highlight.js"; |
5 |
| -import*asMarkdownItfrom"markdown-it"; |
6 |
| -import*aspathfrom"path"; |
7 |
| -import*asvscodefrom"vscode"; |
8 | 4 | import{Disposable,ExtensionContext,ViewColumn,WebviewPanel,window}from"vscode";
|
9 |
| -import{leetCodeChannel}from"./leetCodeChannel"; |
10 | 5 | import{IProblem}from"./shared";
|
| 6 | +import{MarkdownEngine}from"./webview/MarkdownEngine"; |
11 | 7 |
|
12 | 8 | classLeetCodeSolutionProviderimplementsDisposable{
|
13 | 9 |
|
14 | 10 | privatecontext:ExtensionContext;
|
15 | 11 | privatepanel:WebviewPanel|undefined;
|
16 |
| -privatemarkdown:MarkdownIt; |
17 |
| -privatemarkdownPath:string;// path of vscode built-in markdown extension |
| 12 | +privatemdEngine:MarkdownEngine; |
18 | 13 | privatesolution:Solution;
|
19 | 14 |
|
20 | 15 | publicinitialize(context:ExtensionContext):void{
|
21 | 16 | this.context=context;
|
22 |
| -this.markdown=newMarkdownIt({ |
23 |
| -linkify:true, |
24 |
| -typographer:true, |
25 |
| -highlight:this.codeHighlighter.bind(this), |
26 |
| -}); |
27 |
| -this.markdownPath=path.join(vscode.env.appRoot,"extensions","markdown-language-features"); |
28 |
| - |
29 |
| -// Override code_block rule for highlighting in solution language |
30 |
| -// tslint:disable-next-line:typedef |
31 |
| -this.markdown.renderer.rules["code_block"]=(tokens,idx,options,_,self)=>{ |
32 |
| -consthighlight:string=options.highlight(tokens[idx].content,undefined); |
33 |
| -return[ |
34 |
| -`<pre><code${self.renderAttrs(tokens[idx])} >`, |
35 |
| -highlight||this.markdown.utils.escapeHtml(tokens[idx].content), |
36 |
| -"</code></pre>", |
37 |
| -].join("\n"); |
38 |
| -}; |
| 17 | +this.mdEngine=newMarkdownEngine(); |
39 | 18 | }
|
40 | 19 |
|
41 | 20 | publicasyncshow(solutionString:string,problem:IProblem):Promise<void>{
|
42 | 21 | if(!this.panel){
|
43 | 22 | this.panel=window.createWebviewPanel("leetCode.solution","Top Voted Solution",ViewColumn.Active,{
|
44 | 23 | retainContextWhenHidden:true,
|
45 | 24 | enableFindWidget:true,
|
46 |
| -localResourceRoots:[vscode.Uri.file(path.join(this.markdownPath,"media"))], |
| 25 | +localResourceRoots:this.mdEngine.localResourceRoots, |
47 | 26 | });
|
48 | 27 |
|
49 | 28 | this.panel.onDidDispose(()=>{
|
@@ -76,41 +55,20 @@ class LeetCodeSolutionProvider implements Disposable {
|
76 | 55 | returnsolution;
|
77 | 56 | }
|
78 | 57 |
|
79 |
| -privatecodeHighlighter(code:string,lang:string|undefined):string{ |
80 |
| -if(!lang){ |
81 |
| -lang=this.solution.lang; |
82 |
| -} |
83 |
| -if(hljs.getLanguage(lang)){ |
84 |
| -try{ |
85 |
| -returnhljs.highlight(lang,code,true).value; |
86 |
| -}catch(error){/* do not highlight */} |
87 |
| -} |
88 |
| -return"";// use external default escaping |
89 |
| -} |
90 |
| - |
91 |
| -privategetMarkdownStyles():vscode.Uri[]{ |
92 |
| -try{ |
93 |
| -conststylePaths:string[]=require(path.join(this.markdownPath,"package.json"))["contributes"]["markdown.previewStyles"]; |
94 |
| -returnstylePaths.map((p:string)=>vscode.Uri.file(path.join(this.markdownPath,p)).with({scheme:"vscode-resource"})); |
95 |
| -}catch(error){ |
96 |
| -leetCodeChannel.appendLine("[Error] Fail to load built-in markdown style file."); |
97 |
| -return[]; |
98 |
| -} |
99 |
| -} |
100 |
| - |
101 | 58 | privategetWebViewContent(solution:Solution):string{
|
102 |
| -conststyles:string=this.getMarkdownStyles() |
103 |
| -.map((style:vscode.Uri)=>`<link rel="stylesheet" type="text/css" href="${style.toString()}">`) |
104 |
| -.join("\n"); |
| 59 | +conststyles:string=this.mdEngine.getStylesHTML(); |
105 | 60 | const{ title, url, lang, author, votes}=solution;
|
106 |
| -consthead:string=this.markdown.render(`# [${title}](${url})`); |
| 61 | +consthead:string=this.mdEngine.render(`# [${title}](${url})`); |
107 | 62 | constauth:string=`[${author}](https://leetcode.com/${author}/)`;
|
108 |
| -constinfo:string=this.markdown.render([ |
| 63 | +constinfo:string=this.mdEngine.render([ |
109 | 64 | `| Language | Author | Votes |`,
|
110 | 65 | `| :------: | :------: | :------: |`,
|
111 | 66 | `|${lang} |${auth} |${votes} |`,
|
112 | 67 | ].join("\n"));
|
113 |
| -constbody:string=this.markdown.render(solution.body); |
| 68 | +constbody:string=this.mdEngine.render(solution.body,{ |
| 69 | +lang:this.solution.lang, |
| 70 | +host:"https://discuss.leetcode.com/", |
| 71 | +}); |
114 | 72 | return`
|
115 | 73 | <!DOCTYPE html>
|
116 | 74 | <html>
|
|