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 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"/> }