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 != "" {
}
for _, a := range m.Alternates {
}
if m.SiteName != "" {
}
if m.Description != "" {
}
if m.Image != "" {
}
if m.Description != "" {
}
if m.Image != "" {
}
}
func twitterCard(m *PageMeta) string {
if m.Image != "" {
return "summary_large_image"
}
return "summary"
}
templ noIndex() {
}