fix: issue #11 wrong page titles compared to old app
This commit is contained in:
@@ -59,6 +59,7 @@ defmodule BDS.Generation do
|
||||
%{
|
||||
project_id: project_id,
|
||||
project_name: project.name,
|
||||
project_description: metadata.description,
|
||||
base_url: normalize_base_url(metadata.public_url),
|
||||
language: metadata.main_language,
|
||||
blog_languages: normalize_blog_languages(metadata.main_language, metadata.blog_languages),
|
||||
|
||||
@@ -6,6 +6,7 @@ defmodule BDS.Generation.Outputs do
|
||||
import BDS.Generation.Sitemap, only: [render_feed: 3, render_atom: 3, render_calendar: 1]
|
||||
|
||||
alias BDS.Rendering.RenderContext
|
||||
alias BDS.Rendering.Metadata, as: RenderMetadata
|
||||
alias BDS.Rendering.TemplateSelection
|
||||
|
||||
@type output_callback :: ({String.t(), iodata()} -> any()) | nil
|
||||
@@ -214,6 +215,7 @@ defmodule BDS.Generation.Outputs do
|
||||
concurrent_flat_map(posts_by_category, fn {category, posts} ->
|
||||
paginated_posts = Enum.chunk_every(posts, max(plan.max_posts_per_page, 1))
|
||||
category_slug = archive_route_segment(category)
|
||||
category_title = RenderMetadata.category_display_name(plan.category_settings, category)
|
||||
total_pages = length(paginated_posts)
|
||||
total_items = length(posts)
|
||||
|
||||
@@ -256,7 +258,7 @@ defmodule BDS.Generation.Outputs do
|
||||
["category", category_slug],
|
||||
page_number
|
||||
),
|
||||
render_archive_page(plan, category, page_posts, language, "category", pagination)
|
||||
render_archive_page(plan, category_title, page_posts, language, "category", pagination)
|
||||
}
|
||||
|
||||
report_output(output, on_output)
|
||||
@@ -508,7 +510,7 @@ defmodule BDS.Generation.Outputs do
|
||||
render_list_output(
|
||||
plan,
|
||||
language,
|
||||
plan.project_name,
|
||||
blog_page_title(plan),
|
||||
page_posts,
|
||||
%{kind: "core"},
|
||||
pagination_for_page(
|
||||
|
||||
@@ -4,20 +4,23 @@ defmodule BDS.Generation.Renderers do
|
||||
alias BDS.Generation.Paths
|
||||
alias BDS.Projects
|
||||
alias BDS.Rendering
|
||||
alias BDS.Rendering.Metadata, as: RenderMetadata
|
||||
alias BDS.Rendering.RenderContext
|
||||
|
||||
@doc "Render the home page (HTML) using the project's template engine."
|
||||
@spec render_home(map(), String.t() | nil) :: String.t()
|
||||
def render_home(plan, language) do
|
||||
title = blog_page_title(plan)
|
||||
|
||||
[
|
||||
"<html>",
|
||||
"<head><title>",
|
||||
plan.project_name,
|
||||
title,
|
||||
"</title></head>",
|
||||
"<body data-language=\"",
|
||||
to_string(language),
|
||||
"\"><main><h1>",
|
||||
plan.project_name,
|
||||
title,
|
||||
"</h1></main></body>",
|
||||
"</html>"
|
||||
]
|
||||
@@ -67,14 +70,15 @@ defmodule BDS.Generation.Renderers do
|
||||
end
|
||||
|
||||
# Shared by every archive kind so category/tag pages get the same post
|
||||
# payloads (loaded bodies, real hrefs) as date archives.
|
||||
# payloads (loaded bodies, real hrefs) as date archives. The document title
|
||||
# stays the blog title; the archive-specific label lives in `archive_context`.
|
||||
defp render_archive_list_page(plan, title, archive_context, posts, language, kind, pagination) do
|
||||
fallback = fn -> render_inline_list_page(title, posts, language, kind) end
|
||||
|
||||
render_list_output(
|
||||
plan,
|
||||
language,
|
||||
title,
|
||||
blog_page_title(plan),
|
||||
build_list_posts(plan, posts, Paths.route_language(plan.language, language)),
|
||||
archive_context,
|
||||
pagination,
|
||||
@@ -128,6 +132,14 @@ defmodule BDS.Generation.Renderers do
|
||||
)
|
||||
end
|
||||
|
||||
@doc "Blog document title (description, then name) shared by list/home pages."
|
||||
@spec blog_page_title(map()) :: String.t()
|
||||
def blog_page_title(plan) do
|
||||
RenderMetadata.blog_page_title(
|
||||
Map.get(plan, :project_description),
|
||||
Map.get(plan, :project_name)
|
||||
)
|
||||
end
|
||||
@doc "Render the project's 404 page via its template, falling back to a static page."
|
||||
@spec render_not_found_output(map(), String.t() | nil) :: String.t()
|
||||
def render_not_found_output(
|
||||
|
||||
@@ -10,6 +10,7 @@ defmodule BDS.Preview.Router do
|
||||
alias BDS.Posts.Post
|
||||
alias BDS.Posts.Translation
|
||||
alias BDS.Rendering
|
||||
alias BDS.Rendering.Metadata, as: RenderMetadata
|
||||
alias BDS.Rendering.TemplateSelection
|
||||
alias BDS.Repo
|
||||
|
||||
@@ -159,7 +160,8 @@ defmodule BDS.Preview.Router do
|
||||
|
||||
render_list(project_id, posts, page_number, metadata, language, main_language, %{
|
||||
kind: "category",
|
||||
name: name
|
||||
name: RenderMetadata.category_display_name(metadata.category_settings, name),
|
||||
slug: name
|
||||
})
|
||||
end
|
||||
|
||||
@@ -294,7 +296,7 @@ defmodule BDS.Preview.Router do
|
||||
assigns = %{
|
||||
language: language,
|
||||
language_prefix: language_prefix,
|
||||
page_title: archive_page_title(archive_ctx),
|
||||
page_title: RenderMetadata.blog_page_title(metadata.description, metadata.name),
|
||||
posts: page_posts,
|
||||
archive_context: archive_ctx,
|
||||
pagination: pagination
|
||||
@@ -334,6 +336,10 @@ defmodule BDS.Preview.Router do
|
||||
end
|
||||
|
||||
defp archive_context_to_segments(%{kind: "core"}), do: []
|
||||
|
||||
defp archive_context_to_segments(%{kind: "category", slug: slug}) when is_binary(slug),
|
||||
do: ["category", slug]
|
||||
|
||||
defp archive_context_to_segments(%{kind: "category", name: name}), do: ["category", name]
|
||||
defp archive_context_to_segments(%{kind: "tag", name: name}), do: ["tag", name]
|
||||
|
||||
|
||||
@@ -25,6 +25,49 @@ defmodule BDS.Rendering.Metadata do
|
||||
metadata
|
||||
end
|
||||
|
||||
@doc """
|
||||
Resolve the document/page title (the `<title>` and the home-page heading).
|
||||
|
||||
Mirrors the old app's `resolvePageTitle`: the blog description wins, then the
|
||||
blog name. The archive-specific label (category/tag/date) is carried
|
||||
separately in `archive_context`, so every page's document title stays the
|
||||
blog title.
|
||||
"""
|
||||
@spec blog_page_title(String.t() | nil, String.t() | nil) :: String.t()
|
||||
def blog_page_title(description, name) do
|
||||
cond do
|
||||
is_binary(description) and String.trim(description) != "" -> String.trim(description)
|
||||
is_binary(name) and String.trim(name) != "" -> String.trim(name)
|
||||
true -> ""
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Resolve a category's descriptive display name for archive headings.
|
||||
|
||||
Mirrors the old app: the category's configured `title` wins, falling back to
|
||||
the raw category key when no title is set.
|
||||
"""
|
||||
@spec category_display_name(map() | nil, String.t()) :: String.t()
|
||||
def category_display_name(category_settings, category) do
|
||||
title =
|
||||
category_settings
|
||||
|> category_settings_for(category)
|
||||
|> category_title()
|
||||
|
||||
if is_binary(title) and String.trim(title) != "", do: String.trim(title), else: category
|
||||
end
|
||||
|
||||
defp category_settings_for(category_settings, category) when is_map(category_settings),
|
||||
do: Map.get(category_settings, category)
|
||||
|
||||
defp category_settings_for(_category_settings, _category), do: nil
|
||||
|
||||
defp category_title(settings) when is_map(settings),
|
||||
do: Map.get(settings, "title") || Map.get(settings, :title)
|
||||
|
||||
defp category_title(_settings), do: nil
|
||||
|
||||
@spec menu_items(String.t()) :: [map()]
|
||||
def menu_items(project_id) do
|
||||
{:ok, %{items: items}} = Menu.get_menu(project_id)
|
||||
|
||||
Reference in New Issue
Block a user