@cloudhal/astro-renderer 0.4.0 → 0.5.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
@@ -1,12 +1,13 @@
1
1
  # @cloudhal/astro-renderer
2
2
 
3
- Renders a Cloudhal Site Graph with Astro 7. Three pieces:
3
+ Renders a Cloudhal Site Graph with Astro 7. The main pieces:
4
4
 
5
5
  | Export | What |
6
6
  |---|---|
7
7
  | `@cloudhal/astro-renderer/integration` | `cloudhal({ dataDir, siteId })` — exposes the project config as `virtual:cloudhal/config`, externalises the native SQLite module, and in dev watches the data directory to clear Astro's route cache when pages are added (Astro ≥6.3 caches `getStaticPaths()` across requests). |
8
8
  | `@cloudhal/astro-renderer/runtime` | `getSource(config)` — a `GraphSource`: **live** nodes in dev (drafts visible), a **snapshot** in builds (`CLOUDHAL_SOURCE=snapshot:<id>`). |
9
9
  | `@cloudhal/astro-renderer/components` | `Sections` dispatcher + `Hero`, `RichText`, `Image`, `ContentList` `.astro` components. Themes override any of them through the `components` prop. |
10
+ | `@cloudhal/astro-renderer/fields` | Typed custom fields (RFC-0041 §9): `entryFields`, `renderFields`, `hasShownFields`, and `resolveFields`, `formatFieldValue`, `fieldLabel`, `fieldHelp`, `optionLabel` re-exported from `@cloudhal/core-ts/fields`. `EntryFields.astro` in `./components` draws them. |
10
11
 
11
12
  A page route in a starter looks like this:
12
13
 
@@ -30,6 +31,20 @@ const page = await source.pageByPath(Astro.params.path ? `/${Astro.params.path}`
30
31
  ## Theme-facing API stability
31
32
  `cloudhal init` copies `starters/blog/src` into each project, so a site keeps calling this package's exports (and `@cloudhal/core-ts`'s) as they were when it was created. Every export a theme imports is therefore a public API: never change its meaning under the same name. A breaking change keeps a deprecated fallback for the old call. In 0.3.11 `entryPath(entry)` became `entryPath(site, entry)` without one, and every site with an older theme copy rendered empty pages until 0.3.13 restored it. `packages/cli/test/old-theme-compat.test.ts` builds frozen copies of earlier starters (`packages/cli/test/fixtures/theme-v*/`) with today's packages. Add a new frozen copy when a release changes theme-facing APIs.
32
33
 
34
+ ### Typed custom fields (RFC-0041, added in 0.5.0)
35
+ Additive; `entry.fields` still means exactly what is stored, and themes that never call these keep rendering as before (`theme-v0.4.0` in the compat test).
36
+
37
+ | Export | Signature |
38
+ |---|---|
39
+ | `GraphSource.entryFields?` | `(entry: ContentEntry, options?: EntryFieldsOptions) => Promise<RenderedFields>` on `LiveSource` and `SnapshotSource` (optional on the interface, like `search?`) |
40
+ | `entryFields` (`./fields`, `.`) | `(source: FieldsSource, entry: ContentEntry, options?: EntryFieldsOptions) => Promise<RenderedFields>`: the same for any source (`site()`, optional `entryById()` and `asset()`) |
41
+ | `renderFields` | `(resolved: ResolvedFields, options: RenderFieldsOptions) => RenderedFields`: the pure step, for values you resolved yourself |
42
+ | `hasShownFields` | `(fields: RenderedFields) => boolean` |
43
+ | `resolveFields`, `formatFieldValue`, `fieldLabel`, `fieldHelp`, `optionLabel` | re-exported from `@cloudhal/core-ts/fields` |
44
+ | `EntryFields` (`./components`) | `<EntryFields fields={…} label?="Details" class?="…" />`: a `<dl class="ch-fields">` of the fields with a value, nothing when none has one; no styles of its own |
45
+
46
+ `EntryFieldsOptions`: `locale` (default: the entry's), `media` (the page's `MediaContext`), `timeZone`, `words` (`{ yes, no }`), `sizes`. Each `RenderedField` has `key`, `label`, `help?`, `empty`, `text` (escaped plain text), and per type `html` (rich text through the body's markdown pipeline), `href` (`safeHref`, dropped when unsafe), `image` (`imageAttributes` plus `assetId`, alt in the page's language) or `datetime`, plus `def`, `value` and `fromOriginal` from `resolveFields`. Theme guide: [`docs/guides/fields.md`](../../docs/guides/fields.md).
47
+
33
48
  ## Site analytics (RFC-0006)
34
49
 
