fix: content descriptors list only locales with content

The post, category, tag and author descriptors keep one entry per
enabled locale but give a locale a URL only when the record has content
in it: posts and categories by their own translation (a translated slug
alone is no content), tags and authors when one of their published
posts is translated into it, the default locale always. Without content
the entry's URL is "", so hreflang and the sitemaps never advertise an
untranslated locale. The menu item types' Resolve is unchanged.
This commit is contained in:
Jakub Zych
2026-11-04 23:27:57 +01:00
parent 7662d54ab3
commit 0a62090435
5 changed files with 283 additions and 13 deletions

View File

@@ -91,7 +91,10 @@ func (m menuTypes) facts(ctx context.Context, db *gorm.DB) descriptorFacts {
// pages are on) and TopHeading (2 while the H1 setting is on, else 0); each
// locale lists the featured image (alt: the file's title, else the
// translated post title, P33-06) and the body's images, YouTube embeds and
// headings (parseContentFacts). The queries are batched per page.
// headings (parseContentFacts). A locale in which the post has no content
// (no translated title, content, content_html or excerpt, P33-64) keeps its
// entry with URL "" (no public page, D-57). The queries are batched per
// page.
func describePosts(ctx context.Context, db *gorm.DB, posts []models.Post, pattern, unprefixed string, facts descriptorFacts) ([]pact.ContentRecord, error) {
in := pact.MenuItemResolve{Pattern: pattern, UnprefixedLocale: unprefixed}
items, err := resolvePostItems(ctx, db, posts, in)
@@ -171,9 +174,15 @@ func describePosts(ctx context.Context, db *gorm.DB, posts []models.Post, patter
images = append(images, pact.ContentImage{URL: image, Alt: altText})
}
images = append(images, found.images...)
url := alt.URL
if !tr.hasContent(locales, alt.Locale, p.ID, postContentFields) {
// No translation in this locale (P33-64, D-57): no public
// page for it, so no hreflang or sitemap address.
url = ""
}
rec.Locales = append(rec.Locales, pact.ContentLocale{
Locale: alt.Locale,
URL: alt.URL,
URL: url,
Title: alt.Title,
Description: strings.TrimSpace(plainText(tr.value(locale, p.ID, "excerpt", p.Excerpt))),
Image: image,
@@ -450,6 +459,8 @@ func (m menuTypes) listCategories(ctx context.Context, in pact.ContentList) (pac
// heading outline (content is not translatable) and TopHeading follows the
// H1 setting (2 on, 0 off), so the pillar text is checked as post content
// is (CONTEXT 2026-10-28 H1 conversion scope).
// A locale in which the category has no translated name or description
// (P33-64) gets URL "".
func describeCategories(ctx context.Context, db *gorm.DB, cats []models.Category, pattern, unprefixed string, facts descriptorFacts) ([]pact.ContentRecord, error) {
ids := make([]uint, 0, len(cats))
for _, c := range cats {
@@ -483,9 +494,13 @@ func describeCategories(ctx context.Context, db *gorm.DB, cats []models.Category
if locale == xDefaultLocale {
locale = ""
}
url := alt.URL
if !ci.tr.hasContent(ci.locales, alt.Locale, c.ID, categoryContentFields) {
url = ""
}
rec.Locales = append(rec.Locales, pact.ContentLocale{
Locale: alt.Locale,
URL: alt.URL,
URL: url,
Title: alt.Title,
Description: plainText(ci.tr.value(locale, c.ID, "description", host)),
Words: words,
@@ -620,13 +635,23 @@ func (m menuTypes) listTags(ctx context.Context, in pact.ContentList) (pact.Cont
}
// describeTags describes tags, always published, one locale per enabled
// locale with the same name and slug; URLs are empty without tag pages.
// locale with the same name and slug; URLs are empty without tag pages, and
// in a locale in which none of the tag's published posts has content
// (P33-64).
func (m menuTypes) describeTags(ctx context.Context, db *gorm.DB, tags []models.Tag, pattern, unprefixed string) ([]pact.ContentRecord, error) {
locales, err := loadMenuLocales(ctx, db, unprefixed)
if err != nil {
return nil, err
}
pages := m.tagPages()
ids := make([]uint, 0, len(tags))
for _, tag := range tags {
ids = append(ids, tag.ID)
}
content, err := tagContentLocales(ctx, db, locales, ids)
if err != nil {
return nil, err
}
out := make([]pact.ContentRecord, 0, len(tags))
for _, tag := range tags {
rec := pact.ContentRecord{ID: strconv.FormatUint(uint64(tag.ID), 10), Published: true, LastMod: copyTime(tag.UpdatedAt)}
@@ -636,7 +661,7 @@ func (m menuTypes) describeTags(ctx context.Context, db *gorm.DB, tags []models.
}
for _, alt := range menuAlternates(pattern, locales, tagParams(tag), func(string) string { return tag.Name }) {
url := alt.URL
if !pages {
if !pages || !ownerHasContent(locales, content, tag.ID, alt.Locale) {
url = ""
}
rec.Locales = append(rec.Locales, pact.ContentLocale{Locale: alt.Locale, URL: url, Title: tag.Name, Description: desc})