28 lines
776 B
TypeScript
28 lines
776 B
TypeScript
import { env } from "@/env/server"
|
|
|
|
import type { BreadcrumbList, ListItem, WithContext } from "schema-dts"
|
|
|
|
import type { Breadcrumbs } from "@/types/trpc/routers/contentstack/breadcrumbs"
|
|
|
|
export function generateBreadcrumbsSchema(breadcrumbs: Breadcrumbs) {
|
|
const itemListElement: ListItem[] = breadcrumbs.map((item, index) => ({
|
|
"@type": "ListItem",
|
|
position: index + 1,
|
|
name: item.title,
|
|
// Only include "item" if "href" exists; otherwise, omit it
|
|
...(item.href ? { item: `${env.PUBLIC_URL}${item.href}` } : {}),
|
|
}))
|
|
|
|
const jsonLd: WithContext<BreadcrumbList> = {
|
|
"@context": "https://schema.org",
|
|
"@type": "BreadcrumbList",
|
|
itemListElement,
|
|
}
|
|
|
|
return {
|
|
key: "breadcrumbs",
|
|
type: "application/ld+json",
|
|
jsonLd,
|
|
}
|
|
}
|