Open a Markdown file in Markdown preview

Is there a way to programmatically open markdown file automatically in markdown preview

[original thread by Soumesh Banerjee]

open(openerService, markdownFileUri, { preview: true }) - try like that

[Soumesh Banerjee]

@anton-kosyakov This opens the Markdown file but not in preview mode

Sample Code:

export class MarkDownPrevOpener implements CommandContribution {
        @inject(WorkspaceService) private readonly workspaceService: WorkspaceService,
        @inject(OpenerService) private readonly openerService: OpenerService
    ) { }
    registerCommands(registry: CommandRegistry): void {
        registry.registerCommand(PrevCommand, {
            execute: async () => {
                const uriStr = this.workspaceService.workspace?.uri;
                const uri = new URI(uriStr + "/");
                open(this.openerService, uri, { preview: true });

try with

PreviewUri.encode(uri) before passing to open

you can remove then the 3rd arg

[Soumesh Banerjee]

Works perfectly :+1: