From 7f39c6be7671621be6eea1c8f4a5a01b61baaf7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Tue, 22 Mar 2022 21:54:59 +0100 Subject: [PATCH] Remove non-ascii characters from autogenerated notebook file names (#1068) --- lib/livebook/session.ex | 5 +++-- test/livebook/session_test.exs | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) 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