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:

@injectable()
export class MarkDownPrevOpener implements CommandContribution {
    constructor(
        @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 + "/README.md");
                open(this.openerService, uri, { preview: true });
            }
        });
    }
}

try with https://github.com/eclipse-theia/theia/blob/0c010984dc73cd642fc71c9ea8be13a7ea5800ec/packages/preview/src/browser/preview-uri.ts#L25

PreviewUri.encode(uri) before passing to open

you can remove then the 3rd arg

[Soumesh Banerjee]

Works perfectly :+1: