@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.
Files changed (38) hide show
  1. package/README.md +40 -1
  2. package/dist/src/cloudflare-entry.d.ts +3 -2
  3. package/dist/src/cloudflare-entry.js +3 -2
  4. package/dist/src/collection-feed-middleware.d.ts +5 -0
  5. package/dist/src/collection-feed-middleware.js +22 -0
  6. package/dist/src/collection-feed-route.d.ts +21 -0
  7. package/dist/src/collection-feed-route.js +92 -0
  8. package/dist/src/collection-feeds.d.ts +40 -0
  9. package/dist/src/collection-feeds.js +87 -0
  10. package/dist/src/components/ContentList.astro +26 -9
  11. package/dist/src/components/EntryFields.astro +43 -0
  12. package/dist/src/components/index.d.ts +1 -0
  13. package/dist/src/components/index.js +1 -0
  14. package/dist/src/entry-query.d.ts +85 -0
  15. package/dist/src/entry-query.js +227 -0
  16. package/dist/src/entry-views.d.ts +36 -0
  17. package/dist/src/entry-views.js +73 -0
  18. package/dist/src/fields.d.ts +77 -0
  19. package/dist/src/fields.js +201 -0
  20. package/dist/src/index.d.ts +6 -1
  21. package/dist/src/index.js +10 -1
  22. package/dist/src/integration.js +30 -19
  23. package/dist/src/live-source.d.ts +53 -0
  24. package/dist/src/live-source.js +318 -0
  25. package/dist/src/locales.js +4 -3
  26. package/dist/src/preview.d.ts +16 -1
  27. package/dist/src/preview.js +89 -14
  28. package/dist/src/routing.d.ts +1 -1
  29. package/dist/src/routing.js +13 -3
  30. package/dist/src/seo.d.ts +4 -1
  31. package/dist/src/seo.js +9 -0
  32. package/dist/src/source-helpers.d.ts +11 -0
  33. package/dist/src/source-helpers.js +48 -0
  34. package/dist/src/source.d.ts +40 -45
  35. package/dist/src/source.js +104 -283
  36. package/dist/src/structured-data.d.ts +35 -0
  37. package/dist/src/structured-data.js +88 -0
  38. package/package.json +31 -6
