Meta Tags Generator — Go source
Build the SEO head tags for any page — title, description, canonical, robots, Open Graph with smart fallbacks, Twitter card and theme-color — with live length warnings.
This is the Go implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Package metatagsgenerator is the Go twin of CosmoDev's src/lib/meta-tags.ts
// (dual source: the web lib is TypeScript, the CLI lib is Go — kept in
// lock-step). Pure + deterministic, never panics. The table-driven tests in
// meta-tags-generator_test.go share vectors with src/lib/meta-tags.test.ts so
// the two implementations are held to the same contract.
//
// Behaviour mirrors the TS lib exactly: build the ordered head-tag set (title,
// description, canonical, robots, Open Graph, Twitter card, theme-color).
// Blank optional fields are skipped; hard errors (missing title/description,
// non-absolute og:image, non-hex theme-color) suppress emission entirely.
// Length warnings never block emission.
package metatagsgenerator
import (
"regexp"
"strconv"
"strings"
"unicode/utf8"
)
// Input is the field set for BuildMetaTags. The zero value (Input{}) matches
// the TS lib's all-optional-fields-absent shape: every field is treated as
// blank until it trims to something non-empty.
type Input struct {
Title string
Description string
CanonicalURL string
Robots string
OgTitle string
OgDescription string
OgImage string
OgURL string
// TwitterCard is "summary" or "summary_large_image" when set; any
// non-empty value is emitted verbatim (matching the TS pass-through).
TwitterCard string
TwitterSite string
ThemeColor string
}
// Result is the ordered tag list plus diagnostics.
type Result struct {
Ok bool
Errors []string
Warnings []string
Tags []string
}
var (
hexColor = regexp.MustCompile(`^#[0-9a-fA-F]{6}$`)
absHTTPRe = regexp.MustCompile(`^https?://`)
)
// trim returns s with surrounding whitespace removed ("" for nil-ish blanks,
// mirroring the TS `?.trim() ?? ”` pattern).
func trim(s string) string { return strings.TrimSpace(s) }
// BuildMetaTags builds the ordered head-tag set: title, description,
// canonical, robots, Open Graph (with title/description/url falling back to
// the base fields, og:type website emitted whenever the block is present),
// Twitter card, theme-color. It is the Go twin of buildMetaTags() in
// src/lib/meta-tags.ts and must agree with it on every shared vector.
func BuildMetaTags(in Input) Result {
var errors, warnings []string
title := trim(in.Title)
description := trim(in.Description)
canonical := trim(in.CanonicalURL)
ogTitle := trim(in.OgTitle)
if ogTitle == "" {
ogTitle = title
}
ogDescription := trim(in.OgDescription)
if ogDescription == "" {
ogDescription = description
}
ogImage := trim(in.OgImage)
ogURL := trim(in.OgURL)
if ogURL == "" {
ogURL = canonical
}
themeColor := trim(in.ThemeColor)
if title == "" {
errors = append(errors, "Title is required.")
}
if description == "" {
errors = append(errors, "Description is required.")
}
if ogImage != "" && !absHTTPRe.MatchString(ogImage) {
errors = append(errors, "og:image must be an absolute http(s) URL.")
}
if themeColor != "" && !hexColor.MatchString(themeColor) {
errors = append(errors, "theme-color must be a #rrggbb hex value.")
}
if len(errors) > 0 {
return Result{Ok: false, Errors: errors, Warnings: nil, Tags: nil}
}
// Length warnings (rune count ≈ the TS lib's string length); never block.
if n := utf8.RuneCountInString(title); n > 60 {
warnings = append(warnings,
"Title is "+strconv.Itoa(n)+" characters — search results truncate around 60.")
}
if n := utf8.RuneCountInString(description); n > 160 {
warnings = append(warnings,
"Description is "+strconv.Itoa(n)+" characters — search results truncate around 160.")
}
tags := []string{
"<title>" + title + "</title>",
`<meta name="description" content="` + description + `" />`,
}
if canonical != "" {
tags = append(tags, `<link rel="canonical" href="`+canonical+`" />`)
}
if robots := trim(in.Robots); robots != "" {
tags = append(tags, `<meta name="robots" content="`+robots+`" />`)
}
hasOg := trim(in.OgTitle) != "" || trim(in.OgDescription) != "" || ogImage != "" || ogURL != ""
if hasOg {
tags = append(tags,
`<meta property="og:title" content="`+ogTitle+`" />`,
`<meta property="og:description" content="`+ogDescription+`" />`)
if ogImage != "" {
tags = append(tags, `<meta property="og:image" content="`+ogImage+`" />`)
}
if ogURL != "" {
tags = append(tags, `<meta property="og:url" content="`+ogURL+`" />`)
}
tags = append(tags, `<meta property="og:type" content="website" />`)
}
if in.TwitterCard != "" {
tags = append(tags, `<meta name="twitter:card" content="`+in.TwitterCard+`" />`)
}
if site := trim(in.TwitterSite); site != "" {
tags = append(tags, `<meta name="twitter:site" content="`+site+`" />`)
}
if themeColor != "" {
tags = append(tags, `<meta name="theme-color" content="`+themeColor+`" />`)
}
return Result{Ok: true, Errors: nil, Warnings: warnings, Tags: tags}
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →