From 7dd109c2dfdb8cc8b0748d4858da08403c4cc3f3 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Wed, 10 Jul 2019 15:49:06 -0700 Subject: [PATCH] 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('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. --- .../markdown-language-features/package.json | 3 +- .../src/commands/index.ts | 1 + .../src/commands/renderDocument.ts | 30 +++++++++++++++++++ .../src/extension.ts | 1 + 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 extensions/markdown-language-features/src/commands/renderDocument.ts diff --git a/extensions/markdown-language-features/package.json b/extensions/markdown-language-features/package.json index 8e841a982aa..8a53e3fa331 100644 --- a/extensions/markdown-language-features/package.json +++ b/extensions/markdown-language-features/package.json @@ -24,6 +24,7 @@ "onCommand:markdown.showLockedPreviewToSide", "onCommand:markdown.showSource", "onCommand:markdown.showPreviewSecuritySelector", + "onCommand:markdown.render", "onWebviewPanel:markdown.preview" ], "contributes": { @@ -325,4 +326,4 @@ "webpack": "^4.1.0", "webpack-cli": "^2.0.10" } -} \ No newline at end of file +} diff --git a/extensions/markdown-language-features/src/commands/index.ts b/extensions/markdown-language-features/src/commands/index.ts index e8c9651ee0c..68aff7ffcf5 100644 --- a/extensions/markdown-language-features/src/commands/index.ts +++ b/extensions/markdown-language-features/src/commands/index.ts @@ -10,3 +10,4 @@ export { RefreshPreviewCommand } from './refreshPreview'; export { ShowPreviewSecuritySelectorCommand } from './showPreviewSecuritySelector'; export { MoveCursorToPositionCommand } from './moveCursorToPosition'; export { ToggleLockCommand } from './toggleLock'; +export { RenderDocument } from './renderDocument'; diff --git a/extensions/markdown-language-features/src/commands/renderDocument.ts b/extensions/markdown-language-features/src/commands/renderDocument.ts new file mode 100644 index 00000000000..f041c1c4adf --- /dev/null +++ b/extensions/markdown-language-features/src/commands/renderDocument.ts @@ -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 { + if (!document) { + if (!vscode.window.activeTextEditor) { + return; + } + + document = vscode.window.activeTextEditor.document; + } + + return this.engine.render(document); + } +} diff --git a/extensions/markdown-language-features/src/extension.ts b/extensions/markdown-language-features/src/extension.ts index 3fe2c9d4cd3..44e8873b16d 100644 --- a/extensions/markdown-language-features/src/extension.ts +++ b/extensions/markdown-language-features/src/extension.ts @@ -83,6 +83,7 @@ function registerMarkdownCommands( commandManager.register(new commands.ShowPreviewSecuritySelectorCommand(previewSecuritySelector, previewManager)); commandManager.register(new commands.OpenDocumentLinkCommand(engine)); commandManager.register(new commands.ToggleLockCommand(previewManager)); + commandManager.register(new commands.RenderDocument(engine)); return commandManager; }