fix(news): group the archive by the month as written, drop the empty lists

- groupArchive compared each post with the last group only, so two posts near midnight
  at the end of a month, written with different offsets, listed the same month twice and
  the build failed with "both published as". Posts are grouped by their own year and
  month, newest first.
- {{news_archive}} is empty when there are no posts, so the index says there is no news
  once, and the heading of an empty {{news_archive}} or {{categories_list}} goes with it.
- tests for exactly ten posts, eleven posts, a single post, a title with quotes, an
  ampersand, a tag and non Latin letters, and a neighbour written in another language

Refs #9
This commit is contained in:
tgiachi 2026-11-16 12:48:17 +01:00
commit 6987541f88
3 changed files with 243 additions and 13 deletions

View file

@ -4,6 +4,7 @@ package build
import (
"fmt"
"sort"
"strconv"
"strings"
@ -109,24 +110,43 @@ type (
}
)
// groupArchive groups posts, newest first, by year and by month, as written in
// their dates. The years and the months are newest first too.
// groupArchive groups posts by year and by month, as written in their dates.
// The years and the months are newest first, and the posts of a group keep the
// order they were given. A post is put in its group by its own year and month,
// not by the group before it: two posts near midnight at the end of a month,
// written with different offsets, can be in the order of their instants and in
// the opposite order of their months.
func groupArchive(posts []content.Post) []archiveYear {
var years []archiveYear
type key struct{ year, month int }
years := map[int]*archiveYear{}
months := map[key]*archiveMonth{}
for _, p := range posts {
y, m := p.Date.Year(), int(p.Date.Month())
if len(years) == 0 || years[len(years)-1].year != y {
years = append(years, archiveYear{year: y})
year := years[y]
if year == nil {
year = &archiveYear{year: y}
years[y] = year
}
year := &years[len(years)-1]
year.posts = append(year.posts, p)
if len(year.months) == 0 || year.months[len(year.months)-1].month != m {
year.months = append(year.months, archiveMonth{year: y, month: m})
month := months[key{y, m}]
if month == nil {
month = &archiveMonth{year: y, month: m}
months[key{y, m}] = month
}
month := &year.months[len(year.months)-1]
month.posts = append(month.posts, p)
}
return years
var out []archiveYear
for _, year := range years {
for k, month := range months {
if k.year == year.year {
year.months = append(year.months, *month)
}
}
sort.Slice(year.months, func(i, j int) bool { return year.months[i].month > year.months[j].month })
out = append(out, *year)
}
sort.Slice(out, func(i, j int) bool { return out[i].year > out[j].year })
return out
}
// yearPath and monthPath are the addresses of an archive year and month.
@ -151,14 +171,15 @@ func (b *builder) monthList(y archiveYear, lang string) string {
}
// newsArchive returns the nested list of the years and the months that have
// posts, or the news_empty text when there are none. It needs the archive pages.
// posts, or nothing when there are none: the blog list says there is no news. It
// needs the archive pages.
func (b *builder) newsArchive(lang string, ctx map[string]string) (string, error) {
if _, err := b.template("archive"); err != nil {
return "", fmt.Errorf("the {{news_archive}} block needs the archive pages: %w", err)
}
years := groupArchive(b.posts)
if len(years) == 0 {
return "<p>\n" + ctx["news_empty"] + "\n</p>", nil
return "", nil
}
items := make([]string, len(years))
for i, y := range years {

View file

@ -3,6 +3,7 @@
package build
import (
"encoding/xml"
"path/filepath"
"strings"
"testing"
@ -526,3 +527,180 @@ func TestPanelOfAPostInAnotherLanguage(t *testing.T) {
t.Error("an English post is marked as written in Italian")
}
}
// TestArchiveGroupsByTheDateAsWritten checks that two posts either side of midnight, written with different
// offsets, land in their own months and that no month is listed twice, whatever the order of the instants.
func TestArchiveGroupsByTheDateAsWritten(t *testing.T) {
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/late.md": "Title: Late\nDate: 2026-11-30T23:45:00+0000\n-\nText.\n",
"news/early.md": "Title: Early\nDate: 2026-12-01T00:30:00+0100\n-\nText.\n",
"news/mid.md": "Title: Mid\nDate: 2026-11-29T12:00:00+0100\n-\nText.\n"})
out, err := buildWith(t, files, func(o *Options) { o.Now = time.Date(2026, 12, 20, 12, 0, 0, 0, time.UTC) })
if err != nil {
t.Fatalf("Build() error = %v", err)
}
index := readOut(t, out, "news/index.html")
mustContain(t, "news index", index, `<a href="/news/2026/">2026</a> (6)`)
dec, nov, sep := strings.Index(index, ">December 2026</a> (1)"), strings.Index(index, ">November 2026</a> (4)"), strings.Index(index, ">September 2026</a> (1)")
if dec < 0 || nov < dec || sep < nov {
t.Errorf("the months are not December, November, September, one each:\n%s", index)
}
if strings.Count(index, "November 2026</a>") != 1 {
t.Error("November is listed more than once")
}
month := readOut(t, out, "news/2026/11/index.html")
late, mid, shop := strings.Index(month, "<strong>Late.</strong>"), strings.Index(month, "<strong>Mid.</strong>"), strings.Index(month, "<strong>A shop.</strong>")
if late < 0 || mid < late || shop < mid || strings.Contains(month, "<strong>Early.</strong>") {
t.Errorf("the month of November is wrong:\n%s", month)
}
mustContain(t, "month of December", readOut(t, out, "news/2026/12/index.html"), "<strong>Early.</strong>")
}
// TestNoNewsAtAll checks that a site without posts says so once, and that the empty lists take their headings with them.
func TestNoNewsAtAll(t *testing.T) {
index := "Title: News\nID: news\nDescription: About News.\n-\n# News\n\n{{news_blog}}\n\n## Categories\n\n{{categories_list}}\n\n## Archive\n\n{{news_archive}}\n"
files := blogSite(map[string]string{"content/en/news/index.md": index, "templates/archive.html": archiveTemplate,
"news/shop.md": "", "news/gnu-linux-day-2026.md": "", "news/status.md": ""})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
got := readOut(t, out, "news/index.html")
if n := strings.Count(got, "No news yet."); n != 1 {
t.Errorf("the index says there is no news %d times, want once:\n%s", n, got)
}
for _, heading := range []string{">Categories</h2>", ">Archive</h2>"} {
if strings.Contains(got, heading) {
t.Errorf("the index keeps the heading %q of an empty list", heading)
}
}
mustContain(t, "news index", got, "<h1>News</h1>")
}
// TestHomeKeepsItsHeadingWithoutNews checks that the heading before the latest news is the page's own and stays.
func TestHomeKeepsItsHeadingWithoutNews(t *testing.T) {
files := blogSite(map[string]string{"news/shop.md": "", "news/gnu-linux-day-2026.md": "", "news/status.md": ""})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "home", readOut(t, out, "index.html"), `<h1 class="hero">Technology.</h1>`)
}
// TestHeadingStaysWithAListThatHasItems checks that the heading of a generated list is dropped only when the list is empty.
func TestHeadingStaysWithAListThatHasItems(t *testing.T) {
index := "Title: News\nID: news\nDescription: About News.\n-\n# News\n\n## Categories\n\n{{categories_list}}\n\n## Archive\n\n{{news_archive}}\n"
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": index, "templates/archive.html": archiveTemplate}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "news index", readOut(t, out, "news/index.html"), ">Categories</h2>", ">Archive</h2>")
}
// listFiles returns the site of the pagination tests: the three base posts and extra ones, so that n posts in all.
func listFiles(t *testing.T, n int) map[string]string {
t.Helper()
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate})
return manyPosts(files, n-3)
}
// TestExactlyOnePageOfPosts checks that ten posts make one page with no pager and no second page.
func TestExactlyOnePageOfPosts(t *testing.T) {
out, err := buildWith(t, listFiles(t, 10), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
index := readOut(t, out, "news/index.html")
if n := strings.Count(index, `role="note"`); n != 10 {
t.Errorf("the index has %d panels, want 10", n)
}
if strings.Contains(index, "News pages") || strings.Contains(index, "Page 1 of") || exists(t, out, "news/page/2/index.html") {
t.Error("ten posts have a pager or a second page")
}
}
// TestEleventhPostIsAloneOnPageTwo checks the page that holds the one post that does not fit.
func TestEleventhPostIsAloneOnPageTwo(t *testing.T) {
out, err := buildWith(t, listFiles(t, 11), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
first := readOut(t, out, "news/index.html")
mustContain(t, "page 1", first, "Page 1 of 2", `<a href="/news/page/2/" rel="next">Older news</a>`)
if n := strings.Count(first, `role="note"`); n != 10 || strings.Contains(first, "GNU/Linux Day 2026.") {
t.Errorf("page 1 has %d panels or the oldest post", n)
}
second := readOut(t, out, "news/page/2/index.html")
mustContain(t, "page 2", second, "Page 2 of 2", `<a href="/news/" rel="prev">Newer news</a>`, "<strong>GNU/Linux Day 2026.</strong>")
if n := strings.Count(second, `role="note"`); n != 1 || strings.Contains(second, `rel="next"`) {
t.Errorf("page 2 has %d panels or a link to a third page", n)
}
if exists(t, out, "news/page/3/index.html") {
t.Error("eleven posts have a third page")
}
}
// TestOnlyOnePost checks that the one post has no link to a newer or an older post anywhere, and no pager.
func TestOnlyOnePost(t *testing.T) {
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/gnu-linux-day-2026.md": "", "news/status.md": ""})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
for _, rel := range []string{"news/2026/11/shop.html", "news/index.html", "news/2026/11/index.html"} {
got := readOut(t, out, rel)
for _, absent := range []string{"Older", "Newer", `rel="prev"`, `rel="next"`, "News pages"} {
if strings.Contains(got, absent) {
t.Errorf("%s has %q, but there is one post", rel, absent)
}
}
}
mustContain(t, "the post", readOut(t, out, "news/2026/11/shop.html"), `<li><a href="/news/">All news</a></li>`)
}
// TestUnusualTitle checks that a title with quotes, an ampersand, a tag and non Latin letters is escaped everywhere.
func TestUnusualTitle(t *testing.T) {
title := `Say "hi" & <b>go</b> ünï 日本語`
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
"news/odd.md": "Title: " + title + "\nDate: 2026-11-15T09:00:00+0100\nCategories: Odd\n-\nText.\n"})
out, err := buildWith(t, files, nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
escaped := `Say "hi" &amp; &lt;b&gt;go&lt;/b&gt; ünï 日本語`
attr := `Say &quot;hi&quot; &amp; &lt;b&gt;go&lt;/b&gt; ünï 日本語`
for _, rel := range []string{"news/index.html", "news/category/odd.html", "news/2026/11/index.html"} {
mustContain(t, rel, readOut(t, out, rel), "<strong>"+escaped+".</strong>", `aria-label="Read more: `+attr+`"`)
}
mustContain(t, "the post", readOut(t, out, "news/2026/11/odd.html"), "<h1>"+escaped+"</h1>")
mustContain(t, "the post below", readOut(t, out, "news/2026/11/shop.html"), `Newer: `+escaped)
for _, rel := range []string{"news/index.html", "news/category/odd.html", "news/2026/11/odd.html", "news/feed.xml"} {
if strings.Contains(readOut(t, out, rel), "<b>go</b>") {
t.Errorf("%s has the tag of the title as markup", rel)
}
}
var feed struct {
Channel struct {
Items []struct {
Title string `xml:"title"`
} `xml:"item"`
} `xml:"channel"`
}
if err := xml.Unmarshal([]byte(readOut(t, out, "news/feed.xml")), &feed); err != nil {
t.Fatalf("the feed is not well formed: %v", err)
}
if len(feed.Channel.Items) == 0 || feed.Channel.Items[0].Title != title {
t.Errorf("the first item of the feed is %+v, want the title %q", feed.Channel.Items, title)
}
}
// TestNeighbourInAnotherLanguageIsMarked checks that the link to an older post in another language names the language.
func TestNeighbourInAnotherLanguageIsMarked(t *testing.T) {
out, err := buildWith(t, blogSite(map[string]string{"news/ciao.md": "Title: Ciao\nDate: 2026-11-05T09:00:00+0100\nLanguage: it\n-\nUn testo.\n"}), nil)
if err != nil {
t.Fatalf("Build() error = %v", err)
}
mustContain(t, "status post", readOut(t, out, "news/2026/11/status.html"), `rel="prev">Older: <span lang="it">Ciao</span></a>`)
mustContain(t, "italian post", readOut(t, out, "news/2026/11/ciao.html"), `rel="next">Piu recente: <span lang="en">Status page</span></a>`)
}

View file

@ -134,7 +134,8 @@ func (b *builder) markdown(name, source, lang string, ctx map[string]string) (st
var codeElement = regexp.MustCompile(`(?s)<code[^>]*>.*?</code>`)
// expandBlocks replaces each paragraph that holds only a block placeholder by
// the generated HTML. A block placeholder inside other text is an error. All
// the generated HTML. An empty list of categories or of the archive goes with the
// heading right before it. A block placeholder inside other text is an error. All
// blocks are checked before any is expanded, so the text of a generated list
// (a news title may say {{latest_news}}) is never taken for a placeholder.
func (b *builder) expandBlocks(name, html, lang string, ctx map[string]string) (string, error) {
@ -154,11 +155,41 @@ func (b *builder) expandBlocks(name, html, lang string, ctx map[string]string) (
if err != nil {
return "", err
}
if generated == "" && headedBlocks[block] {
html = removeWithHeading(html, marker)
continue
}
html = strings.ReplaceAll(html, marker, generated)
}
return html, nil
}
// headedBlocks are the lists a page puts under a heading of its own, which goes
// when the list is empty. The latest news bring their heading with them, and a
// heading before them is the page's.
var headedBlocks = map[string]bool{"categories_list": true, "news_archive": true}
// headingEnd matches the end of a heading element at the end of a text.
var headingEnd = regexp.MustCompile(`</h([1-6])>$`)
// removeWithHeading removes every marker of an empty block and the heading right
// before it, which would be left with nothing under it.
func removeWithHeading(html, marker string) string {
for {
at := strings.Index(html, marker)
if at < 0 {
return html
}
before := strings.TrimRight(html[:at], " \t\r\n")
if m := headingEnd.FindStringSubmatchIndex(before); m != nil {
if open := strings.LastIndex(before[:m[0]], "<h"+before[m[2]:m[3]]); open >= 0 {
before = before[:open]
}
}
html = before + "\n" + strings.TrimLeft(html[at+len(marker):], "\n")
}
}
// block returns the HTML of a generated list.
func (b *builder) block(name, lang string, ctx map[string]string) (string, error) {
switch name {