Something went wrong. Try again.
Mirror of https://github.com/kualta/site — web infrastructure
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148---import type { Article } from "@/layouts/BaseLayout.astro";import type { OgImage } from "@/lib/seo";import { SAME_AS, SITE_NAME, TAGLINE } from "@/lib/seo";
interface Props { title: string; documentTitle: string; description: string; canonical: string; ogImage: OgImage; type: "website" | "article"; article?: Article; schema?: object[];}
const { title, documentTitle, description, canonical, ogImage, type, article, schema = [] } = Astro.props;
const SITE_URL = Astro.site?.toString().replace(/\/$/, "") ?? "https://kualta.dev";const path = new URL(canonical).pathname.replace(/\/$/, "");const isHome = path === "" || path === "/";const isPostsIndex = path === "/posts";const isArticle = type === "article";
const person = { "@type": "Person", "@id": `${SITE_URL}/#person`, name: SITE_NAME, url: `${SITE_URL}/`, description: TAGLINE, sameAs: SAME_AS,};
/** the blog is one entity across the site, so posts can point back at it */const blog = { "@type": "Blog", "@id": `${SITE_URL}/posts#blog`, url: `${SITE_URL}/posts`, name: `${SITE_NAME} - posts`, publisher: { "@id": `${SITE_URL}/#person` }, inLanguage: "en-US",};
const website = { "@type": "WebSite", "@id": `${SITE_URL}/#website`, url: `${SITE_URL}/`, name: SITE_NAME, description: TAGLINE, publisher: { "@id": `${SITE_URL}/#person` }, inLanguage: "en-US",};
/** the share card, described well enough for Google to reuse it */const primaryImage = { "@type": "ImageObject", "@id": `${canonical}#primaryimage`, url: ogImage.url, contentUrl: ogImage.url, ...(ogImage.width ? { width: ogImage.width } : {}), ...(ogImage.height ? { height: ogImage.height } : {}),};
/** * Every page earns a trail, built from its own path. The posts, tag and music * pages all sit two or three levels down and never said so. */const LABELS: Record<string, string> = { posts: "posts", projects: "projects", music: "music", contacts: "contacts", join: "join", kaomoji: "kaomoji", api: "api", tags: "tags",};
const segments = path.split("/").filter(Boolean);const crumbs = [{ name: SITE_NAME, item: `${SITE_URL}/` }];segments.forEach((segment, index) => { const isLast = index === segments.length - 1; const trail = `${SITE_URL}/${segments.slice(0, index + 1).join("/")}`; const label = LABELS[segment] ?? (isLast ? title : segment.replace(/-/g, " ")); crumbs.push({ name: label, item: trail });});
const breadcrumb = { "@type": "BreadcrumbList", "@id": `${canonical}#breadcrumb`, itemListElement: crumbs.map((crumb, index) => ({ "@type": "ListItem", position: index + 1, name: crumb.name, item: crumb.item, })),};
const webPageType = isArticle ? "WebPage" : isHome ? "WebPage" : "CollectionPage";
const page = { "@type": webPageType, "@id": canonical, url: canonical, name: documentTitle, description, isPartOf: { "@id": `${SITE_URL}/#website` }, primaryImageOfPage: { "@id": `${canonical}#primaryimage` }, ...(crumbs.length > 1 ? { breadcrumb: { "@id": `${canonical}#breadcrumb` } } : {}), ...(isHome ? { about: { "@id": `${SITE_URL}/#person` } } : {}), ...(isPostsIndex ? { mainEntity: { "@id": `${SITE_URL}/posts#blog` } } : {}), inLanguage: "en-US",};
const graph: object[] = [person, website, primaryImage, page];
// posts and the index both reference the blog, so it has to exist in both graphsif (isPostsIndex || isArticle) graph.push(blog);
if (crumbs.length > 1) graph.push(breadcrumb);
if (isArticle && article) { graph.push({ "@type": "BlogPosting", "@id": `${canonical}#article`, url: canonical, headline: title, description, image: { "@id": `${canonical}#primaryimage` }, datePublished: article.publishedTime, dateModified: article.modifiedTime ?? article.publishedTime, author: { "@id": `${SITE_URL}/#person` }, publisher: { "@id": `${SITE_URL}/#person` }, mainEntityOfPage: { "@id": canonical }, isPartOf: { "@id": `${SITE_URL}/posts#blog` }, ...(article.tags?.length ? { keywords: article.tags.join(", ") } : {}), ...(article.section ? { articleSection: article.section } : {}), ...(article.wordCount ? { wordCount: article.wordCount } : {}), inLanguage: "en-US", });}
graph.push(...schema);
const jsonLd = { "@context": "https://schema.org", "@graph": graph };---<script type="application/ld+json" set:html={JSON.stringify(jsonLd)} />