import { TextEditor , Uri , ViewColumn } from "vscode" ; import { DocumentContentManagerInterface } from "./documentContentManagerInterface" ; import { HtmlPreview } from "../util/htmlPreview" ; let Markdown2HtmlLess = require ( "markdown2html-less" ) . Markdown2HtmlLess ; const markdown2htmlLess = new Markdown2HtmlLess ( ) ; export class MarkdownDocumentContentManager implements DocumentContentManagerInterface { private _editor : TextEditor ; public constructor ( editor ) { this . _editor = editor ; return this ; } public editor ( ) { return this . _editor ; } public async createContentSnippet ( ) { let editor = this . _editor ; if ( ! editor || ! editor . document ) { return HtmlPreview . errorSnippet ( this . getWindowErrorMessage ( ) ) ; } if ( this . _editor . document . languageId !== "markdown" ) { return HtmlPreview . errorSnippet ( this . getErrorMessage ( ) ) ; } let previewSnippet = await this . generatePreviewSnippet ( editor ) ; if ( ! previewSnippet || previewSnippet . length <= 0 ) { return HtmlPreview . errorSnippet ( this . getErrorMessage ( ) ) ; } console . info ( "previewSnippet = " + previewSnippet ) ; return previewSnippet ; } public sendPreviewCommand ( previewUri , displayColumn ) { return HtmlPreview . sendPreviewCommand ( previewUri , displayColumn ) ; } private getErrorMessage ( ) { return `Active editor doesn't show a MarkDown document - no properties to preview.` ; } private getWindowErrorMessage ( ) { return `No Active editor - no properties to preview.` ; } private generatePreviewSnippet ( editor ) { return ( async ( ) : Promise < string > => { if ( ! editor || ! editor . document ) { return HtmlPreview . errorSnippet ( this . getWindowErrorMessage ( ) ) ; } let doc = editor . document ; let html = await this . getHTML ( doc . getText ( ) ) ; return HtmlPreview . fixNoneNetLinks ( html , doc . fileName ) ; } ) ( ) ; } private getHTML ( md ) { const html = markdown2htmlLess . markdown2html ( md ) ; html . head = html . head || '' ; html . body = html . body || '' ; return Promise . resolve ( HtmlPreview . createFullHtmlSnippetFrom ( `${ html . head }` , `${ html . body }` ) ) ; } }