35
50
  The renderer carries the site side of Cloudhal's cookie-less analytics (`@cloudhal/analytics`):
@@ -0,0 +1,43 @@
1
+ ---
2
+ /**
3
+ * An entry's typed fields (RFC-0041 §9) as a plain definition list: one `dt`/`dd` pair per field
4
+ * with something to show, in definition order; nothing at all when no field has a value. Pass what
5
+ * `source.entryFields(entry, { locale, media })` (or `entryFields(source, entry, …)`) returns.
6
+ *
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>"]`.
10
+ */
11
+ import type { RenderedFields } from "../fields.js";
12
+
13
+ interface Props {
14
+ fields: RenderedFields;
15
+ /** An accessible name for the list (e.g. "Details" in the page's language). */
16
+ label?: string;
17
+ /** Extra classes on the `dl`. */
18
+ class?: string;
19
+ }
20
+ const { fields, label, class: className } = Astro.props;
21
+ const shown = Object.values(fields).filter((f) => !f.empty);
22
+ const multiline = (f: (typeof shown)[number]) => f.def.type === "text" && f.def.multiline === true;
23
+ ---
24
+ {shown.length > 0 && (
25
+ <dl class:list={["ch-fields", className]} aria-label={label}>
26
+ {shown.map((f) => (
27
+ <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
+ <dt>{f.label}</dt>
29
+ {f.html !== undefined ? (
30
+ <dd set:html={f.html} />
31
+ ) : f.href !== undefined ? (
32
+ <dd><a href={f.href}>{f.text}</a></dd>
33
+ ) : f.image ? (
34
+ <dd><img src={f.image.src} srcset={f.image.srcset} sizes={f.image.sizes} alt={f.image.alt} width={f.image.width || undefined} height={f.image.height || undefined} loading={f.image.loading} decoding={f.image.decoding} style={f.image.style} /></dd>
35
+ ) : f.datetime !== undefined ? (
36
+ <dd><time datetime={f.datetime}>{f.text}</time></dd>
37
+ ) : (
38
+ <dd>{f.text}</dd>
39
+ )}
40
+ </div>
41
+ ))}
42
+ </dl>
43
+ )}
@@ -3,6 +3,7 @@ export { default as Body } from "./Body.astro";
3
3
  export { default as Comments } from "./Comments.astro";
4
4
  export { default as ContentList } from "./ContentList.astro";
5
5
  export { default as EntryArticle } from "./EntryArticle.astro";
6
+ export { default as EntryFields } from "./EntryFields.astro";
6
7
  export { default as Hero } from "./Hero.astro";
7
8
  export { default as Image } from "./Image.astro";
8
9
  export { default as RichText } from "./RichText.astro";
@@ -3,6 +3,7 @@ export { default as Body } from "./Body.astro";
3
3
  export { default as Comments } from "./Comments.astro";
4
4
  export { default as ContentList } from "./ContentList.astro";
5
5
  export { default as EntryArticle } from "./EntryArticle.astro";
6
+ export { default as EntryFields } from "./EntryFields.astro";
6
7
  export { default as Hero } from "./Hero.astro";
7
8
  export { default as Image } from "./Image.astro";
8
9
  export { default as RichText } from "./RichText.astro";
@@ -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,12 @@
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 "./fields.js";
10
12
  export * from "./gates.js";
11
13
  export { type CloudhalRendererConfig, cloudhal } from "./integration.js";
12
14
  export { renderBody, renderEntryArticle, renderMarkdown, safeHref } from "./markdown.js";
package/dist/src/index.js CHANGED
@@ -3,10 +3,12 @@
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 "./fields.js";
10
12
  export * from "./gates.js";
11
13
  export { cloudhal } from "./integration.js";
12
14
  export { renderBody, renderEntryArticle, renderMarkdown, safeHref } from "./markdown.js";
