Skip to content

Commit

Permalink
Fix hreflang link helpers to be links, not headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jul 24, 2022
1 parent cab3e6c commit 00d545e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ This is the changelog for Cldr Routes version 0.6.0 released on July 24th, 2022.
```elixir
iex> alias MyApp.Router.LocalizedHelpers
iex> LocalizedHelpers.user_links(conn, :show, 1)
...> |> LocalizedHelpers.hreflang_link_headers()
...> |> LocalizedHelpers.hreflang_links()
{
:safe,
[
["<Link: ", "http://localhost/users_de/1", "; rel=alternate; hreflang=", "\"de\"", " />"],
["<link href=", "\"http://localhost/users_de/1\"", "; rel=alternate; hreflang=", "\"de\"", " />"],
"\n",
["<Link: ", "http://localhost/users/1", "; rel=alternate; hreflang=", "\"en\"", " />"],
["<link href=", "\"http://localhost/users/1\"", "; rel=alternate; hreflang=", "\"en\"", " />"],
"\n",
["<Link: ", "http://localhost/users_fr/1", "; rel=alternate; hreflang=", "\"fr\"", " />"]
["<link href=", "\"http://localhost/users_fr/1\"", "; rel=alternate; hreflang=", "\"fr\"", " />"]
]
}
```
Expand Down
28 changes: 14 additions & 14 deletions lib/cldr/routes/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ defmodule Cldr.Route.LocalizedHelpers do
@doc """
Generates an HTTP `Link` header for a given map of locale => URLs
This function generates `Link` headers that should be placed in the
`HEAD` section of an HTML document to indicate the different language
This function generates `<link .../>` tags that should be placed in the
`<head>` section of an HTML document to indicate the different language
versions of a given page.
The `MyApp.Router.LocalizedHelpers.<helper>_link` functions can
Expand All @@ -347,14 +347,14 @@ defmodule Cldr.Route.LocalizedHelpers do
### Example
===> MyApp.Helpers.LocalizedHelpers.user_links(conn, :show, 1)
...> |> MyApp.Helpers.LocalizedHelpers.hreflang_link_headers()
...> |> MyApp.Helpers.LocalizedHelpers.hreflang_links()
"""
@spec hreflang_link_headers(%{LocalizedHelpers.locale_name() => LocalizedHelpers.url()}) ::
@spec hreflang_links(%{LocalizedHelpers.locale_name() => LocalizedHelpers.url()}) ::
Phoenix.HTML.safe()

def hreflang_link_headers(url_map) do
Cldr.Route.LocalizedHelpers.hreflang_link_headers(url_map)
def hreflang_links(url_map) do
Cldr.Route.LocalizedHelpers.hreflang_links(url_map)
end
end
end
Expand Down Expand Up @@ -479,8 +479,8 @@ defmodule Cldr.Route.LocalizedHelpers do
@doc """
Generates an HTTP `Link` header for a given map of locale => URLs
This function generates `Link` headers that should be placed in the
`HEAD` section of an HTML document to indicate the different language
This function generates `<link ... />` headers that should be placed in the
`<head>` section of an HTML document to indicate the different language
versions of a given page.
The `MyApp.Router.LocalizedHelpers.<helper>_link` functions can
Expand All @@ -497,18 +497,18 @@ defmodule Cldr.Route.LocalizedHelpers do
### Example
===> MyApp.Helpers.LocalizedHelpers.user_links(conn, :show, 1)
...> |> Cldr.Route.LocalizedHelpers.hreflang_link_headers()
...> |> Cldr.Route.LocalizedHelpers.hreflang_links()
"""
@spec hreflang_link_headers(%{locale_name() => url()}) :: Phoenix.HTML.safe()
def hreflang_link_headers(url_map) do
link_headers =
@spec hreflang_links(%{locale_name() => url()}) :: Phoenix.HTML.safe()
def hreflang_links(url_map) do
links =
for {locale, url} <- url_map do
["<Link: ", url, "; rel=alternate; hreflang=", inspect(locale), " />"]
["<link href=", inspect(url), "; rel=alternate; hreflang=", inspect(locale), " />"]
end
|> Enum.intersperse("\n")

{:safe, link_headers}
{:safe, links}
end

@doc false
Expand Down
16 changes: 8 additions & 8 deletions test/cldr_routes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ defmodule Cldr.Route.Test do
conn = get(build_conn(), "/users/1")

links = MyApp.Router.LocalizedHelpers.user_links(conn, :show, 1)
header_io_data = MyApp.Router.LocalizedHelpers.hreflang_link_headers(links)
header_io_data = MyApp.Router.LocalizedHelpers.hreflang_links(links)
header = Phoenix.HTML.safe_to_string(header_io_data)

assert links == %{
Expand All @@ -206,25 +206,25 @@ defmodule Cldr.Route.Test do
assert header_io_data == {
:safe,
[
["<Link: ", "http://localhost/users_de/1", "; rel=alternate; hreflang=", "\"de\"", " />"],
["<link href=", "\"http://localhost/users_de/1\"", "; rel=alternate; hreflang=", "\"de\"", " />"],
"\n",
["<Link: ", "http://localhost/users/1", "; rel=alternate; hreflang=", "\"en\"", " />"],
["<link href=", "\"http://localhost/users/1\"", "; rel=alternate; hreflang=", "\"en\"", " />"],
"\n",
["<Link: ", "http://localhost/users_fr/1", "; rel=alternate; hreflang=", "\"fr\"", " />"]
["<link href=", "\"http://localhost/users_fr/1\"", "; rel=alternate; hreflang=", "\"fr\"", " />"]
]
}

assert header ==
"<Link: http://localhost/users_de/1; rel=alternate; hreflang=\"de\" />\n" <>
"<Link: http://localhost/users/1; rel=alternate; hreflang=\"en\" />\n" <>
"<Link: http://localhost/users_fr/1; rel=alternate; hreflang=\"fr\" />"
"<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\" />"
end

test "hreflang test helper for non-localized route" do
conn = get(build_conn(), "/not_localized/1")

links = MyApp.Router.LocalizedHelpers.not_localized_links(conn, :show, 1)
header_io_data = MyApp.Router.LocalizedHelpers.hreflang_link_headers(links)
header_io_data = MyApp.Router.LocalizedHelpers.hreflang_links(links)
header = Phoenix.HTML.safe_to_string(header_io_data)

assert links == %{}
Expand Down

0 comments on commit 00d545e

Please sign in to comment.