fix: issue #14 wrong sorting in category and tag archives

This commit is contained in:
2026-07-05 16:09:33 +02:00
parent 7dfcd78abd
commit bb82f1d4a1
5 changed files with 109 additions and 5 deletions

View File

@@ -175,6 +175,7 @@ defmodule BDS.Generation.Data do
end)
end
)
|> sort_generation_index_posts()
end
## --- internals -----------------------------------------------------------
@@ -398,4 +399,17 @@ defmodule BDS.Generation.Data do
defp append_generation_index(index, field, key, post) do
update_in(index[field], fn grouped -> Map.update(grouped, key, [post], &[post | &1]) end)
end
# Archive listings (categories, tags, dates) must render newest first. The
# index is built by prepending, so each grouped list is sorted explicitly by
# creation date (descending) to guarantee a stable newest-first order.
defp sort_generation_index_posts(index) do
Map.new(index, fn {field, grouped} ->
{field, Map.new(grouped, fn {key, posts} -> {key, sort_index_posts_desc(posts)} end)}
end)
end
defp sort_index_posts_desc(posts) do
Enum.sort_by(posts, &{-(&1.created_at || 0), -(&1.published_at || 0), to_string(&1.slug)})
end
end

View File

@@ -273,7 +273,7 @@ defmodule BDS.Rendering.ListArchive do
|> DateTime.to_date()
|> Date.to_iso8601())
)
|> Enum.sort_by(fn {label, _posts} -> label end)
|> Enum.sort_by(fn {label, _posts} -> label end, :desc)
last_index = length(grouped_blocks) - 1
@@ -284,7 +284,7 @@ defmodule BDS.Rendering.ListArchive do
date_label: date_label,
show_date_marker: true,
show_separator: index < last_index,
posts: Enum.sort_by(grouped_posts, &Map.get(&1, :created_at))
posts: Enum.sort_by(grouped_posts, &Map.get(&1, :created_at), :desc)
}
end)
|> case do