Files
crosscode-platform/packages/seo/src/church.ts
T
joshuaandClaude Opus 5.5 390b80e56e packages/seo: schema.org JSON-LD and meta helpers on @crosscode/contracts
eventJsonLd (crosscode.event/1), churchJsonLd (crosscode.church/1),
canonicalUrl, pageMeta and jsonLdString (ROADMAP-v3 §11).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 12:01:20 +02:00

79 lines
3.1 KiB
TypeScript

import type { Address, Church } from "@crosscode/contracts";
/**
* schema.org `Church` JSON-LD (an `Organization` and a `Place`) from a `crosscode.church/1`, for
* a church's home and contact pages, so it shows up in local search and maps (ROADMAP-v3 §11).
*/
type JsonLd = Record<string, unknown>;
const DAYS: Record<string, string> = {
monday: "Monday",
tuesday: "Tuesday",
wednesday: "Wednesday",
thursday: "Thursday",
friday: "Friday",
saturday: "Saturday",
sunday: "Sunday",
};
function postalAddress(a: Address): JsonLd {
return {
"@type": "PostalAddress",
streetAddress: [a.line1, a.line2].filter(Boolean).join(", "),
...(a.suburb || a.city ? { addressLocality: a.suburb ? `${a.suburb}, ${a.city}` : a.city } : {}),
...(a.province ? { addressRegion: a.province } : {}),
...(a.postalCode ? { postalCode: a.postalCode } : {}),
addressCountry: a.country,
};
}
function geo(a: Address | null | undefined): JsonLd | undefined {
return a?.geo ? { "@type": "GeoCoordinates", latitude: a.geo.lat, longitude: a.geo.lng } : undefined;
}
export interface ChurchJsonLdOptions {
/** The page's own address (the church's home page by default: `links.website`). */
url?: string;
}
export function churchJsonLd(church: Church, opts: ChurchJsonLdOptions = {}): JsonLd {
// The church's main address: its contact address, else its only (or first) campus's.
const main = church.contact.address ?? church.campuses.find((c) => c.address)?.address ?? null;
const url = opts.url ?? church.links.website ?? church.links.events ?? undefined;
const mainGeo = geo(main);
const services = church.serviceTimes.filter((s) => !s.campus || church.campuses.length <= 1);
return {
"@context": "https://schema.org",
"@type": "Church",
name: church.name,
...(church.shortName ? { alternateName: church.shortName } : {}),
...(url ? { url } : {}),
...(church.branding.logoUrl ? { logo: church.branding.logoUrl, image: church.branding.logoUrl } : {}),
...(church.contact.email ? { email: church.contact.email } : {}),
...(church.contact.phone ? { telephone: church.contact.phone } : {}),
...(main ? { address: postalAddress(main) } : {}),
...(mainGeo ? { geo: mainGeo } : {}),
...(church.social.length > 0 ? { sameAs: church.social.map((s) => s.url) } : {}),
// Service times: when the doors are open, as Google reads it for a place.
...(services.length > 0
? { openingHoursSpecification: services.map((s) => ({ "@type": "OpeningHoursSpecification", dayOfWeek: `https://schema.org/${DAYS[s.day]}`, opens: s.time, ...(s.label ? { name: s.label } : {}) })) }
: {}),
// Several campuses: each one a place of its own.
...(church.campuses.length > 1
? {
department: church.campuses.map((c) => {
const g = geo(c.address);
return {
"@type": "Church",
name: `${church.name} ${c.name}`,
...(c.address ? { address: postalAddress(c.address) } : {}),
...(g ? { geo: g } : {}),
};
}),
}
: {}),
};
}