@@ -0,0 +1,11 @@
1
+ /** Small pure helpers of `source.ts`: ordering, visibility and filter compilation. */
2
+ import type { ContentEntry, Page, Taxonomy, Term } from "@cloudhal/core-contracts";
3
+ export declare function byNewest(a: ContentEntry, b: ContentEntry): number;
4
+ export declare function byPosition(locale: string): (a: Term, b: Term) => number;
5
+ export declare function shown(term: Term, includeDrafts: boolean): boolean;
6
+ export declare function termFilter(key: string, slug: string, descendants: boolean): string;
7
+ /** Resolves a filter against a vocabulary; unknown terms match nothing and are logged. */
8
+ export declare function compile(filter: string, taxonomies: Taxonomy[], terms: Term[]): import("@cloudhal/core-ts").ResolvedFilter;
9
+ export declare function visible<T extends {
10
+ status: Page["status"];
11
+ }>(nodes: T[], includeDrafts: boolean): T[];
@@ -0,0 +1,48 @@
1
+ /** Small pure helpers of `source.ts`: ordering, visibility and filter compilation. */
2
+
3
+ import { termName } from "@cloudhal/core-ts";
4
+ import { parseFilter, resolveFilter } from "@cloudhal/core-ts/filter";
5
+
6
+ export function byNewest(a , b ) {
7
+ return (b.publishedAt ?? b.createdAt).localeCompare(a.publishedAt ?? a.createdAt);
8
+ }
9
+
10
+ export function byPosition(locale ) {
11
+ return (a , b ) =>
12
+ (a.position ?? Number.MAX_SAFE_INTEGER) - (b.position ?? Number.MAX_SAFE_INTEGER) ||
13
+ termName(a, locale).localeCompare(termName(b, locale)) ||
14
+ a.slug.localeCompare(b.slug);
15
+ }
16
+
17
+ export function shown(term , includeDrafts ) {
18
+ return includeDrafts
19
+ ? term.status !== "retired"
20
+ : term.status === "active" && term.visibility === "public";
21
+ }
22
+
23
+ export function termFilter(key , slug , descendants ) {
24
+ return `${key}:${slug}${descendants ? ">*" : ""}`;
25
+ }
26
+
27
+ /** Resolves a filter against a vocabulary; unknown terms match nothing and are logged. */
28
+ export function compile(filter , taxonomies , terms ) {
29
+ const resolved = resolveFilter(
30
+ parseFilter(filter),
31
+ { taxonomies, terms },
32
+ { unknown: "match-nothing" },
33
+ );
34
+ if (resolved.unknown.length > 0)
35
+ console.warn(
36
+ `cloudhal: filter "${filter}" names unknown terms: ${resolved.unknown.join(", ")}`,
37
+ );
38
+ return resolved;
39
+ }
40
+
41
+ export function visible (
42
+ nodes ,
43
+ includeDrafts ,
44
+ ) {
45
+ return includeDrafts
46
+ ? nodes.filter((n) => n.status !== "archived")
47
+ : nodes.filter((n) => n.status === "published");
48
+ }
@@ -1,5 +1,6 @@
1
1
  import type { Asset, ContentEntry, Menu, Page, Redirect, Site, Snapshot, Taxonomy, Term } from "@cloudhal/core-contracts";
2
2
  import type { RedirectMatch, SearchKind, SearchResults, SiteGraphStore } from "@cloudhal/core-ts";
3
+ import { type EntryFieldsOptions, type RenderedFields } from "./fields.js";
3
4
  import { type Navigation, type NavigationOptions } from "./navigation.js";
4
5
  /**
5
6
  * What a renderer needs from a site, independent of whether it reads live nodes (dev) or a
@@ -80,6 +81,14 @@ export interface GraphSource {
80
81
  * entries it shows, drafts included. Optional for sources that predate search.
81
82
  */
82
83
  search?(query: string, options?: SourceSearchOptions): Promise<SearchResults>;
84
+ /**
85
+ * RFC-0041 §9: an entry's typed fields as a theme draws them: resolved (a translation reads the
86
+ * original's values where §5 says so), labelled in `options.locale` (default: the entry's), rich
87
+ * text as sanitized HTML, URLs through `safeHref`, images as `<img>` attributes. `{}` when the
88
+ * collection has no definitions. Optional for sources that predate fields; any source works
89
+ * with the standalone `entryFields(source, entry)`.
90
+ */
91
+ entryFields?(entry: ContentEntry, options?: EntryFieldsOptions): Promise<RenderedFields>;
83
92
  }
