Skip to content

Meta Tags Generator — Swift 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 Swift 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.
//
// Language: Swift (5.9+), Foundation only
// CosmoDev polyglot showcase port of the `meta-tags-generator` tool.
// Ported from src/lib/meta-tags.ts — display source, part of CosmoDev's
// polyglot tool pages.

import Foundation

/// The input field set; every field defaults to "" (all-optional shape).
struct MetaTagsInput {
    var title = ""
    var description = ""
    var canonicalURL = ""
    var robots = ""
    var ogTitle = ""
    var ogDescription = ""
    var ogImage = ""
    var ogURL = ""
    var twitterCard = ""
    var twitterSite = ""
    var themeColor = ""
}

/// The ordered tag list plus diagnostics.
struct MetaTagsResult {
    var ok: Bool
    var errors: [String] = []
    var warnings: [String] = []
    var tags: [String] = []
}

private func trim(_ s: String) -> String {
    s.trimmingCharacters(in: .whitespacesAndNewlines)
}

/// Build the ordered head-tag set. Blank optional fields are skipped; hard
/// errors suppress emission entirely; length warnings never block emission.
func buildMetaTags(_ input: MetaTagsInput) -> MetaTagsResult {
    var errors: [String] = []
    var warnings: [String] = []

    let title = trim(input.title)
    let description = trim(input.description)
    let canonical = trim(input.canonicalURL)
    let ogTitleIn = trim(input.ogTitle)
    let ogTitle = ogTitleIn.isEmpty ? title : ogTitleIn
    let ogDescriptionIn = trim(input.ogDescription)
    let ogDescription = ogDescriptionIn.isEmpty ? description : ogDescriptionIn
    let ogImage = trim(input.ogImage)
    let ogURLIn = trim(input.ogURL)
    let ogURL = ogURLIn.isEmpty ? canonical : ogURLIn
    let themeColor = trim(input.themeColor)

    if title.isEmpty { errors.append("Title is required.") }
    if description.isEmpty { errors.append("Description is required.") }
    if !ogImage.isEmpty && !ogImage.hasPrefix("http://") && !ogImage.hasPrefix("https://") {
        errors.append("og:image must be an absolute http(s) URL.")
    }
    if !themeColor.isEmpty && themeColor.range(of: "^#[0-9a-fA-F]{6}$", options: .regularExpression) == nil {
        errors.append("theme-color must be a #rrggbb hex value.")
    }
    if !errors.isEmpty {
        return MetaTagsResult(ok: false, errors: errors)
    }

    if title.count > 60 {
        warnings.append("Title is \(title.count) characters — search results truncate around 60.")
    }
    if description.count > 160 {
        warnings.append("Description is \(description.count) characters — search results truncate around 160.")
    }

    var tags: [String] = [
        "<title>\(title)</title>",
        "<meta name=\"description\" content=\"\(description)\" />",
    ]
    if !canonical.isEmpty { tags.append("<link rel=\"canonical\" href=\"\(canonical)\" />") }
    let robots = trim(input.robots)
    if !robots.isEmpty { tags.append("<meta name=\"robots\" content=\"\(robots)\" />") }

    let hasOg = !ogTitleIn.isEmpty || !ogDescriptionIn.isEmpty || !ogImage.isEmpty || !ogURL.isEmpty
    if hasOg {
        tags.append("<meta property=\"og:title\" content=\"\(ogTitle)\" />")
        tags.append("<meta property=\"og:description\" content=\"\(ogDescription)\" />")
        if !ogImage.isEmpty { tags.append("<meta property=\"og:image\" content=\"\(ogImage)\" />") }
        if !ogURL.isEmpty { tags.append("<meta property=\"og:url\" content=\"\(ogURL)\" />") }
        tags.append("<meta property=\"og:type\" content=\"website\" />")
    }

    if !input.twitterCard.isEmpty { tags.append("<meta name=\"twitter:card\" content=\"\(input.twitterCard)\" />") }
    let twitterSite = trim(input.twitterSite)
    if !twitterSite.isEmpty { tags.append("<meta name=\"twitter:site\" content=\"\(twitterSite)\" />") }
    if !themeColor.isEmpty { tags.append("<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 →