Meta Tags Generator — JavaScript 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 JavaScript 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 (og:title/og:description/og:url
// falling back to the base fields, og:type website whenever the block is
// present), Twitter card, theme-color. Plain-JS twin of src/lib/meta-tags.ts:
// 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).
/**
* Build the ordered head-tag set. Blank optional fields are skipped; hard
* errors suppress emission entirely; length warnings never block emission.
*/
export function buildMetaTags(input) {
const errors = [];
const warnings = [];
const orEmpty = (v) => (typeof v === 'string' ? v.trim() : '');
const title = orEmpty(input.title);
const description = orEmpty(input.description);
const canonical = orEmpty(input.canonicalUrl);
const ogTitle = orEmpty(input.ogTitle) || title;
const ogDescription = orEmpty(input.ogDescription) || description;
const ogImage = orEmpty(input.ogImage);
const ogUrl = orEmpty(input.ogUrl) || canonical;
const themeColor = orEmpty(input.themeColor);
if (!title) errors.push('Title is required.');
if (!description) errors.push('Description is required.');
if (ogImage && !/^https?:\/\//.test(ogImage)) errors.push('og:image must be an absolute http(s) URL.');
if (themeColor && !/^#[0-9a-fA-F]{6}$/.test(themeColor)) errors.push('theme-color must be a #rrggbb hex value.');
if (errors.length > 0) return { ok: false, errors, warnings: [], tags: [] };
if (title.length > 60) warnings.push(`Title is ${title.length} characters — search results truncate around 60.`);
if (description.length > 160) warnings.push(`Description is ${description.length} characters — search results truncate around 160.`);
const tags = [
`<title>${title}</title>`,
`<meta name="description" content="${description}" />`,
];
if (canonical) tags.push(`<link rel="canonical" href="${canonical}" />`);
if (orEmpty(input.robots)) tags.push(`<meta name="robots" content="${orEmpty(input.robots)}" />`);
const hasOg = !!(orEmpty(input.ogTitle) || orEmpty(input.ogDescription) || ogImage || ogUrl);
if (hasOg) {
tags.push(`<meta property="og:title" content="${ogTitle}" />`);
tags.push(`<meta property="og:description" content="${ogDescription}" />`);
if (ogImage) tags.push(`<meta property="og:image" content="${ogImage}" />`);
if (ogUrl) tags.push(`<meta property="og:url" content="${ogUrl}" />`);
tags.push('<meta property="og:type" content="website" />');
}
if (input.twitterCard) tags.push(`<meta name="twitter:card" content="${input.twitterCard}" />`);
if (orEmpty(input.twitterSite)) tags.push(`<meta name="twitter:site" content="${orEmpty(input.twitterSite)}" />`);
if (themeColor) tags.push(`<meta name="theme-color" content="${themeColor}" />`);
return { ok: true, errors, warnings, 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 →