Meta Tags Generator — Kotlin 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 Kotlin implementation — the same logic the interactive tool runs, in a shareable, citable form.
// meta-tags-generator — build the ordered SEO head-tag set: title,
// description, canonical, robots, Open Graph (ogTitle/ogDescription/ogUrl
// falling back to the base fields, og:type website whenever the block is
// present), Twitter card, theme-color. Kotlin port (canonical TS:
// src/lib/meta-tags.ts; Go twin:
// cli/meta-tags-generator/meta-tags-generator.go). Same fields, same
// fallbacks, same emission order, same warnings (title > 60,
// description > 160) and the same hard errors (missing title/description,
// non-absolute og:image, non-hex theme-color suppress emission entirely).
/** The input field set; every field defaults to "" (all-optional shape). */
data class MetaTagsInput(
val title: String = "",
val description: String = "",
val canonicalUrl: String = "",
val robots: String = "",
val ogTitle: String = "",
val ogDescription: String = "",
val ogImage: String = "",
val ogUrl: String = "",
val twitterCard: String = "",
val twitterSite: String = "",
val themeColor: String = "",
)
/** The ordered tag list plus diagnostics. */
data class MetaTagsResult(
val ok: Boolean,
val errors: List<String> = emptyList(),
val warnings: List<String> = emptyList(),
val tags: List<String> = emptyList(),
)
private val HEX = Regex("^#[0-9a-fA-F]{6}$")
private fun trim(s: String?): String = s?.trim() ?: ""
/** Build the ordered head-tag set. Blank optional fields are skipped; hard
* errors suppress emission entirely; length warnings never block emission. */
fun buildMetaTags(input: MetaTagsInput): MetaTagsResult {
val errors = mutableListOf<String>()
val title = trim(input.title)
val description = trim(input.description)
val canonical = trim(input.canonicalUrl)
val ogTitleIn = trim(input.ogTitle)
val ogTitle = ogTitleIn.ifEmpty { title }
val ogDescriptionIn = trim(input.ogDescription)
val ogDescription = ogDescriptionIn.ifEmpty { description }
val ogImage = trim(input.ogImage)
val ogUrlIn = trim(input.ogUrl)
val ogUrl = ogUrlIn.ifEmpty { canonical }
val themeColor = trim(input.themeColor)
if (title.isEmpty()) errors.add("Title is required.")
if (description.isEmpty()) errors.add("Description is required.")
if (ogImage.isNotEmpty() && !ogImage.startsWith("http://") && !ogImage.startsWith("https://")) {
errors.add("og:image must be an absolute http(s) URL.")
}
if (themeColor.isNotEmpty() && !HEX.matches(themeColor)) {
errors.add("theme-color must be a #rrggbb hex value.")
}
if (errors.isNotEmpty()) return MetaTagsResult(ok = false, errors = errors)
val warnings = mutableListOf<String>()
if (title.length > 60) {
warnings.add("Title is ${title.length} characters — search results truncate around 60.")
}
if (description.length > 160) {
warnings.add("Description is ${description.length} characters — search results truncate around 160.")
}
val tags = mutableListOf(
"<title>$title</title>",
"<meta name=\"description\" content=\"$description\" />",
)
if (canonical.isNotEmpty()) tags.add("<link rel=\"canonical\" href=\"$canonical\" />")
val robots = trim(input.robots)
if (robots.isNotEmpty()) tags.add("<meta name=\"robots\" content=\"$robots\" />")
val hasOg = ogTitleIn.isNotEmpty() || ogDescriptionIn.isNotEmpty() || ogImage.isNotEmpty() || ogUrl.isNotEmpty()
if (hasOg) {
tags.add("<meta property=\"og:title\" content=\"$ogTitle\" />")
tags.add("<meta property=\"og:description\" content=\"$ogDescription\" />")
if (ogImage.isNotEmpty()) tags.add("<meta property=\"og:image\" content=\"$ogImage\" />")
if (ogUrl.isNotEmpty()) tags.add("<meta property=\"og:url\" content=\"$ogUrl\" />")
tags.add("<meta property=\"og:type\" content=\"website\" />")
}
if (input.twitterCard.isNotEmpty()) tags.add("<meta name=\"twitter:card\" content=\"${input.twitterCard}\" />")
val twitterSite = trim(input.twitterSite)
if (twitterSite.isNotEmpty()) tags.add("<meta name=\"twitter:site\" content=\"$twitterSite\" />")
if (themeColor.isNotEmpty()) tags.add("<meta name=\"theme-color\" content=\"$themeColor\" />")
return MetaTagsResult(ok = true, 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 →