Private
Public Access
The server still groups articles into "Received today/yesterday/<date>" headers using UTC, so an article published near UTC midnight could land in a mismatched day group. Each article row now ships its raw UTC instant (data-published-utc); static/js/date-groups.js discards the server-rendered headers on load and rebuilds them using the browser's own local calendar day, same approach as the article-time localization this follows up on. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
62 lines
2.2 KiB
JavaScript
62 lines
2.2 KiB
JavaScript
(function () {
|
|
// The server groups articles into "Received today/yesterday/<date>"
|
|
// headers using UTC (config.settings.base TIME_ZONE), so an article
|
|
// published near UTC midnight can land in the wrong day group from the
|
|
// viewer's own perspective. Each article row carries its UTC instant in
|
|
// data-published-utc; this throws away the server's date-header <li>s
|
|
// and rebuilds them using the browser's local calendar day instead.
|
|
var MONTHS = [
|
|
"January", "February", "March", "April", "May", "June",
|
|
"July", "August", "September", "October", "November", "December",
|
|
];
|
|
|
|
function localDateOf(iso) {
|
|
if (!iso) return null;
|
|
var date = new Date(iso);
|
|
if (isNaN(date.getTime())) return null;
|
|
date.setHours(0, 0, 0, 0);
|
|
return date;
|
|
}
|
|
|
|
function labelFor(day, today) {
|
|
if (day === null) return "Undated";
|
|
var formatted = day.getDate() + " " + MONTHS[day.getMonth()] + " " + day.getFullYear();
|
|
var diffDays = Math.round((today - day) / 86400000);
|
|
if (diffDays === 0) return "Received today — " + formatted;
|
|
if (diffDays === 1) return "Received yesterday — " + formatted;
|
|
return "Received " + formatted;
|
|
}
|
|
|
|
function regroupDateHeaders(root) {
|
|
if (!root) return;
|
|
var list = root.matches && root.matches("ul.article-list") ? root : root.querySelector("ul.article-list");
|
|
if (!list || !list.querySelector("li.article-row[data-published-utc], li.article-row")) return;
|
|
|
|
list.querySelectorAll("li.date-header").forEach(function (el) {
|
|
el.parentNode.removeChild(el);
|
|
});
|
|
|
|
var today = new Date();
|
|
today.setHours(0, 0, 0, 0);
|
|
|
|
var currentKey;
|
|
list.querySelectorAll("li.article-row").forEach(function (row) {
|
|
var day = localDateOf(row.getAttribute("data-published-utc"));
|
|
var key = day === null ? "undated" : day.getTime();
|
|
if (key !== currentKey) {
|
|
var header = document.createElement("li");
|
|
header.className = "date-header";
|
|
header.textContent = labelFor(day, today);
|
|
list.insertBefore(header, row);
|
|
currentKey = key;
|
|
}
|
|
});
|
|
}
|
|
|
|
regroupDateHeaders(document);
|
|
|
|
document.body.addEventListener("htmx:afterSwap", function (event) {
|
|
regroupDateHeaders(event.target);
|
|
});
|
|
})();
|