studiocms
Version:
Astro Native CMS for AstroDB. Built from the ground up by the Astro community.
78 lines (77 loc) • 2.8 kB
JavaScript
import { extname } from "node:path";
import version from "studiocms:version";
import { lookup } from "mrmime";
import { StudioCMSCoreError } from "../../errors.js";
import { HeadConfigSchema } from "./head.js";
const _schema = HeadConfigSchema;
const faviconTypes = [".ico", ".gif", ".jpeg", ".jpg", ".png", ".svg"];
function isFaviconExt(ext) {
if (faviconTypes.includes(ext)) {
return true;
}
return false;
}
const makeFavicon = (favicon) => {
const ext = extname(favicon).toLocaleLowerCase();
if (isFaviconExt(ext)) {
const faviconHref = favicon;
const faviconType = lookup(ext);
return { href: faviconHref, type: faviconType };
}
throw new StudioCMSCoreError(
`Unsupported favicon extension: ${ext}`,
`The favicon must be one of the following types: ${faviconTypes.join(", ")}`
);
};
const headDefaults = (title, description, lang, Astro, favicon, ogImage, canonical) => {
const headDefaults2 = [
{ tag: "meta", attrs: { charset: "utf-8" } },
{
tag: "meta",
attrs: { name: "viewport", content: "width=device-width, initial-scale=1" }
},
{ tag: "title", content: `${title}` },
{ tag: "meta", attrs: { name: "title", content: title } },
{ tag: "meta", attrs: { name: "description", content: description } },
{ tag: "link", attrs: { rel: "canonical", href: canonical?.href } },
{ tag: "meta", attrs: { name: "generator", content: Astro.generator } },
{
tag: "meta",
attrs: { name: "generator", content: `StudioCMS v${version}` }
},
// Favicon
{
tag: "link",
attrs: {
rel: "shortcut icon",
href: makeFavicon(favicon).href,
type: makeFavicon(favicon).type
}
},
// OpenGraph Tags
{ tag: "meta", attrs: { property: "og:title", content: title } },
{ tag: "meta", attrs: { property: "og:type", content: "website" } },
{ tag: "meta", attrs: { property: "og:url", content: canonical?.href } },
{ tag: "meta", attrs: { property: "og:locale", content: lang } },
{ tag: "meta", attrs: { property: "og:description", content: description } },
{ tag: "meta", attrs: { property: "og:site_name", content: title } },
// Twitter Tags
{
tag: "meta",
attrs: { name: "twitter:card", content: "summary_large_image" }
},
{ tag: "meta", attrs: { name: "twitter:url", content: canonical?.href } },
{ tag: "meta", attrs: { name: "twitter:title", content: title } },
{ tag: "meta", attrs: { name: "twitter:description", content: description } }
];
if (ogImage) {
headDefaults2.push(
{ tag: "meta", attrs: { property: "og:image", content: ogImage } },
{ tag: "meta", attrs: { name: "twitter:image", content: ogImage } }
);
}
return headDefaults2;
};
export {
headDefaults
};