@cloudhal/astro-renderer 0.0.0-stage → 0.1.1
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/LICENSE +202 -0
- package/README.md +36 -2
- package/dist/src/ambient-client.d.ts +17 -0
- package/dist/src/ambient-client.js +55 -0
- package/dist/src/analytics-client.d.ts +17 -0
- package/dist/src/analytics-client.js +96 -0
- package/dist/src/analytics-middleware.d.ts +6 -0
- package/dist/src/analytics-middleware.js +43 -0
- package/dist/src/analytics-node.d.ts +46 -0
- package/dist/src/analytics-node.js +134 -0
- package/dist/src/analytics.d.ts +117 -0
- package/dist/src/analytics.js +372 -0
- package/dist/src/archive.d.ts +106 -0
- package/dist/src/archive.js +207 -0
- package/dist/src/astro-env.d.ts +21 -0
- package/dist/src/block-html.d.ts +14 -0
- package/dist/src/block-html.js +103 -0
- package/dist/src/blocks.d.ts +24 -0
- package/dist/src/blocks.js +66 -0
- package/dist/src/cache-tags.d.ts +25 -0
- package/dist/src/cache-tags.js +59 -0
- package/dist/src/carousel-client.d.ts +28 -0
- package/dist/src/carousel-client.js +67 -0
- package/dist/src/chart.d.ts +34 -0
- package/dist/src/chart.js +182 -0
- package/dist/src/chrome-client.d.ts +27 -0
- package/dist/src/chrome-client.js +64 -0
- package/dist/src/cloudflare-entry.d.ts +87 -0
- package/dist/src/cloudflare-entry.js +548 -0
- package/dist/src/comments-client.d.ts +11 -0
- package/dist/src/comments-client.js +58 -0
- package/dist/src/comments-html.d.ts +42 -0
- package/dist/src/comments-html.js +303 -0
- package/dist/src/comments-middleware.d.ts +6 -0
- package/dist/src/comments-middleware.js +38 -0
- package/dist/src/comments-node.d.ts +42 -0
- package/dist/src/comments-node.js +112 -0
- package/dist/src/comments-view.d.ts +68 -0
- package/dist/src/comments-view.js +174 -0
- package/dist/src/comments.d.ts +66 -0
- package/dist/src/comments.js +258 -0
- package/dist/src/components/Block.astro +39 -0
- package/dist/src/components/Body.astro +43 -0
- package/dist/src/components/Comments.astro +19 -0
- package/dist/src/components/ContentList.astro +31 -0
- package/dist/src/components/EntryArticle.astro +31 -0
- package/dist/src/components/Hero.astro +36 -0
- package/dist/src/components/Image.astro +36 -0
- package/dist/src/components/RichText.astro +19 -0
- package/dist/src/components/Sections.astro +66 -0
- package/dist/src/components/SeoHead.astro +22 -0
- package/dist/src/components/Surface.astro +36 -0
- package/dist/src/components/SurfaceNode.astro +82 -0
- package/dist/src/components/index.d.ts +12 -0
- package/dist/src/components/index.js +12 -0
- package/dist/src/csp.d.ts +20 -0
- package/dist/src/csp.js +28 -0
- package/dist/src/embed-client.d.ts +38 -0
- package/dist/src/embed-client.js +98 -0
- package/dist/src/embed-middleware.d.ts +9 -0
- package/dist/src/embed-middleware.js +49 -0
- package/dist/src/feeds.d.ts +72 -0
- package/dist/src/feeds.js +246 -0
- package/dist/src/gates.d.ts +25 -0
- package/dist/src/gates.js +63 -0
- package/dist/src/index.d.ts +17 -0
- package/dist/src/index.js +17 -0
- package/dist/src/integration.d.ts +55 -0
- package/dist/src/integration.js +244 -0
- package/dist/src/kit.d.ts +48 -0
- package/dist/src/kit.js +83 -0
- package/dist/src/locale-feeds.d.ts +29 -0
- package/dist/src/locale-feeds.js +152 -0
- package/dist/src/locale-middleware.d.ts +5 -0
- package/dist/src/locale-middleware.js +26 -0
- package/dist/src/locales.d.ts +87 -0
- package/dist/src/locales.js +336 -0
- package/dist/src/markdown.d.ts +83 -0
- package/dist/src/markdown.js +533 -0
- package/dist/src/media-middleware.d.ts +5 -0
- package/dist/src/media-middleware.js +18 -0
- package/dist/src/media-route.d.ts +47 -0
- package/dist/src/media-route.js +238 -0
- package/dist/src/media.d.ts +109 -0
- package/dist/src/media.js +298 -0
- package/dist/src/motion-client.d.ts +39 -0
- package/dist/src/motion-client.js +115 -0
- package/dist/src/navigation.d.ts +127 -0
- package/dist/src/navigation.js +382 -0
- package/dist/src/not-found.d.ts +16 -0
- package/dist/src/not-found.js +41 -0
- package/dist/src/overlay-client.d.ts +20 -0
- package/dist/src/overlay-client.js +49 -0
- package/dist/src/prefs-client.d.ts +14 -0
- package/dist/src/prefs-client.js +18 -0
- package/dist/src/preview.d.ts +70 -0
- package/dist/src/preview.js +551 -0
- package/dist/src/resolver.d.ts +71 -0
- package/dist/src/resolver.js +206 -0
- package/dist/src/routing.d.ts +108 -0
- package/dist/src/routing.js +285 -0
- package/dist/src/runtime-workerd.d.ts +87 -0
- package/dist/src/runtime-workerd.js +275 -0
- package/dist/src/runtime.d.ts +59 -0
- package/dist/src/runtime.js +153 -0
- package/dist/src/safe-urls.d.ts +13 -0
- package/dist/src/safe-urls.js +35 -0
- package/dist/src/sanitize.d.ts +7 -0
- package/dist/src/sanitize.js +217 -0
- package/dist/src/search-client.d.ts +29 -0
- package/dist/src/search-client.js +92 -0
- package/dist/src/search-limit.d.ts +36 -0
- package/dist/src/search-limit.js +66 -0
- package/dist/src/search.d.ts +64 -0
- package/dist/src/search.js +155 -0
- package/dist/src/seo.d.ts +85 -0
- package/dist/src/seo.js +468 -0
- package/dist/src/site-dispatch.d.ts +37 -0
- package/dist/src/site-dispatch.js +86 -0
- package/dist/src/site-hosts.d.ts +35 -0
- package/dist/src/site-hosts.js +110 -0
- package/dist/src/source.d.ts +227 -0
- package/dist/src/source.js +794 -0
- package/dist/src/static-gates.d.ts +3 -0
- package/dist/src/static-gates.js +28 -0
- package/dist/src/static-headers.d.ts +30 -0
- package/dist/src/static-headers.js +140 -0
- package/dist/src/static-media.d.ts +4 -0
- package/dist/src/static-media.js +65 -0
- package/dist/src/static-redirects.d.ts +4 -0
- package/dist/src/static-redirects.js +40 -0
- package/dist/src/surface-client.d.ts +34 -0
- package/dist/src/surface-client.js +146 -0
- package/dist/src/surfaces/html.d.ts +35 -0
- package/dist/src/surfaces/html.js +184 -0
- package/dist/src/surfaces/markup.d.ts +57 -0
- package/dist/src/surfaces/markup.js +135 -0
- package/dist/src/surfaces/motion.d.ts +33 -0
- package/dist/src/surfaces/motion.js +132 -0
- package/dist/src/surfaces/primitives/carousel.d.ts +2 -0
- package/dist/src/surfaces/primitives/carousel.js +95 -0
- package/dist/src/surfaces/primitives/content.d.ts +13 -0
- package/dist/src/surfaces/primitives/content.js +159 -0
- package/dist/src/surfaces/primitives/data.d.ts +5 -0
- package/dist/src/surfaces/primitives/data.js +80 -0
- package/dist/src/surfaces/primitives/index.d.ts +54 -0
- package/dist/src/surfaces/primitives/index.js +106 -0
- package/dist/src/surfaces/primitives/inputs.d.ts +8 -0
- package/dist/src/surfaces/primitives/inputs.js +68 -0
- package/dist/src/surfaces/primitives/layout.d.ts +14 -0
- package/dist/src/surfaces/primitives/layout.js +61 -0
- package/dist/src/surfaces/primitives/motion.d.ts +9 -0
- package/dist/src/surfaces/primitives/motion.js +45 -0
- package/dist/src/surfaces/primitives/overlays.d.ts +6 -0
- package/dist/src/surfaces/primitives/overlays.js +79 -0
- package/dist/src/surfaces/tree.d.ts +107 -0
- package/dist/src/surfaces/tree.js +321 -0
- package/dist/src/surfaces.d.ts +14 -0
- package/dist/src/surfaces.js +33 -0
- package/dist/src/taxonomy.d.ts +86 -0
- package/dist/src/taxonomy.js +268 -0
- package/dist/src/theme-settings.d.ts +24 -0
- package/dist/src/theme-settings.js +111 -0
- package/package.json +219 -3
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Machine-readable views of the published site: RSS 2.0, `sitemap.xml` and `robots.txt`. Pure
|
|
3
|
+
* functions over graph nodes (the starter's endpoints only fetch and wrap them), so the rules —
|
|
4
|
+
* published only, noindex respected, canonical elsewhere excluded — live in one tested place.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
import { markdownText, renderBody } from "./markdown.js";
|
|
9
|
+
|
|
10
|
+
import { notFoundPageIds } from "./not-found.js";
|
|
11
|
+
import { entryPath } from "./routing.js";
|
|
12
|
+
import { absoluteUrl } from "./seo.js";
|
|
13
|
+
import { entryTaxonomy, } from "./taxonomy.js";
|
|
14
|
+
|
|
15
|
+
export const FEED_LIMIT = 50;
|
|
16
|
+
|
|
17
|
+
const xml = (s ) =>
|
|
18
|
+
s
|
|
19
|
+
.replace(/&/g, "&")
|
|
20
|
+
.replace(/</g, "<")
|
|
21
|
+
.replace(/>/g, ">")
|
|
22
|
+
.replace(/"/g, """)
|
|
23
|
+
.replace(/'/g, "'");
|
|
24
|
+
|
|
25
|
+
/** CDATA cannot contain `]]>`; split it across two sections. */
|
|
26
|
+
const cdata = (s ) => `<![CDATA[${s.replace(/]]>/g, "]]]]><![CDATA[>")}]]>`;
|
|
27
|
+
|
|
28
|
+
function published (nodes ) {
|
|
29
|
+
return nodes.filter((n) => n.status === "published");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
/** Category names of an item: public terms (section first), else legacy tags. */
|
|
62
|
+
function itemCategories(entry , index ) {
|
|
63
|
+
const shown = entryTaxonomy(index, entry);
|
|
64
|
+
const names = shown.groups
|
|
65
|
+
.flatMap((g) => (g.taxonomy.key === "tag" ? [] : g.terms))
|
|
66
|
+
.map((t) => t.name);
|
|
67
|
+
return [...new Set([...names, ...shown.keywords])];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** RSS 2.0 with full content (`content:encoded`), as feed readers and Ghost/WordPress readers expect. */
|
|
71
|
+
export function rssXml(options ) {
|
|
72
|
+
const { site, origin } = options;
|
|
73
|
+
const base = options.linkOrigin ?? origin;
|
|
74
|
+
const self = `${base}${options.feedPath ?? "/rss.xml"}`;
|
|
75
|
+
const items = published(options.entries)
|
|
76
|
+
.slice(0, FEED_LIMIT)
|
|
77
|
+
.map((e) => {
|
|
78
|
+
const url = `${base}${entryPath(e)}`;
|
|
79
|
+
const date = new Date(e.publishedAt ?? e.createdAt).toUTCString();
|
|
80
|
+
const cover = e.coverImage?.assetId ? options.assets?.get(e.coverImage.assetId) : undefined;
|
|
81
|
+
const image = cover
|
|
82
|
+
? absoluteUrl(
|
|
83
|
+
origin,
|
|
84
|
+
cover.storage.kind === "external" ? cover.storage.url : e.coverImage?.src,
|
|
85
|
+
)
|
|
86
|
+
: absoluteUrl(origin, e.coverImage?.src);
|
|
87
|
+
const mediaAttrs = cover
|
|
88
|
+
? ` type="${xml(cover.mediaType)}" fileSize="${cover.bytes}"${cover.image ? ` width="${cover.image.width}" height="${cover.image.height}"` : ""}`
|
|
89
|
+
: "";
|
|
90
|
+
const author = e.author.kind === "human" && e.author.name ? e.author.name : site.name;
|
|
91
|
+
return [
|
|
92
|
+
"<item>",
|
|
93
|
+
`<title>${xml(e.title)}</title>`,
|
|
94
|
+
`<link>${xml(url)}</link>`,
|
|
95
|
+
`<guid isPermaLink="true">${xml(url)}</guid>`,
|
|
96
|
+
`<pubDate>${date}</pubDate>`,
|
|
97
|
+
`<dc:creator>${cdata(author)}</dc:creator>`,
|
|
98
|
+
...itemCategories(e, options.taxonomy).map((t) => `<category>${xml(t)}</category>`),
|
|
99
|
+
e.summary ? `<description>${cdata(e.summary)}</description>` : "",
|
|
100
|
+
image ? `<media:content url="${xml(image)}" medium="image"${mediaAttrs}/>` : "",
|
|
101
|
+
`<content:encoded>${cdata(renderBody(e.body, options.assets ? { assets: options.assets, locale: e.locale, defaultLocale: site.defaultLocale } : undefined))}</content:encoded>`,
|
|
102
|
+
"</item>",
|
|
103
|
+
]
|
|
104
|
+
.filter(Boolean)
|
|
105
|
+
.join("");
|
|
106
|
+
});
|
|
107
|
+
const newest = published(options.entries)[0];
|
|
108
|
+
return [
|
|
109
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
110
|
+
'<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">',
|
|
111
|
+
"<channel>",
|
|
112
|
+
`<title>${xml(options.title ?? site.name)}</title>`,
|
|
113
|
+
`<link>${xml(`${base}${options.linkPath ?? "/"}`)}</link>`,
|
|
114
|
+
`<description>${xml(options.description ?? site.name)}</description>`,
|
|
115
|
+
`<language>${xml(options.locale ?? site.defaultLocale)}</language>`,
|
|
116
|
+
`<atom:link href="${xml(self)}" rel="self" type="application/rss+xml"/>`,
|
|
117
|
+
"<generator>Cloudhal</generator>",
|
|
118
|
+
newest
|
|
119
|
+
? `<lastBuildDate>${new Date(newest.publishedAt ?? newest.createdAt).toUTCString()}</lastBuildDate>`
|
|
120
|
+
: "",
|
|
121
|
+
...items,
|
|
122
|
+
"</channel>",
|
|
123
|
+
"</rss>",
|
|
124
|
+
]
|
|
125
|
+
.filter(Boolean)
|
|
126
|
+
.join("\n");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* A term's feed (`<archive>/rss.xml`, RFC-0002 §7): the archive's published entries, titled after
|
|
131
|
+
* the term, linking to its archive, described by its SEO description or intro.
|
|
132
|
+
*/
|
|
133
|
+
export function archiveFeedXml(
|
|
134
|
+
options
|
|
135
|
+
|
|
136
|
+
,
|
|
137
|
+
) {
|
|
138
|
+
const { archive, ...rest } = options;
|
|
139
|
+
const label = archive.term.labels[archive.locale];
|
|
140
|
+
const description =
|
|
141
|
+
label?.seo?.description ?? (label?.description ? markdownText(label.description) : undefined);
|
|
142
|
+
return rssXml({
|
|
143
|
+
...rest,
|
|
144
|
+
entries: archive.entries,
|
|
145
|
+
title: `${archive.shown.name} · ${options.site.name}`,
|
|
146
|
+
linkPath: archive.root,
|
|
147
|
+
...(archive.feedPath ? { feedPath: archive.feedPath } : {}),
|
|
148
|
+
...(description ? { description } : {}),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* What belongs in `sitemap.xml`: published pages and entries of the default locale, and the
|
|
161
|
+
* indexable term archives (page 1 only, `lastmod` = the later of the newest listed entry and the
|
|
162
|
+
* term; see `archiveSummaries` — pass it published entries). Pages and archives asking for
|
|
163
|
+
* `noindex`, or canonicalised to another URL, are left out (a sitemap lists canonical URLs).
|
|
164
|
+
*/
|
|
165
|
+
export function sitemapUrls(
|
|
166
|
+
site ,
|
|
167
|
+
pages ,
|
|
168
|
+
entries ,
|
|
169
|
+
origin ,
|
|
170
|
+
archives = [],
|
|
171
|
+
) {
|
|
172
|
+
const urls = [];
|
|
173
|
+
const seen = new Set ();
|
|
174
|
+
const notFound = notFoundPageIds(pages);
|
|
175
|
+
for (const page of published(pages)) {
|
|
176
|
+
if (page.locale !== site.defaultLocale || page.seo?.noindex || notFound.has(page.id)) continue;
|
|
177
|
+
const own = `${origin}${page.path === "/" ? "/" : page.path}`;
|
|
178
|
+
const canonical = absoluteUrl(origin, page.seo?.canonical);
|
|
179
|
+
if (canonical && canonical !== own) continue;
|
|
180
|
+
seen.add(page.path);
|
|
181
|
+
urls.push({ loc: own, lastmod: page.updatedAt });
|
|
182
|
+
}
|
|
183
|
+
for (const entry of published(entries)) {
|
|
184
|
+
const path = entryPath(entry);
|
|
185
|
+
if (entry.locale !== site.defaultLocale || seen.has(path)) continue;
|
|
186
|
+
seen.add(path);
|
|
187
|
+
urls.push({ loc: `${origin}${path}`, lastmod: entry.updatedAt });
|
|
188
|
+
}
|
|
189
|
+
for (const archive of archives) {
|
|
190
|
+
if (!archive.indexable || seen.has(archive.path)) continue;
|
|
191
|
+
const own = `${origin}${archive.path}`;
|
|
192
|
+
const canonical = absoluteUrl(origin, archive.term.labels[site.defaultLocale]?.seo?.canonical);
|
|
193
|
+
if (canonical && canonical !== own) continue;
|
|
194
|
+
seen.add(archive.path);
|
|
195
|
+
urls.push({ loc: own, lastmod: archive.lastmod });
|
|
196
|
+
}
|
|
197
|
+
return urls;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The site's redirects as a `_redirects` file (Cloudflare Workers/Pages static assets, Netlify):
|
|
202
|
+
* static hosts that read it answer with real 301/302s, where Astro's meta-refresh pages are only a
|
|
203
|
+
* fallback. Exact redirects come first, then prefix redirects as `from` + `from/*` splats, longest
|
|
204
|
+
* first (hosts apply the first match).
|
|
205
|
+
*/
|
|
206
|
+
export function redirectsFile(redirects ) {
|
|
207
|
+
const exact = redirects.filter((r) => r.match === "exact");
|
|
208
|
+
const prefix = redirects
|
|
209
|
+
.filter((r) => r.match === "prefix")
|
|
210
|
+
.sort((a, b) => b.from.length - a.from.length || a.from.localeCompare(b.from));
|
|
211
|
+
const splat = (to ) => {
|
|
212
|
+
const i = to.search(/[?#]/);
|
|
213
|
+
const head = (i < 0 ? to : to.slice(0, i)).replace(/\/+$/, "");
|
|
214
|
+
return `${head}/:splat${i < 0 ? "" : to.slice(i)}`;
|
|
215
|
+
};
|
|
216
|
+
const lines = [
|
|
217
|
+
"# Generated by Cloudhal from the rendered snapshot's Redirect nodes (RFC-0002). Do not edit.",
|
|
218
|
+
...exact.map((r) => `${r.from} ${r.to} ${r.status}`),
|
|
219
|
+
...prefix.flatMap((r) => [
|
|
220
|
+
`${r.from} ${r.to} ${r.status}`,
|
|
221
|
+
`${r.from === "/" ? "" : r.from}/* ${splat(r.to)} ${r.status}`,
|
|
222
|
+
]),
|
|
223
|
+
];
|
|
224
|
+
return `${lines.join("\n")}\n`;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function sitemapXml(urls ) {
|
|
228
|
+
const localized = urls.some((u) => u.alternates?.length);
|
|
229
|
+
const link = (a ) =>
|
|
230
|
+
`<xhtml:link rel="alternate" hreflang="${xml(a.locale)}" href="${xml(a.href)}"/>`;
|
|
231
|
+
return [
|
|
232
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
233
|
+
localized
|
|
234
|
+
? '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">'
|
|
235
|
+
: '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
|
|
236
|
+
...urls.map(
|
|
237
|
+
(u) =>
|
|
238
|
+
`<url><loc>${xml(u.loc)}</loc><lastmod>${xml(u.lastmod)}</lastmod>${(u.alternates ?? []).map(link).join("")}</url>`,
|
|
239
|
+
),
|
|
240
|
+
"</urlset>",
|
|
241
|
+
].join("\n");
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function robotsTxt(origin ) {
|
|
245
|
+
return `User-agent: *\nAllow: /\n\nSitemap: ${origin}/sitemap.xml\n`;
|
|
246
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Time gates on the page (RFC-0008 §7.3): what a preview says about a page or entry the public
|
|
3
|
+
* cannot see right now — "Embargoed until 2 November 2026, 09:00 (Istanbul)" or "Expired on …".
|
|
4
|
+
* The public routes never render a gated node (the published source leaves it out), so this only
|
|
5
|
+
* ever shows in previews and `cloudhal dev`. Pure and Worker-safe: `Intl` formats the instant in
|
|
6
|
+
* the site's zone; no Temporal on the site Worker (§9.4).
|
|
7
|
+
*/
|
|
8
|
+
import type { Availability, Site } from "@cloudhal/core-contracts";
|
|
9
|
+
export { edgeMaxAge, type GateState, gateState, isAvailable, MAX_EDGE_AGE, nextGate, } from "@cloudhal/core-ts/availability";
|
|
10
|
+
export interface GateNotice {
|
|
11
|
+
readonly state: "embargoed" | "expired";
|
|
12
|
+
/** The gate instant (RFC 3339). */
|
|
13
|
+
readonly at: string;
|
|
14
|
+
/** The instant as a reader of the site reads it: date and time in the site's zone, and the zone. */
|
|
15
|
+
readonly when: string;
|
|
16
|
+
}
|
|
17
|
+
/** `2 November 2026, 09:00 (Istanbul)` in the site's language and zone (UTC when it has none). */
|
|
18
|
+
export declare function formatGateTime(at: string, site: Pick<Site, "timeZone">, locale: string): string;
|
|
19
|
+
/**
|
|
20
|
+
* The notice for a node whose gates are closed at `now`, or `undefined` when it is open. `now`
|
|
21
|
+
* is the caller's clock (the request's instant), never read here.
|
|
22
|
+
*/
|
|
23
|
+
export declare function gateNotice(node: {
|
|
24
|
+
readonly availability?: Availability | undefined;
|
|
25
|
+
}, site: Pick<Site, "timeZone">, locale: string, now: number): GateNotice | undefined;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Time gates on the page (RFC-0008 §7.3): what a preview says about a page or entry the public
|
|
3
|
+
* cannot see right now — "Embargoed until 2 November 2026, 09:00 (Istanbul)" or "Expired on …".
|
|
4
|
+
* The public routes never render a gated node (the published source leaves it out), so this only
|
|
5
|
+
* ever shows in previews and `cloudhal dev`. Pure and Worker-safe: `Intl` formats the instant in
|
|
6
|
+
* the site's zone; no Temporal on the site Worker (§9.4).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { gateState, isTimeZone } from "@cloudhal/core-ts/availability";
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
edgeMaxAge,
|
|
13
|
+
|
|
14
|
+
gateState,
|
|
15
|
+
isAvailable,
|
|
16
|
+
MAX_EDGE_AGE,
|
|
17
|
+
nextGate,
|
|
18
|
+
} from "@cloudhal/core-ts/availability";
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
/** The zone's short place name: `Europe/Istanbul` → `Istanbul`, `UTC` → `UTC`. */
|
|
29
|
+
function zoneLabel(zone ) {
|
|
30
|
+
return (zone.split("/").pop() ?? zone).replace(/_/g, " ");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** `2 November 2026, 09:00 (Istanbul)` in the site's language and zone (UTC when it has none). */
|
|
34
|
+
export function formatGateTime(at , site , locale ) {
|
|
35
|
+
const zone = site.timeZone && isTimeZone(site.timeZone) ? site.timeZone : "UTC";
|
|
36
|
+
let text ;
|
|
37
|
+
try {
|
|
38
|
+
text = new Intl.DateTimeFormat(locale, {
|
|
39
|
+
dateStyle: "long",
|
|
40
|
+
timeStyle: "short",
|
|
41
|
+
timeZone: zone,
|
|
42
|
+
}).format(new Date(at));
|
|
43
|
+
} catch {
|
|
44
|
+
text = new Date(at).toISOString();
|
|
45
|
+
}
|
|
46
|
+
return `${text} (${zoneLabel(zone)})`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The notice for a node whose gates are closed at `now`, or `undefined` when it is open. `now`
|
|
51
|
+
* is the caller's clock (the request's instant), never read here.
|
|
52
|
+
*/
|
|
53
|
+
export function gateNotice(
|
|
54
|
+
node ,
|
|
55
|
+
site ,
|
|
56
|
+
locale ,
|
|
57
|
+
now ,
|
|
58
|
+
) {
|
|
59
|
+
const state = gateState(node, now);
|
|
60
|
+
if (state === "open") return undefined;
|
|
61
|
+
const at = (state === "embargoed" ? node.availability?.from : node.availability?.until) ;
|
|
62
|
+
return { state, at, when: formatGateTime(at, site, locale) };
|
|
63
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cloudhal/astro-renderer — render a Site Graph with Astro.
|
|
3
|
+
*
|
|
4
|
+
* - `cloudhal()` integration (`./integration`) exposes the project config as `virtual:cloudhal/config`.
|
|
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.
|
|
7
|
+
*/
|
|
8
|
+
export * from "./archive.js";
|
|
9
|
+
export * from "./cache-tags.js";
|
|
10
|
+
export * from "./gates.js";
|
|
11
|
+
export { type CloudhalRendererConfig, cloudhal } from "./integration.js";
|
|
12
|
+
export { renderBody, renderEntryArticle, renderMarkdown, safeHref } from "./markdown.js";
|
|
13
|
+
export * from "./navigation.js";
|
|
14
|
+
export * from "./preview.js";
|
|
15
|
+
export { getSource, resetSource } from "./runtime.js";
|
|
16
|
+
export * from "./source.js";
|
|
17
|
+
export * from "./taxonomy.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cloudhal/astro-renderer — render a Site Graph with Astro.
|
|
3
|
+
*
|
|
4
|
+
* - `cloudhal()` integration (`./integration`) exposes the project config as `virtual:cloudhal/config`.
|
|
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.
|
|
7
|
+
*/
|
|
8
|
+
export * from "./archive.js";
|
|
9
|
+
export * from "./cache-tags.js";
|
|
10
|
+
export * from "./gates.js";
|
|
11
|
+
export { cloudhal } from "./integration.js";
|
|
12
|
+
export { renderBody, renderEntryArticle, renderMarkdown, safeHref } from "./markdown.js";
|
|
13
|
+
export * from "./navigation.js";
|
|
14
|
+
export * from "./preview.js";
|
|
15
|
+
export { getSource, resetSource } from "./runtime.js";
|
|
16
|
+
export * from "./source.js";
|
|
17
|
+
export * from "./taxonomy.js";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { AstroIntegration } from "astro";
|
|
2
|
+
/** Options for the `cloudhal()` integration; made available to pages via `virtual:cloudhal/config`. */
|
|
3
|
+
export interface CloudhalRendererConfig {
|
|
4
|
+
/** Directory holding `data.db` and `blobs/` (absolute or relative to the Astro project root). */
|
|
5
|
+
readonly dataDir: string;
|
|
6
|
+
readonly siteId: string;
|
|
7
|
+
/**
|
|
8
|
+
* `live` reads current nodes on every request (dev); `snapshot:<id>` renders a frozen snapshot
|
|
9
|
+
* (build/production). `CLOUDHAL_SOURCE` in the environment overrides this.
|
|
10
|
+
*/
|
|
11
|
+
readonly source?: "live" | `snapshot:${string}`;
|
|
12
|
+
/**
|
|
13
|
+
* `node` (default): `cloudhal dev` / static `cloudhal build` from the local store.
|
|
14
|
+
* `cloudflare`: the deployed Site Worker — every page renders on demand from D1/R2 so publish and
|
|
15
|
+
* rollback are pointer flips (needs the `@astrojs/cloudflare` adapter; see ADR-0012).
|
|
16
|
+
*/
|
|
17
|
+
readonly target?: "node" | "cloudflare";
|
|
18
|
+
/**
|
|
19
|
+
* Media (RFC-0004 §7.5): `transform: "cdn-cgi"` emits responsive `srcset`s through Cloudflare
|
|
20
|
+
* image transformations on the site's own zone. Opt-in (answer 13): it needs Transformations
|
|
21
|
+
* enabled on a custom domain; the default `"none"` serves originals.
|
|
22
|
+
*/
|
|
23
|
+
readonly media?: {
|
|
24
|
+
readonly transform?: "cdn-cgi" | "none";
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Site analytics (RFC-0006) on **Node** targets: `cloudhal.json`'s `analytics` block. Without it,
|
|
28
|
+
* `CLOUDHAL_ANALYTICS` / `CLOUDHAL_ANALYTICS_DEFAULTS` in the environment decide, as on the
|
|
29
|
+
* Cloudflare Site Worker (where the Worker vars always decide and this is ignored). `dev: true`
|
|
30
|
+
* records `cloudhal dev` traffic into the local SQLite store; by default `cloudhal dev` answers the
|
|
31
|
+
* beacon endpoint and records nothing. Static builds never carry analytics (no endpoint).
|
|
32
|
+
*/
|
|
33
|
+
readonly analytics?: {
|
|
34
|
+
readonly enabled?: boolean;
|
|
35
|
+
readonly uniqueVisitors?: boolean;
|
|
36
|
+
readonly vitalsOneIn?: number;
|
|
37
|
+
readonly dev?: boolean;
|
|
38
|
+
};
|
|
39
|
+
/** Set by the integration: URL of the built page-view script (`/_cloudhal/p.<hash>.js`). */
|
|
40
|
+
readonly analyticsScript?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Reader comments (RFC-0007): `cloudhal.json`'s `comments` block. `available: false` removes
|
|
43
|
+
* the endpoints, the module and the section for the deployment; by default they exist and each
|
|
44
|
+
* site turns comments on in its settings (off until then). Static builds never have comments.
|
|
45
|
+
*/
|
|
46
|
+
readonly comments?: {
|
|
47
|
+
readonly available?: boolean;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Set by the integration when comments are available on this target: URL of the built comments
|
|
51
|
+
* module (`/_cloudhal/c.<hash>.js`, loaded only on pages with an open thread).
|
|
52
|
+
*/
|
|
53
|
+
readonly commentsScript?: string;
|
|
54
|
+
}
|
|
55
|
+
export declare function cloudhal(config: CloudhalRendererConfig): AstroIntegration;
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/** Options for the `cloudhal()` integration; made available to pages via `virtual:cloudhal/config`. */
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
const VIRTUAL_ID = "virtual:cloudhal/config";
|
|
56
|
+
const RESOLVED_ID = `\0${VIRTUAL_ID}`;
|
|
57
|
+
/** Dev only: the built analytics scripts, served from memory by the analytics middleware. */
|
|
58
|
+
const CLIENT_ID = "virtual:cloudhal/analytics-client";
|
|
59
|
+
const RESOLVED_CLIENT_ID = `\0${CLIENT_ID}`;
|
|
60
|
+
/** Dev only: the built comments module and worker, served from memory by the comments middleware. */
|
|
61
|
+
const COMMENTS_CLIENT_ID = "virtual:cloudhal/comments-client";
|
|
62
|
+
const RESOLVED_COMMENTS_CLIENT_ID = `\0${COMMENTS_CLIENT_ID}`;
|
|
63
|
+
|
|
64
|
+
export function cloudhal(config ) {
|
|
65
|
+
let resolved = config;
|
|
66
|
+
let client ;
|
|
67
|
+
let comments ;
|
|
68
|
+
/** `astro dev` (`cloudhal dev`): pages render per request. */
|
|
69
|
+
let devServer = false;
|
|
70
|
+
return {
|
|
71
|
+
name: "@cloudhal/astro-renderer",
|
|
72
|
+
hooks: {
|
|
73
|
+
"astro:config:setup": async ({
|
|
74
|
+
updateConfig,
|
|
75
|
+
addMiddleware,
|
|
76
|
+
config: astroConfig,
|
|
77
|
+
command,
|
|
78
|
+
}) => {
|
|
79
|
+
devServer = command === "dev";
|
|
80
|
+
const root = astroConfig.root.pathname;
|
|
81
|
+
resolved = {
|
|
82
|
+
...config,
|
|
83
|
+
dataDir: config.dataDir.startsWith("/") ? config.dataDir : `${root}${config.dataDir}`,
|
|
84
|
+
};
|
|
85
|
+
const cloudflare = config.target === "cloudflare";
|
|
86
|
+
// `cloudhal dev` answers the media route from the local blob store (RFC-0004 §7.8); the
|
|
87
|
+
// Cloudflare Site Worker answers it in its entry, before Astro.
|
|
88
|
+
if (!cloudflare)
|
|
89
|
+
addMiddleware({ entrypoint: "@cloudhal/astro-renderer/media-middleware", order: "pre" });
|
|
90
|
+
// Analytics (RFC-0006 §5.1, §11): the Site Worker build ships the two hashed scripts as
|
|
91
|
+
// static assets and its entry injects the tag; `cloudhal dev` serves them and answers the
|
|
92
|
+
// beacon from a middleware. Static builds have no endpoint, so no script at all.
|
|
93
|
+
if ((cloudflare && command === "build") || (!cloudflare && command === "dev")) {
|
|
94
|
+
const { buildAnalyticsClient } = await import("./analytics-client.js");
|
|
95
|
+
client = await buildAnalyticsClient();
|
|
96
|
+
resolved = { ...resolved, analyticsScript: client.script.path };
|
|
97
|
+
}
|
|
98
|
+
if (!cloudflare && command === "dev")
|
|
99
|
+
addMiddleware({
|
|
100
|
+
entrypoint: "@cloudhal/astro-renderer/analytics-middleware",
|
|
101
|
+
order: "pre",
|
|
102
|
+
});
|
|
103
|
+
// The click-to-load video player (RFC-0015 §4) and the interaction runtime (RFC-0020 §4):
|
|
104
|
+
// served here in dev, written by builds.
|
|
105
|
+
if (!cloudflare && command === "dev")
|
|
106
|
+
addMiddleware({ entrypoint: "@cloudhal/astro-renderer/embed-middleware", order: "pre" });
|
|
107
|
+
// Reader comments (RFC-0007 §8.2, §15.2), on the same targets: the Site Worker build ships
|
|
108
|
+
// the module and its proof-of-work worker as hashed static assets and its entry answers the
|
|
109
|
+
// endpoints; `cloudhal dev` serves both and answers them from a middleware on SQLite.
|
|
110
|
+
if (
|
|
111
|
+
config.comments?.available !== false &&
|
|
112
|
+
((cloudflare && command === "build") || (!cloudflare && command === "dev"))
|
|
113
|
+
) {
|
|
114
|
+
const { buildCommentsClient } = await import("./comments-client.js");
|
|
115
|
+
comments = await buildCommentsClient();
|
|
116
|
+
resolved = { ...resolved, commentsScript: comments.module.path };
|
|
117
|
+
if (!cloudflare)
|
|
118
|
+
addMiddleware({
|
|
119
|
+
entrypoint: "@cloudhal/astro-renderer/comments-middleware",
|
|
120
|
+
order: "pre",
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
updateConfig({
|
|
124
|
+
vite: {
|
|
125
|
+
plugins: [
|
|
126
|
+
{
|
|
127
|
+
name: "cloudhal-config",
|
|
128
|
+
resolveId: (id ) =>
|
|
129
|
+
id === VIRTUAL_ID
|
|
130
|
+
? RESOLVED_ID
|
|
131
|
+
: id === CLIENT_ID
|
|
132
|
+
? RESOLVED_CLIENT_ID
|
|
133
|
+
: id === COMMENTS_CLIENT_ID
|
|
134
|
+
? RESOLVED_COMMENTS_CLIENT_ID
|
|
135
|
+
: undefined,
|
|
136
|
+
load: (id ) =>
|
|
137
|
+
id === RESOLVED_ID
|
|
138
|
+
? `export default ${JSON.stringify(resolved)};`
|
|
139
|
+
: id === RESOLVED_CLIENT_ID
|
|
140
|
+
? `export default ${JSON.stringify(cloudflare ? null : (client ?? null))};`
|
|
141
|
+
: id === RESOLVED_COMMENTS_CLIENT_ID
|
|
142
|
+
? `export default ${JSON.stringify(cloudflare ? null : (comments ?? null))};`
|
|
143
|
+
: undefined,
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
// Native module must stay external in Node SSR; workerd never loads it.
|
|
147
|
+
...(cloudflare ? {} : { ssr: { external: ["better-sqlite3"] } }),
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
/**
|
|
152
|
+
* Static builds also get `_redirects` (real 301s on hosts that read it; the meta-refresh pages
|
|
153
|
+
* stay as a fallback). The Cloudflare Site Worker answers redirects per request instead, so
|
|
154
|
+
* they follow publishes without a rebuild.
|
|
155
|
+
*/
|
|
156
|
+
"astro:build:done": async ({ dir, logger }) => {
|
|
157
|
+
if (config.target === "cloudflare") {
|
|
158
|
+
// the client directory: Workers static assets, served before the Worker runs (A6)
|
|
159
|
+
if (client) {
|
|
160
|
+
const { writeAnalyticsClient } = await import("./analytics-client.js");
|
|
161
|
+
await writeAnalyticsClient(client, dir);
|
|
162
|
+
logger.info(`wrote ${client.script.path} and ${client.vitals.path}`);
|
|
163
|
+
}
|
|
164
|
+
if (comments) {
|
|
165
|
+
const { writeCommentsClient } = await import("./comments-client.js");
|
|
166
|
+
await writeCommentsClient(comments, dir);
|
|
167
|
+
logger.info(`wrote ${comments.module.path} and ${comments.solver.path}`);
|
|
168
|
+
}
|
|
169
|
+
const { writeEmbedClient, writeSurfaceClient } = await import("./static-headers.js");
|
|
170
|
+
await writeEmbedClient(dir);
|
|
171
|
+
// RFC-0020 §4: pages render per request, so the runtime is always there.
|
|
172
|
+
await writeSurfaceClient(dir);
|
|
173
|
+
const { writeCloudhalAssetHeaders } = await import("./static-headers.js");
|
|
174
|
+
await writeCloudhalAssetHeaders(dir);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
// The Site Worker's CSP and security headers, for hosts that read `_headers`; the video
|
|
178
|
+
// players' frames are allowed only when a page of this build shows one (RFC-0015 §4).
|
|
179
|
+
const { buildShowsEmbeds, writeEmbedClient, writeStaticSecurityHeaders } = await import(
|
|
180
|
+
"./static-headers.js"
|
|
181
|
+
);
|
|
182
|
+
const embeds = await buildShowsEmbeds(dir);
|
|
183
|
+
if (embeds) await writeEmbedClient(dir);
|
|
184
|
+
const { buildShowsLiveSurfaces, writeSurfaceClient } = await import("./static-headers.js");
|
|
185
|
+
if (await buildShowsLiveSurfaces(dir)) await writeSurfaceClient(dir);
|
|
186
|
+
if (await writeStaticSecurityHeaders(dir, { embeds }))
|
|
187
|
+
logger.info(
|
|
188
|
+
`wrote _headers (security headers${embeds ? ", video players allowed" : ""})`,
|
|
189
|
+
);
|
|
190
|
+
const { writeStaticRedirects } = await import("./static-redirects.js");
|
|
191
|
+
const count = await writeStaticRedirects(resolved, dir);
|
|
192
|
+
if (count > 0) logger.info(`wrote _redirects (${count} redirects)`);
|
|
193
|
+
const { writeStaticMedia } = await import("./static-media.js");
|
|
194
|
+
const media = await writeStaticMedia(resolved, dir);
|
|
195
|
+
if (media > 0) logger.info(`copied ${media} media file(s) to /media`);
|
|
196
|
+
// RFC-0008: a static build cannot open or close a time gate by itself.
|
|
197
|
+
const { staticNextGate } = await import("./static-gates.js");
|
|
198
|
+
const next = await staticNextGate(resolved, Date.now());
|
|
199
|
+
if (next !== undefined)
|
|
200
|
+
logger.warn(
|
|
201
|
+
`this build shows the site as of now; a time gate changes it at ${new Date(next).toISOString()} — build again then (the Cloudflare Site Worker needs no rebuild)`,
|
|
202
|
+
);
|
|
203
|
+
},
|
|
204
|
+
/**
|
|
205
|
+
* On Cloudflare every project page is rendered per request (pointer-flip publishing). So is
|
|
206
|
+
* every page under `cloudhal dev`: requests keep their query string (the search page,
|
|
207
|
+
* comment pages, RFC-0019 §3) and new graph nodes need no route cache. Static builds still
|
|
208
|
+
* prerender.
|
|
209
|
+
*/
|
|
210
|
+
"astro:route:setup": ({ route }) => {
|
|
211
|
+
if (
|
|
212
|
+
(config.target === "cloudflare" || devServer) &&
|
|
213
|
+
route.component.startsWith("src/pages/")
|
|
214
|
+
)
|
|
215
|
+
route.prerender = false;
|
|
216
|
+
},
|
|
217
|
+
/**
|
|
218
|
+
* Astro ≥6.3 caches `getStaticPaths()` results across dev requests, so pages added to the
|
|
219
|
+
* graph would not get routes until a restart. Watch the data directory (SQLite writes go to
|
|
220
|
+
* the WAL file, hence the directory, not just data.db) and clear the route cache the same way
|
|
221
|
+
* Astro's content layer does: the `astro:content-changed` HMR event plus a full reload.
|
|
222
|
+
*/
|
|
223
|
+
"astro:server:setup": ({ server }) => {
|
|
224
|
+
const dir = resolved.dataDir.replace(/\/$/, "");
|
|
225
|
+
server.watcher.add(dir);
|
|
226
|
+
let timer ;
|
|
227
|
+
const onChange = (file ) => {
|
|
228
|
+
if (!file.startsWith(dir)) return;
|
|
229
|
+
// the local comments key (created on the first comment request) is not content
|
|
230
|
+
if (file.endsWith("/comments.key")) return;
|
|
231
|
+
clearTimeout(timer);
|
|
232
|
+
timer = setTimeout(() => {
|
|
233
|
+
for (const env of Object.values(server.environments)) {
|
|
234
|
+
env.hot.send("astro:content-changed", {});
|
|
235
|
+
}
|
|
236
|
+
server.hot.send({ type: "full-reload" });
|
|
237
|
+
}, 100);
|
|
238
|
+
};
|
|
239
|
+
server.watcher.on("change", onChange);
|
|
240
|
+
server.watcher.on("add", onChange);
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
}
|