Created
May 7, 2020 14:29
-
-
Save GrandSchtroumpf/01fd388092e8aa84863d3bd19dc950ba to your computer and use it in GitHub Desktop.
Markdium-VSCode extension inside a nx workspace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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