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

Commita91c783

Browse files
Vigilansjdneo
authored andcommitted
Apply vscode's markdown settings to MarkdownEngine (LeetCode-OpenSource#260)
1 parent1430ac4 commita91c783

File tree

3 files changed

+74
-18
lines changed

3 files changed

+74
-18
lines changed

‎src/webview/leetCodeResultProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class LeetCodeResultProvider implements Disposable {
4242
<head>
4343
<meta charset="UTF-8">
4444
<meta name="viewport" content="width=device-width, initial-scale=1.0">
45-
${markdownEngine.getStylesHTML()}
45+
${markdownEngine.getStyles()}
4646
</head>
4747
<body>
4848
<pre><code>${result.trim()}</code></pre>

‎src/webview/leetCodeSolutionProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class LeetCodeSolutionProvider implements Disposable {
5353
}
5454

5555
privategetWebViewContent(solution:Solution):string{
56-
conststyles:string=markdownEngine.getStylesHTML();
56+
conststyles:string=markdownEngine.getStyles();
5757
const{ title, url, lang, author, votes}=solution;
5858
consthead:string=markdownEngine.render(`# [${title}](${url})`);
5959
constauth:string=`[${author}](https://leetcode.com/${author}/)`;

‎src/webview/markdownEngine.ts

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,70 @@ import * as os from "os";
77
import*aspathfrom"path";
88
import*asvscodefrom"vscode";
99
import{leetCodeChannel}from"../leetCodeChannel";
10+
import{isWindows}from"../utils/osUtils";
1011

11-
classMarkdownEngine{
12+
classMarkdownEngineimplementsvscode.Disposable{
1213

13-
privatereadonlyengine:MarkdownIt;
14-
privatereadonlyextRoot:string;// root path of vscode built-in markdown extension
14+
privateengine:MarkdownIt;
15+
privateconfig:MarkdownConfiguration;
16+
privatelistener:vscode.Disposable;
1517

1618
publicconstructor(){
17-
this.engine=this.initEngine();
18-
this.extRoot=path.join(vscode.env.appRoot,"extensions","markdown-language-features");
19+
this.reload();
20+
this.listener=vscode.workspace.onDidChangeConfiguration((event:vscode.ConfigurationChangeEvent)=>{
21+
if(event.affectsConfiguration("markdown")){
22+
this.reload();
23+
}
24+
},this);
1925
}
2026

2127
publicgetlocalResourceRoots():vscode.Uri[]{
22-
return[vscode.Uri.file(path.join(this.extRoot,"media"))];
28+
return[vscode.Uri.file(path.join(this.config.extRoot,"media"))];
2329
}
2430

25-
publicgetstyles():vscode.Uri[]{
26-
try{
27-
conststylePaths:string[]=require(path.join(this.extRoot,"package.json"))["contributes"]["markdown.previewStyles"];
28-
returnstylePaths.map((p:string)=>vscode.Uri.file(path.join(this.extRoot,p)).with({scheme:"vscode-resource"}));
29-
}catch(error){
30-
leetCodeChannel.appendLine("[Error] Fail to load built-in markdown style file.");
31-
return[];
32-
}
31+
publicdispose():void{
32+
this.listener.dispose();
3333
}
3434

35-
publicgetStylesHTML():string{
36-
returnthis.styles.map((style:vscode.Uri)=>`<link rel="stylesheet" type="text/css" href="${style.toString()}">`).join(os.EOL);
35+
publicreload():void{
36+
this.engine=this.initEngine();
37+
this.config=newMarkdownConfiguration();
3738
}
3839

3940
publicrender(md:string,env?:any):string{
4041
returnthis.engine.render(md,env);
4142
}
4243

44+
publicgetStyles():string{
45+
return[
46+
this.getBuiltinStyles(),
47+
this.getSettingsStyles(),
48+
].join(os.EOL);
49+
}
50+
51+
privategetBuiltinStyles():string{
52+
letstyles:vscode.Uri[]=[];
53+
try{
54+
conststylePaths:string[]=require(path.join(this.config.extRoot,"package.json"))["contributes"]["markdown.previewStyles"];
55+
styles=stylePaths.map((p:string)=>vscode.Uri.file(path.join(this.config.extRoot,p)).with({scheme:"vscode-resource"}));
56+
}catch(error){
57+
leetCodeChannel.appendLine("[Error] Fail to load built-in markdown style file.");
58+
}
59+
returnstyles.map((style:vscode.Uri)=>`<link rel="stylesheet" type="text/css" href="${style.toString()}">`).join(os.EOL);
60+
}
61+
62+
privategetSettingsStyles():string{
63+
return[
64+
`<style>`,
65+
`body {`,
66+
`${this.config.fontFamily ?`font-family:${this.config.fontFamily};` :``}`,
67+
`${isNaN(this.config.fontSize) ?`` :`font-size:${this.config.fontSize}px;`}`,
68+
`${isNaN(this.config.lineHeight) ?`` :`line-height:${this.config.lineHeight};`}`,
69+
`}`,
70+
`</style>`,
71+
].join(os.EOL);
72+
}
73+
4374
privateinitEngine():MarkdownIt{
4475
constmd:MarkdownIt=newMarkdownIt({
4576
linkify:true,
@@ -107,4 +138,29 @@ class MarkdownEngine {
107138
}
108139
}
109140

141+
// tslint:disable-next-line: max-classes-per-file
142+
classMarkdownConfiguration{
143+
144+
publicreadonlyextRoot:string;// root path of vscode built-in markdown extension
145+
publicreadonlylineHeight:number;
146+
publicreadonlyfontSize:number;
147+
publicreadonlyfontFamily:string;
148+
149+
publicconstructor(){
150+
constmarkdownConfig:vscode.WorkspaceConfiguration=vscode.workspace.getConfiguration("markdown");
151+
this.extRoot=path.join(vscode.env.appRoot,"extensions","markdown-language-features");
152+
this.lineHeight=Math.max(0.6,+markdownConfig.get<number>("preview.lineHeight",NaN));
153+
this.fontSize=Math.max(8,+markdownConfig.get<number>("preview.fontSize",NaN));
154+
this.fontFamily=this.resolveFontFamily(markdownConfig);
155+
}
156+
157+
privateresolveFontFamily(config:vscode.WorkspaceConfiguration):string{
158+
letfontFamily:string=config.get<string>("preview.fontFamily","");
159+
if(isWindows()&&fontFamily===config.inspect<string>("preview.fontFamily")!.defaultValue){
160+
fontFamily=`${fontFamily}, 'Microsoft Yahei UI'`;
161+
}
162+
returnfontFamily;
163+
}
164+
}
165+
110166
exportconstmarkdownEngine:MarkdownEngine=newMarkdownEngine();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp