Skip to content

Commit 221ff29

Browse files
committed
Add markdown-it parser with syntax highlight
1 parent 2a83af2 commit 221ff29

File tree

3 files changed

+107
-29
lines changed

3 files changed

+107
-29
lines changed

package-lock.json

Lines changed: 60 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@
287287
},
288288
"devDependencies": {
289289
"@types/fs-extra": "5.0.0",
290+
"@types/highlight.js": "^9.12.3",
290291
"@types/lodash.kebabcase": "^4.1.5",
292+
"@types/markdown-it": "0.0.7",
291293
"@types/mocha": "^2.2.42",
292294
"@types/node": "^7.0.43",
293295
"@types/require-from-string": "^1.2.0",
@@ -297,8 +299,10 @@
297299
},
298300
"dependencies": {
299301
"fs-extra": "^6.0.1",
300-
"vsc-leetcode-cli": "2.6.2",
302+
"highlight.js": "^9.15.6",
301303
"lodash.kebabcase": "^4.1.1",
302-
"require-from-string": "^2.0.2"
304+
"markdown-it": "^8.4.2",
305+
"require-from-string": "^2.0.2",
306+
"vsc-leetcode-cli": "2.6.2"
303307
}
304308
}

src/leetCodeSolutionProvider.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
// Copyright (c) jdneo. All rights reserved.
22
// Licensed under the MIT license.
33

4+
import * as hljs from "highlight.js";
5+
import * as MarkdownIt from "markdown-it";
46
import { Disposable, ExtensionContext, ViewColumn, WebviewPanel, window } from "vscode";
57
import { Solution } from "./shared";
68

79
class LeetCodeSolutionProvider implements Disposable {
810

911
private context: ExtensionContext;
1012
private panel: WebviewPanel | undefined;
13+
private markdown: MarkdownIt;
14+
private solution: Solution;
1115

1216
public initialize(context: ExtensionContext): void {
1317
this.context = context;
18+
this.markdown = new MarkdownIt({
19+
linkify: true,
20+
typographer: true,
21+
highlight: this.codeHighlighter.bind(this),
22+
});
1423
}
1524

1625
public async show(solutionString: string): Promise<void> {
@@ -25,9 +34,9 @@ class LeetCodeSolutionProvider implements Disposable {
2534
}, null, this.context.subscriptions);
2635
}
2736

28-
const solution: Solution = this.parseSolution(solutionString);
29-
this.panel.title = solution.title;
30-
this.panel.webview.html = this.getWebViewContent(solution.body);
37+
this.solution = this.parseSolution(solutionString);
38+
this.panel.title = this.solution.title;
39+
this.panel.webview.html = this.getWebViewContent(this.solution.body);
3140
this.panel.reveal(ViewColumn.Active);
3241
}
3342

@@ -50,20 +59,35 @@ class LeetCodeSolutionProvider implements Disposable {
5059
return solution;
5160
}
5261

53-
private getWebViewContent(result: string): string {
54-
return `
55-
<!DOCTYPE html>
56-
<html lang="en">
57-
<head>
58-
<meta charset="UTF-8">
59-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
60-
<title>LeetCode Top Voted Solution</title>
61-
</head>
62-
<body>
63-
<pre>${result}</pre>
64-
</body>
65-
</html>
66-
`;
62+
private codeHighlighter(code: string, lang: string | undefined): string {
63+
if (!lang) {
64+
lang = this.solution.lang;
65+
}
66+
// tslint:disable-next-line:typedef
67+
const hljst = hljs;
68+
if (hljst.getLanguage(lang)) {
69+
try {
70+
return hljst.highlight(lang, code).value;
71+
} catch (error) { /* do not highlight */ }
72+
}
73+
return ""; // use external default escaping
74+
}
75+
76+
private getWebViewContent(body: string): string {
77+
return this.markdown.render(body);
78+
// return `
79+
// <!DOCTYPE html>
80+
// <html lang="en">
81+
// <head>
82+
// <meta charset="UTF-8">
83+
// <meta name="viewport" content="width=device-width, initial-scale=1.0">
84+
// <title>LeetCode Top Voted Solution</title>
85+
// </head>
86+
// <body>
87+
// <pre>${body}</pre>
88+
// </body>
89+
// </html>
90+
// `;
6791
}
6892
}
6993

0 commit comments

Comments
 (0)