Files
crosscode-platform/packages/seo/src/event.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

115 lines
4.9 KiB
TypeScript

import type { CrosscodeEvent } from "@crosscode/contracts";
/**
* schema.org `Event` JSON-LD from a `crosscode.event/1`, the same for every product (ROADMAP-v3
* §11), so Google can show the event with its date, venue and ticket link. Checked with
* https://search.google.com/test/rich-results.
*
* Only the **origin** of an event puts this on its page. An imported copy has a canonical link to
* the origin instead and no JSON-LD, so search engines see one event, not one per product.
*/
export interface Organizer {
name: string;
url: string;
}
export interface EventJsonLdOptions {
/** The page this JSON-LD is on. Defaults to `event.origin.url`. */
pageUrl?: string;
/** The church (or the site's organisation) running the event. */
organizer?: Organizer;
/**
* The origin's own price range, in cents. `crosscode.event/1` only carries the lowest price,
* but the origin knows the highest too, and Google shows a range (`AggregateOffer`).
*/
priceRange?: { lowCents: number; highCents: number };
/** Where the ticket offer points. Defaults to `registration.url`. */
offerUrl?: string;
/** When tickets went (or go) on sale. Defaults to `registration.opensAt`. */
validFrom?: string | null;
}
type JsonLd = Record<string, unknown>;
const SCHEMA = "https://schema.org/";
const rand = (cents: number) => (cents / 100).toFixed(2);
function attendanceMode(event: CrosscodeEvent): string {
const online = Boolean(event.location?.onlineUrl);
const inPerson = Boolean(event.location?.name || event.location?.address);
if (online && inPerson) return `${SCHEMA}MixedEventAttendanceMode`;
if (online) return `${SCHEMA}OnlineEventAttendanceMode`;
return `${SCHEMA}OfflineEventAttendanceMode`;
}
function location(event: CrosscodeEvent): JsonLd | JsonLd[] | undefined {
const l = event.location;
if (!l) return undefined;
const places: JsonLd[] = [];
if (l.name || l.address) {
// Google wants an address; an event with only a place name uses the name for both.
places.push({ "@type": "Place", name: l.name ?? l.address, address: l.address ?? l.name });
}
if (l.onlineUrl) places.push({ "@type": "VirtualLocation", url: l.onlineUrl });
if (places.length === 0) return undefined;
return places.length === 1 ? places[0] : places;
}
function offers(event: CrosscodeEvent, opts: EventJsonLdOptions): JsonLd | undefined {
const r = event.registration;
// Contact-only and no-registration events have no tickets to offer.
if (r.mode === "contact" || r.mode === "none") return undefined;
// An external link without a price has nothing Google can show as an offer.
if (r.mode === "external" && !r.priceFrom && !opts.priceRange) return undefined;
const low = opts.priceRange?.lowCents ?? r.priceFrom?.amountCents ?? 0;
const high = opts.priceRange?.highCents ?? low;
const soldOut = r.status === "full" || r.status === "waitlist";
const validFrom = opts.validFrom === undefined ? r.opensAt : opts.validFrom;
const url = opts.offerUrl ?? r.url;
return {
"@type": low === high ? "Offer" : "AggregateOffer",
...(low === high ? { price: rand(low) } : { lowPrice: rand(low), highPrice: rand(high) }),
priceCurrency: r.priceFrom?.currency ?? "ZAR",
availability: soldOut ? `${SCHEMA}SoldOut` : `${SCHEMA}InStock`,
...(url ? { url } : {}),
...(validFrom ? { validFrom: new Date(validFrom).toISOString() } : {}),
};
}
export function eventJsonLd(event: CrosscodeEvent, opts: EventJsonLdOptions = {}): JsonLd {
const live = event.sessions.filter((s) => !s.cancelled);
const offer = offers(event, opts);
const place = location(event);
return {
"@context": "https://schema.org",
"@type": "Event",
name: event.title,
...(event.description ? { description: event.description.slice(0, 5000) } : event.summary ? { description: event.summary } : {}),
startDate: new Date(event.startsAt).toISOString(),
endDate: new Date(event.endsAt).toISOString(),
eventStatus: event.status === "cancelled" ? `${SCHEMA}EventCancelled` : `${SCHEMA}EventScheduled`,
eventAttendanceMode: attendanceMode(event),
...(place ? { location: place } : {}),
...(event.imageUrl ? { image: [event.imageUrl] } : {}),
url: opts.pageUrl ?? event.origin.url,
...(opts.organizer ? { organizer: { "@type": "Organization", name: opts.organizer.name, url: opts.organizer.url } } : {}),
...(offer ? { offers: offer } : {}),
// A multi-day event: each date as a sub-event, so Google can list the dates.
...(live.length > 1
? {
subEvent: live.map((s) => ({
"@type": "Event",
name: s.label ? `${event.title}: ${s.label}` : event.title,
startDate: new Date(s.startsAt).toISOString(),
endDate: new Date(s.endsAt).toISOString(),
...(s.location ? { location: { "@type": "Place", name: s.location, address: s.location } } : place ? { location: place } : {}),
})),
}
: {}),
};
}