mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-02-21 05:19:42 +08:00
add function to open notebook urls (#986)
* add function to open notebook urls * Update lib/livebook/utils.ex Co-authored-by: José Valim <jose.valim@gmail.com> * Update lib/livebook/utils.ex Co-authored-by: José Valim <jose.valim@gmail.com> * open notebook with notebook_open_url/2 * Update server.ex * format Livebook.Utils * Update lib/livebook_cli/server.ex Co-authored-by: José Valim <jose.valim@gmail.com> * correct function doc Co-authored-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
5e82147db0
commit
494ab40ac8
2 changed files with 19 additions and 2 deletions
|
@ -387,6 +387,24 @@ defmodule Livebook.Utils do
|
|||
|> URI.to_string()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a URL (including localhost) to open the given `url` as a notebook
|
||||
|
||||
iex> Livebook.Utils.notebook_open_url("https://example.com/foo.livemd")
|
||||
"http://localhost:4002/open?path=https%3A%2F%2Fexample.com%2Ffoo.livemd"
|
||||
|
||||
iex> Livebook.Utils.notebook_open_url("https://my_host", "https://example.com/foo.livemd")
|
||||
"https://my_host/open?path=https%3A%2F%2Fexample.com%2Ffoo.livemd"
|
||||
|
||||
"""
|
||||
def notebook_open_url(base_url \\ LivebookWeb.Endpoint.access_struct_url(), url) do
|
||||
base_url
|
||||
|> URI.parse()
|
||||
|> Map.replace!(:path, "/open")
|
||||
|> append_query("path=#{URI.encode_www_form(url)}")
|
||||
|> URI.to_string()
|
||||
end
|
||||
|
||||
# TODO: On Elixir v1.14, use URI.append_query/2
|
||||
def append_query(%URI{query: query} = uri, query_to_add) when query in [nil, ""] do
|
||||
%{uri | query: query_to_add}
|
||||
|
|
|
@ -168,8 +168,7 @@ defmodule LivebookCLI.Server do
|
|||
|
||||
File.regular?(path) ->
|
||||
base_url
|
||||
|> append_path("open")
|
||||
|> update_query(%{"path" => path})
|
||||
|> Livebook.Utils.notebook_open_url(url_or_file_or_dir)
|
||||
|> Livebook.Utils.browser_open()
|
||||
|
||||
File.dir?(path) ->
|
||||
|
|
Loading…
Reference in a new issue