diff --git a/lib/livebook/session.ex b/lib/livebook/session.ex index 87389a16e..e614bb856 100644 --- a/lib/livebook/session.ex +++ b/lib/livebook/session.ex @@ -1400,8 +1400,9 @@ defmodule Livebook.Session do defp notebook_name_to_file_name(notebook_name) do notebook_name |> String.downcase() - |> String.replace(~r/\s+/, "_") - |> String.replace(~r/[^\w]/, "") + |> String.replace(~r/[^\s\w]/u, "") + |> String.trim() + |> String.replace(~r/\s+/u, "_") |> case do "" -> "untitled_notebook" name -> name diff --git a/test/livebook/session_test.exs b/test/livebook/session_test.exs index a34227f73..6c713d06d 100644 --- a/test/livebook/session_test.exs +++ b/test/livebook/session_test.exs @@ -29,6 +29,14 @@ defmodule Livebook.SessionTest do assert Session.file_name_for_download(session) == "cats_guide_to_life" end + + test "removes non-ascii characters from notebook name", %{session: session} do + Session.set_notebook_name(session.pid, "Notebook 😺") + # Get the updated struct + session = Session.get_by_pid(session.pid) + + assert Session.file_name_for_download(session) == "notebook" + end end describe "set_notebook_attributes/2" do