|
1 | 1 | // Copyright (c) jdneo. All rights reserved.
|
2 | 2 | // Licensed under the MIT license.
|
3 | 3 |
|
4 | | -import * as hljs from "highlight.js"; |
5 | | -import * as MarkdownIt from "markdown-it"; |
6 | | -import * as path from "path"; |
7 | | -import * as vscode from "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 | class LeetCodeSolutionProvider implements Disposable {
|
13 | 9 |
|
14 | 10 | private context: ExtensionContext;
|
15 | 11 | private panel: WebviewPanel | undefined;
|
16 | | - private markdown: MarkdownIt; |
17 | | - private markdownPath: string; // path of vscode built-in markdown extension |
| 12 | + private mdEngine: MarkdownEngine; |
18 | 13 | private solution: Solution;
|
19 | 14 |
|
20 | 15 | public initialize(context: ExtensionContext): void {
|
21 | 16 | this.context = context;
|
22 | | - this.markdown = new MarkdownIt({ |
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 | | - const highlight: 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 = new MarkdownEngine(); |
39 | 18 | }
|
40 | 19 |
|
41 | 20 | public async show(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 | return solution;
|
77 | 56 | }
|
78 | 57 |
|
79 | | - private codeHighlighter(code: string, lang: string | undefined): string { |
80 | | - if (!lang) { |
81 | | - lang = this.solution.lang; |
82 | | - } |
83 | | - if (hljs.getLanguage(lang)) { |
84 | | - try { |
85 | | - return hljs.highlight(lang, code, true).value; |
86 | | - } catch (error) { /* do not highlight */ } |
87 | | - } |
88 | | - return ""; // use external default escaping |
89 | | - } |
90 | | - |
91 | | - private getMarkdownStyles(): vscode.Uri[] { |
92 | | - try { |
93 | | - const stylePaths: string[] = require(path.join(this.markdownPath, "package.json"))["contributes"]["markdown.previewStyles"]; |
94 | | - return stylePaths.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 | private getWebViewContent(solution: Solution): string {
|
102 | | - const styles: string = this.getMarkdownStyles() |
103 | | - .map((style: vscode.Uri) => `<link rel="stylesheet" type="text/css" href="${style.toString()}">`) |
104 | | - .join("\n"); |
| 59 | + const styles: string = this.mdEngine.getStylesHTML(); |
105 | 60 | const { title, url, lang, author, votes } = solution;
|
106 | | - const head: string = this.markdown.render(`# [${title}](${url})`); |
| 61 | + const head: string = this.mdEngine.render(`# [${title}](${url})`); |
107 | 62 | const auth: string = `[${author}](https://leetcode.com/${author}/)`;
|
108 | | - const info: string = this.markdown.render([ |
| 63 | + const info: string = this.mdEngine.render([ |
109 | 64 | `| Language | Author | Votes |`,
|
110 | 65 | `| :------: | :------: | :------: |`,
|
111 | 66 | `| ${lang} | ${auth} | ${votes} |`,
|
112 | 67 | ].join("\n"));
|
113 | | - const body: string = this.markdown.render(solution.body); |
| 68 | + const body: 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>
|
|
0 commit comments