@cloudhal/astro-renderer 0.8.0 → 0.10.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 +14 -0
- package/dist/src/astro-env.d.ts +2 -1
- package/dist/src/cloudflare-entry.js +15 -4
- package/dist/src/comments-middleware.js +5 -1
- package/dist/src/components/EntryFields.astro +20 -5
- package/dist/src/fields.d.ts +25 -4
- package/dist/src/fields.js +210 -53
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +5 -1
- package/dist/src/integration.d.ts +14 -0
- package/dist/src/integration.js +24 -1
- package/dist/src/live-source.d.ts +17 -0
- package/dist/src/live-source.js +86 -2
- package/dist/src/media.d.ts +11 -1
- package/dist/src/media.js +15 -2
- package/dist/src/reference-conversions.d.ts +35 -0
- package/dist/src/reference-conversions.js +89 -0
- package/dist/src/references.d.ts +119 -0
- package/dist/src/references.js +346 -0
- package/dist/src/runtime-workerd.d.ts +3 -3
- package/dist/src/runtime-workerd.js +3 -3
- package/dist/src/runtime.d.ts +4 -2
- package/dist/src/runtime.js +14 -5
- package/dist/src/site-dispatch.d.ts +10 -0
- package/dist/src/site-dispatch.js +13 -0
- package/dist/src/source.d.ts +29 -1
- package/dist/src/source.js +71 -1
- package/package.json +6 -6
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. Sites define the two types since rollout step 7, wherever the site Worker reports `fields: 2` (Node always). `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`):
|
package/dist/src/astro-env.d.ts
CHANGED
|
@@ -10,7 +10,8 @@ declare module "virtual:cloudhal/analytics-client" {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
declare module "virtual:cloudhal/config" {
|
|
13
|
-
|
|
13
|
+
/** No `dataDir` on the `cloudflare` target (`virtualConfig`). */
|
|
14
|
+
const config: import("./integration.js").VirtualCloudhalConfig;
|
|
14
15
|
export default config;
|
|
15
16
|
}
|
|
16
17
|
|
|
@@ -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,
|
|
@@ -75,7 +76,13 @@ import { EMBED_HEADER, withEmbedFrames } from "./embed-client.js";
|
|
|
75
76
|
import { hostRedirect, } from "./host-redirect.js";
|
|
76
77
|
import { PREVIEW_PARAM } from "./resolver.js";
|
|
77
78
|
import { SearchLimiter, searchCost } from "./search-limit.js";
|
|
78
|
-
import {
|
|
79
|
+
import {
|
|
80
|
+
|
|
81
|
+
SiteDispatch,
|
|
82
|
+
singleSiteId,
|
|
83
|
+
unknownHost,
|
|
84
|
+
withNoindex,
|
|
85
|
+
} from "./site-dispatch.js";
|
|
79
86
|
import {
|
|
80
87
|
errorPage,
|
|
81
88
|
HEALTH_PATH,
|
|
@@ -173,7 +180,7 @@ const statusHook = themeStatusHook(loadTheme, (m, e) => console.error(m, e));
|
|
|
173
180
|
/** One per isolate: which site a request is for (RFC-0034 §4). */
|
|
174
181
|
function siteDispatch(env , siteIdFromBuild ) {
|
|
175
182
|
dispatch ??= new SiteDispatch({
|
|
176
|
-
siteId:
|
|
183
|
+
siteId: singleSiteId(env, siteIdFromBuild),
|
|
177
184
|
siteIds: env.CLOUDHAL_SITE_IDS,
|
|
178
185
|
sql: () => new D1Driver(env.DB ),
|
|
179
186
|
});
|
|
@@ -277,7 +284,8 @@ function fromAdmin(request , env ) {
|
|
|
277
284
|
|
|
278
285
|
/**
|
|
279
286
|
* What this site Worker enforces (RFC-0008 §7.6–§7.7): the admin Worker asks before it lets anyone
|
|
280
|
-
* write time gates
|
|
287
|
+
* write time gates, and (RFC-0045 §1, `fields: 2`) reference and repeater fields, which this
|
|
288
|
+
* renderer resolves. Authenticated like the purge (it is not a public endpoint), never cached.
|
|
281
289
|
*/
|
|
282
290
|
function features(request , env ) {
|
|
283
291
|
const headers = { "cache-control": "no-store", "cloudflare-cdn-cache-control": "no-store" };
|
|
@@ -288,7 +296,10 @@ function features(request , env ) {
|
|
|
288
296
|
});
|
|
289
297
|
if (!fromAdmin(request, env))
|
|
290
298
|
return Response.json({ error: "unauthorized" }, { status: 401, headers });
|
|
291
|
-
return Response.json(
|
|
299
|
+
return Response.json(
|
|
300
|
+
{ gates: GATES_FEATURE_VERSION, fields: FIELDS_FEATURE_VERSION },
|
|
301
|
+
{ headers },
|
|
302
|
+
);
|
|
292
303
|
}
|
|
293
304
|
|
|
294
305
|
/**
|
|
@@ -19,7 +19,11 @@ const handler = createNodeComments({
|
|
|
19
19
|
// the same local store, site row and published snapshots analytics read
|
|
20
20
|
backend: analyticsBackend(config.siteId, config) ,
|
|
21
21
|
client: config.comments?.available === false ? null : client,
|
|
22
|
-
|
|
22
|
+
// `cloudhal dev` only (a Node target), where the integration always passes dataDir.
|
|
23
|
+
secret: () => {
|
|
24
|
+
if (!config.dataDir) throw new Error("the comments middleware needs the renderer's dataDir");
|
|
25
|
+
return localCommentsKey(config.dataDir);
|
|
26
|
+
},
|
|
23
27
|
onError: (m, e) => console.warn(`cloudhal: ${m}`, e),
|
|
24
28
|
});
|
|
25
29
|
|
|
@@ -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).
|
|
9
|
-
*
|
|
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:
|
|
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.
|
|
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>
|
package/dist/src/fields.d.ts
CHANGED
|
@@ -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
|
|
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). */
|
package/dist/src/fields.js
CHANGED
|
@@ -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 {
|
|
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
|
|
105
|
-
|
|
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 =
|
|
122
|
-
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
|
182
|
-
const
|
|
183
|
-
|
|
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
|
-
|
|
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). */
|
package/dist/src/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export * from "./collection-feeds.js";
|
|
|
12
12
|
export * from "./entry-query.js";
|
|
13
13
|
export * from "./fields.js";
|
|
14
14
|
export * from "./gates.js";
|
|
15
|
-
export { type CloudhalRendererConfig, cloudhal } from "./integration.js";
|
|
15
|
+
export { type CloudhalRendererConfig, cloudhal, type VirtualCloudhalConfig, } from "./integration.js";
|
|
16
16
|
export { renderBody, renderEntryArticle, renderMarkdown, safeHref } from "./markdown.js";
|
|
17
17
|
export * from "./navigation.js";
|
|
18
18
|
export * from "./preview.js";
|
package/dist/src/index.js
CHANGED
|
@@ -12,7 +12,11 @@ export * from "./collection-feeds.js";
|
|
|
12
12
|
export * from "./entry-query.js";
|
|
13
13
|
export * from "./fields.js";
|
|
14
14
|
export * from "./gates.js";
|
|
15
|
-
export {
|
|
15
|
+
export {
|
|
16
|
+
|
|
17
|
+
cloudhal,
|
|
18
|
+
|
|
19
|
+
} from "./integration.js";
|
|
16
20
|
export { renderBody, renderEntryArticle, renderMarkdown, safeHref } from "./markdown.js";
|
|
17
21
|
export * from "./navigation.js";
|
|
18
22
|
export * from "./preview.js";
|
|
@@ -52,4 +52,18 @@ export interface CloudhalRendererConfig {
|
|
|
52
52
|
*/
|
|
53
53
|
readonly commentsScript?: string;
|
|
54
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* The type of `virtual:cloudhal/config`: the integration's config, with `dataDir` absent on the
|
|
57
|
+
* `cloudflare` target (`virtualConfig`). Theme code that reads `dataDir` must handle that.
|
|
58
|
+
*/
|
|
59
|
+
export type VirtualCloudhalConfig = Omit<CloudhalRendererConfig, "dataDir"> & {
|
|
60
|
+
readonly dataDir?: string;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* What pages see as `virtual:cloudhal/config`. On the `cloudflare` target there is no `dataDir`:
|
|
64
|
+
* the Site Worker reads D1 and R2 through its bindings (`runtime-workerd.ts` ignores the field),
|
|
65
|
+
* and the resolved value is the build machine's absolute path, which a release bundle built in CI
|
|
66
|
+
* would otherwise carry to every deployment (RFC-0046 §11b, #239).
|
|
67
|
+
*/
|
|
68
|
+
export declare function virtualConfig(resolved: CloudhalRendererConfig, cloudflare: boolean): VirtualCloudhalConfig;
|
|
55
69
|
export declare function cloudhal(config: CloudhalRendererConfig): AstroIntegration;
|