feat(markdown): add render command (fixes #75612)

This adds a command which renders the provided document, or the active
editor if one is provided. Following the pattern of some of the preview
commands, it returned `undefined` if there's no document provided and
no active text editor. Otherwise, seems to work...

```ts
const html = await vscode.commands.executeCommand<string>('markdown.render');
```

A way to render arbitrary strings in addition to documents may be useful at
some point in the future. However, I didn't implement that here as that'd
require some refactoring of the markdown engine. If we're interested though
I could certainly give that a shot.
This commit is contained in:
Connor Peet
2019-07-10 15:49:06 -07:00
parent 878fc5ddee
commit 7dd109c2df
4 changed files with 34 additions and 1 deletions

View File

@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { Command } from '../commandManager';
import { MarkdownEngine } from '../markdownEngine';
import { SkinnyTextDocument } from '../tableOfContentsProvider';
export class RenderDocument implements Command {
public readonly id = 'markdown.render';
public constructor(
private readonly engine: MarkdownEngine
) { }
public async execute(document?: SkinnyTextDocument): Promise<string | undefined> {
if (!document) {
if (!vscode.window.activeTextEditor) {
return;
}
document = vscode.window.activeTextEditor.document;
}
return this.engine.render(document);
}
}