feat(news): show the posts of the lists as note panels
All checks were successful
check / check (pull_request) Successful in 30s
All checks were successful
check / check (pull_request) Successful in 30s
The blog list, the pages of a category and the pages of an archive show every post as a
note panel: a small line with the long date and the categories, the title in bold with
a full stop (unless it ends with a full stop, a question mark or an exclamation mark),
the description and a read more link whose aria-label names the post. The page of a
post, {{news_list}} and the latest news of the home page are unchanged.
A language without read_more gets the title as the link on the category and archive
pages, as those pages worked before; the blog list still needs the string.
Refs #9
This commit is contained in:
parent
633da1c46b
commit
71cf14006d
4 changed files with 185 additions and 43 deletions
|
|
@ -40,35 +40,6 @@ func (b *builder) pageLabel(lang string, n, total int) (string, error) {
|
|||
return strings.NewReplacer("{n}", strconv.Itoa(n), "{total}", strconv.Itoa(total)).Replace(text), nil
|
||||
}
|
||||
|
||||
// blogEntry returns one entry of the blog list: the title as a link, the date
|
||||
// and the categories, the description as a preview and a link to read more.
|
||||
// lang is the language of the page; a post in another language is marked.
|
||||
func (b *builder) blogEntry(p content.Post, lang string) (string, error) {
|
||||
l := b.langs[lang]
|
||||
more := l.Strings["read_more"]
|
||||
if more == "" {
|
||||
return "", fmt.Errorf("lang/%s.json: the language has no read_more string, which the blog list needs", lang)
|
||||
}
|
||||
mark := ""
|
||||
if p.Language != "" && p.Language != lang {
|
||||
mark = fmt.Sprintf(` hreflang="%s" lang="%s"`, p.Language, p.Language)
|
||||
}
|
||||
meta := fmt.Sprintf(`<time datetime="%s">%s</time>`, p.Date.Format(dateOnly), render.Escape(longDate(p.Date, l)))
|
||||
if links := categoryLinks(p); links != "" {
|
||||
meta += " · " + links
|
||||
}
|
||||
var out strings.Builder
|
||||
out.WriteString("<article>\n")
|
||||
fmt.Fprintf(&out, "<h2><a href=\"%s\"%s>%s</a></h2>\n", p.Path, mark, render.Escape(p.Title))
|
||||
fmt.Fprintf(&out, "<p class=\"meta\">%s</p>\n", meta)
|
||||
if p.Description != "" {
|
||||
fmt.Fprintf(&out, "<p>%s</p>\n", render.Escape(p.Description))
|
||||
}
|
||||
fmt.Fprintf(&out, "<p><a href=\"%s\" aria-label=\"%s: %s\"%s>%s</a></p>\n", p.Path, render.EscapeAttr(more), render.EscapeAttr(p.Title), mark, render.Escape(more))
|
||||
out.WriteString("</article>")
|
||||
return out.String(), nil
|
||||
}
|
||||
|
||||
// newsBlog returns the page n of the blog list of the language lang, with the
|
||||
// pager when the list has more than one page. A site without posts gets the
|
||||
// news_empty text.
|
||||
|
|
@ -86,7 +57,7 @@ func (b *builder) newsBlog(lang string, n int, ctx map[string]string) (string, e
|
|||
}
|
||||
entries := make([]string, 0, newsPerPage)
|
||||
for _, p := range b.posts[(n-1)*newsPerPage : end] {
|
||||
entry, err := b.blogEntry(p, lang)
|
||||
entry, err := b.newsPanel(p, lang, true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -216,7 +187,11 @@ func (b *builder) renderArchives() error {
|
|||
ctx := b.documentContext(d)
|
||||
ctx["archive_name"] = render.Escape(name)
|
||||
ctx["archive_nav"] = nav
|
||||
ctx["archive_posts"] = newsList(posts, lang)
|
||||
panels, err := b.newsPanels(posts, lang)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx["archive_posts"] = panels
|
||||
body, err := render.Substitute("templates/archive.html", tmpl, ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -324,13 +324,20 @@ func TestNewsBlogEntries(t *testing.T) {
|
|||
}
|
||||
index := readOut(t, out, "news/index.html")
|
||||
mustContain(t, "news index", index,
|
||||
"<article>\n<h2><a href=\"/news/2026/11/shop.html\">A shop</a></h2>",
|
||||
`<p class="meta"><time datetime="2026-11-14">14 November 2026</time> · <a href="/news/category/flug.html">FLUG</a></p>`,
|
||||
"<p>About the shop.</p>",
|
||||
`<p><a href="/news/2026/11/shop.html" aria-label="Read more: A shop">Read more</a></p>`,
|
||||
"<p class=\"note\" role=\"note\">\n"+
|
||||
`<span class="meta"><time datetime="2026-11-14">14 November 2026</time> · <a href="/news/category/flug.html">FLUG</a></span><br>`+"\n"+
|
||||
"<strong>A shop.</strong>\n"+
|
||||
"About the shop.\n"+
|
||||
`<a href="/news/2026/11/shop.html" aria-label="Read more: A shop">Read more</a>.`+"\n</p>",
|
||||
// a post with two categories, and one with a description made of its first sentence
|
||||
`<a href="/news/category/linux-day.html">Linux Day</a>, <a href="/news/category/flug.html">FLUG</a></span><br>`,
|
||||
`<strong>Status page.</strong>`,
|
||||
)
|
||||
if got := strings.Count(index, "<article>"); got != 3 {
|
||||
t.Errorf("the index has %d entries, want 3", got)
|
||||
if got := strings.Count(index, `role="note"`); got != 3 {
|
||||
t.Errorf("the index has %d panels, want 3", got)
|
||||
}
|
||||
if strings.Contains(index, "<article") {
|
||||
t.Error("the index has an article element, the panels replaced them")
|
||||
}
|
||||
if strings.Contains(index, "News pages") {
|
||||
t.Error("a list that fits one page has a pager")
|
||||
|
|
@ -346,18 +353,18 @@ func TestNewsBlogPagination(t *testing.T) {
|
|||
}
|
||||
first := readOut(t, out, "news/index.html")
|
||||
mustContain(t, "page 1", first, `<nav aria-label="News pages">`, `Page 1 of 3`, `<a href="/news/page/2/" rel="next">Older news</a>`)
|
||||
if strings.Contains(first, `rel="prev"`) || strings.Count(first, "<article>") != 10 || !strings.Contains(first, "Post 15") || strings.Contains(first, "Post 14") {
|
||||
if strings.Contains(first, `rel="prev"`) || strings.Count(first, `role="note"`) != 10 || !strings.Contains(first, "Post 15") || strings.Contains(first, "Post 14") {
|
||||
t.Errorf("page 1 is wrong:\n%s", first)
|
||||
}
|
||||
second := readOut(t, out, "news/page/2/index.html")
|
||||
mustContain(t, "page 2", second, `Page 2 of 3`, `<a href="/news/" rel="prev">Newer news</a>`, `<a href="/news/page/3/" rel="next">Older news</a>`,
|
||||
"<title>News, Page 2 of 3</title>", "Post 14", "Post 05")
|
||||
if strings.Count(second, "<article>") != 10 || strings.Contains(second, "Post 15") || strings.Contains(second, "Post 04") {
|
||||
if strings.Count(second, `role="note"`) != 10 || strings.Contains(second, "Post 15") || strings.Contains(second, "Post 04") {
|
||||
t.Errorf("page 2 is wrong:\n%s", second)
|
||||
}
|
||||
third := readOut(t, out, "news/page/3/index.html")
|
||||
mustContain(t, "page 3", third, `Page 3 of 3`, `<a href="/news/page/2/" rel="prev">Newer news</a>`, "GNU/Linux Day 2026")
|
||||
if strings.Contains(third, `rel="next"`) || strings.Count(third, "<article>") != 5 {
|
||||
if strings.Contains(third, `rel="next"`) || strings.Count(third, `role="note"`) != 5 {
|
||||
t.Errorf("page 3 is wrong:\n%s", third)
|
||||
}
|
||||
if exists(t, out, "news/page/4/index.html") || exists(t, out, "news/page/1/index.html") {
|
||||
|
|
@ -421,3 +428,101 @@ func TestNoArchiveWithoutTemplate(t *testing.T) {
|
|||
t.Errorf("error = %v, want one that names the archive template", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewsPanelTitlePunctuation checks the full stop after the title of a panel: added, unless the title ends with one, a question mark or an exclamation mark.
|
||||
func TestNewsPanelTitlePunctuation(t *testing.T) {
|
||||
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
|
||||
"news/q.md": "Title: Why a shop?\nDate: 2026-11-01T12:00:00+0100\n-\nText.\n",
|
||||
"news/e.md": "Title: It is open!\nDate: 2026-11-02T12:00:00+0100\n-\nText.\n",
|
||||
"news/d.md": "Title: Done.\nDate: 2026-11-03T12:00:00+0100\n-\nText.\n",
|
||||
"news/a.md": "Title: Tom & Jerry\nDate: 2026-11-04T12:00:00+0100\n-\nText.\n"})
|
||||
out, err := buildWith(t, files, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v", err)
|
||||
}
|
||||
index := readOut(t, out, "news/index.html")
|
||||
mustContain(t, "news index", index, "<strong>Why a shop?</strong>", "<strong>It is open!</strong>", "<strong>Done.</strong>", "<strong>Tom & Jerry.</strong>",
|
||||
`aria-label="Read more: Tom & Jerry"`)
|
||||
for _, wrong := range []string{"shop?.", "open!.", "Done..", "Done.</strong>."} {
|
||||
if strings.Contains(index, wrong) {
|
||||
t.Errorf("the index has %q", wrong)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewsPanelWithoutCategoriesOrDescription checks the two optional lines of a panel.
|
||||
func TestNewsPanelWithoutCategoriesOrDescription(t *testing.T) {
|
||||
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
|
||||
"news/bare.md": "Title: Bare\nDate: 2026-11-13T12:00:00+0100\nDescription:\n-\n"}), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v", err)
|
||||
}
|
||||
mustContain(t, "news index", readOut(t, out, "news/index.html"),
|
||||
"<p class=\"note\" role=\"note\">\n"+
|
||||
`<span class="meta"><time datetime="2026-11-13">13 November 2026</time></span><br>`+"\n"+
|
||||
"<strong>Bare.</strong>\n"+
|
||||
`<a href="/news/2026/11/bare.html" aria-label="Read more: Bare">Read more</a>.`+"\n</p>")
|
||||
}
|
||||
|
||||
// TestPanelsOnCategoryAndArchivePages checks that the lists of the categories and of the archives are panels too,
|
||||
// and that the page of a post and the latest news of the home page are not.
|
||||
func TestPanelsOnCategoryAndArchivePages(t *testing.T) {
|
||||
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v", err)
|
||||
}
|
||||
shop := "<strong>A shop.</strong>\nAbout the shop.\n" + `<a href="/news/2026/11/shop.html" aria-label="Read more: A shop">Read more</a>.`
|
||||
for rel, want := range map[string]int{"news/category/flug.html": 2, "news/2026/index.html": 3, "news/2026/11/index.html": 2} {
|
||||
got := readOut(t, out, rel)
|
||||
if n := strings.Count(got, `role="note"`); n != want {
|
||||
t.Errorf("%s has %d panels, want %d", rel, n, want)
|
||||
}
|
||||
if !strings.Contains(got, shop) {
|
||||
t.Errorf("%s lacks the panel of the shop post", rel)
|
||||
}
|
||||
if strings.Contains(got, "<li><time") {
|
||||
t.Errorf("%s still has the list of dates", rel)
|
||||
}
|
||||
}
|
||||
if post := readOut(t, out, "news/2026/11/shop.html"); strings.Contains(post, `role="note"`) {
|
||||
t.Error("the page of a post has a panel")
|
||||
}
|
||||
if home := readOut(t, out, "index.html"); strings.Contains(home, `role="note"`) || !strings.Contains(home, "<li><time") {
|
||||
t.Error("the latest news of the home page changed")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPanelsWithoutReadMore checks that a language without read_more gets panels whose title is the link on the
|
||||
// category and archive pages, as before the panels, and an error on the blog list, which has no other link.
|
||||
func TestPanelsWithoutReadMore(t *testing.T) {
|
||||
withoutMore := strings.Replace(blogLangEn, `"read_more": "Read more", `, "", 1)
|
||||
out, err := buildWith(t, blogSite(map[string]string{"lang/en.json": withoutMore, "templates/archive.html": archiveTemplate}), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v", err)
|
||||
}
|
||||
for _, rel := range []string{"news/category/flug.html", "news/2026/11/index.html"} {
|
||||
got := readOut(t, out, rel)
|
||||
mustContain(t, rel, got, `<strong><a href="/news/2026/11/shop.html">A shop.</a></strong>`+"\nAbout the shop.\n</p>")
|
||||
if strings.Contains(got, "aria-label") || strings.Contains(got, "Read more") {
|
||||
t.Errorf("%s has a read more link without the string", rel)
|
||||
}
|
||||
}
|
||||
_, err = buildWith(t, blogSite(map[string]string{"lang/en.json": withoutMore, "content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "read_more") {
|
||||
t.Errorf("error = %v, want one that names read_more", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPanelOfAPostInAnotherLanguage checks that the link of a panel says the language of a post that is not in the language of the page.
|
||||
func TestPanelOfAPostInAnotherLanguage(t *testing.T) {
|
||||
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
|
||||
"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)
|
||||
}
|
||||
index := readOut(t, out, "news/index.html")
|
||||
mustContain(t, "news index", index, `<a href="/news/2026/11/ciao.html" aria-label="Read more: Ciao" hreflang="it" lang="it">Read more</a>.`)
|
||||
if strings.Count(index, `hreflang="it" lang="it">Read more`) != 1 {
|
||||
t.Error("an English post is marked as written in Italian")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,64 @@ func newsItem(p content.Post, lang string, withDescription bool) string {
|
|||
return item + "</li>"
|
||||
}
|
||||
|
||||
// newsPanel returns a post for a list of the news as a note panel: first a small
|
||||
// line with the date and the categories, then the title in bold with a full stop
|
||||
// (unless it ends with one, a question mark or an exclamation mark), then the
|
||||
// description, then the link to read more, which names the post for a reader that
|
||||
// hears the links out of context. lang is the language of the page; the link of a
|
||||
// post in another language says so. Without the string read_more the title is the
|
||||
// link and there is no link to read more, unless needMore is set: the blog list has
|
||||
// no other link to the post, so there it is an error.
|
||||
func (b *builder) newsPanel(p content.Post, lang string, needMore bool) (string, error) {
|
||||
l := b.langs[lang]
|
||||
more := l.Strings["read_more"]
|
||||
if more == "" && needMore {
|
||||
return "", fmt.Errorf("lang/%s.json: the language has no read_more string, which the blog list needs", lang)
|
||||
}
|
||||
mark := ""
|
||||
if p.Language != "" && p.Language != lang {
|
||||
mark = fmt.Sprintf(` hreflang="%s" lang="%s"`, p.Language, p.Language)
|
||||
}
|
||||
meta := fmt.Sprintf(`<time datetime="%s">%s</time>`, p.Date.Format(dateOnly), render.Escape(longDate(p.Date, l)))
|
||||
if links := categoryLinks(p); links != "" {
|
||||
meta += " · " + links
|
||||
}
|
||||
title := p.Title
|
||||
if !strings.HasSuffix(title, ".") && !strings.HasSuffix(title, "?") && !strings.HasSuffix(title, "!") {
|
||||
title += "."
|
||||
}
|
||||
var out strings.Builder
|
||||
out.WriteString("<p class=\"note\" role=\"note\">\n")
|
||||
fmt.Fprintf(&out, "<span class=\"meta\">%s</span><br>\n", meta)
|
||||
if more == "" {
|
||||
fmt.Fprintf(&out, "<strong><a href=\"%s\"%s>%s</a></strong>\n", p.Path, mark, render.Escape(title))
|
||||
} else {
|
||||
fmt.Fprintf(&out, "<strong>%s</strong>\n", render.Escape(title))
|
||||
}
|
||||
if p.Description != "" {
|
||||
fmt.Fprintf(&out, "%s\n", render.Escape(p.Description))
|
||||
}
|
||||
if more != "" {
|
||||
fmt.Fprintf(&out, "<a href=\"%s\" aria-label=\"%s: %s\"%s>%s</a>.\n", p.Path, render.EscapeAttr(more), render.EscapeAttr(p.Title), mark, render.Escape(more))
|
||||
}
|
||||
out.WriteString("</p>")
|
||||
return out.String(), nil
|
||||
}
|
||||
|
||||
// newsPanels returns the panels of the posts of a category or of an archive,
|
||||
// one after the other, or an empty string when there are none.
|
||||
func (b *builder) newsPanels(posts []content.Post, lang string) (string, error) {
|
||||
panels := make([]string, len(posts))
|
||||
for i, p := range posts {
|
||||
panel, err := b.newsPanel(p, lang, false)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
panels[i] = panel
|
||||
}
|
||||
return strings.Join(panels, "\n"), nil
|
||||
}
|
||||
|
||||
// newsList returns the full list of the news as a <ul>, or an empty string
|
||||
// when there are no posts.
|
||||
func newsList(posts []content.Post, lang string) string {
|
||||
|
|
|
|||
|
|
@ -255,7 +255,11 @@ func (b *builder) renderCategories() error {
|
|||
}
|
||||
ctx := b.documentContext(d)
|
||||
ctx["category_name"] = render.Escape(c.Name)
|
||||
ctx["category_posts"] = newsList(c.posts, b.newsLang)
|
||||
panels, err := b.newsPanels(c.posts, b.newsLang)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx["category_posts"] = panels
|
||||
body, err := render.Substitute("templates/category.html", tmpl, ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
Loading…
Reference in a new issue