@cloudhal/astro-renderer 0.4.0 → 0.6.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.
- package/README.md +40 -1
- package/dist/src/cloudflare-entry.d.ts +3 -2
- package/dist/src/cloudflare-entry.js +3 -2
- package/dist/src/collection-feed-middleware.d.ts +5 -0
- package/dist/src/collection-feed-middleware.js +22 -0
- package/dist/src/collection-feed-route.d.ts +21 -0
- package/dist/src/collection-feed-route.js +92 -0
- package/dist/src/collection-feeds.d.ts +40 -0
- package/dist/src/collection-feeds.js +87 -0
- package/dist/src/components/ContentList.astro +26 -9
- package/dist/src/components/EntryFields.astro +43 -0
- package/dist/src/components/index.d.ts +1 -0
- package/dist/src/components/index.js +1 -0
- package/dist/src/entry-query.d.ts +85 -0
- package/dist/src/entry-query.js +227 -0
- package/dist/src/entry-views.d.ts +36 -0
- package/dist/src/entry-views.js +73 -0
- package/dist/src/fields.d.ts +77 -0
- package/dist/src/fields.js +201 -0
- package/dist/src/index.d.ts +6 -1
- package/dist/src/index.js +10 -1
- package/dist/src/integration.js +30 -19
- package/dist/src/live-source.d.ts +53 -0
- package/dist/src/live-source.js +318 -0
- package/dist/src/locales.js +4 -3
- package/dist/src/preview.d.ts +16 -1
- package/dist/src/preview.js +89 -14
- package/dist/src/routing.d.ts +1 -1
- package/dist/src/routing.js +13 -3
- package/dist/src/seo.d.ts +4 -1
- package/dist/src/seo.js +9 -0
- package/dist/src/source-helpers.d.ts +11 -0
- package/dist/src/source-helpers.js +48 -0
- package/dist/src/source.d.ts +40 -45
- package/dist/src/source.js +104 -283
- package/dist/src/structured-data.d.ts +35 -0
- package/dist/src/structured-data.js +88 -0
- package/package.json +31 -6
package/dist/src/source.js
CHANGED
|
@@ -11,23 +11,23 @@
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
searchRows,
|
|
19
|
-
termName,
|
|
20
|
-
} from "@cloudhal/core-ts";
|
|
21
|
-
import {
|
|
22
|
-
gateInstants,
|
|
23
|
-
hasGates,
|
|
24
|
-
inheritedGate,
|
|
25
|
-
isAvailable,
|
|
26
|
-
nextInstant,
|
|
27
|
-
} from "@cloudhal/core-ts/availability";
|
|
28
|
-
import { matches, parseFilter, resolveFilter } from "@cloudhal/core-ts/filter";
|
|
14
|
+
import { matchRedirect, redirectLocale } from "@cloudhal/core-ts";
|
|
15
|
+
import { hasGates, inheritedGate, isAvailable, nextInstant } from "@cloudhal/core-ts/availability";
|
|
16
|
+
import { derivedGates, } from "@cloudhal/core-ts/fields";
|
|
17
|
+
import { matches } from "@cloudhal/core-ts/filter";
|
|
29
18
|
import { assetRefsOf } from "@cloudhal/core-ts/media";
|
|
19
|
+
import {
|
|
20
|
+
evaluateEntryQuery,
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
rememberList,
|
|
24
|
+
} from "./entry-query.js";
|
|
25
|
+
import { gateIndex, splitEntries } from "./entry-views.js";
|
|
26
|
+
import { entryFields, } from "./fields.js";
|
|
30
27
|
import { navigationFor } from "./navigation.js";
|
|
28
|
+
import { byNewest, byPosition, compile, shown, termFilter, visible } from "./source-helpers.js";
|
|
29
|
+
|
|
30
|
+
export { LiveSource } from "./live-source.js";
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* What a renderer needs from a site, independent of whether it reads live nodes (dev) or a
|
|
@@ -38,11 +38,23 @@ import { navigationFor } from "./naviga
|
|
|
38
38
|
|
|
39
39
|
|
|
40
40
|
|
|
41
|
+
|
|
41
42
|
|
|
43
|
+
|
|
42
44
|
|
|
43
|
-
|
|
45
|
+
|
|
44
46
|
|
|
45
47
|
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
46
58
|
|
|
47
59
|
|
|
48
60
|
|
|
@@ -112,6 +124,14 @@ import { navigationFor } from "./naviga
|
|
|
112
124
|
|
|
113
125
|
|
|
114
126
|
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
115
135
|
|
|
116
136
|
|
|
117
137
|
|
|
@@ -123,271 +143,20 @@ import { navigationFor } from "./naviga
|
|
|
123
143
|
|
|
124
144
|
|
|
125
145
|
|
|
126
|
-
function byNewest(a , b ) {
|
|
127
|
-
return (b.publishedAt ?? b.createdAt).localeCompare(a.publishedAt ?? a.createdAt);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function byPosition(locale ) {
|
|
131
|
-
return (a , b ) =>
|
|
132
|
-
(a.position ?? Number.MAX_SAFE_INTEGER) - (b.position ?? Number.MAX_SAFE_INTEGER) ||
|
|
133
|
-
termName(a, locale).localeCompare(termName(b, locale)) ||
|
|
134
|
-
a.slug.localeCompare(b.slug);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function shown(term , includeDrafts ) {
|
|
138
|
-
return includeDrafts
|
|
139
|
-
? term.status !== "retired"
|
|
140
|
-
: term.status === "active" && term.visibility === "public";
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function termFilter(key , slug , descendants ) {
|
|
144
|
-
return `${key}:${slug}${descendants ? ">*" : ""}`;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** Resolves a filter against a vocabulary; unknown terms match nothing and are logged. */
|
|
148
|
-
function compile(filter , taxonomies , terms ) {
|
|
149
|
-
const resolved = resolveFilter(
|
|
150
|
-
parseFilter(filter),
|
|
151
|
-
{ taxonomies, terms },
|
|
152
|
-
{ unknown: "match-nothing" },
|
|
153
|
-
);
|
|
154
|
-
if (resolved.unknown.length > 0)
|
|
155
|
-
console.warn(
|
|
156
|
-
`cloudhal: filter "${filter}" names unknown terms: ${resolved.unknown.join(", ")}`,
|
|
157
|
-
);
|
|
158
|
-
return resolved;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function visible (nodes , includeDrafts ) {
|
|
162
|
-
return includeDrafts
|
|
163
|
-
? nodes.filter((n) => n.status !== "archived")
|
|
164
|
-
: nodes.filter((n) => n.status === "published");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Reads the current nodes on every call. Drafts are included so authors can preview them in dev,
|
|
169
|
-
* and so are gated nodes (a preview shows embargoed and expired content, RFC-0008 §7.3). Without
|
|
170
|
-
* drafts it serves what the public would see: published nodes whose gates are open at `now`.
|
|
171
|
-
*/
|
|
172
|
-
export class LiveSource {
|
|
173
|
-
mode = "live";
|
|
174
|
-
#store ;
|
|
175
|
-
#siteId ;
|
|
176
|
-
#includeDrafts ;
|
|
177
|
-
#now ;
|
|
178
|
-
|
|
179
|
-
constructor(
|
|
180
|
-
store ,
|
|
181
|
-
siteId ,
|
|
182
|
-
options = {},
|
|
183
|
-
) {
|
|
184
|
-
this.#store = store;
|
|
185
|
-
this.#siteId = siteId;
|
|
186
|
-
this.#includeDrafts = options.includeDrafts ?? true;
|
|
187
|
-
this.#now = options.now ?? Date.now;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/** Drafts and gated nodes, or what the public sees now. */
|
|
191
|
-
#visible (nodes ) {
|
|
192
|
-
if (this.#includeDrafts) return visible(nodes, true);
|
|
193
|
-
const now = this.#now();
|
|
194
|
-
return nodes.filter((n) => isAvailable(n, now));
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async nextGate() {
|
|
198
|
-
if (this.#includeDrafts) return undefined;
|
|
199
|
-
const [pages, entries] = await Promise.all([
|
|
200
|
-
this.#store.listPages(this.#siteId),
|
|
201
|
-
this.#store.listContentEntries(this.#siteId),
|
|
202
|
-
]);
|
|
203
|
-
const published = [...pages, ...entries].filter((n) => n.status === "published");
|
|
204
|
-
return nextInstant(gateInstants(published), this.#now());
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
async site() {
|
|
208
|
-
const site = await this.#store.getSite(this.#siteId);
|
|
209
|
-
if (!site) throw new Error(`site ${this.#siteId} not found`);
|
|
210
|
-
return site;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
async pages() {
|
|
214
|
-
const site = await this.site();
|
|
215
|
-
const pages = this.#visible(await this.#store.listPages(this.#siteId));
|
|
216
|
-
const order = new Map(site.pageIds.map((id, i) => [id, i]));
|
|
217
|
-
return pages.sort((a, b) => (order.get(a.id) ?? 1e9) - (order.get(b.id) ?? 1e9));
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async pageByPath(path , locale ) {
|
|
221
|
-
const loc = locale ?? (await this.site()).defaultLocale;
|
|
222
|
-
return (await this.pages()).find((p) => p.path === path && p.locale === loc);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async entries(collection , locale ) {
|
|
226
|
-
const loc = locale ?? (await this.site()).defaultLocale;
|
|
227
|
-
const all = this.#visible(await this.#store.listContentEntries(this.#siteId, collection));
|
|
228
|
-
return all.filter((e) => e.locale === loc).sort(byNewest);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
async entryBySlug(
|
|
232
|
-
collection ,
|
|
233
|
-
slug ,
|
|
234
|
-
locale ,
|
|
235
|
-
) {
|
|
236
|
-
return (await this.entries(collection, locale)).find((e) => e.slug === slug);
|
|
237
|
-
}
|
|
238
|
-
async allEntries(locale ) {
|
|
239
|
-
const loc = locale ?? (await this.site()).defaultLocale;
|
|
240
|
-
return this.#visible(await this.#store.listContentEntries(this.#siteId))
|
|
241
|
-
.filter((e) => e.locale === loc)
|
|
242
|
-
.sort(byNewest);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
async search(query , options = {}) {
|
|
246
|
-
const site = await this.site();
|
|
247
|
-
const locale = options.locale ?? site.defaultLocale;
|
|
248
|
-
// What this source shows decided visibility and gates already (drafts included).
|
|
249
|
-
const [entries, pages, taxonomies, terms] = await Promise.all([
|
|
250
|
-
this.allEntries(locale),
|
|
251
|
-
this.pages(),
|
|
252
|
-
this.taxonomies(),
|
|
253
|
-
this.vocabulary(),
|
|
254
|
-
]);
|
|
255
|
-
const rows = searchDocuments({
|
|
256
|
-
locales: [locale],
|
|
257
|
-
defaultLocale: site.defaultLocale,
|
|
258
|
-
site,
|
|
259
|
-
entries,
|
|
260
|
-
pages: pages.filter((p) => p.locale === locale),
|
|
261
|
-
taxonomies,
|
|
262
|
-
terms,
|
|
263
|
-
drafts: true,
|
|
264
|
-
}).map((r) => ({ ...r, gateFrom: undefined, gateUntil: undefined }));
|
|
265
|
-
return searchRows(rows, query, { ...options, locale, now: this.#now() });
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
async entryById(id ) {
|
|
269
|
-
const entry = await this.#store.getContentEntry(id);
|
|
270
|
-
return entry && entry.siteId === this.#siteId && this.#visible([entry]).length
|
|
271
|
-
? entry
|
|
272
|
-
: undefined;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
taxonomies() {
|
|
276
|
-
return this.#store.listTaxonomies(this.#siteId);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
async terms(key ) {
|
|
280
|
-
const loc = (await this.site()).defaultLocale;
|
|
281
|
-
return (await this.#store.listTerms(this.#siteId, { taxonomy: key }))
|
|
282
|
-
.filter((t) => shown(t, this.#includeDrafts))
|
|
283
|
-
.sort(byPosition(loc));
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
async termBySlug(key , slug ) {
|
|
287
|
-
return (await this.terms(key)).find((t) => t.slug === slug);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
vocabulary(key ) {
|
|
291
|
-
return this.#store.listTerms(this.#siteId, key === undefined ? {} : { taxonomy: key });
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
async entriesByFilter(
|
|
295
|
-
filter ,
|
|
296
|
-
options = {},
|
|
297
|
-
) {
|
|
298
|
-
const loc = options.locale ?? (await this.site()).defaultLocale;
|
|
299
|
-
const [taxonomies, terms, entries] = await Promise.all([
|
|
300
|
-
this.#store.listTaxonomies(this.#siteId),
|
|
301
|
-
this.#store.listTerms(this.#siteId),
|
|
302
|
-
this.#store.listContentEntries(this.#siteId, { locale: loc }),
|
|
303
|
-
]);
|
|
304
|
-
const f = compile(filter, taxonomies, terms);
|
|
305
|
-
const out = this.#visible(entries)
|
|
306
|
-
.filter((e) => matches(f, e))
|
|
307
|
-
.sort(byNewest);
|
|
308
|
-
return options.limit === undefined ? out : out.slice(0, options.limit);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
entriesByTerm(
|
|
312
|
-
key ,
|
|
313
|
-
slug ,
|
|
314
|
-
options = {},
|
|
315
|
-
) {
|
|
316
|
-
return this.entriesByFilter(termFilter(key, slug, options.descendants === true), options);
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
redirectFor(path , locale ) {
|
|
320
|
-
return this.#store.redirectFor(this.#siteId, path, locale);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
redirects() {
|
|
324
|
-
return this.#store.listRedirects(this.#siteId);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
menu(key ) {
|
|
328
|
-
return this.#store.getMenuByKey(this.#siteId, key);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
async navigation(
|
|
332
|
-
key ,
|
|
333
|
-
locale ,
|
|
334
|
-
options = {},
|
|
335
|
-
) {
|
|
336
|
-
const menu = await this.menu(key);
|
|
337
|
-
if (!menu) return undefined;
|
|
338
|
-
const site = await this.site();
|
|
339
|
-
const [pages, entries, taxonomies, terms] = await Promise.all([
|
|
340
|
-
this.#store.listPages(this.#siteId),
|
|
341
|
-
this.#store.listContentEntries(this.#siteId),
|
|
342
|
-
this.#store.listTaxonomies(this.#siteId),
|
|
343
|
-
this.#store.listTerms(this.#siteId),
|
|
344
|
-
]);
|
|
345
|
-
return navigationFor(
|
|
346
|
-
menu,
|
|
347
|
-
{
|
|
348
|
-
site,
|
|
349
|
-
// drafts: everything but archived (the lookup decides); public: what is available now
|
|
350
|
-
pages: this.#includeDrafts ? pages : this.#visible(pages),
|
|
351
|
-
entries: this.#includeDrafts ? entries : this.#visible(entries),
|
|
352
|
-
taxonomies,
|
|
353
|
-
terms,
|
|
354
|
-
includeDrafts: this.#includeDrafts,
|
|
355
|
-
...(options.collectionIndex ? { collectionIndex: options.collectionIndex } : {}),
|
|
356
|
-
},
|
|
357
|
-
locale ?? site.defaultLocale,
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
async assets() {
|
|
362
|
-
const out = [];
|
|
363
|
-
let cursor ;
|
|
364
|
-
do {
|
|
365
|
-
const page = await this.#store.listAssets(this.#siteId, {
|
|
366
|
-
limit: 200,
|
|
367
|
-
...(cursor ? { cursor } : {}),
|
|
368
|
-
});
|
|
369
|
-
out.push(...page.assets);
|
|
370
|
-
cursor = page.nextCursor;
|
|
371
|
-
} while (cursor);
|
|
372
|
-
return out;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
async asset(id ) {
|
|
376
|
-
const a = await this.#store.getAsset(id);
|
|
377
|
-
return a?.siteId === this.#siteId ? a : undefined;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
assetByPrefix(prefix16 ) {
|
|
381
|
-
return this.#store.getAssetByPrefix(this.#siteId, prefix16);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
146
|
/** Everything a snapshot shows before gates are applied; built once per snapshot and isolate. */
|
|
386
147
|
|
|
387
148
|
|
|
388
149
|
|
|
389
150
|
|
|
390
151
|
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
391
160
|
|
|
392
161
|
|
|
393
162
|
|
|
@@ -397,7 +166,8 @@ export class LiveSource {
|
|
|
397
166
|
|
|
398
167
|
|
|
399
168
|
|
|
400
|
-
|
|
169
|
+
|
|
170
|
+
|
|
401
171
|
|
|
402
172
|
|
|
403
173
|
|
|
@@ -406,7 +176,14 @@ export class LiveSource {
|
|
|
406
176
|
|
|
407
177
|
|
|
408
178
|
|
|
179
|
+
|
|
409
180
|
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
410
187
|
|
|
411
188
|
|
|
412
189
|
|
|
@@ -573,18 +350,23 @@ export class SnapshotSource {
|
|
|
573
350
|
(a, b) => (order.get(a.id) ?? 1e9) - (order.get(b.id) ?? 1e9),
|
|
574
351
|
);
|
|
575
352
|
const shownEntries = visible(entries, false);
|
|
353
|
+
const derived = derivedGates(site , shownEntries);
|
|
576
354
|
if (fetched) this.#shared.fetched = fetched;
|
|
577
355
|
return {
|
|
578
356
|
site: site ,
|
|
579
357
|
pages: shownPages,
|
|
580
358
|
entries: shownEntries,
|
|
359
|
+
published: shownEntries,
|
|
360
|
+
reachable: shownEntries,
|
|
361
|
+
derived,
|
|
362
|
+
resolved: new Map(),
|
|
581
363
|
taxonomies,
|
|
582
364
|
terms,
|
|
583
365
|
redirects,
|
|
584
366
|
menus,
|
|
585
367
|
assets,
|
|
586
368
|
assetIndex,
|
|
587
|
-
gates:
|
|
369
|
+
gates: gateIndex(site , shownPages, shownEntries, derived),
|
|
588
370
|
};
|
|
589
371
|
})();
|
|
590
372
|
this.#shared.loaded.catch(() => {
|
|
@@ -638,10 +420,12 @@ export class SnapshotSource {
|
|
|
638
420
|
hidden.size === 0
|
|
639
421
|
? loaded.assetIndex
|
|
640
422
|
: new Map([...loaded.assetIndex].filter(([, a]) => !hidden.has(a.id)));
|
|
423
|
+
const split = splitEntries(loaded.entries, now, loaded.derived);
|
|
641
424
|
return {
|
|
642
425
|
...loaded,
|
|
643
426
|
pages: loaded.pages.filter((p) => isAvailable(p, now)),
|
|
644
|
-
entries:
|
|
427
|
+
entries: split.listed,
|
|
428
|
+
reachable: split.reachable,
|
|
645
429
|
assets,
|
|
646
430
|
assetIndex,
|
|
647
431
|
};
|
|
@@ -667,7 +451,8 @@ export class SnapshotSource {
|
|
|
667
451
|
async entries(collection , locale ) {
|
|
668
452
|
const { site, entries } = await this.#current();
|
|
669
453
|
const loc = locale ?? site.defaultLocale;
|
|
670
|
-
|
|
454
|
+
const out = entries.filter((e) => e.collection === collection && e.locale === loc);
|
|
455
|
+
return rememberList(out.sort(byNewest), { source: this, locale: loc });
|
|
671
456
|
}
|
|
672
457
|
|
|
673
458
|
async entryBySlug(
|
|
@@ -675,14 +460,43 @@ export class SnapshotSource {
|
|
|
675
460
|
slug ,
|
|
676
461
|
locale ,
|
|
677
462
|
) {
|
|
678
|
-
|
|
463
|
+
const { site, reachable } = await this.#current();
|
|
464
|
+
const loc = locale ?? site.defaultLocale;
|
|
465
|
+
return reachable.find(
|
|
466
|
+
(e) => e.collection === collection && e.slug === slug && e.locale === loc,
|
|
467
|
+
);
|
|
679
468
|
}
|
|
469
|
+
|
|
680
470
|
async allEntries(locale ) {
|
|
681
471
|
const { site, entries } = await this.#current();
|
|
682
472
|
const loc = locale ?? site.defaultLocale;
|
|
683
473
|
return entries.filter((e) => e.locale === loc).sort(byNewest);
|
|
684
474
|
}
|
|
685
475
|
|
|
476
|
+
/** Every entry of a locale with a public URL, unlisted ones included (RFC-0042 §3). */
|
|
477
|
+
async routableEntries(locale ) {
|
|
478
|
+
const { site, reachable } = await this.#current();
|
|
479
|
+
const loc = locale ?? site.defaultLocale;
|
|
480
|
+
return reachable.filter((e) => e.locale === loc).sort(byNewest);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** RFC-0042 §4: a collection by its list fields, `now` at this view's instant. */
|
|
484
|
+
async queryEntries(q ) {
|
|
485
|
+
const view = await this.#current();
|
|
486
|
+
const loc = q.locale ?? view.site.defaultLocale;
|
|
487
|
+
const shownHere = q.includeExpired ? view.reachable : view.entries;
|
|
488
|
+
return evaluateEntryQuery({
|
|
489
|
+
site: view.site,
|
|
490
|
+
candidates: shownHere.filter((e) => e.collection === q.collection && e.locale === loc),
|
|
491
|
+
published: view.published,
|
|
492
|
+
taxonomies: view.taxonomies,
|
|
493
|
+
terms: view.terms,
|
|
494
|
+
query: q,
|
|
495
|
+
now: this.#clock?.() ?? Date.now(),
|
|
496
|
+
cache: view.resolved,
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
|
|
686
500
|
async search(query , options = {}) {
|
|
687
501
|
const locale = options.locale ?? (await this.site()).defaultLocale;
|
|
688
502
|
return this.#store.search(this.#snapshot.siteId, query, {
|
|
@@ -695,7 +509,11 @@ export class SnapshotSource {
|
|
|
695
509
|
|
|
696
510
|
/** A published entry of this snapshot by id (any locale), if its gates are open. */
|
|
697
511
|
async entryById(id ) {
|
|
698
|
-
return (await this.#current()).
|
|
512
|
+
return (await this.#current()).reachable.find((e) => e.id === id);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
entryFields(entry , options ) {
|
|
516
|
+
return entryFields(this, entry, options);
|
|
699
517
|
}
|
|
700
518
|
|
|
701
519
|
async taxonomies() {
|
|
@@ -731,7 +549,10 @@ export class SnapshotSource {
|
|
|
731
549
|
const loc = options.locale ?? site.defaultLocale;
|
|
732
550
|
const f = compile(filter, taxonomies, terms);
|
|
733
551
|
const out = entries.filter((e) => e.locale === loc && matches(f, e)).sort(byNewest);
|
|
734
|
-
return options.limit === undefined ? out : out.slice(0, options.limit)
|
|
552
|
+
return rememberList(options.limit === undefined ? out : out.slice(0, options.limit), {
|
|
553
|
+
source: this,
|
|
554
|
+
locale: loc,
|
|
555
|
+
});
|
|
735
556
|
}
|
|
736
557
|
|
|
737
558
|
entriesByTerm(
|
|
@@ -770,7 +591,7 @@ export class SnapshotSource {
|
|
|
770
591
|
{
|
|
771
592
|
site: view.site,
|
|
772
593
|
pages: view.pages,
|
|
773
|
-
entries: view.
|
|
594
|
+
entries: view.reachable,
|
|
774
595
|
taxonomies: view.taxonomies,
|
|
775
596
|
terms: view.terms,
|
|
776
597
|
...(options.collectionIndex ? { collectionIndex: options.collectionIndex } : {}),
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0042 §2 in `<head>`: an entry whose collection has a JSON-LD preset (`Event`, `JobPosting`,
|
|
3
|
+
* `Product`, `Recipe`) describes itself with it instead of the generic `BlogPosting` that
|
|
4
|
+
* `entryHead` emits. `entryHead` itself never changes, so a theme copy from before RFC-0042 keeps
|
|
5
|
+
* today's output; a theme opts in with one `withStructuredData` call after `entryHead`.
|
|
6
|
+
*
|
|
7
|
+
* The document is built by core-ts's pure `structuredDataFor` from typed values (resolved, so a
|
|
8
|
+
* translation reads its original's non-localized ones), capped at 32 KB, and rendered through
|
|
9
|
+
* `jsonLdText` by `SeoHead.astro` like every other JSON-LD block. An entry missing a required value
|
|
10
|
+
* keeps its `BlogPosting`; the `BreadcrumbList` always stays.
|
|
11
|
+
*/
|
|
12
|
+
import type { ContentEntry } from "@cloudhal/core-contracts";
|
|
13
|
+
import { type StructuredDataResult } from "@cloudhal/core-ts/structured-data";
|
|
14
|
+
import { type MediaContext } from "./media.js";
|
|
15
|
+
import type { HeadMeta } from "./seo.js";
|
|
16
|
+
import type { GraphSource } from "./source.js";
|
|
17
|
+
export interface StructuredDataOptions {
|
|
18
|
+
/** The site's public origin (`siteOrigin`); every URL in the document is absolute on it. */
|
|
19
|
+
readonly origin: string;
|
|
20
|
+
/** The page's media context: its asset index is reused when given. */
|
|
21
|
+
readonly media?: MediaContext | undefined;
|
|
22
|
+
}
|
|
23
|
+
type StructuredDataSource = Pick<GraphSource, "site"> & Partial<Pick<GraphSource, "entryById" | "assets">>;
|
|
24
|
+
/**
|
|
25
|
+
* The preset document of an entry and why it has none (`issues`): no preset, a required value
|
|
26
|
+
* missing, or over the cap. What `withStructuredData` emits, and what previews and checks show.
|
|
27
|
+
*/
|
|
28
|
+
export declare function entryStructuredData(source: StructuredDataSource, entry: ContentEntry, options: StructuredDataOptions): Promise<StructuredDataResult>;
|
|
29
|
+
/**
|
|
30
|
+
* `head` with the entry's preset document in place of the `BlogPosting` (RFC-0042 §2), when its
|
|
31
|
+
* collection has a preset and the entry has every required value; otherwise `head` unchanged.
|
|
32
|
+
* The `BreadcrumbList` stays; the `BlogPosting`'s `commentCount` does not move to the preset.
|
|
33
|
+
*/
|
|
34
|
+
export declare function withStructuredData(head: HeadMeta, source: StructuredDataSource, entry: ContentEntry, options: StructuredDataOptions): Promise<HeadMeta>;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0042 §2 in `<head>`: an entry whose collection has a JSON-LD preset (`Event`, `JobPosting`,
|
|
3
|
+
* `Product`, `Recipe`) describes itself with it instead of the generic `BlogPosting` that
|
|
4
|
+
* `entryHead` emits. `entryHead` itself never changes, so a theme copy from before RFC-0042 keeps
|
|
5
|
+
* today's output; a theme opts in with one `withStructuredData` call after `entryHead`.
|
|
6
|
+
*
|
|
7
|
+
* The document is built by core-ts's pure `structuredDataFor` from typed values (resolved, so a
|
|
8
|
+
* translation reads its original's non-localized ones), capped at 32 KB, and rendered through
|
|
9
|
+
* `jsonLdText` by `SeoHead.astro` like every other JSON-LD block. An entry missing a required value
|
|
10
|
+
* keeps its `BlogPosting`; the `BreadcrumbList` always stays.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { entryPath } from "@cloudhal/core-ts";
|
|
14
|
+
import { resolveFields } from "@cloudhal/core-ts/fields";
|
|
15
|
+
import {
|
|
16
|
+
absoluteHttpUrl,
|
|
17
|
+
|
|
18
|
+
structuredDataFor,
|
|
19
|
+
} from "@cloudhal/core-ts/structured-data";
|
|
20
|
+
import { loadAssetIndex, socialImageUrl } from "./media.js";
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
/** The asset index to resolve image fields, the cover image and the site logo with. */
|
|
35
|
+
async function assetsFor(
|
|
36
|
+
source ,
|
|
37
|
+
media ,
|
|
38
|
+
) {
|
|
39
|
+
return media?.assets ?? (await loadAssetIndex(source));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The preset document of an entry and why it has none (`issues`): no preset, a required value
|
|
44
|
+
* missing, or over the cap. What `withStructuredData` emits, and what previews and checks show.
|
|
45
|
+
*/
|
|
46
|
+
export async function entryStructuredData(
|
|
47
|
+
source ,
|
|
48
|
+
entry ,
|
|
49
|
+
options ,
|
|
50
|
+
) {
|
|
51
|
+
const site = await source.site();
|
|
52
|
+
if (!site.collections?.[entry.collection]?.structuredData) return { issues: [] };
|
|
53
|
+
const group = entry.translation?.group;
|
|
54
|
+
const original =
|
|
55
|
+
group && group !== entry.id && source.entryById ? await source.entryById(group) : undefined;
|
|
56
|
+
const resolved = resolveFields(site, entry, original);
|
|
57
|
+
const assets = await assetsFor(source, options.media);
|
|
58
|
+
const { origin } = options;
|
|
59
|
+
return structuredDataFor(site, resolved, entry, {
|
|
60
|
+
origin,
|
|
61
|
+
entryUrl: `${origin}${entryPath(site, entry)}`,
|
|
62
|
+
imageUrl: (image) => {
|
|
63
|
+
const asset = image.assetId ? assets.get(image.assetId) : undefined;
|
|
64
|
+
if (asset) return asset.kind === "image" ? socialImageUrl(origin, asset) : undefined;
|
|
65
|
+
return image.src ? absoluteHttpUrl(image.src, origin) : undefined;
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const isBlogPosting = (doc ) => (doc )["@type"] === "BlogPosting";
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* `head` with the entry's preset document in place of the `BlogPosting` (RFC-0042 §2), when its
|
|
74
|
+
* collection has a preset and the entry has every required value; otherwise `head` unchanged.
|
|
75
|
+
* The `BreadcrumbList` stays; the `BlogPosting`'s `commentCount` does not move to the preset.
|
|
76
|
+
*/
|
|
77
|
+
export async function withStructuredData(
|
|
78
|
+
head ,
|
|
79
|
+
source ,
|
|
80
|
+
entry ,
|
|
81
|
+
options ,
|
|
82
|
+
) {
|
|
83
|
+
const at = head.jsonLd.findIndex(isBlogPosting);
|
|
84
|
+
if (at < 0) return head;
|
|
85
|
+
const { doc } = await entryStructuredData(source, entry, options);
|
|
86
|
+
if (!doc) return head;
|
|
87
|
+
return { ...head, jsonLd: head.jsonLd.map((d, i) => (i === at ? doc : d)) };
|
|
88
|
+
}
|