@@ -0,0 +1,227 @@
1
+ /**
2
+ * RFC-0042 §4: listings by field for themes. `queryEntries(source, q)` lists a collection's entries
3
+ * filtered by its list fields (`where`), ordered by one (`orderBy`), on top of the RFC-0002 term
4
+ * filter and the collection's `sort`, paged. Both published sources run the same evaluator
5
+ * (`evaluateEntryQuery`) over what they show, with core-ts's `fields/query.ts` deciding each
6
+ * condition, so live and snapshot answer alike. Pure, Worker-safe; `now` is the source's clock.
7
+ *
8
+ * On a published site an invalid query matches nothing and is logged ("a published snapshot must
9
+ * keep rendering", like unknown terms in `entriesByFilter`): writers refuse invalid queries, so it
10
+ * only happens after a definition changed.
11
+ */
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+ import {
23
+ compareByField,
24
+ matchesFieldQuery,
25
+ originalsOf,
26
+ parseFieldQuery,
27
+
28
+ resolveFields,
29
+ } from "@cloudhal/core-ts/fields";
30
+ import { matches } from "@cloudhal/core-ts/filter";
31
+
32
+ import { compile } from "./source-helpers.js";
33
+
34
+ /** A theme's listing by field (RFC-0042 §4). */
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+  
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+ export const QUERY_DEFAULT_LIMIT = 20;
59
+ export const QUERY_MAX_LIMIT = 100;
60
+
61
+ /** What a source hands the evaluator. */
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+ const published = (e ) => e.publishedAt ?? e.createdAt;
78
+
79
+ /** The listing order of RFC-0040's `sort`, as `ContentList` has always drawn it. */
80
+ function bySort(sort ) {
81
+ if (sort === "title") return (a, b) => a.title.localeCompare(b.title);
82
+ if (sort === "oldest") return (a, b) => published(a).localeCompare(published(b));
83
+ return (a, b) => published(b).localeCompare(published(a));
84
+ }
85
+
86
+ const byId = (a , b ) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
87
+
88
+ const clampInt = (n , min , max , fallback ) =>
89
+ n === undefined || !Number.isFinite(n) ? fallback : Math.min(max, Math.max(min, Math.trunc(n)));
90
+
91
+ /** The term filter as a predicate; an invalid one matches nothing (logged). */
92
+ function termPredicate(input ) {
93
+ const { filter } = input.query;
94
+ if (!filter) return undefined;
95
+ try {
96
+ const f = compile(filter, [...input.taxonomies], [...input.terms]);
97
+ return (e) => matches(f, e);
98
+ } catch (err) {
99
+ console.warn(`cloudhal: query filter "${filter}" is invalid: ${String(err)}`);
100
+ return () => false;
101
+ }
102
+ }
103
+
104
+ /** Runs a query over what a source shows. Pure. */
105
+ export function evaluateEntryQuery(input ) {
106
+ const { site, query: q } = input;
107
+ const limit = clampInt(q.limit, 1, QUERY_MAX_LIMIT, QUERY_DEFAULT_LIMIT);
108
+ const offset = clampInt(q.offset, 0, Number.MAX_SAFE_INTEGER, 0);
109
+ const sort = bySort(q.sort ?? site.collections?.[q.collection]?.sort ?? "newest");
110
+ const byTerms = termPredicate(input);
111
+ let rows = input.candidates.filter((e) => !byTerms || byTerms(e));
112
+ if (q.where?.length || q.orderBy) {
113
+ const parsed = parseFieldQuery(site, q.collection, { where: q.where, orderBy: q.orderBy });
114
+ if (!parsed.ok) {
115
+ const shown = parsed.issues.map((i) => `${i.path}: ${i.message}`).join("; ");
116
+ console.warn(`cloudhal: query on ${q.collection} matches nothing: ${shown}`);
117
+ return { entries: [], total: 0 };
118
+ }
119
+ const originalOf = originalsOf(input.published);
120
+ const cache = input.cache ?? new Map ();
121
+ const values = (e ) => {
122
+ const known = cache.get(e.id);
123
+ if (known) return known;
124
+ const v = resolveFields(site, e, originalOf(e));
125
+ cache.set(e.id, v);
126
+ return v;
127
+ };
128
+ rows = rows.filter((e) => matchesFieldQuery(values(e), parsed.query, input.now));
129
+ rows.sort(
130
+ (a, b) => compareByField(values(a), values(b), parsed.query) || sort(a, b) || byId(a, b),
131
+ );
132
+ } else rows = [...rows].sort((a, b) => sort(a, b) || byId(a, b));
133
+ return { entries: rows.slice(offset, offset + limit), total: rows.length };
134
+ }
135
+
136
+ /** What a list handed to a theme came from: its source and locale, or a query already run. */
137
+
138
+ const origins = new WeakMap ();
139
+
140
+ /**
141
+ * Marks a list a source returned (`entries`, `entriesByFilter`) so `ContentList` can apply a
142
+ * section's `where`/`orderBy` even for theme copies from before RFC-0042, which pass the
143
+ * collection's list as it is. Returns the list.
144
+ */
145
+ export function rememberList (list , origin ) {
146
+ origins.set(list, origin);
147
+ return list;
148
+ }
149
+
150
+ /** Where a list came from, if a source or `queryEntries` made it. */
151
+ export function listOrigin(list ) {
152
+ return origins.get(list);
153
+ }
154
+
155
+ /**
156
+ * Lists `q.collection` by its list fields on any source: the source's own `queryEntries`, or, on
157
+ * sources from before RFC-0042, the same evaluator over `entries()` (no `includeExpired` there).
158
+ */
159
+ export async function queryEntries(
160
+ source ,
161
+ q ,
162
+ ) {
163
+ const result = source.queryEntries
164
+ ? await source.queryEntries(q)
165
+ : await (async () => {
166
+ const site = await source.site();
167
+ const candidates = await source.entries(q.collection, q.locale);
168
+ const [taxonomies, terms] = q.filter
169
+ ? await Promise.all([source.taxonomies(), source.vocabulary()])
170
+ : [[], []];
171
+ return evaluateEntryQuery({
172
+ site,
173
+ candidates,
174
+ published: candidates,
175
+ taxonomies,
176
+ terms,
177
+ query: q,
178
+ now: Date.now(),
179
+ });
180
+ })();
181
+ rememberList(result.entries, "queried");
182
+ return result;
183
+ }
184
+
185
+ /** The query a `contentList` section with `where`/`orderBy` runs (its own `limit`, at most 100). */
186
+ export function sectionQuery(section , locale ) {
187
+ return {
188
+ collection: section.collection,
189
+ locale,
190
+ sort: section.sort,
191
+ limit: Math.min(section.limit, QUERY_MAX_LIMIT),
192
+ ...(section.where ? { where: section.where } : {}),
193
+ ...(section.orderBy ? { orderBy: section.orderBy } : {}),
194
+ ...(section.filter ? { filter: section.filter } : {}),
195
+ };
196
+ }
197
+
198
+ /** True when a section lists by field (RFC-0042). */
199
+ export const listsByField = (section ) =>
200
+ section.where !== undefined || section.orderBy !== undefined;
201
+
202
+ /**
203
+ * The entries a page's `contentList` sections draw, keyed as `Sections` reads them: by collection
204
+ * (the collection's newest listed entries), by section id for a section with a term `filter`
205
+ * (RFC-0002), and by section id for a section with `where`/`orderBy` (RFC-0042, through
206
+ * `queryEntries`). An invalid filter lists nothing and is logged.
207
+ */
208
+ export async function contentListEntries(
209
+ source ,
210
+ sections ,
211
+ locale ,
212
+ ) {
213
+ const lists = sections.filter((s) => s.kind === "contentList");
214
+ const out = {};
215
+ for (const c of new Set(lists.map((s) => s.collection))) out[c] = await source.entries(c, locale);
216
+ for (const s of lists) {
217
+ if (listsByField(s)) out[s.id] = (await queryEntries(source, sectionQuery(s, locale))).entries;
218
+ else if (s.filter)
219
+ out[s.id] = await source
220
+ .entriesByFilter(`collection:${s.collection}+${s.filter}`, locale ? { locale } : {})
221
+ .catch((err ) => {
222
+ console.warn(`cloudhal: section ${s.id} has an invalid filter: ${String(err)}`);
223
+ return [];
224
+ });
225
+ }
226
+ return out;
227
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * RFC-0042 §3 in the published sources: an entry's derived gate (its collection's `expiry`) next to
3
+ * its own `availability` (RFC-0008). `unlist` keeps the entry's page (and comments, menus,
4
+ * alternates) but leaves it out of every listing, feed, the sitemap and search; `hide` answers 404
5
+ * like an expired `availability`. The gate index gets every derived instant and, for `now`
6
+ * conditions on list fields (§4), every value instant, so the edge cache expires at each one.
7
+ * Pure helpers shared by the live and snapshot sources; previews never call them.
8
+ */
9
+ import type { ContentEntry, Page, Site } from "@cloudhal/core-contracts";
10
+ import { type DerivedGate } from "@cloudhal/core-ts/fields";
11
+ import type { GraphSource } from "./source.js";
12
+ /** What the public sees of a set of entries at one instant. */
13
+ export interface EntrySplit {
14
+ /** Shown entries: every listing, feed, sitemap and search. */
15
+ readonly listed: ContentEntry[];
16
+ /** Shown and unlisted entries: everything with a public URL (routes, comments, menus). */
17
+ readonly reachable: ContentEntry[];
18
+ }
19
+ /**
20
+ * Splits `entries` at `now` by status, `availability` and their derived gates (`derivedGates` over
21
+ * every published entry a translation may read from; a snapshot computes it once). The snapshot
22
+ * and the live source pass the same sets, so they agree.
23
+ */
24
+ export declare function splitEntries(entries: readonly ContentEntry[], now: number, derived: ReadonlyMap<string, DerivedGate>): EntrySplit;
25
+ /**
26
+ * The gate index of published nodes (RFC-0008 §7.4, RFC-0042 §3.4 and §4): every `from`/`until`,
27
+ * every derived gate, and every instant a `now` condition on a list field may flip at; sorted,
28
+ * unique. Empty for a site that schedules nothing and has no such settings.
29
+ */
30
+ export declare function gateIndex(site: Site, pages: readonly Page[], entries: readonly ContentEntry[], gates?: ReadonlyMap<string, DerivedGate>): number[];
31
+ /**
32
+ * Every entry of a locale with a public URL (unlisted ones included), newest first: what routes,
33
+ * static builds and language alternates need. Sources from before RFC-0042 list all of them in
34
+ * `allEntries`.
35
+ */
36
+ export declare function routableEntries(source: GraphSource, locale?: string): Promise<ContentEntry[]>;
@@ -0,0 +1,73 @@
1
+ /**
2
+ * RFC-0042 §3 in the published sources: an entry's derived gate (its collection's `expiry`) next to
3
+ * its own `availability` (RFC-0008). `unlist` keeps the entry's page (and comments, menus,
4
+ * alternates) but leaves it out of every listing, feed, the sitemap and search; `hide` answers 404
5
+ * like an expired `availability`. The gate index gets every derived instant and, for `now`
6
+ * conditions on list fields (§4), every value instant, so the edge cache expires at each one.
7
+ * Pure helpers shared by the live and snapshot sources; previews never call them.
8
+ */
9
+
10
+ import { gateInstants } from "@cloudhal/core-ts/availability";
11
+ import {
12
+
13
+ derivedGates,
14
+ entryVisibility,
15
+ listFieldInstants,
16
+ } from "@cloudhal/core-ts/fields";
17
+
18
+
19
+ /** What the public sees of a set of entries at one instant. */
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+ /**
28
+ * Splits `entries` at `now` by status, `availability` and their derived gates (`derivedGates` over
29
+ * every published entry a translation may read from; a snapshot computes it once). The snapshot
30
+ * and the live source pass the same sets, so they agree.
31
+ */
32
+ export function splitEntries(
33
+ entries ,
34
+ now ,
35
+ derived ,
36
+ ) {
37
+ const listed = [];
38
+ const reachable = [];
39
+ for (const e of entries) {
40
+ const v = entryVisibility(e, derived.get(e.id), now);
41
+ if (v === "hidden") continue;
42
+ reachable.push(e);
43
+ if (v === "shown") listed.push(e);
44
+ }
45
+ return { listed, reachable };
46
+ }
47
+
48
+ /**
49
+ * The gate index of published nodes (RFC-0008 §7.4, RFC-0042 §3.4 and §4): every `from`/`until`,
50
+ * every derived gate, and every instant a `now` condition on a list field may flip at; sorted,
51
+ * unique. Empty for a site that schedules nothing and has no such settings.
52
+ */
53
+ export function gateIndex(
54
+ site ,
55
+ pages ,
56
+ entries ,
57
+ gates = derivedGates(site, entries),
58
+ ) {
59
+ const own = gateInstants([...pages, ...entries]);
60
+ const derived = [...gates.values()].map((g) => g.at);
61
+ const lists = listFieldInstants(site, entries);
62
+ if (derived.length === 0 && lists.length === 0) return own;
63
+ return [...new Set([...own, ...derived, ...lists])].sort((a, b) => a - b);
64
+ }
65
+
66
+ /**
67
+ * Every entry of a locale with a public URL (unlisted ones included), newest first: what routes,
68
+ * static builds and language alternates need. Sources from before RFC-0042 list all of them in
69
+ * `allEntries`.
70
+ */
71
+ export function routableEntries(source , locale ) {
72
+ return source.routableEntries ? source.routableEntries(locale) : source.allEntries(locale);
73
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Typed custom fields for themes (RFC-0041 §9 / ADR-0043). `entry.fields` keeps meaning exactly
3
+ * what is stored; this module gives themes the values a reader should see: resolved (§5: a
4
+ * translation's empty localized field reads the original's, non-localized fields always come from
5
+ * the translation group's original), labelled in the page's language and made safe to draw.
6
+ *
7
+ * - rich text: HTML from the body's markdown pipeline (raw HTML escaped, `safeHref` on every link,
8
+ * `asset:` references resolved), never stored HTML;
9
+ * - URLs: through `safeHref`, dropped when unsafe;
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.
12
+ *
13
+ * Worker-safe; the same function serves live and snapshot sources.
14
+ */
15
+ import type { Asset, ContentEntry, Site } from "@cloudhal/core-contracts";
16
+ import { type FormatFieldOptions, type ResolvedField, type ResolvedFields } from "@cloudhal/core-ts/fields";
17
+ import { type ImageAttributes, type MediaContext } from "./media.js";
18
+ export { type FormatFieldOptions, fieldHelp, fieldLabel, formatFieldValue, optionLabel, type ResolvedField, type ResolvedFields, resolveFields, } from "@cloudhal/core-ts/fields";
19
+ /** An image field's picture: `<img>` attributes plus the asset it shows. */
20
+ export interface FieldImage extends ImageAttributes {
21
+ assetId: string;
22
+ }
23
+ /** One field as a theme draws it. */
24
+ export interface RenderedField extends ResolvedField {
25
+ key: string;
26
+ /** The label in the page's language (then the site's default, then any, then the key). */
27
+ label: string;
28
+ help?: string;
29
+ /** Nothing to show: no value, an unreadable one, an unsafe URL or a missing image. */
30
+ empty: boolean;
31
+ /** The value as plain text in the page's language ("" when empty). Print it escaped. */
32
+ text: string;
33
+ /** `richText`: sanitized HTML from the body's markdown pipeline. */
34
+ html?: string;
35
+ /** `url`: the link target, checked by `safeHref`. */
36
+ href?: string;
37
+ /** `image`: the picture. */
38
+ image?: FieldImage;
39
+ /** `date` (`YYYY-MM-DD`) and `datetime` (the UTC instant): for `<time datetime>`. */
40
+ datetime?: string;
41
+ }
42
+ /** Every defined field of an entry by key, in definition order (`{}` without definitions). */
43
+ export type RenderedFields = Record<string, RenderedField>;
44
+ export interface EntryFieldsOptions {
45
+ /** The page's language (default: the entry's). Labels, formatting and image alt follow it. */
46
+ locale?: string;
47
+ /** The page's media context: assets for images and `asset:` links, transforms, placeholders. */
48
+ media?: MediaContext;
49
+ /** `datetime` values show in this zone (default: each value's own zone). */
50
+ timeZone?: string;
51
+ /** The words for yes/no fields (default: English). */
52
+ words?: FormatFieldOptions["words"];
53
+ /** `sizes` for image fields (default: the article column). */
54
+ sizes?: string;
55
+ }
56
+ /** What `entryFields` needs from a source: the site, the original by id, assets by id. */
57
+ export interface FieldsSource {
58
+ site(): Promise<Site>;
59
+ entryById?(id: string): Promise<ContentEntry | undefined>;
60
+ asset?(id: string): Promise<Asset | undefined>;
61
+ }
62
+ export interface RenderFieldsOptions extends EntryFieldsOptions {
63
+ locale: string;
64
+ defaultLocale: string;
65
+ /** Assets by id (image fields; rich text's `asset:` links when `media.assets` is absent). */
66
+ assets?: ReadonlyMap<string, Asset> | undefined;
67
+ }
68
+ /** Resolved fields made ready to draw (pure; `entryFields` loads what it needs and calls this). */
69
+ export declare function renderFields(resolved: ResolvedFields, o: RenderFieldsOptions): RenderedFields;
70
+ /**
71
+ * An entry's typed fields as a theme draws them (RFC-0041 §9), from any source: loads the site,
72
+ * 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).
74
+ */
75
+ export declare function entryFields(source: FieldsSource, entry: ContentEntry, options?: EntryFieldsOptions): Promise<RenderedFields>;
76
+ /** True when at least one field has something to show (themes skip the whole list otherwise). */
77
+ export declare function hasShownFields(fields: RenderedFields): boolean;
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Typed custom fields for themes (RFC-0041 §9 / ADR-0043). `entry.fields` keeps meaning exactly
3
+ * what is stored; this module gives themes the values a reader should see: resolved (§5: a
4
+ * translation's empty localized field reads the original's, non-localized fields always come from
5
+ * the translation group's original), labelled in the page's language and made safe to draw.
6
+ *
7
+ * - rich text: HTML from the body's markdown pipeline (raw HTML escaped, `safeHref` on every link,
8
+ * `asset:` references resolved), never stored HTML;
9
+ * - URLs: through `safeHref`, dropped when unsafe;
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.
12
+ *
13
+ * Worker-safe; the same function serves live and snapshot sources.
14
+ */
15
+
16
+ import {
17
+
18
+ fieldHelp,
19
+ fieldLabel,
20
+ formatFieldValue,
21
+ hasFieldValue,
22
+
23
+
24
+ resolveFields,
25
+ } from "@cloudhal/core-ts/fields";
26
+ import { renderMarkdown } from "./markdown.js";
27
+ import { imageAttributes, } from "./media.js";
28
+ import { safeHref } from "./safe-urls.js";
29
+
30
+ export {
31
+
32
+ fieldHelp,
33
+ fieldLabel,
34
+ formatFieldValue,
35
+ optionLabel,
36
+
37
+
38
+ resolveFields,
39
+ } from "@cloudhal/core-ts/fields";
40
+
41
+ /** An image field's picture: `<img>` attributes plus the asset it shows. */
42
+
43
+
44
+
45
+
46
+ /** One field as a theme draws it. */
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+ /** Every defined field of an entry by key, in definition order (`{}` without definitions). */
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+ /** What `entryFields` needs from a source: the site, the original by id, assets by id. */
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+ const SIZES = "(min-width: 48rem) 48rem, 100vw";
97
+
98
+ /** A translation in this site reads its group's original (RFC-0041 §5). */
99
+ function originalId(entry ) {
100
+ const t = entry.translation;
101
+ return t && t.group !== entry.id && t.source?.siteId === undefined ? t.group : undefined;
102
+ }
103
+
104
+ function shape(field , o ) {
105
+ const { def, value } = field;
106
+ const ctx = {
107
+ ...o.media,
108
+ locale: o.locale,
109
+ defaultLocale: o.defaultLocale,
110
+ assets: o.media?.assets ?? o.assets,
111
+ };
112
+ if (def.type === "richText" && typeof value === "string")
113
+ return { html: renderMarkdown(value, ctx) };
114
+ if (def.type === "url" && typeof value === "string") {
115
+ const href = safeHref(value);
116
+ return href ? { href, text: href } : { empty: true };
117
+ }
118
+ if (def.type === "image" && typeof value === "string") {
119
+ const asset = ctx.assets?.get(value) ?? o.assets?.get(value);
120
+ 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 };
130
+ }
131
+ if (def.type === "date" && typeof value === "string") return { datetime: value };
132
+ if (def.type === "datetime" && typeof value === "object" && value !== null)
133
+ return { datetime: (value ).at };
134
+ return {};
135
+ }
136
+
137
+ /** Resolved fields made ready to draw (pure; `entryFields` loads what it needs and calls this). */
138
+ 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;
163
+ }
164
+ return out;
165
+ }
166
+
167
+ /**
168
+ * An entry's typed fields as a theme draws them (RFC-0041 §9), from any source: loads the site,
169
+ * 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).
171
+ */
172
+ export async function entryFields(
173
+ source ,
174
+ entry ,
175
+ options = {},
176
+ ) {
177
+ const site = await source.site();
178
+ if (!site.collections?.[entry.collection]?.fields) return {};
179
+ const id = originalId(entry);
180
+ 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, {
191
+ ...options,
192
+ locale: options.locale ?? entry.locale,
193
+ defaultLocale: site.defaultLocale,
194
+ assets,
195
+ });
196
+ }
197
+
198
+ /** True when at least one field has something to show (themes skip the whole list otherwise). */
199
+ export function hasShownFields(fields ) {
200
+ return Object.values(fields).some((f) => !f.empty);
201
+ }
@@ -3,10 +3,14 @@
3
3
  *