84
93
  export interface SourceSearchOptions {
85
94
  page?: number;
@@ -110,6 +119,7 @@ export declare class LiveSource implements GraphSource {
110
119
  allEntries(locale?: string): Promise<ContentEntry[]>;
111
120
  search(query: string, options?: SourceSearchOptions): Promise<SearchResults>;
112
121
  entryById(id: string): Promise<ContentEntry | undefined>;
122
+ entryFields(entry: ContentEntry, options?: EntryFieldsOptions): Promise<RenderedFields>;
113
123
  taxonomies(): Promise<Taxonomy[]>;
114
124
  terms(key: string): Promise<Term[]>;
115
125
  termBySlug(key: string, slug: string): Promise<Term | undefined>;
@@ -203,6 +213,7 @@ export declare class SnapshotSource implements GraphSource {
203
213
  search(query: string, options?: SourceSearchOptions): Promise<SearchResults>;
204
214
  /** A published entry of this snapshot by id (any locale), if its gates are open. */
205
215
  entryById(id: string): Promise<ContentEntry | undefined>;
216
+ entryFields(entry: ContentEntry, options?: EntryFieldsOptions): Promise<RenderedFields>;
206
217
  taxonomies(): Promise<Taxonomy[]>;
207
218
  terms(key: string): Promise<Term[]>;
208
219
  termBySlug(key: string, slug: string): Promise<Term | undefined>;
@@ -11,13 +11,7 @@
11
11
 
12
12
 
13
13
 
14
- import {
15
- matchRedirect,
16
- redirectLocale,
17
- searchDocuments,
18
- searchRows,
19
- termName,
20
- } from "@cloudhal/core-ts";
14
+ import { matchRedirect, redirectLocale, searchDocuments, searchRows } from "@cloudhal/core-ts";
21
15
  import {
22
16
  gateInstants,
23
17
  hasGates,
@@ -25,9 +19,11 @@ import {
25
19
  isAvailable,
26
20
  nextInstant,
27
21
  } from "@cloudhal/core-ts/availability";
28
- import { matches, parseFilter, resolveFilter } from "@cloudhal/core-ts/filter";
22
+ import { matches } from "@cloudhal/core-ts/filter";
29
23
  import { assetRefsOf } from "@cloudhal/core-ts/media";
24
+ import { entryFields, } from "./fields.js";
30
25
  import { navigationFor } from "./navigation.js";
26
+ import { byNewest, byPosition, compile, shown, termFilter, visible } from "./source-helpers.js";
31
27
 
32
28
  /**
33
29
  * What a renderer needs from a site, independent of whether it reads live nodes (dev) or a
@@ -112,6 +108,14 @@ import { navigationFor } from "./naviga
112
108
 
113
109
 
114
110
 
111
+
112
+  
113
+  
114
+
115
+
116
+
117
+
118
+
115
119
 
116
120
 
117
121
 
@@ -123,47 +127,6 @@ import { navigationFor } from "./naviga
123
127
 
124
128
 
125
129
 
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
130
  /**
168
131
  * Reads the current nodes on every call. Drafts are included so authors can preview them in dev,
169
132
  * and so are gated nodes (a preview shows embargoed and expired content, RFC-0008 §7.3). Without
@@ -272,6 +235,10 @@ export class LiveSource {
272
235
  : undefined;
273
236
  }
274
237
 
238
+ entryFields(entry , options ) {
239
+ return entryFields(this, entry, options);
240
+ }
241
+
275
242
  taxonomies() {
276
243
  return this.#store.listTaxonomies(this.#siteId);
277
244
  }
@@ -698,6 +665,10 @@ export class SnapshotSource {
698
665
  return (await this.#current()).entries.find((e) => e.id === id);
699
666
  }
700
667
 
668
+ entryFields(entry , options ) {
669
+ return entryFields(this, entry, options);
670
+ }
671
+
701
672
  async taxonomies() {
702
673
  return (await this.#current()).taxonomies;
703
674
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudhal/astro-renderer",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Render a Cloudhal Site Graph with Astro: data sources (live/snapshot), section components and the cloudhal() integration",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://cloudhal.dev",
@@ -22,6 +22,10 @@
22
22
  "types": "./dist/src/source.d.ts",
23
23
  "default": "./dist/src/source.js"
24
24
  },
25
+ "./fields": {
26
+ "types": "./dist/src/fields.d.ts",
27
+ "default": "./dist/src/fields.js"
28
+ },
25
29
  "./routing": {
26
30
  "types": "./dist/src/routing.d.ts",
27
31
  "default": "./dist/src/routing.js"
@@ -179,14 +183,14 @@
179
183
  "README.md"
180
184
  ],
181
185
  "dependencies": {
182
- "@cloudhal/core-contracts": "0.4.0",
183
- "@cloudhal/core-ts": "0.4.0",
184
- "@cloudhal/storage": "0.4.0",
186
+ "@cloudhal/core-contracts": "0.5.0",
187
+ "@cloudhal/core-ts": "0.5.0",
188
+ "@cloudhal/storage": "0.5.0",
185
189
  "marked": "^18.1.0",
186
190
  "parse5": "8.0.1",
187
- "@cloudhal/analytics": "0.4.0",
191
+ "@cloudhal/analytics": "0.5.0",
188
192
  "vite": "^8.3.3",
189
- "@cloudhal/comments": "0.4.0"
193
+ "@cloudhal/comments": "0.5.0"
190
194
  },
191
195
  "peerDependencies": {
192
196
  "astro": "^7.3.0",