defmodule ChesstrainerWeb.SitemapController do
@moduledoc """
Generates `/sitemap.xml` for the indexable public pages.
Endgame play pages are deliberately left out: there are tens of thousands of
them and they render the same interactive surface with only the position
differing, so listing them all would be index bloat. Revisit that if public
browse pages (by Key or theme) exist and are worth ranking.
"""
use ChesstrainerWeb, :controller
def index(conn, _params) do
# The landing page changes every day (daily puzzle, counts), so its
# lastmod is honest. The legal pages have no stored modification time, and
# an invented lastmod is worse than none.
entries = [
{url(conn, ~p"/"), Date.utc_today()},
{url(conn, ~p"/privacy"), nil},
{url(conn, ~p"/terms-conditions"), nil},
{url(conn, ~p"/mentions-legales"), nil}
]
conn
|> put_resp_content_type("application/xml")
|> send_resp(200, render_sitemap(entries))
end
defp render_sitemap(entries) do
urls =
Enum.map_join(entries, fn {loc, lastmod} ->
" #{escape(loc)}#{lastmod_entry(lastmod)}\n"
end)
"""
#{urls}
"""
end
defp lastmod_entry(nil), do: ""
defp lastmod_entry(%Date{} = date), do: "#{Date.to_iso8601(date)}"
# Templates escape automatically; this response is built as a plain string,
# so XML-significant characters must be escaped here.
defp escape(value) do
value
|> String.replace("&", "&")
|> String.replace("<", "<")
|> String.replace(">", ">")
|> String.replace("\"", """)
|> String.replace("'", "'")
end
end