-
Notifications
You must be signed in to change notification settings - Fork 672
Refactor solution webview to reuse markdown engine #224
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
Changes from 3 commits
69a6686
a5be4fe
5083692
d042c06
86354ce
33ed68a
7323615
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import * as hljs from "highlight.js"; | ||
Vigilans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import * as MarkdownIt from "markdown-it"; | ||
import * as path from "path"; | ||
import * as vscode from "vscode"; | ||
import { leetCodeChannel } from "../leetCodeChannel"; | ||
|
||
export class MarkdownEngine { | ||
|
||
public readonly engine: MarkdownIt; | ||
Vigilans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public readonly extRoot: string; // root path of vscode built-in markdown extension | ||
Vigilans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public constructor() { | ||
this.engine = this.initEngine(); | ||
this.extRoot = path.join(vscode.env.appRoot, "extensions", "markdown-language-features"); | ||
} | ||
|
||
public get localResourceRoots(): vscode.Uri[] { | ||
return [vscode.Uri.file(path.join(this.extRoot, "media"))]; | ||
} | ||
|
||
public get styles(): vscode.Uri[] { | ||
try { | ||
const stylePaths: string[] = require(path.join(this.extRoot, "package.json"))["contributes"]["markdown.previewStyles"]; | ||
return stylePaths.map((p: string) => vscode.Uri.file(path.join(this.extRoot, p)).with({ scheme: "vscode-resource" })); | ||
} catch (error) { | ||
leetCodeChannel.appendLine("[Error] Fail to load built-in markdown style file."); | ||
return []; | ||
} | ||
} | ||
|
||
public get options(): MarkdownIt.Options { | ||
return (this.engine as any).options; | ||
} | ||
|
||
public getStylesHTML(): string { | ||
return this.styles.map((style: vscode.Uri) => `<link rel="stylesheet" type="text/css" href="${style.toString()}">`).join("\n"); | ||
Vigilans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public render(md: string, env?: any): string { | ||
return this.engine.render(md, env); | ||
} | ||
|
||
private initEngine(): MarkdownIt { | ||
const md: MarkdownIt = new MarkdownIt({ | ||
linkify: true, | ||
typographer: true, | ||
highlight: (code: string, lang?: string): string => { | ||
if (lang && ["tsx", "typescriptreact"].indexOf(lang.toLocaleLowerCase()) >= 0) { | ||
Vigilans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lang = "jsx"; | ||
} | ||
if (lang && lang.toLocaleLowerCase() === "python3") { | ||
lang = "python"; | ||
} | ||
if (lang && lang.toLocaleLowerCase() === "c#") { | ||
lang = "cs"; | ||
} | ||
if (lang && lang.toLocaleLowerCase() === "json5") { | ||
lang = "json"; | ||
} | ||
if (lang && hljs.getLanguage(lang)) { | ||
try { | ||
return hljs.highlight(lang, code, true).value; | ||
} catch (error) { /* do not highlight */ } | ||
} | ||
return ""; // use external default escaping | ||
}, | ||
}); | ||
|
||
this.addCodeBlockHighlight(md); | ||
this.addLinkValidator(md); | ||
return md; | ||
} | ||
|
||
private addCodeBlockHighlight(md: MarkdownIt): void { | ||
const origin: MarkdownIt.TokenRender = md.renderer.rules["code_block"]; | ||
Vigilans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// tslint:disable-next-line:typedef | ||
md.renderer.rules["code_block"] = (tokens, idx, options, env, self) => { | ||
// if any token uses lang-specified code fence, then do not highlight code block | ||
if (tokens.some((token: any) => token.type === "fence")) { | ||
return origin(tokens, idx, options, env, self); | ||
} | ||
// otherwise, highlight with undefined lang, which could be handled in outside logic. | ||
const highlighted: string = options.highlight(tokens[idx].content, undefined); | ||
return [ | ||
`<pre><code ${self.renderAttrs(tokens[idx])} >`, | ||
highlighted || md.utils.escapeHtml(tokens[idx].content), | ||
"</code></pre>", | ||
].join("\n"); | ||
Vigilans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} | ||
|
||
private addLinkValidator(md: MarkdownIt): void { | ||
const validateLink: (link: string) => boolean = md.validateLink; | ||
md.validateLink = (link: string): boolean => { | ||
// support file:// protocal link | ||
return validateLink(link) || link.startsWith("file:"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain more why we need the validator here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the routine adopted by built-in markdown exntesion's MarkdownEngine, I think it may enables us to validate local file link, which may be useful in local debugger. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok... Actually still not quite understand... I think it's a worth investigating topic. It's fine to leave it here in this PR... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document from markdown-it:
Link validation function. CommonMark allows too much in links. By default we disable You can change this behaviour: var md = require('markdown-it')();
// enable everything
md.validateLink = function () { return true; } Since we will deal with local files in WebView, chances are that it will be helpful to enable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into the source code, and found that it was made to prevent XSS attack. Here are some examples:
if (res.ok) {
href = state.md.normalizeLink(res.str);
if (state.md.validateLink(href)) {
pos = res.pos;
} else {
href = '';
}
} If
href = state.md.normalizeLink(res.str);
if (!state.md.validateLink(href)) { return false; } So, it is indeed necessary to loose the restriction to allow |
||
}; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.