Skip to content

Meta Tags Generator — Zig 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 Zig implementation — the same logic the interactive tool runs, in a shareable, citable form.

// meta-tags-generator — Zig port: build the ordered SEO head-tag set: title,
// description, canonical, robots, Open Graph (og_title/og_description/og_url
// falling back to the base fields, og:type website whenever the block is
// present), Twitter card, theme-color. Zig (0.15+), standard library only.
// Port of src/lib/meta-tags.ts — same logic as this dir's typescript.ts and
// the live Go twin at 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).
const std = @import("std");

/// The input field set; the default field values are the all-optional shape.
pub const Input = struct {
    title: []const u8 = "",
    description: []const u8 = "",
    canonical_url: []const u8 = "",
    robots: []const u8 = "",
    og_title: []const u8 = "",
    og_description: []const u8 = "",
    og_image: []const u8 = "",
    og_url: []const u8 = "",
    twitter_card: []const u8 = "",
    twitter_site: []const u8 = "",
    theme_color: []const u8 = "",
};

/// The ordered tag list plus diagnostics. The slices are allocated from
/// `alloc` and owned by the caller.
pub const Result = struct {
    ok: bool,
    errors: []const []const u8 = &.{},
    warnings: []const []const u8 = &.{},
    tags: []const []const u8 = &.{},
};

// Hard-error / warning / tag counts are bounded by the contract, so the
// result slices are carved out of fixed-capacity heap buffers.
const max_errors = 4;
const max_warnings = 2;
const max_tags = 12;

fn trimmed(s: []const u8) []const u8 {
    return std.mem.trim(u8, s, " \t\r\n\x0b\x0c");
}

fn isHexColor(s: []const u8) bool {
    if (s.len != 7 or s[0] != '#') return false;
    for (s[1..]) |c| {
        if (!std.ascii.isHex(c)) return false;
    }
    return true;
}

fn isAbsoluteHttp(s: []const u8) bool {
    return std.mem.startsWith(u8, s, "http://") or std.mem.startsWith(u8, s, "https://");
}

/// Build the ordered head-tag set. Blank optional fields are skipped; hard
/// errors suppress emission entirely; length warnings never block emission.
pub fn buildMetaTags(alloc: std.mem.Allocator, in: Input) std.mem.Allocator.Error!Result {
    const err_buf = try alloc.alloc([]const u8, max_errors);
    var nerr: usize = 0;
    const warn_buf = try alloc.alloc([]const u8, max_warnings);
    var nwarn: usize = 0;
    const tag_buf = try alloc.alloc([]const u8, max_tags);
    var ntag: usize = 0;

    const title = trimmed(in.title);
    const description = trimmed(in.description);
    const canonical = trimmed(in.canonical_url);
    const og_title_in = trimmed(in.og_title);
    const og_title = if (og_title_in.len == 0) title else og_title_in;
    const og_description_in = trimmed(in.og_description);
    const og_description = if (og_description_in.len == 0) description else og_description_in;
    const og_image = trimmed(in.og_image);
    const og_url_in = trimmed(in.og_url);
    const og_url = if (og_url_in.len == 0) canonical else og_url_in;
    const theme_color = trimmed(in.theme_color);

    if (title.len == 0) {
        err_buf[nerr] = "Title is required.";
        nerr += 1;
    }
    if (description.len == 0) {
        err_buf[nerr] = "Description is required.";
        nerr += 1;
    }
    if (og_image.len != 0 and !isAbsoluteHttp(og_image)) {
        err_buf[nerr] = "og:image must be an absolute http(s) URL.";
        nerr += 1;
    }
    if (theme_color.len != 0 and !isHexColor(theme_color)) {
        err_buf[nerr] = "theme-color must be a #rrggbb hex value.";
        nerr += 1;
    }
    if (nerr > 0) {
        return Result{ .ok = false, .errors = err_buf[0..nerr] };
    }

    // Length warnings — never block emission.
    if (title.len > 60) {
        warn_buf[nwarn] = try std.fmt.allocPrint(
            alloc,
            "Title is {d} characters — search results truncate around 60.",
            .{title.len},
        );
        nwarn += 1;
    }
    if (description.len > 160) {
        warn_buf[nwarn] = try std.fmt.allocPrint(
            alloc,
            "Description is {d} characters — search results truncate around 160.",
            .{description.len},
        );
        nwarn += 1;
    }

    tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<title>{s}</title>", .{title});
    ntag += 1;
    tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<meta name=\"description\" content=\"{s}\" />", .{description});
    ntag += 1;
    if (canonical.len != 0) {
        tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<link rel=\"canonical\" href=\"{s}\" />", .{canonical});
        ntag += 1;
    }
    const robots = trimmed(in.robots);
    if (robots.len != 0) {
        tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<meta name=\"robots\" content=\"{s}\" />", .{robots});
        ntag += 1;
    }

    const has_og = og_title_in.len != 0 or og_description_in.len != 0 or og_image.len != 0 or og_url.len != 0;
    if (has_og) {
        tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<meta property=\"og:title\" content=\"{s}\" />", .{og_title});
        ntag += 1;
        tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<meta property=\"og:description\" content=\"{s}\" />", .{og_description});
        ntag += 1;
        if (og_image.len != 0) {
            tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<meta property=\"og:image\" content=\"{s}\" />", .{og_image});
            ntag += 1;
        }
        if (og_url.len != 0) {
            tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<meta property=\"og:url\" content=\"{s}\" />", .{og_url});
            ntag += 1;
        }
        tag_buf[ntag] = "<meta property=\"og:type\" content=\"website\" />";
        ntag += 1;
    }

    if (in.twitter_card.len != 0) {
        tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<meta name=\"twitter:card\" content=\"{s}\" />", .{in.twitter_card});
        ntag += 1;
    }
    const twitter_site = trimmed(in.twitter_site);
    if (twitter_site.len != 0) {
        tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<meta name=\"twitter:site\" content=\"{s}\" />", .{twitter_site});
        ntag += 1;
    }
    if (theme_color.len != 0) {
        tag_buf[ntag] = try std.fmt.allocPrint(alloc, "<meta name=\"theme-color\" content=\"{s}\" />", .{theme_color});
        ntag += 1;
    }

    return Result{ .ok = true, .warnings = warn_buf[0..nwarn], .tags = tag_buf[0..ntag] };
}

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 →