|
| 1 | +import * as hljs from "highlight.js"; |
| 2 | +import * as MarkdownIt from "markdown-it"; |
| 3 | +import * as os from "os"; |
| 4 | +import * as path from "path"; |
| 5 | +import * as vscode from "vscode"; |
| 6 | +import { leetCodeChannel } from "../leetCodeChannel"; |
| 7 | + |
| 8 | +export class MarkdownEngine { |
| 9 | + |
| 10 | + private readonly engine: MarkdownIt; |
| 11 | + private readonly extRoot: string; // root path of vscode built-in markdown extension |
| 12 | + |
| 13 | + public constructor() { |
| 14 | + this.engine = this.initEngine(); |
| 15 | + this.extRoot = path.join(vscode.env.appRoot, "extensions", "markdown-language-features"); |
| 16 | + } |
| 17 | + |
| 18 | + public get localResourceRoots(): vscode.Uri[] { |
| 19 | + return [vscode.Uri.file(path.join(this.extRoot, "media"))]; |
| 20 | + } |
| 21 | + |
| 22 | + public get styles(): vscode.Uri[] { |
| 23 | + try { |
| 24 | + const stylePaths: string[] = require(path.join(this.extRoot, "package.json"))["contributes"]["markdown.previewStyles"]; |
| 25 | + return stylePaths.map((p: string) => vscode.Uri.file(path.join(this.extRoot, p)).with({ scheme: "vscode-resource" })); |
| 26 | + } catch (error) { |
| 27 | + leetCodeChannel.appendLine("[Error] Fail to load built-in markdown style file."); |
| 28 | + return []; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public getStylesHTML(): string { |
| 33 | + return this.styles.map((style: vscode.Uri) => `<link rel="stylesheet" type="text/css" href="${style.toString()}">`).join(os.EOL); |
| 34 | + } |
| 35 | + |
| 36 | + public render(md: string, env?: any): string { |
| 37 | + return this.engine.render(md, env); |
| 38 | + } |
| 39 | + |
| 40 | + private initEngine(): MarkdownIt { |
| 41 | + const md: MarkdownIt = new MarkdownIt({ |
| 42 | + linkify: true, |
| 43 | + typographer: true, |
| 44 | + highlight: (code: string, lang?: string): string => { |
| 45 | + switch (lang && lang.toLowerCase()) { |
| 46 | + case "mysql": |
| 47 | + lang = "sql"; break; |
| 48 | + case "json5": |
| 49 | + lang = "json"; break; |
| 50 | + case "python3": |
| 51 | + lang = "python"; break; |
| 52 | + } |
| 53 | + if (lang && hljs.getLanguage(lang)) { |
| 54 | + try { |
| 55 | + return hljs.highlight(lang, code, true).value; |
| 56 | + } catch (error) { /* do not highlight */ } |
| 57 | + } |
| 58 | + return ""; // use external default escaping |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + this.addCodeBlockHighlight(md); |
| 63 | + this.addImageUrlCompletion(md); |
| 64 | + this.addLinkValidator(md); |
| 65 | + return md; |
| 66 | + } |
| 67 | + |
| 68 | + private addCodeBlockHighlight(md: MarkdownIt): void { |
| 69 | + const codeBlock: MarkdownIt.TokenRender = md.renderer.rules["code_block"]; |
| 70 | + // tslint:disable-next-line:typedef |
| 71 | + md.renderer.rules["code_block"] = (tokens, idx, options, env, self) => { |
| 72 | + // if any token uses lang-specified code fence, then do not highlight code block |
| 73 | + if (tokens.some((token: any) => token.type === "fence")) { |
| 74 | + return codeBlock(tokens, idx, options, env, self); |
| 75 | + } |
| 76 | + // otherwise, highlight with default lang in env object. |
| 77 | + const highlighted: string = options.highlight(tokens[idx].content, env.lang); |
| 78 | + return [ |
| 79 | + `<pre><code ${self.renderAttrs(tokens[idx])} >`, |
| 80 | + highlighted || md.utils.escapeHtml(tokens[idx].content), |
| 81 | + "</code></pre>", |
| 82 | + ].join(os.EOL); |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + private addImageUrlCompletion(md: MarkdownIt): void { |
| 87 | + const image: MarkdownIt.TokenRender = md.renderer.rules["image"]; |
| 88 | + // tslint:disable-next-line:typedef |
| 89 | + md.renderer.rules["image"] = (tokens, idx, options, env, self) => { |
| 90 | + const imageSrc: string[] | undefined = tokens[idx].attrs.find((value: string[]) => value[0] === "src"); |
| 91 | + if (env.host && imageSrc && imageSrc[1].startsWith("/")) { |
| 92 | + imageSrc[1] = `${env.host}${imageSrc[1]}`; |
| 93 | + } |
| 94 | + return image(tokens, idx, options, env, self); |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + private addLinkValidator(md: MarkdownIt): void { |
| 99 | + const validateLink: (link: string) => boolean = md.validateLink; |
| 100 | + md.validateLink = (link: string): boolean => { |
| 101 | + // support file:// protocal link |
| 102 | + return validateLink(link) || link.startsWith("file:"); |
| 103 | + }; |
| 104 | + } |
| 105 | +} |
0 commit comments