4
4
  * - `cloudhal()` integration (`./integration`) exposes the project config as `virtual:cloudhal/config`.
5
5
  * - `getSource(config)` (`./runtime`) returns a `GraphSource`: live nodes in dev, a snapshot in builds.
6
- * - `.astro` components (`./components`): `Sections` dispatcher + Hero/RichText/Image/ContentList.
6
+ * - `.astro` components (`./components`): `Sections` dispatcher + Hero/RichText/Image/ContentList,
7
+ * `EntryFields` (RFC-0041's typed fields; values from `./fields`).
7
8
  */
8
9
  export * from "./archive.js";
9
10
  export * from "./cache-tags.js";
11
+ export * from "./collection-feeds.js";
12
+ export * from "./entry-query.js";
13
+ export * from "./fields.js";
10
14
  export * from "./gates.js";
11
15
  export { type CloudhalRendererConfig, cloudhal } from "./integration.js";
12
16
  export { renderBody, renderEntryArticle, renderMarkdown, safeHref } from "./markdown.js";
@@ -14,4 +18,5 @@ export * from "./navigation.js";
14
18
  export * from "./preview.js";
15
19
  export { getSource, resetSource } from "./runtime.js";
16
20
  export * from "./source.js";
21
+ export { entryStructuredData, type StructuredDataOptions, withStructuredData, } from "./structured-data.js";
17
22
  export * from "./taxonomy.js";
package/dist/src/index.js CHANGED
@@ -3,10 +3,14 @@
3
3
  *
4
4
  * - `cloudhal()` integration (`./integration`) exposes the project config as `virtual:cloudhal/config`.
5
5
  * - `getSource(config)` (`./runtime`) returns a `GraphSource`: live nodes in dev, a snapshot in builds.
6
- * - `.astro` components (`./components`): `Sections` dispatcher + Hero/RichText/Image/ContentList.
6
+ * - `.astro` components (`./components`): `Sections` dispatcher + Hero/RichText/Image/ContentList,
7
+ * `EntryFields` (RFC-0041's typed fields; values from `./fields`).
7
8
  */
8
9
  export * from "./archive.js";
9
10
  export * from "./cache-tags.js";
11
+ export * from "./collection-feeds.js";
12
+ export * from "./entry-query.js";
13
+ export * from "./fields.js";
10
14
  export * from "./gates.js";
11
15
  export { cloudhal } from "./integration.js";
12
16
  export { renderBody, renderEntryArticle, renderMarkdown, safeHref } from "./markdown.js";
@@ -14,4 +18,9 @@ export * from "./navigation.js";
14
18
  export * from "./preview.js";
15
19
  export { getSource, resetSource } from "./runtime.js";
16
20
  export * from "./source.js";
21
+ export {
22
+ entryStructuredData,
23
+
24
+ withStructuredData,
25
+ } from "./structured-data.js";
17
26
  export * from "./taxonomy.js";