Fix generating html id with special characters (#2495)

This commit is contained in:
Alexandre de Souza 2024-02-23 15:55:17 -03:00 committed by GitHub
parent 4105266ad2
commit bd3232dedb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 3 deletions

View file

@ -34,9 +34,14 @@ defmodule LivebookWeb.HTMLHelpers do
defp name_to_html_id(name) do
name
|> String.trim()
|> String.downcase()
|> String.replace(~r/[^\s\w]/u, "")
|> String.trim()
|> String.replace(~r/\s+/u, "-")
|> case do
"" -> Base.url_encode64(name, padding: false)
id -> id
end
end
@doc """

View file

@ -18,11 +18,11 @@ defmodule LivebookWeb.HTMLHelpersTest do
end
test "emoji at end" do
assert HTMLHelpers.names_to_html_ids(["Test 🦦 "]) == ["test-🦦"]
assert HTMLHelpers.names_to_html_ids(["Test 🦦 "]) == ["test"]
end
test "emoji in middle" do
assert HTMLHelpers.names_to_html_ids(["One 🥮 Two"]) == ["one-🥮-two"]
assert HTMLHelpers.names_to_html_ids(["One 🥮 Two"]) == ["one-two"]
end
test "returns empty list for an empty list" do