@cloudhal/astro-renderer 0.7.3 → 0.9.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 CHANGED
@@ -69,6 +69,20 @@ Guide: [`docs/guides/structured-data.md`](../../docs/guides/structured-data.md).
69
69
 
70
70
  Derived gates: `unlist` keeps an entry's page (200), comments and menu links but leaves `entries()`, `allEntries()`, `entriesByFilter`/`entriesByTerm` (archives, related), feeds, the sitemap and search; `hide` is a 404 like an expired `availability`. Both sources apply them, the gate index caps the edge cache at every derived instant and at every value instant of date/datetime list fields (for `now` conditions), and previews ignore them with an "Ended on …" notice. The site Worker's `GET /__cloudhal/features` answers `{ "gates": 2 }`; writers refuse `expiry` until it does.
71
71
 
72
+ ### References and repeaters (RFC-0045, rollout step 3)
73
+ Additive, and dormant until rollout step 7 lets sites define the two types. `entryFields` (and `source.entryFields`) resolve a `reference` to cards of the targets the page's *view* shows (gates applied at its instant; draft, archived, embargoed and hidden targets resolve to nothing and leave no id, title or empty item) and a `repeater` to its items. `EntryFields.astro` draws both, so old theme copies show them with no change of their own (`theme-v0.8.0` in the compat test).
74
+
75
+ | Export | Signature |
76
+ |---|---|
77
+ | `RenderedField.references?` | `ReferencedEntry[]`: `{ groupId, id, collection, locale, slug, title, summary?, path, url, image?, fromFallbackLocale, fields? }` in stored order. Link with `path` (the graph path, which the locale middleware localizes like every entry link); `url` is the canonical URL, already localized; the member in the page's locale, else the default locale's under `localization.fallback: default-with-notice` (flagged: set `lang`) |
78
+ | `RenderedField.items?` | `RenderedFields[]`: each repeater item's sub-fields, rendered like fields; empty items left out |
79
+ | `EntryFieldsOptions.depth?` | `1` (default): cards; `2`: each card's own `fields`, with *their* references as cards without fields |
80
+ | `GraphSource.groupMembers?` | `(groupIds) => Promise<Map<string, ContentEntry[]>>`: the members this view shows, by group (snapshot: in memory; live: one store read per call) |
81
+ | `GraphSource.referencedBy?` | `(groupId, { collection?, field?, locale?, limit? }) => Promise<ContentEntry[]>`: entries this view lists whose resolved values name the group (a translation inheriting its original's value included), newest first, at most 100 |
82
+ | `PlaceholderBudget.references?` | the request's resolved-target allowance (200, in `placeholderBudget()`); past it targets resolve to nothing, with one `console.warn`. The starter passes one `placeholderBudget()` per request in `media.budget`; a theme that calls `entryFields` itself must do the same, once per request (without one, every call gets its own 200) |
83
+
84
+ Resolution is breadth-first over a visited set of groups, one `groupMembers` call per level, so cycles cost nothing. A "used by" list drawn from `referencedBy` is purged with the target's page when a referrer appears, goes or changes (the purge planner reads snapshot bodies, rollout step 3b); drawing *those* entries' fields is outside that guarantee. The site Worker's `GET /__cloudhal/features` answers `{ "gates": 2, "fields": 2 }`.
85
+
72
86
  ## Site analytics (RFC-0006)
73
87
 
74
88
  The renderer carries the site side of Cloudhal's cookie-less analytics (`@cloudhal/analytics`):
@@ -60,6 +60,7 @@ import {
60
60
  } from "@cloudhal/astro-renderer/runtime";
61
61
  import { SqlCommentStore } from "@cloudhal/comments/sql";
62
62
  import { GATES_FEATURE_VERSION } from "@cloudhal/core-ts/availability";
63
+ import { FIELDS_FEATURE_VERSION } from "@cloudhal/core-ts/fields";
63
64
  import { D1Driver } from "@cloudhal/storage/d1";
64
65
  import {
65
66
  ANALYTICS_BEACON_PATH,
@@ -277,7 +278,8 @@ function fromAdmin(request , env ) {
277
278
 
278
279
  /**
279
280
  * What this site Worker enforces (RFC-0008 §7.6–§7.7): the admin Worker asks before it lets anyone
280
- * write time gates. Authenticated like the purge (it is not a public endpoint), never cached.
281
+ * write time gates, and (RFC-0045 §1, `fields: 2`) reference and repeater fields, which this
282
+ * renderer resolves. Authenticated like the purge (it is not a public endpoint), never cached.
281
283
  */
282
284
  function features(request , env ) {
283
285
  const headers = { "cache-control": "no-store", "cloudflare-cdn-cache-control": "no-store" };
@@ -288,7 +290,10 @@ function features(request , env ) {
288
290
  });
289
291
  if (!fromAdmin(request, env))
290
292
  return Response.json({ error: "unauthorized" }, { status: 401, headers });
291
- return Response.json({ gates: GATES_FEATURE_VERSION }, { headers });
293
+ return Response.json(
294
+ { gates: GATES_FEATURE_VERSION, fields: FIELDS_FEATURE_VERSION },
295
+ { headers },
296
+ );
292
297
  }
293
298
 
294
299
  /**
@@ -5,10 +5,15 @@
5
5
  * `source.entryFields(entry, { locale, media })` (or `entryFields(source, entry, …)`) returns.
6
6
  *
7
7
  * Values are escaped text, except rich text (the body's markdown pipeline: raw HTML escaped, links
8
- * through `safeHref`) and images (`imageAttributes`, the asset's alt in the page's language). No
9
- * styles of its own: themes style `.ch-fields`, `.ch-field[data-type]` and `[data-field="<key>"]`.
8
+ * through `safeHref`) and images (`imageAttributes`, the asset's alt in the page's language).
9
+ * RFC-0045: a reference is a link to each target the page's view shows, by its graph path (the
10
+ * locale middleware localizes it, like every entry link; `lang` on a card from the fallback
11
+ * locale; several in a list), a repeater a nested `dl` per item with something to show.
12
+ * Unresolved targets leave nothing: no id, no attribute, no empty link. No styles of its own:
13
+ * themes style `.ch-fields`, `.ch-field[data-type]`, `[data-field="<key>"]`, `.ch-references` and
14
+ * `.ch-items`.
10
15
  */
11
- import type { RenderedFields } from "../fields.js";
16
+ import type { RenderedField, RenderedFields } from "../fields.js";
12
17
 
13
18
  interface Props {
14
19
  fields: RenderedFields;
@@ -19,14 +24,24 @@ interface Props {
19
24
  }
20
25
  const { fields, label, class: className } = Astro.props;
21
26
  const shown = Object.values(fields).filter((f) => !f.empty);
22
- const multiline = (f: (typeof shown)[number]) => f.def.type === "text" && f.def.multiline === true;
27
+ const multiline = (f: RenderedField) => f.def.type === "text" && f.def.multiline === true;
28
+ const langOf = (c: { fromFallbackLocale: boolean; locale: string }) =>
29
+ c.fromFallbackLocale ? c.locale : undefined;
23
30
  ---
24
31
  {shown.length > 0 && (
25
32
  <dl class:list={["ch-fields", className]} aria-label={label}>
26
33
  {shown.map((f) => (
27
34
  <div class="ch-field" data-field={f.key} data-type={f.def.type} data-multiline={multiline(f) ? "" : undefined} data-from-original={f.fromOriginal ? "" : undefined}>
28
35
  <dt>{f.label}</dt>
29
- {f.html !== undefined ? (
36
+ {f.references !== undefined ? (
37
+ f.references.length === 1 && !Array.isArray(f.value) ? (
38
+ <dd><a href={f.references[0]?.path} lang={f.references[0] && langOf(f.references[0])}>{f.references[0]?.title}</a></dd>
39
+ ) : (
40
+ <dd><ul class="ch-references">{f.references.map((c) => <li><a href={c.path} lang={langOf(c)}>{c.title}</a></li>)}</ul></dd>
41
+ )
42
+ ) : f.items !== undefined ? (
43
+ <dd><ol class="ch-items">{f.items.map((item) => <li><Astro.self fields={item} /></li>)}</ol></dd>
44
+ ) : f.html !== undefined ? (
30
45
  <dd set:html={f.html} />
31
46
  ) : f.href !== undefined ? (
32
47
  <dd><a href={f.href}>{f.text}</a></dd>
@@ -8,14 +8,19 @@
8
8
  * `asset:` references resolved), never stored HTML;
9
9
  * - URLs: through `safeHref`, dropped when unsafe;
10
10
  * - images: `<img>` attributes from `imageAttributes`, with the asset's alt in the page's language;
11
- * - everything else: `formatFieldValue` text, which themes print escaped.
11
+ * - everything else: `formatFieldValue` text, which themes print escaped;
12
+ * - RFC-0045: references as cards of the targets this view shows (`references`, depth 1 by
13
+ * default, 2 at most, within a per-request budget), repeaters as their items' sub-fields
14
+ * (`items`). An unresolved target leaves nothing behind: no id, no title, no empty item.
12
15
  *
13
16
  * Worker-safe; the same function serves live and snapshot sources.
14
17
  */
15
18
  import type { Asset, ContentEntry, Site } from "@cloudhal/core-contracts";
16
- import { type FormatFieldOptions, type ResolvedField, type ResolvedFields } from "@cloudhal/core-ts/fields";
19
+ import { type ConversionEntries, type FormatFieldOptions, type ResolvedField, type ResolvedFields } from "@cloudhal/core-ts/fields";
17
20
  import { type ImageAttributes, type MediaContext } from "./media.js";
21
+ import { type ReferencedEntry } from "./references.js";
18
22
  export { type FormatFieldOptions, fieldHelp, fieldLabel, formatFieldValue, optionLabel, type ResolvedField, type ResolvedFields, resolveFields, } from "@cloudhal/core-ts/fields";
23
+ export type { ReferencedEntry } from "./references.js";
19
24
  /** An image field's picture: `<img>` attributes plus the asset it shows. */
20
25
  export interface FieldImage extends ImageAttributes {
21
26
  assetId: string;
@@ -26,7 +31,7 @@ export interface RenderedField extends ResolvedField {
26
31
  /** The label in the page's language (then the site's default, then any, then the key). */
27
32
  label: string;
28
33
  help?: string;
29
- /** Nothing to show: no value, an unreadable one, an unsafe URL or a missing image. */
34
+ /** Nothing to show: no value, an unreadable one, an unsafe URL, a missing image or target. */
30
35
  empty: boolean;
31
36
  /** The value as plain text in the page's language ("" when empty). Print it escaped. */
32
37
  text: string;
@@ -38,6 +43,10 @@ export interface RenderedField extends ResolvedField {
38
43
  image?: FieldImage;
39
44
  /** `date` (`YYYY-MM-DD`) and `datetime` (the UTC instant): for `<time datetime>`. */
40
45
  datetime?: string;
46
+ /** RFC-0045 `reference`: the targets this view shows, in stored order (unresolved ones left out). */
47
+ references?: ReferencedEntry[];
48
+ /** RFC-0045 `repeater`: each item's sub-fields, rendered like fields (empty items left out). */
49
+ items?: RenderedFields[];
41
50
  }
42
51
  /** Every defined field of an entry by key, in definition order (`{}` without definitions). */
43
52
  export type RenderedFields = Record<string, RenderedField>;
@@ -52,25 +61,37 @@ export interface EntryFieldsOptions {
52
61
  words?: FormatFieldOptions["words"];
53
62
  /** `sizes` for image fields (default: the article column). */
54
63
  sizes?: string;
64
+ /**
65
+ * RFC-0045 §4: 1 (default) resolves references to cards; 2 adds each card's own fields, with
66
+ * *their* references as cards without fields.
67
+ */
68
+ depth?: 1 | 2;
55
69
  }
56
70
  /** What `entryFields` needs from a source: the site, the original by id, assets by id. */
57
71
  export interface FieldsSource {
58
72
  site(): Promise<Site>;
59
73
  entryById?(id: string): Promise<ContentEntry | undefined>;
60
74
  asset?(id: string): Promise<Asset | undefined>;
75
+ /** RFC-0045 §4: the members of groups this view shows (absent: references resolve to nothing). */
76
+ groupMembers?(groupIds: readonly string[]): Promise<Map<string, ContentEntry[]>>;
77
+ /** RFC-0045 §7: the entry maps of a running text ↔ reference conversion in a collection. */
78
+ conversionEntries?(collection: string): Promise<ConversionEntries | undefined>;
61
79
  }
62
80
  export interface RenderFieldsOptions extends EntryFieldsOptions {
63
81
  locale: string;
64
82
  defaultLocale: string;
65
83
  /** Assets by id (image fields; rich text's `asset:` links when `media.assets` is absent). */
66
84
  assets?: ReadonlyMap<string, Asset> | undefined;
85
+ /** RFC-0045: resolved reference targets by value path (`fields/venue`, `fields/speakers/0/person`). */
86
+ cards?: ReadonlyMap<string, ReferencedEntry> | undefined;
67
87
  }
68
88
  /** Resolved fields made ready to draw (pure; `entryFields` loads what it needs and calls this). */
69
89
  export declare function renderFields(resolved: ResolvedFields, o: RenderFieldsOptions): RenderedFields;
70
90
  /**
71
91
  * An entry's typed fields as a theme draws them (RFC-0041 §9), from any source: loads the site,
72
92
  * the translation group's original when the entry is a translation in this site, and the assets
73
- * of image fields (from `media.assets` when given).
93
+ * of image fields (from `media.assets` when given). RFC-0045: references resolve through the
94
+ * source's `groupMembers` (one read per level), gated by the source's view.
74
95
  */
75
96
  export declare function entryFields(source: FieldsSource, entry: ContentEntry, options?: EntryFieldsOptions): Promise<RenderedFields>;
76
97
  /** True when at least one field has something to show (themes skip the whole list otherwise). */
@@ -8,12 +8,16 @@
8
8
  * `asset:` references resolved), never stored HTML;
9
9
  * - URLs: through `safeHref`, dropped when unsafe;
10
10
  * - images: `<img>` attributes from `imageAttributes`, with the asset's alt in the page's language;
11
- * - everything else: `formatFieldValue` text, which themes print escaped.
11
+ * - everything else: `formatFieldValue` text, which themes print escaped;
12
+ * - RFC-0045: references as cards of the targets this view shows (`references`, depth 1 by
13
+ * default, 2 at most, within a per-request budget), repeaters as their items' sub-fields
14
+ * (`items`). An unresolved target leaves nothing behind: no id, no title, no empty item.
12
15
  *
13
16
  * Worker-safe; the same function serves live and snapshot sources.
14
17
  */
15
-
18
+
16
19
  import {
20
+
17
21
 
18
22
  fieldHelp,
19
23
  fieldLabel,
@@ -21,10 +25,19 @@ import {
21
25
  hasFieldValue,
22
26
 
23
27
 
28
+ referenceUses,
24
29
  resolveFields,
25
30
  } from "@cloudhal/core-ts/fields";
26
31
  import { renderMarkdown } from "./markdown.js";
27
- import { imageAttributes, } from "./media.js";
32
+ import {
33
+
34
+ imageAttributes,
35
+
36
+
37
+ REFERENCE_BUDGET,
38
+ } from "./media.js";
39
+ import { conversionsFor } from "./reference-conversions.js";
40
+ import { referenceCards } from "./references.js";
28
41
  import { safeHref } from "./safe-urls.js";
29
42
 
30
43
  export {
@@ -37,6 +50,7 @@ export {
37
50
 
38
51
  resolveFields,
39
52
  } from "@cloudhal/core-ts/fields";
53
+
40
54
 
41
55
  /** An image field's picture: `<img>` attributes plus the asset it shows. */
42
56
 
@@ -49,7 +63,7 @@ export {
49
63
 
50
64
 
51
65
 
52
-
66
+
53
67
 
54
68
 
55
69
 
@@ -61,6 +75,10 @@ export {
61
75
 
62
76
 
63
77
 
78
+
79
+
80
+
81
+
64
82
 
65
83
 
66
84
  /** Every defined field of an entry by key, in definition order (`{}` without definitions). */
@@ -77,6 +95,11 @@ export {
77
95
 
78
96
 
79
97
 
98
+
99
+  
100
+
101
+
102
+
80
103
 
81
104
 
82
105
  /** What `entryFields` needs from a source: the site, the original by id, assets by id. */
@@ -84,6 +107,10 @@ export {
84
107
 
85
108
 
86
109
 
110
+  
111
+
112
+    
113
+
87
114
 
88
115
 
89
116
 
@@ -91,9 +118,12 @@ export {
91
118
 
92
119
 
93
120
 
121
+
122
+
94
123
 
95
124
 
96
125
  const SIZES = "(min-width: 48rem) 48rem, 100vw";
126
+ const CARD_SIZES = "(min-width: 48rem) 24rem, 50vw";
97
127
 
98
128
  /** A translation in this site reads its group's original (RFC-0041 §5). */
99
129
  function originalId(entry ) {
@@ -101,14 +131,35 @@ function originalId(entry ) {
101
131
  return t && t.group !== entry.id && t.source?.siteId === undefined ? t.group : undefined;
102
132
  }
103
133
 
104
- function shape(field , o ) {
105
- const { def, value } = field;
106
- const ctx = {
134
+ function mediaOf(o ) {
135
+ return {
107
136
  ...o.media,
108
137
  locale: o.locale,
109
138
  defaultLocale: o.defaultLocale,
110
139
  assets: o.media?.assets ?? o.assets,
111
140
  };
141
+ }
142
+
143
+ function picture(
144
+ asset ,
145
+ ref ,
146
+ o ,
147
+ ) {
148
+ const ctx = mediaOf(o);
149
+ const image = imageAttributes(asset, ref, {
150
+ locale: o.locale,
151
+ defaultLocale: o.defaultLocale,
152
+ transform: ctx.transform ?? "none",
153
+ sizes: o.sizes,
154
+ ...(ctx.preview ? { preview: true } : {}),
155
+ ...(ctx.budget ? { budget: ctx.budget } : {}),
156
+ });
157
+ return { ...image, assetId: asset.id };
158
+ }
159
+
160
+ function shape(field , o ) {
161
+ const { def, value } = field;
162
+ const ctx = mediaOf(o);
112
163
  if (def.type === "richText" && typeof value === "string")
113
164
  return { html: renderMarkdown(value, ctx) };
114
165
  if (def.type === "url" && typeof value === "string") {
@@ -118,15 +169,8 @@ function shape(field , o )
118
169
  if (def.type === "image" && typeof value === "string") {
119
170
  const asset = ctx.assets?.get(value) ?? o.assets?.get(value);
120
171
  if (!asset?.mediaType.startsWith("image/")) return { empty: true };
121
- const image = imageAttributes(asset, undefined, {
122
- locale: o.locale,
123
- defaultLocale: o.defaultLocale,
124
- transform: ctx.transform ?? "none",
125
- sizes: o.sizes ?? SIZES,
126
- ...(ctx.preview ? { preview: true } : {}),
127
- ...(ctx.budget ? { budget: ctx.budget } : {}),
128
- });
129
- return { image: { ...image, assetId: asset.id }, text: image.alt };
172
+ const image = picture(asset, undefined, { ...o, sizes: o.sizes ?? SIZES });
173
+ return { image, text: image.alt };
130
174
  }
131
175
  if (def.type === "date" && typeof value === "string") return { datetime: value };
132
176
  if (def.type === "datetime" && typeof value === "object" && value !== null)
@@ -134,40 +178,138 @@ function shape(field , o )
134
178
  return {};
135
179
  }
136
180
 
181
+ /** A reference: the cards this view resolved, in stored order; nothing of the unresolved ones. */
182
+ function referenceField(base , path , o ) {
183
+ const uses = referenceUses(base.def, base.value, path);
184
+ const cards = uses.flatMap((u) => {
185
+ const card = o.cards?.get(u.path);
186
+ return card ? [card] : [];
187
+ });
188
+ if (cards.length === 0) return { ...base, value: undefined };
189
+ const ids = cards.map((c) => c.groupId);
190
+ return {
191
+ ...base,
192
+ value: Array.isArray(base.value) ? ids : ids[0],
193
+ empty: false,
194
+ text: cards.map((c) => c.title).join(", "),
195
+ references: cards,
196
+ };
197
+ }
198
+
199
+ /** A repeater sub-field read as a field (sub-fields take the repeater's localization). */
200
+ const subField = (sub , parent ) =>
201
+ ({ ...sub, localized: parent.localized }) ;
202
+
203
+ /** A repeater: each item's sub-fields rendered like fields; items with nothing to show left out. */
204
+ function repeaterField(base , path , o ) {
205
+ const def = base.def;
206
+ if (def.type !== "repeater" || !Array.isArray(base.value)) return { ...base, value: undefined };
207
+ const items = [];
208
+ const values = [];
209
+ base.value.forEach((item , i) => {
210
+ if (typeof item !== "object" || item === null || Array.isArray(item)) return;
211
+ const resolved = {};
212
+ for (const sub of def.fields) {
213
+ const value = Object.hasOwn(item, sub.key)
214
+ ? (item )[sub.key]
215
+ : undefined;
216
+ resolved[sub.key] = { def: subField(sub, def), value, fromOriginal: base.fromOriginal };
217
+ }
218
+ const rendered = renderAt(resolved, o, `${path}/${i}`);
219
+ if (!hasShownFields(rendered)) return;
220
+ items.push(rendered);
221
+ values.push(
222
+ Object.fromEntries(
223
+ Object.values(rendered).flatMap((f) => (f.empty ? [] : [[f.key, f.value]])),
224
+ ),
225
+ );
226
+ });
227
+ if (items.length === 0) return { ...base, value: undefined };
228
+ const line = (item ) =>
229
+ Object.values(item)
230
+ .filter((f) => !f.empty && f.text !== "")
231
+ .map((f) => f.text)
232
+ .join(", ");
233
+ return { ...base, value: values, empty: false, text: items.map(line).join("; "), items };
234
+ }
235
+
236
+ function renderOne(
237
+ key ,
238
+ field ,
239
+ o ,
240
+ path ,
241
+ ) {
242
+ const help = fieldHelp(field.def, o.locale, o.defaultLocale);
243
+ const base = {
244
+ ...field,
245
+ key,
246
+ label: fieldLabel(field.def, o.locale, o.defaultLocale),
247
+ ...(help ? { help } : {}),
248
+ empty: true,
249
+ text: "",
250
+ };
251
+ if (!hasFieldValue(field.value)) return base;
252
+ if (field.def.type === "reference") return referenceField(base, path, o);
253
+ if (field.def.type === "repeater") return repeaterField(base, path, o);
254
+ const text = formatFieldValue(field.value, field.def, {
255
+ locale: o.locale,
256
+ defaultLocale: o.defaultLocale,
257
+ timeZone: o.timeZone,
258
+ words: o.words,
259
+ });
260
+ const shown = { ...base, empty: false, text, ...shape(field, o) };
261
+ return shown.empty ? { ...base, value: undefined } : shown;
262
+ }
263
+
264
+ function renderAt(resolved , o , at ) {
265
+ const out = {};
266
+ for (const [key, field] of Object.entries(resolved))
267
+ out[key] = renderOne(key, field, o, `${at}/${key}`);
268
+ return out;
269
+ }
270
+
137
271
  /** Resolved fields made ready to draw (pure; `entryFields` loads what it needs and calls this). */
138
272
  export function renderFields(resolved , o ) {
139
- const out = {};
140
- for (const [key, field] of Object.entries(resolved)) {
141
- const help = fieldHelp(field.def, o.locale, o.defaultLocale);
142
- const base = {
143
- ...field,
144
- key,
145
- label: fieldLabel(field.def, o.locale, o.defaultLocale),
146
- ...(help ? { help } : {}),
147
- empty: true,
148
- text: "",
149
- };
150
- if (!hasFieldValue(field.value)) {
151
- out[key] = base;
152
- continue;
153
- }
154
- const text = formatFieldValue(field.value, field.def, {
155
- locale: o.locale,
156
- defaultLocale: o.defaultLocale,
157
- timeZone: o.timeZone,
158
- words: o.words,
159
- });
160
- const extra = shape(field, o);
161
- const shown = { ...base, empty: false, text, ...extra };
162
- out[key] = shown.empty ? { ...base, value: undefined } : shown;
273
+ return renderAt(resolved, o, "fields");
274
+ }
275
+
276
+ /** The assets image fields and repeater image sub-fields name, by id, that `media` lacks. */
277
+ async function loadAssets(
278
+ source ,
279
+ resolved ,
280
+ media ,
281
+ ) {
282
+ const ids = new Set ();
283
+ for (const field of Object.values(resolved)) {
284
+ const { def, value } = field;
285
+ if (def.type === "image" && typeof value === "string") ids.add(value);
286
+ if (def.type !== "repeater" || !Array.isArray(value)) continue;
287
+ const images = def.fields.filter((s) => s.type === "image").map((s) => s.key);
288
+ for (const item of value )
289
+ for (const k of images) {
290
+ const v = (item )?.[k];
291
+ if (typeof v === "string") ids.add(v);
292
+ }
293
+ }
294
+ const out = new Map ();
295
+ for (const id of ids) {
296
+ if (media?.assets?.has(id)) continue;
297
+ const asset = source.asset ? await source.asset(id) : undefined;
298
+ if (asset) out.set(id, asset);
163
299
  }
164
300
  return out;
165
301
  }
166
302
 
303
+ /** The budget `entryFields` spends: the request's (`media.budget`), else one of its own. */
304
+ function budgetOf(media ) {
305
+ return media?.budget ?? { left: 0, references: REFERENCE_BUDGET };
306
+ }
307
+
167
308
  /**
168
309
  * An entry's typed fields as a theme draws them (RFC-0041 §9), from any source: loads the site,
169
310
  * the translation group's original when the entry is a translation in this site, and the assets
170
- * of image fields (from `media.assets` when given).
311
+ * of image fields (from `media.assets` when given). RFC-0045: references resolve through the
312
+ * source's `groupMembers` (one read per level), gated by the source's view.
171
313
  */
172
314
  export async function entryFields(
173
315
  source ,
@@ -178,21 +320,36 @@ export async function entryFields(
178
320
  if (!site.collections?.[entry.collection]?.fields) return {};
179
321
  const id = originalId(entry);
180
322
  const original = id && source.entryById ? await source.entryById(id) : undefined;
181
- const resolved = resolveFields(site, entry, original);
182
- const assets = new Map ();
183
- for (const field of Object.values(resolved)) {
184
- const v = field.value;
185
- if (field.def.type !== "image" || typeof v !== "string" || options.media?.assets?.has(v))
186
- continue;
187
- const asset = source.asset ? await source.asset(v) : undefined;
188
- if (asset) assets.set(v, asset);
189
- }
190
- return renderFields(resolved, {
323
+ const entries = await conversionsFor(source, site, entry.collection);
324
+ const resolved = resolveFields(site, entry, original, entries ? { entries } : undefined);
325
+ const base = {
191
326
  ...options,
192
327
  locale: options.locale ?? entry.locale,
193
328
  defaultLocale: site.defaultLocale,
194
- assets,
195
- });
329
+ };
330
+ const render = async (r , cards ) =>
331
+ renderFields(r, { ...base, assets: await loadAssets(source, r, options.media), cards });
332
+ const cards = await referenceCards(
333
+ {
334
+ source,
335
+ site,
336
+ locale: base.locale,
337
+ depth: options.depth === 2 ? 2 : 1,
338
+ budget: budgetOf(options.media),
339
+ cover: async (member) => {
340
+ const ref = member.coverImage;
341
+ if (!ref?.assetId) return undefined;
342
+ const asset =
343
+ options.media?.assets?.get(ref.assetId) ??
344
+ (source.asset ? await source.asset(ref.assetId) : undefined);
345
+ if (!asset?.mediaType.startsWith("image/")) return undefined;
346
+ return picture(asset, ref, { ...base, sizes: CARD_SIZES });
347
+ },
348
+ render,
349
+ },
350
+ resolved,
351
+ );
352
+ return render(resolved, cards);
196
353
  }
197
354
 
198
355
  /** True when at least one field has something to show (themes skip the whole list otherwise). */
@@ -1,8 +1,10 @@
1
1
  import type { Asset, ContentEntry, Menu, Page, Redirect, Site, Taxonomy, Term } from "@cloudhal/core-contracts";
2
2
  import type { RedirectMatch, SearchResults, SiteGraphStore } from "@cloudhal/core-ts";
3
+ import { type ConversionEntries } from "@cloudhal/core-ts/fields";
3
4
  import { type FieldEntryQuery, type FieldEntryResult } from "./entry-query.js";
4
5
  import { type EntryFieldsOptions, type RenderedFields } from "./fields.js";
5
6
  import { type Navigation, type NavigationOptions } from "./navigation.js";
7
+ import { type ReferencedByQuery } from "./references.js";
6
8
  import type { GraphSource, SourceSearchOptions } from "./source.js";
7
9
  /**
8
10
  * Reads the current nodes on every call. Drafts are included so authors can preview them in dev,
@@ -30,6 +32,21 @@ export declare class LiveSource implements GraphSource {
30
32
  search(query: string, options?: SourceSearchOptions): Promise<SearchResults>;
31
33
  entryById(id: string): Promise<ContentEntry | undefined>;
32
34
  entryFields(entry: ContentEntry, options?: EntryFieldsOptions): Promise<RenderedFields>;
35
+ /** RFC-0045 §4: one store read (`getGroupMembers`), then this view's gates. */
36
+ groupMembers(groupIds: readonly string[]): Promise<Map<string, ContentEntry[]>>;
37
+ /**
38
+ * RFC-0045 §4: the `entry_refs` rows naming the group, expanded by the `shared` rule, each
39
+ * candidate then resolved like the snapshot's index (its original from the same pool), so both
40
+ * sources answer alike on the same graph.
41
+ */
42
+ referencedBy(groupId: string, q?: ReferencedByQuery): Promise<ContentEntry[]>;
43
+ /**
44
+ * RFC-0045 §7: built from the working copy, only while the collection runs a conversion, from
45
+ * the collections it reads. Kept for `CONVERSION_MAPS_TTL_MS` per collection: one page's calls
46
+ * (every `entryFields`, depth-2 members, `referencedBy`) share one build, while `cloudhal dev`,
47
+ * whose one source serves every request, still sees edits.
48
+ */
49
+ conversionEntries(collection: string): Promise<ConversionEntries | undefined>;
33
50
  taxonomies(): Promise<Taxonomy[]>;
34
51
  terms(key: string): Promise<Term[]>;
35
52
  termBySlug(key: string, slug: string): Promise<Term | undefined>;