@cloudhal/astro-renderer 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -0
- package/dist/src/cloudflare-entry.d.ts +3 -2
- package/dist/src/cloudflare-entry.js +3 -2
- package/dist/src/collection-feed-middleware.d.ts +5 -0
- package/dist/src/collection-feed-middleware.js +22 -0
- package/dist/src/collection-feed-route.d.ts +21 -0
- package/dist/src/collection-feed-route.js +92 -0
- package/dist/src/collection-feeds.d.ts +40 -0
- package/dist/src/collection-feeds.js +87 -0
- package/dist/src/components/ContentList.astro +26 -9
- package/dist/src/entry-query.d.ts +85 -0
- package/dist/src/entry-query.js +227 -0
- package/dist/src/entry-views.d.ts +36 -0
- package/dist/src/entry-views.js +73 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +7 -0
- package/dist/src/integration.js +30 -19
- package/dist/src/live-source.d.ts +53 -0
- package/dist/src/live-source.js +318 -0
- package/dist/src/locales.js +4 -3
- package/dist/src/preview.d.ts +16 -1
- package/dist/src/preview.js +89 -14
- package/dist/src/routing.d.ts +1 -1
- package/dist/src/routing.js +13 -3
- package/dist/src/seo.d.ts +4 -1
- package/dist/src/seo.js +9 -0
- package/dist/src/source.d.ts +30 -46
- package/dist/src/source.js +89 -239
- package/dist/src/structured-data.d.ts +35 -0
- package/dist/src/structured-data.js +88 -0
- package/package.json +27 -6
package/README.md
CHANGED
|
@@ -45,6 +45,30 @@ Additive; `entry.fields` still means exactly what is stored, and themes that nev
|
|
|
45
45
|
|
|
46
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
47
|
|
|
48
|
+
### Collections stage 3 (RFC-0042, added in 0.6.0)
|
|
49
|
+
Additive. `entryHead`, `entries()`, `allEntries()` and every existing export keep their meaning; the only differences an old theme copy renders come from settings a site opts into (`theme-v0.5.0` in the compat test): entries past a collection's `expiry` leave lists, feeds, the sitemap and search, and `contentList` sections with `where`/`orderBy` list by field (`ContentList.astro` ships here and queries the source a theme's list came from).
|
|
50
|
+
|
|
51
|
+
| Export | Signature |
|
|
52
|
+
|---|---|
|
|
53
|
+
| `GraphSource.queryEntries?` | `(q: FieldEntryQuery) => Promise<FieldEntryResult>` on `LiveSource` and `SnapshotSource`: `{ collection, locale?, where?, orderBy?, sort?, filter?, limit? (1–100, default 20), offset?, includeExpired? }` → `{ entries, total }`; `now` in conditions is the source's instant |
|
|
54
|
+
| `queryEntries` (`./queries`, `.`) | `(source: GraphSource, q: FieldEntryQuery) => Promise<FieldEntryResult>`: any source (falls back to `entries()` on sources without `queryEntries`); an invalid query matches nothing and is logged |
|
|
55
|
+
| `contentListEntries` (`./queries`, `.`) | `(source, sections, locale?) => Promise<Record<string, ContentEntry[]>>`: the `entries` map `Sections` reads (by collection; by section id for `filter` and for `where`/`orderBy`) |
|
|
56
|
+
| `sectionQuery`, `listsByField`, `evaluateEntryQuery` (`./queries`) | the query a section runs; whether it lists by field; the pure evaluator both sources share |
|
|
57
|
+
| `GraphSource.routableEntries?` | `(locale?) => Promise<ContentEntry[]>`: every entry with a public URL, unlisted ones included (routes, static builds, alternates); `allEntries` stays the listed ones |
|
|
58
|
+
| `withStructuredData` (`./seo`, `.`) | `(head: HeadMeta, source, entry, { origin, media? }) => Promise<HeadMeta>`: call after `entryHead`; replaces the `BlogPosting` with the collection's preset (`Event`, `JobPosting`, `Product`, `Recipe`) when the entry has every required value, keeps the `BreadcrumbList`, returns `head` unchanged otherwise |
|
|
59
|
+
| `entryStructuredData` (`./seo`, `.`) | `(source, entry, { origin, media? }) => Promise<{ doc?, issues }>`: the preset document and why there is none |
|
|
60
|
+
| `collectionFeedLink` (`./seo`, `./collection-feeds`, `.`) | `(site, collection, origin, locale?) => FeedLink \| undefined`: `{ title, href }` of the collection's feed (`<prefix>/rss.xml`) on the locale's URL, `undefined` with `feed: false` or a permalink without a prefix; add it to `head.feeds` after `localizeHead` |
|
|
61
|
+
| `collectionFeedPath`, `feedCollections`, `collectionFeedPaths`, `collectionForFeedPath` (`./collection-feeds`) | which collections have a feed and where; `staticPaths().feeds` now includes every collection feed path |
|
|
62
|
+
| `collectionFeedXml`, `serveCollectionFeed` (`./collection-feed-route`) | a collection's RSS in a locale; the middleware's decision (a theme's 404 for a feed path becomes the feed) |
|
|
63
|
+
|
|
64
|
+
Collection feeds: the `cloudhal()` integration adds a middleware (`collection-feed-middleware`) on every target that serves `<prefix>/rss.xml` (and `/<locale><prefix>/rss.xml`) only where the theme's own routes answered 404, so a theme's own route wins and old copies gain the feeds; static builds write them through the starter's `[...path]/rss.xml` route, whose paths come from `staticPaths().feeds`. Same rules and cache tag as `rss.xml`.
|
|
65
|
+
|
|
66
|
+
Previews: `renderSectionHtml(section, entries, media?, listing?)` takes `{ site, now? }` as a fourth argument, and `renderPagePreview` passes it, so `contentList` sections with `where`/`orderBy` preview through `evaluateEntryQuery` like the site (a term `filter` is still applied on the site only).
|
|
67
|
+
|
|
68
|
+
Guide: [`docs/guides/structured-data.md`](../../docs/guides/structured-data.md).
|
|
69
|
+
|
|
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
|
+
|
|
48
72
|
## Site analytics (RFC-0006)
|
|
49
73
|
|
|
50
74
|
The renderer carries the site side of Cloudhal's cookie-less analytics (`@cloudhal/analytics`):
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
* sends after working out what a publish changed; without one, everything is purged.
|
|
11
11
|
* - `GET /__cloudhal/features` (Bearer `CLOUDHAL_PURGE_TOKEN`) says what this Worker enforces, so
|
|
12
12
|
* the admin Worker never writes data an older site Worker would mishandle (RFC-0008 §7.7):
|
|
13
|
-
* `{ "gates":
|
|
14
|
-
*
|
|
13
|
+
* `{ "gates": 2 }` — every public path checks pages' and entries' time gates per request, and
|
|
14
|
+
* entries' derived gates from a collection's `expiry` (RFC-0042 §3), and caps the edge cache at
|
|
15
|
+
* the next one. Writers refuse `availability` below 1 and `expiry` below 2.
|
|
15
16
|
* - Redirects (RFC-0002) are answered by the routes themselves (real 301s, `no-store` here like
|
|
16
17
|
* every non-200), so a publish that adds or removes one needs no purge of its own.
|
|
17
18
|
* - Preview responses get every internal link rewritten to keep the signed token, so navigation
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
* sends after working out what a publish changed; without one, everything is purged.
|
|
11
11
|
* - `GET /__cloudhal/features` (Bearer `CLOUDHAL_PURGE_TOKEN`) says what this Worker enforces, so
|
|
12
12
|
* the admin Worker never writes data an older site Worker would mishandle (RFC-0008 §7.7):
|
|
13
|
-
* `{ "gates":
|
|
14
|
-
*
|
|
13
|
+
* `{ "gates": 2 }` — every public path checks pages' and entries' time gates per request, and
|
|
14
|
+
* entries' derived gates from a collection's `expiry` (RFC-0042 §3), and caps the edge cache at
|
|
15
|
+
* the next one. Writers refuse `availability` below 1 and `expiry` below 2.
|
|
15
16
|
* - Redirects (RFC-0002) are answered by the routes themselves (real 301s, `no-store` here like
|
|
16
17
|
* every non-200), so a publish that adds or removes one needs no purge of its own.
|
|
17
18
|
* - Preview responses get every internal link rewritten to keep the signed token, so navigation
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0042 §5 (added by the `cloudhal()` integration on every target): a collection's feed at
|
|
3
|
+
* `<prefix>/rss.xml`, served only where the theme's own routes answered 404
|
|
4
|
+
* (`collection-feed-route.ts`).
|
|
5
|
+
*/
|
|
6
|
+
import config from "virtual:cloudhal/config";
|
|
7
|
+
import { serveCollectionFeed } from "@cloudhal/astro-renderer/collection-feed-route";
|
|
8
|
+
import { getSource } from "@cloudhal/astro-renderer/runtime";
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export async function onRequest(
|
|
15
|
+
context ,
|
|
16
|
+
next ,
|
|
17
|
+
) {
|
|
18
|
+
const response = await next();
|
|
19
|
+
return serveCollectionFeed(context.request, response, (headers) =>
|
|
20
|
+
getSource(config, { request: context.request, response: { headers } }),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { GraphSource } from "./source.js";
|
|
2
|
+
/**
|
|
3
|
+
* A collection's feed in `locale`: its newest listed entries (expired and gated ones left out by
|
|
4
|
+
* the source), titled after its plural label, linking to the page at its prefix when there is one.
|
|
5
|
+
*/
|
|
6
|
+
export declare function collectionFeedXml(source: GraphSource, input: {
|
|
7
|
+
origin: string;
|
|
8
|
+
locale: string;
|
|
9
|
+
collection: string;
|
|
10
|
+
}): Promise<string>;
|
|
11
|
+
/**
|
|
12
|
+
* Opens the request's source and lets it set caching headers on `headers` (the runtime's
|
|
13
|
+
* `getSource(config, { request, response: { headers } })`); undefined when unpublished.
|
|
14
|
+
*/
|
|
15
|
+
export type FeedSourceOpener = (headers: Headers) => Promise<GraphSource | undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* The middleware's decision: `response` unchanged unless it is a 404 to a `GET`/`HEAD` of a
|
|
18
|
+
* collection's feed path (in the path's or host's locale), which is then answered with the feed,
|
|
19
|
+
* the source's cache headers and the site's list tag (like `rss.xml`).
|
|
20
|
+
*/
|
|
21
|
+
export declare function serveCollectionFeed(request: Request, response: Response, open: FeedSourceOpener): Promise<Response>;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0042 §5: the collection feed route. `collectionFeedXml` builds a collection's RSS 2.0 feed in
|
|
3
|
+
* a locale on top of `rssXml` (the site feed's rules: published, listed, `feed: false` left out);
|
|
4
|
+
* `serveCollectionFeed` turns a theme's 404 for `<prefix>/rss.xml` into that feed. The `cloudhal()`
|
|
5
|
+
* integration runs it as a middleware on every target (`collection-feed-middleware.ts`), so a
|
|
6
|
+
* theme with its own route for the path keeps it, and old theme copies gain feeds on upgrade.
|
|
7
|
+
* Static builds write the files because `staticPaths().feeds` lists these paths for the starter's
|
|
8
|
+
* `[...path]/rss.xml` route, whose 404 this replaces.
|
|
9
|
+
*/
|
|
10
|
+
import { collectionLabel } from "@cloudhal/core-ts";
|
|
11
|
+
import { localeUrl } from "@cloudhal/core-ts/locale-paths";
|
|
12
|
+
import { addCacheTags, listCacheTag } from "./cache-tags.js";
|
|
13
|
+
import { collectionFeedPath, collectionForFeedPath } from "./collection-feeds.js";
|
|
14
|
+
import { rssXml } from "./feeds.js";
|
|
15
|
+
import { localeRequest } from "./locales.js";
|
|
16
|
+
import { loadAssetIndex } from "./media.js";
|
|
17
|
+
import { siteOrigin } from "./seo.js";
|
|
18
|
+
|
|
19
|
+
import { loadTaxonomyIndex } from "./taxonomy.js";
|
|
20
|
+
|
|
21
|
+
const SUFFIX = "/rss.xml";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A collection's feed in `locale`: its newest listed entries (expired and gated ones left out by
|
|
25
|
+
* the source), titled after its plural label, linking to the page at its prefix when there is one.
|
|
26
|
+
*/
|
|
27
|
+
export async function collectionFeedXml(
|
|
28
|
+
source ,
|
|
29
|
+
input ,
|
|
30
|
+
) {
|
|
31
|
+
const { origin, locale, collection } = input;
|
|
32
|
+
const site = await source.site();
|
|
33
|
+
const feedPath = collectionFeedPath(site, collection) ?? `/${collection}${SUFFIX}`;
|
|
34
|
+
const prefix = feedPath.slice(0, -SUFFIX.length);
|
|
35
|
+
const index = await source.pageByPath(prefix, locale);
|
|
36
|
+
const description = index?.seo?.description;
|
|
37
|
+
return rssXml({
|
|
38
|
+
site,
|
|
39
|
+
origin,
|
|
40
|
+
locale,
|
|
41
|
+
linkOrigin: localeUrl(site, locale, "", origin),
|
|
42
|
+
entries: await source.entries(collection, locale),
|
|
43
|
+
feedPath,
|
|
44
|
+
title: `${collectionLabel(site, collection, locale).plural} · ${site.name}`,
|
|
45
|
+
linkPath: index ? prefix : "/",
|
|
46
|
+
taxonomy: await loadTaxonomyIndex(source, locale),
|
|
47
|
+
assets: await loadAssetIndex(source),
|
|
48
|
+
...(description ? { description } : {}),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Opens the request's source and lets it set caching headers on `headers` (the runtime's
|
|
54
|
+
* `getSource(config, { request, response: { headers } })`); undefined when unpublished.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The middleware's decision: `response` unchanged unless it is a 404 to a `GET`/`HEAD` of a
|
|
60
|
+
* collection's feed path (in the path's or host's locale), which is then answered with the feed,
|
|
61
|
+
* the source's cache headers and the site's list tag (like `rss.xml`).
|
|
62
|
+
*/
|
|
63
|
+
export async function serveCollectionFeed(
|
|
64
|
+
request ,
|
|
65
|
+
response ,
|
|
66
|
+
open ,
|
|
67
|
+
) {
|
|
68
|
+
if (response.status !== 404 || (request.method !== "GET" && request.method !== "HEAD"))
|
|
69
|
+
return response;
|
|
70
|
+
const url = new URL(request.url);
|
|
71
|
+
if (!url.pathname.endsWith(SUFFIX)) return response;
|
|
72
|
+
const headers = new Headers();
|
|
73
|
+
const source = await open(headers);
|
|
74
|
+
if (!source) return response;
|
|
75
|
+
const site = await source.site();
|
|
76
|
+
const lr = localeRequest(site, url.host, url.pathname);
|
|
77
|
+
if (lr.redirect !== undefined) return response;
|
|
78
|
+
const collection =
|
|
79
|
+
collectionForFeedPath(site, lr.path) ??
|
|
80
|
+
collectionForFeedPath(site, lr.path, await source.allEntries(lr.locale));
|
|
81
|
+
if (!collection) return response;
|
|
82
|
+
await response.body?.cancel();
|
|
83
|
+
headers.set("content-type", "application/rss+xml; charset=utf-8");
|
|
84
|
+
// Lists a collection's newest entries: any entry change purges it, as it does the site feed.
|
|
85
|
+
addCacheTags(headers, [listCacheTag(site.id)]);
|
|
86
|
+
const body = await collectionFeedXml(source, {
|
|
87
|
+
origin: siteOrigin(site, url),
|
|
88
|
+
locale: lr.locale,
|
|
89
|
+
collection,
|
|
90
|
+
});
|
|
91
|
+
return new Response(body, { status: 200, headers });
|
|
92
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0042 §5: per-collection feeds. Each collection with a permalink prefix (RFC-0038: `/events`
|
|
3
|
+
* for `/events/{slug}`) has an RSS feed at `<prefix>/rss.xml` (and `/<locale><prefix>/rss.xml`),
|
|
4
|
+
* listing its newest listed entries: expiry and gates apply as in the site feed, `feed: false`
|
|
5
|
+
* answers 404. The renderer serves it after a theme's routes answered 404
|
|
6
|
+
* (`collection-feed-route.ts`), so old theme copies get it and a theme's own route wins.
|
|
7
|
+
*
|
|
8
|
+
* This module holds the pure parts: which collections have a feed, their paths, and the
|
|
9
|
+
* `<link rel="alternate">` a theme puts in its head. Worker-safe.
|
|
10
|
+
*/
|
|
11
|
+
import type { ContentEntry, Site } from "@cloudhal/core-contracts";
|
|
12
|
+
/**
|
|
13
|
+
* Where a collection's feed is served (unlocalized), or undefined when it has none: `feed: false`,
|
|
14
|
+
* or a permalink without a fixed prefix (`/{slug}`, whose feed would be the site's `/rss.xml`).
|
|
15
|
+
*/
|
|
16
|
+
export declare function collectionFeedPath(site: Site, collection: string): string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* The collections with a feed: declared ones, the built-in `posts`, and collections inferred from
|
|
19
|
+
* `entries` (the entries a source lists), each in that order, without duplicates.
|
|
20
|
+
*/
|
|
21
|
+
export declare function feedCollections(site: Site, entries?: readonly ContentEntry[]): string[];
|
|
22
|
+
/** Every collection feed path (unlocalized), for static builds (`staticPaths().feeds`). */
|
|
23
|
+
export declare function collectionFeedPaths(site: Site, entries?: readonly ContentEntry[]): string[];
|
|
24
|
+
/**
|
|
25
|
+
* The collection a request path (without its locale prefix) addresses as a feed. `entries` are
|
|
26
|
+
* the entries the source lists, so an undeclared collection has a feed only once it has entries.
|
|
27
|
+
*/
|
|
28
|
+
export declare function collectionForFeedPath(site: Site, path: string, entries?: readonly ContentEntry[]): string | undefined;
|
|
29
|
+
/** A feed for `<link rel="alternate" type="application/rss+xml">`, as `HeadMeta.feeds` holds it. */
|
|
30
|
+
export interface FeedLink {
|
|
31
|
+
readonly title: string;
|
|
32
|
+
readonly href: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The `<link rel="alternate">` of a collection's feed in `locale` (default: the site's), on the
|
|
36
|
+
* locale's URL (its prefix or its own domain): titled `<plural label> · <site name>`. Undefined
|
|
37
|
+
* when the collection has no feed (`feed: false`, no prefix). Add it to the head after
|
|
38
|
+
* `localizeHead`, whose feed links it would localize a second time.
|
|
39
|
+
*/
|
|
40
|
+
export declare function collectionFeedLink(site: Site, collection: string, origin: string, locale?: string): FeedLink | undefined;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0042 §5: per-collection feeds. Each collection with a permalink prefix (RFC-0038: `/events`
|
|
3
|
+
* for `/events/{slug}`) has an RSS feed at `<prefix>/rss.xml` (and `/<locale><prefix>/rss.xml`),
|
|
4
|
+
* listing its newest listed entries: expiry and gates apply as in the site feed, `feed: false`
|
|
5
|
+
* answers 404. The renderer serves it after a theme's routes answered 404
|
|
6
|
+
* (`collection-feed-route.ts`), so old theme copies get it and a theme's own route wins.
|
|
7
|
+
*
|
|
8
|
+
* This module holds the pure parts: which collections have a feed, their paths, and the
|
|
9
|
+
* `<link rel="alternate">` a theme puts in its head. Worker-safe.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
BUILT_IN_COLLECTION,
|
|
14
|
+
collectionLabel,
|
|
15
|
+
collectionPermalink,
|
|
16
|
+
collectionSettings,
|
|
17
|
+
permalinkPrefix,
|
|
18
|
+
} from "@cloudhal/core-ts";
|
|
19
|
+
import { localeUrl } from "@cloudhal/core-ts/locale-paths";
|
|
20
|
+
|
|
21
|
+
const SUFFIX = "/rss.xml";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Where a collection's feed is served (unlocalized), or undefined when it has none: `feed: false`,
|
|
25
|
+
* or a permalink without a fixed prefix (`/{slug}`, whose feed would be the site's `/rss.xml`).
|
|
26
|
+
*/
|
|
27
|
+
export function collectionFeedPath(site , collection ) {
|
|
28
|
+
if (!collectionSettings(site, collection).feed) return undefined;
|
|
29
|
+
const prefix = permalinkPrefix(collectionPermalink(site, collection));
|
|
30
|
+
if (prefix === "" || prefix === "/" || prefix.includes("{")) return undefined;
|
|
31
|
+
return `${prefix}${SUFFIX}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The collections with a feed: declared ones, the built-in `posts`, and collections inferred from
|
|
36
|
+
* `entries` (the entries a source lists), each in that order, without duplicates.
|
|
37
|
+
*/
|
|
38
|
+
export function feedCollections(site , entries = []) {
|
|
39
|
+
const keys = new Set([
|
|
40
|
+
...Object.keys(site.collections ?? {}),
|
|
41
|
+
BUILT_IN_COLLECTION,
|
|
42
|
+
...entries.map((e) => e.collection),
|
|
43
|
+
]);
|
|
44
|
+
return [...keys].filter((key) => collectionFeedPath(site, key) !== undefined);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Every collection feed path (unlocalized), for static builds (`staticPaths().feeds`). */
|
|
48
|
+
export function collectionFeedPaths(site , entries = []) {
|
|
49
|
+
return feedCollections(site, entries).map((key) => collectionFeedPath(site, key) );
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The collection a request path (without its locale prefix) addresses as a feed. `entries` are
|
|
54
|
+
* the entries the source lists, so an undeclared collection has a feed only once it has entries.
|
|
55
|
+
*/
|
|
56
|
+
export function collectionForFeedPath(
|
|
57
|
+
site ,
|
|
58
|
+
path ,
|
|
59
|
+
entries = [],
|
|
60
|
+
) {
|
|
61
|
+
if (!path.endsWith(SUFFIX)) return undefined;
|
|
62
|
+
return feedCollections(site, entries).find((key) => collectionFeedPath(site, key) === path);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A feed for `<link rel="alternate" type="application/rss+xml">`, as `HeadMeta.feeds` holds it. */
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The `<link rel="alternate">` of a collection's feed in `locale` (default: the site's), on the
|
|
73
|
+
* locale's URL (its prefix or its own domain): titled `<plural label> · <site name>`. Undefined
|
|
74
|
+
* when the collection has no feed (`feed: false`, no prefix). Add it to the head after
|
|
75
|
+
* `localizeHead`, whose feed links it would localize a second time.
|
|
76
|
+
*/
|
|
77
|
+
export function collectionFeedLink(
|
|
78
|
+
site ,
|
|
79
|
+
collection ,
|
|
80
|
+
origin ,
|
|
81
|
+
locale = site.defaultLocale,
|
|
82
|
+
) {
|
|
83
|
+
const path = collectionFeedPath(site, collection);
|
|
84
|
+
if (!path) return undefined;
|
|
85
|
+
const { plural } = collectionLabel(site, collection, locale);
|
|
86
|
+
return { title: `${plural} · ${site.name}`, href: localeUrl(site, locale, path, origin) };
|
|
87
|
+
}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
---
|
|
2
|
+
/**
|
|
3
|
+
* A `contentList` section. Without `where`/`orderBy` it draws `entries` by the section's `sort`
|
|
4
|
+
* and `limit`, as it always has. With them (RFC-0042 §4) it lists by field: a list `queryEntries`
|
|
5
|
+
* made is drawn as it is; the collection's list a source returned (what theme copies from before
|
|
6
|
+
* RFC-0042 pass) is queried on that source here, so those themes get field listings on upgrade.
|
|
7
|
+
*/
|
|
2
8
|
import type { ContentEntry, ContentListSection } from "@cloudhal/core-contracts";
|
|
3
9
|
import { entryPath } from "@cloudhal/core-ts";
|
|
10
|
+
import { listOrigin, listsByField, queryEntries, sectionQuery } from "../entry-query.js";
|
|
4
11
|
import type { MediaContext } from "../media.js";
|
|
5
12
|
|
|
6
13
|
interface Props {
|
|
@@ -10,15 +17,25 @@ interface Props {
|
|
|
10
17
|
hrefFor?: (entry: ContentEntry) => string;
|
|
11
18
|
}
|
|
12
19
|
const { section, entries, media, hrefFor = (e) => entryPath(media?.permalinks, e) } = Astro.props;
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
const origin = listsByField(section) ? listOrigin(entries) : undefined;
|
|
21
|
+
if (listsByField(section) && origin === undefined)
|
|
22
|
+
console.warn(
|
|
23
|
+
`cloudhal: section ${section.id} lists by field, but its entries came from elsewhere`,
|
|
24
|
+
);
|
|
25
|
+
const sorted =
|
|
26
|
+
origin === "queried"
|
|
27
|
+
? entries.slice(0, section.limit)
|
|
28
|
+
: origin
|
|
29
|
+
? (await queryEntries(origin.source, sectionQuery(section, origin.locale))).entries
|
|
30
|
+
: [...entries]
|
|
31
|
+
.sort((a, b) =>
|
|
32
|
+
section.sort === "title"
|
|
33
|
+
? a.title.localeCompare(b.title)
|
|
34
|
+
: section.sort === "oldest"
|
|
35
|
+
? (a.publishedAt ?? a.createdAt).localeCompare(b.publishedAt ?? b.createdAt)
|
|
36
|
+
: (b.publishedAt ?? b.createdAt).localeCompare(a.publishedAt ?? a.createdAt),
|
|
37
|
+
)
|
|
38
|
+
.slice(0, section.limit);
|
|
22
39
|
---
|
|
23
40
|
<section class="ch-contentlist" data-section-id={section.id} data-kind="contentList" data-collection={section.collection}>
|
|
24
41
|
{sorted.length === 0 ? <p class="ch-contentlist__empty">Nothing published yet.</p> : (
|
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
import type { ContentEntry, ContentListSection, FieldCondition, FieldOrder, Section, Site, Taxonomy, Term } from "@cloudhal/core-contracts";
|
|
13
|
+
import { type ResolvedFields } from "@cloudhal/core-ts/fields";
|
|
14
|
+
import type { GraphSource } from "./source.js";
|
|
15
|
+
/** A theme's listing by field (RFC-0042 §4). */
|
|
16
|
+
export interface FieldEntryQuery {
|
|
17
|
+
readonly collection: string;
|
|
18
|
+
/** Default: the site's. */
|
|
19
|
+
readonly locale?: string | undefined;
|
|
20
|
+
readonly where?: readonly FieldCondition[] | undefined;
|
|
21
|
+
readonly orderBy?: FieldOrder | undefined;
|
|
22
|
+
/** Default: the collection's `sort` (absent: newest). Ties after `orderBy`, then `id`. */
|
|
23
|
+
readonly sort?: "newest" | "oldest" | "title" | undefined;
|
|
24
|
+
/** An RFC-0002 term filter, unchanged (`category:news+tag:ai`). */
|
|
25
|
+
readonly filter?: string | undefined;
|
|
26
|
+
/** 1–100; default 20. */
|
|
27
|
+
readonly limit?: number | undefined;
|
|
28
|
+
readonly offset?: number | undefined;
|
|
29
|
+
/** Also unlisted (not hidden) entries past their derived gate: "past events". */
|
|
30
|
+
readonly includeExpired?: boolean | undefined;
|
|
31
|
+
}
|
|
32
|
+
export interface FieldEntryResult {
|
|
33
|
+
readonly entries: ContentEntry[];
|
|
34
|
+
/** Matches before paging. */
|
|
35
|
+
readonly total: number;
|
|
36
|
+
}
|
|
37
|
+
export declare const QUERY_DEFAULT_LIMIT = 20;
|
|
38
|
+
export declare const QUERY_MAX_LIMIT = 100;
|
|
39
|
+
/** What a source hands the evaluator. */
|
|
40
|
+
export interface EntryQueryInput {
|
|
41
|
+
readonly site: Site;
|
|
42
|
+
/** The entries this source shows for the query (listed, or reachable with `includeExpired`). */
|
|
43
|
+
readonly candidates: readonly ContentEntry[];
|
|
44
|
+
/** Published entries a translation may read non-localized values from. */
|
|
45
|
+
readonly published: readonly ContentEntry[];
|
|
46
|
+
readonly taxonomies: readonly Taxonomy[];
|
|
47
|
+
readonly terms: readonly Term[];
|
|
48
|
+
readonly query: FieldEntryQuery;
|
|
49
|
+
/** Epoch ms; `now` in conditions compares with it. */
|
|
50
|
+
readonly now: number;
|
|
51
|
+
/** Resolved values by entry id, reused across queries (a snapshot's are immutable). */
|
|
52
|
+
readonly cache?: Map<string, ResolvedFields> | undefined;
|
|
53
|
+
}
|
|
54
|
+
/** Runs a query over what a source shows. Pure. */
|
|
55
|
+
export declare function evaluateEntryQuery(input: EntryQueryInput): FieldEntryResult;
|
|
56
|
+
/** What a list handed to a theme came from: its source and locale, or a query already run. */
|
|
57
|
+
type ListOrigin = {
|
|
58
|
+
readonly source: GraphSource;
|
|
59
|
+
readonly locale: string;
|
|
60
|
+
} | "queried";
|
|
61
|
+
/**
|
|
62
|
+
* Marks a list a source returned (`entries`, `entriesByFilter`) so `ContentList` can apply a
|
|
63
|
+
* section's `where`/`orderBy` even for theme copies from before RFC-0042, which pass the
|
|
64
|
+
* collection's list as it is. Returns the list.
|
|
65
|
+
*/
|
|
66
|
+
export declare function rememberList<T extends readonly ContentEntry[]>(list: T, origin: ListOrigin): T;
|
|
67
|
+
/** Where a list came from, if a source or `queryEntries` made it. */
|
|
68
|
+
export declare function listOrigin(list: readonly ContentEntry[]): ListOrigin | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Lists `q.collection` by its list fields on any source: the source's own `queryEntries`, or, on
|
|
71
|
+
* sources from before RFC-0042, the same evaluator over `entries()` (no `includeExpired` there).
|
|
72
|
+
*/
|
|
73
|
+
export declare function queryEntries(source: GraphSource, q: FieldEntryQuery): Promise<FieldEntryResult>;
|
|
74
|
+
/** The query a `contentList` section with `where`/`orderBy` runs (its own `limit`, at most 100). */
|
|
75
|
+
export declare function sectionQuery(section: ContentListSection, locale?: string): FieldEntryQuery;
|
|
76
|
+
/** True when a section lists by field (RFC-0042). */
|
|
77
|
+
export declare const listsByField: (section: ContentListSection) => boolean;
|
|
78
|
+
/**
|
|
79
|
+
* The entries a page's `contentList` sections draw, keyed as `Sections` reads them: by collection
|
|
80
|
+
* (the collection's newest listed entries), by section id for a section with a term `filter`
|
|
81
|
+
* (RFC-0002), and by section id for a section with `where`/`orderBy` (RFC-0042, through
|
|
82
|
+
* `queryEntries`). An invalid filter lists nothing and is logged.
|
|
83
|
+
*/
|
|
84
|
+
export declare function contentListEntries(source: GraphSource, sections: readonly Section[], locale?: string): Promise<Record<string, ContentEntry[]>>;
|
|
85
|
+
export {};
|