Skip to content

Commit

Permalink
Use Phoenix.HTML.Tag.tag/2 to generate hreflang links
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jul 24, 2022
1 parent 7fd41ba commit 6a44463
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
20 changes: 11 additions & 9 deletions lib/cldr/routes/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,14 @@ defmodule Cldr.Route.LocalizedHelpers do
...> "fr" => "https://localhost/utilisateurs/1"
...> }
iex> Cldr.Route.LocalizedHelpers.hreflang_links(links)
{:safe, [
["<link href=", "\\"https://localhost/users/1\\"",
"; rel=alternate; hreflang=", "\\"en\\"", " />"],
"\\n",
["<link href=", "\\"https://localhost/utilisateurs/1\\"",
"; rel=alternate; hreflang=", "\\"fr\\"", " />"]
]}
{
:safe,
[
[60, "link", [32, "href", 61, 34, "https://localhost/users/1", 34, 32, "hreflang", 61, 34, "en", 34, 32, "rel", 61, 34, "alternate", 34], 62],
10,
[60, "link", [32, "href", 61, 34, "https://localhost/utilisateurs/1", 34, 32, "hreflang", 61, 34, "fr", 34, 32, "rel", 61, 34, "alternate", 34], 62]
]
}
iex> Cldr.Route.LocalizedHelpers.hreflang_links(nil)
{:safe, []}
Expand All @@ -524,9 +525,10 @@ defmodule Cldr.Route.LocalizedHelpers do
def hreflang_links(url_map) when is_map(url_map) do
links =
for {locale, url} <- url_map do
["<link href=", inspect(url), "; rel=alternate; hreflang=", inspect(locale), " />"]
{:safe, link} = Phoenix.HTML.Tag.tag(:link, href: url, rel: "alternate", hreflang: locale)
link
end
|> Enum.intersperse("\n")
|> Enum.intersperse(?\n)

{:safe, links}
end
Expand Down
20 changes: 10 additions & 10 deletions test/cldr_routes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,20 @@ defmodule Cldr.Route.Test do
}

assert header_io_data == {
:safe,
[
["<link href=", "\"http://localhost/users_de/1\"", "; rel=alternate; hreflang=", "\"de\"", " />"],
"\n",
["<link href=", "\"http://localhost/users/1\"", "; rel=alternate; hreflang=", "\"en\"", " />"],
"\n",
["<link href=", "\"http://localhost/users_fr/1\"", "; rel=alternate; hreflang=", "\"fr\"", " />"]
:safe,
[
[60, "link", [32, "href", 61, 34, "http://localhost/users_de/1", 34, 32, "hreflang", 61, 34, "de", 34, 32, "rel", 61, 34, "alternate", 34], 62],
10,
[60, "link", [32, "href", 61, 34, "http://localhost/users/1", 34, 32, "hreflang", 61, 34, "en", 34, 32, "rel", 61, 34, "alternate", 34], 62],
10,
[60, "link", [32, "href", 61, 34, "http://localhost/users_fr/1", 34, 32, "hreflang", 61, 34, "fr", 34, 32, "rel", 61, 34, "alternate", 34], 62]
]
}

assert header ==
"<link href=\"http://localhost/users_de/1\"; rel=alternate; hreflang=\"de\" />\n" <>
"<link href=\"http://localhost/users/1\"; rel=alternate; hreflang=\"en\" />\n" <>
"<link href=\"http://localhost/users_fr/1\"; rel=alternate; hreflang=\"fr\" />"
"<link href=\"http://localhost/users_de/1\" hreflang=\"de\" rel=\"alternate\">\n" <>
"<link href=\"http://localhost/users/1\" hreflang=\"en\" rel=\"alternate\">\n" <>
"<link href=\"http://localhost/users_fr/1\" hreflang=\"fr\" rel=\"alternate\">"
end

test "hreflang test helper for non-localized route" do
Expand Down

0 comments on commit 6a44463

Please sign in to comment.