ferst-core 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,8 +28,10 @@
28
28
  */
29
29
  import ContactForm from './ContactForm.astro';
30
30
  import Heading from '../blocks/Heading.astro';
31
- import { bookTarget, loadBookResource, loadBookService, metaLine, whereText, type BookService, type BookWho } from '../../lib/book';
31
+ import { bookTarget, eventsData, ldJson, loadBookResource, loadBookService, loadEventDates, metaLine, whereText, type BookService, type BookWho, type EventsContext } from '../../lib/book';
32
32
  import { resolveGitBase, resolveSiteId } from '../../lib/git-base.mjs';
33
+ import { canonicalUrl } from '../../lib/seo.mjs';
34
+ import { loadSiteSettings } from '../../lib/siteSettings';
33
35
 
34
36
  interface Props {
35
37
  service?: string;
@@ -84,6 +86,29 @@ const many = !list && offers.length > 1;
84
86
  const panels = list ? [lead] : offers;
85
87
  const sources = list ? JSON.stringify(offers.map((o) => ({ book: o.definition, event: o.event ?? '' }))) : undefined;
86
88
  const placeOf = (d: BookService) => d.resources.find((r) => r.kind === 'place')?.name ?? '';
89
+
90
+ // For search engines (0.15.0; myai4-webdev docs/EVENTS.md slice 6d): each coming date of a class or an
91
+ // event as a schema.org Event, once the site has an address (as all its structured data, Base.astro).
92
+ // Read at build, and brought up to date as the page loads. A preview (no site address) has none.
93
+ const siteUrl = (import.meta.env.FERST_SITE_URL as string | undefined) || '';
94
+ let ldContext: EventsContext | null = null;
95
+ if (siteUrl) {
96
+ const settings = await loadSiteSettings().catch(() => null);
97
+ ldContext = {
98
+ url: `${canonicalUrl(siteUrl, Astro.url?.pathname ?? '/')}#${anchor}`,
99
+ organisation: { name: settings?.identity?.siteTitle, url: `${siteUrl.replace(/\/+$/, '')}/` },
100
+ places: (settings?.places ?? []) as EventsContext['places'],
101
+ };
102
+ }
103
+ const ldOf = async (def: BookService): Promise<string | null> => {
104
+ if (!ldContext || def.mode !== 'places') return null;
105
+ const dates = list
106
+ ? (await Promise.all(offers.map((o) => loadEventDates(o.definition, { event: o.event })))).flat().sort((a, b) => a.start.localeCompare(b.start))
107
+ : await loadEventDates(def.definition, { event: def.event, resource: def.resource });
108
+ const data = eventsData(dates, ldContext);
109
+ return data ? ldJson(data) : null;
110
+ };
111
+ const lds = await Promise.all(panels.map(ldOf));
87
112
  ---
88
113
 
89
114
  <section class="b-book" id={anchor} data-book-block data-demo={demo ? '1' : undefined} data-find={`${base}/sites/${encodeURIComponent(siteId ?? '')}/book/find`}>
@@ -116,6 +141,7 @@ const placeOf = (d: BookService) => d.resources.find((r) => r.kind === 'place')?
116
141
  data-list={sources}
117
142
  data-look={one ? 'button' : undefined} data-date={one ? date ?? '' : undefined}
118
143
  data-with={whereText(def.resources)} hidden={i > 0}>
144
+ {lds[i] && <script type="application/ld+json" is:inline data-book-ld data-ctx={JSON.stringify(ldContext)} set:html={lds[i]} />}
119
145
  {many && def.description && <p class="b-book__intro">{def.description}</p>}
120
146
  <div class="b-book__notice" role="alert" data-book-error hidden></div>
121
147
 
@@ -193,7 +219,7 @@ const placeOf = (d: BookService) => d.resources.find((r) => r.kind === 'place')?
193
219
  </section>
194
220
 
195
221
  <script>
196
- import { addDays, bookingMessage, calendarFile, dayChip, endChoices, googleCalendarLink, londonDay, whenText, whichFree, type EventDate, type TimesAnswer } from '../../lib/book';
222
+ import { addDays, bookingMessage, calendarFile, dayChip, endChoices, eventsData, googleCalendarLink, londonDay, whenText, whichFree, type EventDate, type EventsContext, type TimesAnswer } from '../../lib/book';
197
223
 
198
224
  const SPAN = 14; // days shown at once (dates for a class: four weeks)
199
225
  const AHEAD = 60; // how far ahead the platform takes bookings
@@ -368,6 +394,15 @@ const placeOf = (d: BookService) => d.resources.find((r) => r.kind === 'place')?
368
394
  if (holdBtn && mode === 'places' && !one) { const n = Math.max(1, Number(count!.value) || 1); holdBtn.textContent = `Hold ${n === 1 ? 'this place' : `${n} places`}`; }
369
395
  }
370
396
 
397
+ /** The page's dates for search engines, brought up to date (the places left) from what's just been read. */
398
+ function freshForSearch() {
399
+ const ld = block.querySelector<HTMLScriptElement>('script[data-book-ld]');
400
+ if (!ld || !answer || from !== today) return;
401
+ const data = eventsData(answer.events ?? [], JSON.parse(ld.dataset.ctx ?? '{}') as EventsContext);
402
+ if (data) ld.textContent = JSON.stringify(data);
403
+ else ld.remove();
404
+ }
405
+
371
406
  async function load() {
372
407
  say(null);
373
408
  timesEl.innerHTML = `<p class="b-book__empty">${mode === 'places' ? 'Loading dates…' : 'Loading free times…'}</p>`;
@@ -380,7 +415,7 @@ const placeOf = (d: BookService) => d.resources.find((r) => r.kind === 'place')?
380
415
  }));
381
416
  answer = { service: { id: '', name: block.dataset.name ?? '', minutes }, resources: [], days: [], events: all.flat().sort((a, b) => a.start.localeCompare(b.start)) };
382
417
  start = null; date = null;
383
- drawDays(); drawTimes();
418
+ drawDays(); drawTimes(); freshForSearch();
384
419
  return;
385
420
  }
386
421
  const res = await fetch(`${src}?from=${from}&to=${addDays(from, span)}${onlyEvent ? `&event=${encodeURIComponent(onlyEvent)}` : ''}${onlyWith ? `&resource=${encodeURIComponent(onlyWith)}` : ''}`, { headers: { Accept: 'application/json' } });
@@ -388,7 +423,7 @@ const placeOf = (d: BookService) => d.resources.find((r) => r.kind === 'place')?
388
423
  answer = (await res.json()) as TimesAnswer;
389
424
  day = answer.days.find((d) => free(d.day).length)?.day ?? null;
390
425
  start = null; date = null;
391
- drawDays(); drawTimes();
426
+ drawDays(); drawTimes(); freshForSearch();
392
427
  } catch (e) {
393
428
  timesEl.innerHTML = '';
394
429
  say(bookingMessage(e instanceof Error ? e.message : ''));