Skip to content

Instantly share code, notes, and snippets.

@GrandSchtroumpf
Created May 7, 2020 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GrandSchtroumpf/01fd388092e8aa84863d3bd19dc950ba to your computer and use it in GitHub Desktop.
Save GrandSchtroumpf/01fd388092e8aa84863d3bd19dc950ba to your computer and use it in GitHub Desktop.
Markdium-VSCode extension inside a nx workspace
import { commands, ExtensionContext, window, ViewColumn, Uri } from 'vscode';
import { promises as fs } from 'fs';
import { join } from 'path';
// On activation
export function activate(context: ExtensionContext) {
// Register command "start"
commands.registerCommand('start', async () => {
const panel = window.createWebviewPanel(
'studio',
'Studio',
ViewColumn.Active,
{
enableScripts: true,
localResourceRoots: [Uri.file(join(context.extensionPath, 'studio'))]
});
const html = await fs.readFile(join(context.extensionPath, 'studio/index.html'), 'utf-8');
const matchLinks = /(href|src)="([^"]*)"/g;
const toUri = (_, prefix: 'href' | 'src', link: string) => {
// For
if (link === '#') {
return `${prefix}="${link}"`;
}
// For scripts & links
const path = join(context.extensionPath, 'studio', link);
const uri = Uri.file(path);
return `${prefix}="${panel.webview['asWebviewUri'](uri)}"`;
};
panel.webview.html = html.replace(matchLinks, toUri);
context.subscriptions.push(panel);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment