microsoft / vscode-livepreview

Hosts a local server in your workspace for you to preview your webpages.
http://aka.ms/live-preview
MIT License
439 stars 64 forks source link

Open from a script #619

Closed memeplex closed 9 months ago

memeplex commented 9 months ago

I've a watchdog that updates an html file produced by pandoc from a markdown source every time I save the latter. I would like to programatically open the html file when I launch the watchdog script, but I don't know if that's possible. code <html-file> will open the html source, not the preview. Even launching a persistent server and then calling code <html-file-url> won't work. So I've either to open the produced html file manually or open the html source automatically and then open the preview for it. Neither option is too appealing.

andreamah commented 9 months ago

You can use the command palette to run > Live Preview: Show Preview. If you want it to update when the script updates it, you can set "livePreview.autoRefreshPreview": "On Changes to Saved Files" in your settings. Does this help?

memeplex commented 9 months ago

Not really. What you describe is exactly what I'm doing now. I'm not saying it's bad, just that it requires to load the html first, which is a byproduct of a compilation, I'm not actually working on the html itself. I would like that this watchdog could invoke code <url> or code <html> after the compilation and then the preview automatically opens. Perhaps code <html> would conflict with the intention of opening the source html instead, which one may argue is the natural behavior for a text editor, but I guess there is some way to define a url that's handled by a particular extension, isn't there?

andreamah commented 9 months ago

Oh, are you looking for something that can be invoked via a command of some sort to preview? We had an issue here https://github.com/microsoft/vscode-livepreview/issues/368 about having a 'call' for starting the server, but that didn't include previewing. There's also a way of configuring a task to preview, which you could attach to a keybinding: https://github.com/microsoft/vscode-livepreview/issues/388. I think that might be the closest to what you want you could use.

memeplex commented 9 months ago

That may be useful indeed. I'm already running the watchdog from a background task so I guess I could add to it a dependency on a new task that opens the preview. I'm going to give it a try. I'm closing this for now and in any case I'll reopen it if I run into further difficulties. Thanks!

memeplex commented 9 months ago

Just one more question related to this, if you don't mind:

"On Changes to Saved Files"

I was using this setting but I realized that there were lots of extra reloadings, so I ended up turning autoreload off and opening a websocket client in the page then notifying it from the watchdog after each successful compilation.

Just to understand, the previewer has no notion of which file is being previewed, right? So every open file in the workspace or similar is monitored for save events?

This is unfortunate in my case because the sequence is more or less like this:

  1. I save the markdown file I'm editing
  2. The watchdog triggers a compilation and at almost the same time the page is reloaded
  3. The compilation generates an updated html

So the previewer may have updated the page too early, and since the html is not open nor explicitly saved from Code, but just changed at the filesystem level, I'm afraid its changes may not be displayed.

andreamah commented 9 months ago

Yes, unfortunately, that is what happens. the refresh will trigger on any workspace change because it's more complicated to follow what would change based on which file changes. You can try modifying livePreview.previewDebounceDelay to a larger number, then perhaps it will just refresh once and on time.

memeplex commented 9 months ago

I see, fortunately I was able to work around that.

I'm trying to follow your advice regarding https://github.com/microsoft/vscode-livepreview/issues/388#issue-1487554643, but bumping into:

2024-03-08 15:56:54.510 [error] [Extension Host] Tried to open a non-file URI with file opener
2024-03-08 15:56:54.514 [error] The "path" argument must be of type string. Received undefined: Error: The "path" argument must be of type string. Received undefined

I tried using a url-like expression like "args": "file:///Users/carlos/Desktop/test.html" but to no avail.

memeplex commented 9 months ago

Oh, this fixed the issue: https://github.com/microsoft/vscode-livepreview/issues/388#issuecomment-1400793551

Anyway I would like to understand the problem above. Does atFile expect a URL object that can't be conveyed by using a URL string representation from tasks.json?

andreamah commented 9 months ago

I think that a full absolute path like C:/Users/andreamah/OneDrive - Microsoft/Documents/vscode-livepreview/test-workspace/page0.html worked for me. So if you look at the comment you linked and replace the "{path_to_preview}" to a string like what I have above.

andreamah commented 9 months ago

I believe that the URI parser had difficulty parsing that string for some reason- aka calling vscode.Uri.file() on it errored out.

memeplex commented 9 months ago

Mmmm, thanks again. Anyway I think the effort is doomed to fail because variable expansion doesn't seem to work for inputs:

  "inputs": [
    {
      "id": "preview-html",
      "type": "command",
      "command": "livePreview.start.preview.atFileString",
      "args": "${relativeFileDirname}/${fileBasenameNoExtension}.html"
    }
  ]
image

I guess I'm going to manually open the preview :(

andreamah commented 9 months ago

@meganrogge curious: is variable expansion supposed to work here, or is this handled by the individual extension?

  "inputs": [
    {
      "id": "preview-html",
      "type": "command",
      "command": "livePreview.start.preview.atFileString",
      "args": "${relativeFileDirname}/${fileBasenameNoExtension}.html"
    }
  ]
image
meganrogge commented 8 months ago

@andreamah it looks like we don't resolve arguments, so at the moment, it's up to the extension to do so

https://github.com/microsoft/vscode/blob/dcafd8876c9365ca3fc6fe0f449aaa9a16570c15/src/vs/workbench/services/configurationResolver/browser/baseConfigurationResolverService.ts#L364