Files

82 lines
2.8 KiB
Go

package handlers
// PageMeta holds all metadata for a page's <head> section.
// Use the builder methods to construct it with a fluent API.
type PageMeta struct {
Title string // Page title (required; empty falls back to SiteName in template)
Description string // Meta description (required; empty omits the tag entirely)
Canonical string // Canonical URL (optional)
Robots string // Robots directive, e.g. "noindex" (optional, defaults to "index, follow")
OGType string // OpenGraph type, defaults to "website"
OGImage string // OpenGraph image URL (optional)
OGImageAlt string // OpenGraph image alt text — improves social-share a11y
OGLocale string // OpenGraph locale (e.g. "en_US"); blank falls back in template
TwitterCard string // Twitter card type, defaults to "summary_large_image"
SiteName string // Site name for og:site_name (falls back to "ATCR" in template)
JSONLD []any // JSON-LD structured data objects (optional)
}
// NewPageMeta creates a new PageMeta with required fields and sensible defaults.
// Callers should not pass empty title/description — the template falls back to
// the SiteName for missing title and omits missing description, but those are
// last-resort defenses.
func NewPageMeta(title, description string) *PageMeta {
return &PageMeta{
Title: title,
Description: description,
OGType: "website",
TwitterCard: "summary_large_image",
}
}
// WithCanonical sets the canonical URL.
func (m *PageMeta) WithCanonical(url string) *PageMeta {
m.Canonical = url
return m
}
// WithOGImage sets the OpenGraph image URL.
func (m *PageMeta) WithOGImage(url string) *PageMeta {
m.OGImage = url
return m
}
// WithOGImageAlt sets the alt text for the OpenGraph image. Strongly recommended
// when OGImage is set — screen readers on social platforms read this out.
func (m *PageMeta) WithOGImageAlt(alt string) *PageMeta {
m.OGImageAlt = alt
return m
}
// WithOGLocale overrides the default "en_US" locale.
func (m *PageMeta) WithOGLocale(locale string) *PageMeta {
m.OGLocale = locale
return m
}
// WithOGType sets the OpenGraph type (e.g., "website", "profile", "article").
func (m *PageMeta) WithOGType(ogType string) *PageMeta {
m.OGType = ogType
return m
}
// WithRobots sets the robots meta directive (e.g., "noindex").
func (m *PageMeta) WithRobots(robots string) *PageMeta {
m.Robots = robots
return m
}
// WithJSONLD sets the JSON-LD structured data objects.
func (m *PageMeta) WithJSONLD(data ...any) *PageMeta {
m.JSONLD = data
return m
}
// WithSiteName sets the site name for og:site_name. Pass the caller's
// ClientShortName — forgetting this on a branded deployment (e.g. Seamark)
// leaks "ATCR" into social previews.
func (m *PageMeta) WithSiteName(name string) *PageMeta {
m.SiteName = name
return m
}