Files
uptimesebi/internal/web/meta.templ
T
Sebastian UnterschützandClaude Opus 5.5 3baeb98ee2
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m23s
ci / test (pull_request) Successful in 12m44s
ci / build (pull_request) Successful in 16m9s
ci / image (pull_request) Successful in 3m24s
feat(seo): metadata, previews and sitemaps for status pages and the app
Status pages describe themselves to search engines and link previews:
description (from the page's text, else its status), canonical address,
language versions via ?lang= with hreflang and x-default, Open Graph and
Twitter tags, and a 1200x630 preview image per page and language showing
its name and current status (/og.png). Each page has a sitemap.xml, and
robots.txt keeps crawlers off feeds, the API and subscription forms.
Component, legal and subscription pages are noindex.

In the cloud edition the app's sign-in, sign-up and legal pages get the
same tags with a preview image of their own, plus robots.txt and a
sitemap. Everything signed in stays noindex, and self-hosted
installations keep the whole app out of search engines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-28 22:50:34 +01:00

139 lines
3.9 KiB
Templ

package web
import (
"html"
"net/url"
"regexp"
"strconv"
"strings"
"unicode/utf8"
"git.zb-server.de/ZB-Server/uptimesebi/internal/ogimage"
)
// PageMeta describes a page for search engines and link previews. Pages
// without one are private and carry noindex.
type PageMeta struct {
// Title is the shared title, without the site suffix of <title>.
Title string
Description string
// URL is the canonical address of the page.
URL string
Image string
SiteName string
// Lang is the language the page is shown in.
Lang string
// Alternates are the page in each language, plus x-default.
Alternates []MetaAlternate
}
// MetaAlternate is a language version of a page; Lang "x-default" marks
// the one that picks the language itself.
type MetaAlternate struct {
Lang string
URL string
}
// LangURL adds ?lang= to a page's address.
func LangURL(base, lang string) string {
u, err := url.Parse(base)
if err != nil {
return base
}
q := u.Query()
q.Set("lang", lang)
u.RawQuery = q.Encode()
return u.String()
}
// NewPageMeta fills the language versions and the canonical address of a
// page at base: the address of its language when the visitor chose one
// with ?lang=, else base, which picks the language itself.
func NewPageMeta(title, description, base, image, siteName, lang string, chosen bool, langs []string) *PageMeta {
m := &PageMeta{Title: title, Description: description, URL: base, Image: image, SiteName: siteName, Lang: lang}
if chosen {
m.URL = LangURL(base, lang)
}
for _, l := range langs {
m.Alternates = append(m.Alternates, MetaAlternate{Lang: l, URL: LangURL(base, l)})
}
m.Alternates = append(m.Alternates, MetaAlternate{Lang: "x-default", URL: base})
return m
}
var tagRE = regexp.MustCompile(`<[^>]*>`)
// PlainText turns Markdown into a single line of text of at most max
// characters, for descriptions.
func PlainText(markdown string, max int) string {
s := html.UnescapeString(tagRE.ReplaceAllString(Markdown(markdown), " "))
s = strings.Join(strings.Fields(s), " ")
if utf8.RuneCountInString(s) <= max {
return s
}
r := []rune(s)[:max-1]
if i := strings.LastIndex(string(r), " "); i > max/2 {
return string(r)[:i] + "…"
}
return string(r) + "…"
}
// ogLocale maps a language to the locale Open Graph expects.
func ogLocale(lang string) string {
switch lang {
case "de":
return "de_DE"
case "en":
return "en_US"
}
return strings.ReplaceAll(lang, "-", "_")
}
// metaTags are the description, canonical address, language versions and
// Open Graph and Twitter tags of m.
templ metaTags(m *PageMeta) {
if m.Description != "" {
<meta name="description" content={ m.Description }/>
}
<link rel="canonical" href={ m.URL }/>
for _, a := range m.Alternates {
<link rel="alternate" hreflang={ a.Lang } href={ a.URL }/>
}
<meta property="og:type" content="website"/>
if m.SiteName != "" {
<meta property="og:site_name" content={ m.SiteName }/>
}
<meta property="og:title" content={ m.Title }/>
if m.Description != "" {
<meta property="og:description" content={ m.Description }/>
}
<meta property="og:url" content={ m.URL }/>
<meta property="og:locale" content={ ogLocale(m.Lang) }/>
if m.Image != "" {
<meta property="og:image" content={ m.Image }/>
<meta property="og:image:type" content="image/png"/>
<meta property="og:image:width" content={ strconv.Itoa(ogimage.Width) }/>
<meta property="og:image:height" content={ strconv.Itoa(ogimage.Height) }/>
<meta property="og:image:alt" content={ m.Title }/>
}
<meta name="twitter:card" content={ twitterCard(m) }/>
<meta name="twitter:title" content={ m.Title }/>
if m.Description != "" {
<meta name="twitter:description" content={ m.Description }/>
}
if m.Image != "" {
<meta name="twitter:image" content={ m.Image }/>
}
}
func twitterCard(m *PageMeta) string {
if m.Image != "" {
return "summary_large_image"
}
return "summary"
}
templ noIndex() {
<meta name="robots" content="noindex, nofollow"/>
}