Skip to content

题解显示 katex 数学公式 #162

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

Merged
merged 1 commit into from
Jan 28, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## version 2.13.1

- 题解显示 katex 数学公式

## version 2.12.3

- hideScore 配置变化时刷新题目列表
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-leetcode-problem-rating",
"displayName": "LeetCode",
"description": "LeetCode 官方插件增强, 代码开源, 增加 LeetCode 题目难度分, 给个star吧, 球球了",
"version": "2.12.3",
"version": "2.13.1",
"author": "ccagml",
"publisher": "ccagml",
"license": "MIT",
Expand Down
2 changes: 2 additions & 0 deletions resources/katexcss/github-markdown.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions resources/katexcss/kates.min.css

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/service/MarkdownService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import * as hljs from "highlight.js";
import * as MarkdownIt from "markdown-it";
import * as MarkDownItKatex from "markdown-it-katex";
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
Expand All @@ -30,7 +31,10 @@ class MarkdownService implements vscode.Disposable {
}

public get localResourceRoots(): vscode.Uri[] {
return [vscode.Uri.file(path.join(this.config.extRoot, "media"))];
return [
vscode.Uri.file(path.join(this.config.extRoot, "media")),
vscode.Uri.file(path.join(__dirname, "..", "..", "..", "resources", "katexcss")),
];
}

public dispose(): void {
Expand Down Expand Up @@ -108,6 +112,7 @@ class MarkdownService implements vscode.Disposable {
},
});

md.use(MarkDownItKatex);
this.addCodeBlockHighlight(md);
this.addImageUrlCompletion(md);
this.addLinkValidator(md);
Expand Down
20 changes: 19 additions & 1 deletion src/service/SolutionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { BaseWebViewService } from "./BaseWebviewService";
import { markdownService } from "./MarkdownService";
import { IWebViewOption } from "../model/Model";

import * as path from "path";

class SolutionService extends BaseWebViewService {
protected readonly viewType: string = "leetcode.solution";
private problemName: string;
Expand Down Expand Up @@ -52,16 +54,32 @@ class SolutionService extends BaseWebViewService {
`| ${lang} | ${auth} | ${votes} |`,
].join("\n")
);

// $\textit
this.solution.body = this.solution.body.replace(/\$\textit/g, "$");

const body: string = markdownService.render(this.solution.body, {
lang: this.solution.lang,
host: "https://discuss.leetcode.com/",
});
// "<link rel=\"stylesheet\" type=\"text/css\" href=\"vscode-resource:/home/cc/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/extensions/markdown-language-features/media/markdown.css\">\n<link rel=\"stylesheet\" type=\"text/css\" href=\"vscode-resource:/home/cc/.vscode-server/bin/30d9c6cd9483b2cc586687151bcbcd635f373630/extensions/markdown-language-features/media/highlight.css\">\n<style>\nbody {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', system-ui, 'Ubuntu', 'Droid Sans', sans-serif;\n font-size: 14px;\n line-height: 1.6;\n}\n</style>"
// <meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource:; style-src vscode-resource:;"/>

return `
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource:; style-src vscode-resource:;"/>
<meta http-equiv="Content-Security-Policy" content="default-src self; img-src vscode-resource:; script-src vscode-resource: 'self' 'unsafe-inline'; style-src vscode-resource: 'self' 'unsafe-inline'; "/>
${styles}
<link rel="stylesheet" type="text/css" href= "vscode-resource:${path.join(
__dirname,
"..",
"..",
"..",
"resources",
"katexcss",
"kates.min.css"
)}">
</head>
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
${head}
Expand Down