@composable-os/pack-content 0.0.0-stage → 0.1.0-beta.7
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 +109 -0
- package/dist/blocks/content-native-post/post-screen.d.ts +11 -0
- package/dist/blocks/content-native-post/post-screen.js +26 -0
- package/dist/blocks/content-native-rich-text/rich-text-view.d.ts +24 -0
- package/dist/blocks/content-native-rich-text/rich-text-view.js +122 -0
- package/dist/blocks/content-post/post-page.d.ts +35 -0
- package/dist/blocks/content-post/post-page.js +41 -0
- package/dist/blocks/content-post-list/post-list.d.ts +15 -0
- package/dist/blocks/content-post-list/post-list.js +25 -0
- package/dist/blocks/content-rich-text/rich-text.d.ts +28 -0
- package/dist/blocks/content-rich-text/rich-text.js +85 -0
- package/dist/client/index.d.ts +513 -0
- package/dist/client/index.js +197 -0
- package/dist/index.d.ts +167 -0
- package/dist/index.js +149 -0
- package/dist/tools.d.ts +11 -0
- package/dist/tools.js +30 -0
- package/package.json +36 -4
- package/README.md +0 -3
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
import { type ClientHolding, type PackClient } from "@composable-os/client";
|
|
2
|
+
import type { z } from "@composable-os/sdk";
|
|
3
|
+
import type { categories } from "../tools.ts";
|
|
4
|
+
type Io<T extends {
|
|
5
|
+
input: z.ZodType;
|
|
6
|
+
output: z.ZodType;
|
|
7
|
+
}> = {
|
|
8
|
+
input: z.input<T["input"]>;
|
|
9
|
+
output: z.output<T["output"]>;
|
|
10
|
+
};
|
|
11
|
+
/** The post object, as the host names it. */
|
|
12
|
+
export declare const POST = "content.post";
|
|
13
|
+
export interface TextRun {
|
|
14
|
+
type: "text";
|
|
15
|
+
text: string;
|
|
16
|
+
bold?: boolean;
|
|
17
|
+
italic?: boolean;
|
|
18
|
+
code?: boolean;
|
|
19
|
+
href?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface LineBreak {
|
|
22
|
+
type: "break";
|
|
23
|
+
}
|
|
24
|
+
export type Inline = TextRun | LineBreak;
|
|
25
|
+
export interface ListItem {
|
|
26
|
+
content: Inline[];
|
|
27
|
+
list?: ListBody;
|
|
28
|
+
}
|
|
29
|
+
export interface ListBody {
|
|
30
|
+
style: "bullet" | "ordered";
|
|
31
|
+
items: ListItem[];
|
|
32
|
+
}
|
|
33
|
+
export type Block = {
|
|
34
|
+
type: "paragraph";
|
|
35
|
+
content: Inline[];
|
|
36
|
+
} | {
|
|
37
|
+
type: "heading";
|
|
38
|
+
level: number;
|
|
39
|
+
content: Inline[];
|
|
40
|
+
} | ({
|
|
41
|
+
type: "list";
|
|
42
|
+
} & ListBody) | {
|
|
43
|
+
type: "quote";
|
|
44
|
+
content: Inline[];
|
|
45
|
+
attribution?: string;
|
|
46
|
+
} | {
|
|
47
|
+
type: "image";
|
|
48
|
+
media: string;
|
|
49
|
+
alt: string;
|
|
50
|
+
caption?: Inline[];
|
|
51
|
+
} | {
|
|
52
|
+
type: "divider";
|
|
53
|
+
} | {
|
|
54
|
+
type: "embed";
|
|
55
|
+
url: string;
|
|
56
|
+
caption?: string;
|
|
57
|
+
} | {
|
|
58
|
+
type: "callout";
|
|
59
|
+
tone: "info" | "note" | "success" | "warning";
|
|
60
|
+
content: Inline[];
|
|
61
|
+
} | {
|
|
62
|
+
type: "button";
|
|
63
|
+
label: string;
|
|
64
|
+
href: string;
|
|
65
|
+
};
|
|
66
|
+
export interface RichText {
|
|
67
|
+
blocks: Block[];
|
|
68
|
+
}
|
|
69
|
+
/** A picture field's value (ADR 0444). */
|
|
70
|
+
export interface Picture {
|
|
71
|
+
media: string;
|
|
72
|
+
alt: string;
|
|
73
|
+
focal?: {
|
|
74
|
+
x: number;
|
|
75
|
+
y: number;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** A published post as the public reads it: only the fields the post declares public. */
|
|
79
|
+
export interface PublicPost {
|
|
80
|
+
object: string;
|
|
81
|
+
id: string;
|
|
82
|
+
values: {
|
|
83
|
+
title?: string | null;
|
|
84
|
+
slug?: string | null;
|
|
85
|
+
excerpt?: string | null;
|
|
86
|
+
body?: RichText | null;
|
|
87
|
+
cover?: Picture | null;
|
|
88
|
+
/** Who wrote it, as readers see it; the author's staff account is never public. */
|
|
89
|
+
byline?: string | null;
|
|
90
|
+
/** What search engines and link previews show (ADR 0470); each optional. */
|
|
91
|
+
seoTitle?: string | null;
|
|
92
|
+
seoDescription?: string | null;
|
|
93
|
+
seoImage?: Picture | null;
|
|
94
|
+
noindex?: boolean | null;
|
|
95
|
+
category?: string | null;
|
|
96
|
+
date?: string | null;
|
|
97
|
+
publishedAt?: string | null;
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export type PostAnswer = (PublicPost & {
|
|
101
|
+
kind: "record";
|
|
102
|
+
}) | {
|
|
103
|
+
kind: "redirect";
|
|
104
|
+
object: string;
|
|
105
|
+
slug: string;
|
|
106
|
+
};
|
|
107
|
+
/** The operations this entry calls, as the generated client types them. */
|
|
108
|
+
export type ContentOperations = {
|
|
109
|
+
"content.get": {
|
|
110
|
+
kind: "query";
|
|
111
|
+
input: {
|
|
112
|
+
object: string;
|
|
113
|
+
slug: string;
|
|
114
|
+
};
|
|
115
|
+
output: PostAnswer;
|
|
116
|
+
};
|
|
117
|
+
"content.list": {
|
|
118
|
+
kind: "query";
|
|
119
|
+
input: {
|
|
120
|
+
object: string;
|
|
121
|
+
filter?: unknown;
|
|
122
|
+
sort?: {
|
|
123
|
+
field: string;
|
|
124
|
+
direction: "asc" | "desc";
|
|
125
|
+
};
|
|
126
|
+
cursor?: string;
|
|
127
|
+
limit?: number;
|
|
128
|
+
};
|
|
129
|
+
output: {
|
|
130
|
+
items: PublicPost[];
|
|
131
|
+
nextCursor: string | null;
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
"content.preview": {
|
|
135
|
+
kind: "query";
|
|
136
|
+
input: {
|
|
137
|
+
object: string;
|
|
138
|
+
slug: string;
|
|
139
|
+
token: string;
|
|
140
|
+
};
|
|
141
|
+
output: PublicPost & {
|
|
142
|
+
kind: "preview";
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
"x.content.categories": {
|
|
146
|
+
kind: "query";
|
|
147
|
+
} & Io<typeof categories>;
|
|
148
|
+
"record.publish": {
|
|
149
|
+
kind: "mutation";
|
|
150
|
+
input: {
|
|
151
|
+
object: string;
|
|
152
|
+
id: string;
|
|
153
|
+
note?: string;
|
|
154
|
+
};
|
|
155
|
+
output: {
|
|
156
|
+
version: number;
|
|
157
|
+
publishedAt: string;
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
"record.schedulePublish": {
|
|
161
|
+
kind: "mutation";
|
|
162
|
+
input: {
|
|
163
|
+
object: string;
|
|
164
|
+
id: string;
|
|
165
|
+
at: string;
|
|
166
|
+
note?: string;
|
|
167
|
+
};
|
|
168
|
+
output: {
|
|
169
|
+
publishAt: string;
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
"record.unpublish": {
|
|
173
|
+
kind: "mutation";
|
|
174
|
+
input: {
|
|
175
|
+
object: string;
|
|
176
|
+
id: string;
|
|
177
|
+
};
|
|
178
|
+
output: {
|
|
179
|
+
unpublished: number;
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
type Name = keyof ContentOperations & string;
|
|
184
|
+
type In<N extends Name> = ContentOperations[N]["input"];
|
|
185
|
+
/** The client of every operation this entry calls (ADR 0453). */
|
|
186
|
+
export type ContentClient = PackClient<ContentOperations>;
|
|
187
|
+
/**
|
|
188
|
+
* Where a picture is, at the width asked for: `/api/media/<id>?w=640` on a site's own hostname,
|
|
189
|
+
* which the client template's media route forwards to the host (issue #181). `base` puts another
|
|
190
|
+
* origin or prefix in front (`https://cdn.example.com`).
|
|
191
|
+
*/
|
|
192
|
+
export declare function mediaPath(id: string, width?: number, base?: string): string;
|
|
193
|
+
/**
|
|
194
|
+
* Where a phone asks the host itself for a picture: its public media route beside the HTTP
|
|
195
|
+
* surface, `https://<host>/api/v1/media/<id>?w=<width>` (bypassed by Cloudflare Access with the
|
|
196
|
+
* rest of /api/v1). For the native blocks' `mediaUrl`: `hostMediaUrl(hostUrl)`.
|
|
197
|
+
*/
|
|
198
|
+
export declare function hostMediaUrl(hostUrl: string): (id: string, width: number) => string;
|
|
199
|
+
/** The text of inline runs, without marks: for excerpts, alt text and screen readers. */
|
|
200
|
+
export declare function inlineText(content: readonly Inline[] | undefined): string;
|
|
201
|
+
/** A document's words, block by block, for a summary or a search box. */
|
|
202
|
+
export declare function plainText(value: RichText | null | undefined): string;
|
|
203
|
+
/** What an embed's address is: a YouTube or Vimeo video the renderer can show, or a link. */
|
|
204
|
+
export declare function embedKind(url: string): {
|
|
205
|
+
kind: "youtube";
|
|
206
|
+
id: string;
|
|
207
|
+
} | {
|
|
208
|
+
kind: "vimeo";
|
|
209
|
+
id: string;
|
|
210
|
+
} | {
|
|
211
|
+
kind: "link";
|
|
212
|
+
};
|
|
213
|
+
/** The player address for a video embed, from the id `embedKind` found (never the raw URL). */
|
|
214
|
+
export declare function embedPlayer(kind: {
|
|
215
|
+
kind: "youtube" | "vimeo";
|
|
216
|
+
id: string;
|
|
217
|
+
}): string;
|
|
218
|
+
/** A post's date as a reader sees it (`21 Nov 2026`), in their own locale. Display only. */
|
|
219
|
+
export declare function postDate(post: PublicPost, locale?: string): string;
|
|
220
|
+
/** What a post's page puts in its <head>: for search engines and link previews. */
|
|
221
|
+
export interface PostHead {
|
|
222
|
+
title: string;
|
|
223
|
+
description: string | null;
|
|
224
|
+
/** The share picture, absolute when `origin` was given. */
|
|
225
|
+
image: {
|
|
226
|
+
url: string;
|
|
227
|
+
alt: string;
|
|
228
|
+
} | null;
|
|
229
|
+
/** `noindex`: the post asks search engines not to list it. */
|
|
230
|
+
robots: "noindex" | null;
|
|
231
|
+
/** The page's own address, when `url` was given. */
|
|
232
|
+
canonical: string | null;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* The head of a post's page, from its published values (ADR 0470): its search title (else its
|
|
236
|
+
* title, with ` · <siteName>`), its search description (else its excerpt, else the start of its
|
|
237
|
+
* body), its share picture (else its cover), and whether to ask search engines not to list it.
|
|
238
|
+
* Plain data, so any page can render it: React 19's hoisted <title>/<meta> (the `PostHead` block),
|
|
239
|
+
* TanStack Start's route `head()`, or an Astro page's <head>.
|
|
240
|
+
*/
|
|
241
|
+
export declare function postHead(post: PublicPost, options?: {
|
|
242
|
+
siteName?: string;
|
|
243
|
+
/** The page's absolute address, for the canonical link and og:url. */
|
|
244
|
+
url?: string;
|
|
245
|
+
/** Where the site's media route is, for an absolute share picture (`https://example.com`). */
|
|
246
|
+
origin?: string;
|
|
247
|
+
mediaUrl?: (id: string, width: number) => string;
|
|
248
|
+
}): PostHead;
|
|
249
|
+
/** The app's `useQuery`, as far as these hooks use it. */
|
|
250
|
+
export type UseQuery = <T>(options: {
|
|
251
|
+
queryKey: readonly unknown[];
|
|
252
|
+
queryFn: () => Promise<T>;
|
|
253
|
+
enabled?: boolean;
|
|
254
|
+
}) => {
|
|
255
|
+
data: T | undefined;
|
|
256
|
+
isPending: boolean;
|
|
257
|
+
error: unknown;
|
|
258
|
+
};
|
|
259
|
+
/** The app's `useMutation`, as far as these hooks use it. */
|
|
260
|
+
export type UseMutation = <I, O>(options: {
|
|
261
|
+
mutationKey: readonly unknown[];
|
|
262
|
+
mutationFn: (input: I) => Promise<O>;
|
|
263
|
+
}) => {
|
|
264
|
+
mutate(input: I): void;
|
|
265
|
+
mutateAsync(input: I): Promise<O>;
|
|
266
|
+
data: O | undefined;
|
|
267
|
+
isPending: boolean;
|
|
268
|
+
error: unknown;
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* The operations each hook calls (ADR 0453). A customer's client and the public's get the
|
|
272
|
+
* reading hooks; the staff client gets the publish controls too.
|
|
273
|
+
*
|
|
274
|
+
* Criterion 12 (an AI actor's publish queued for a person) is undecided: `usePublish` calls
|
|
275
|
+
* `record.publish`, which is a person's only. Whatever is decided changes this map and that one
|
|
276
|
+
* hook, not the blocks.
|
|
277
|
+
*/
|
|
278
|
+
export declare const CONTENT_HOOKS_CALL: {
|
|
279
|
+
readonly usePosts: readonly ["content.list"];
|
|
280
|
+
readonly usePost: readonly ["content.get"];
|
|
281
|
+
readonly usePreview: readonly ["content.preview"];
|
|
282
|
+
readonly useCategories: readonly ["x.content.categories"];
|
|
283
|
+
readonly usePublish: readonly ["record.publish"];
|
|
284
|
+
readonly useSchedulePublish: readonly ["record.schedulePublish"];
|
|
285
|
+
readonly useUnpublish: readonly ["record.unpublish"];
|
|
286
|
+
};
|
|
287
|
+
/**
|
|
288
|
+
* Hooks for a React or React Native app:
|
|
289
|
+
* `const content = createContentHooks({ useQuery, useMutation }, api)`. `api` is any generated
|
|
290
|
+
* client: the customer's, the public's (the same customer client, signed out) or the staff one.
|
|
291
|
+
* The hooks it cannot back are not there, and calling one does not compile.
|
|
292
|
+
*/
|
|
293
|
+
export declare function createContentHooks<C extends ClientHolding>(react: {
|
|
294
|
+
useQuery: UseQuery;
|
|
295
|
+
useMutation: UseMutation;
|
|
296
|
+
}, client: C): import("@composable-os/client").HooksFor<{
|
|
297
|
+
/** A page of published posts, newest first (or as asked). */
|
|
298
|
+
usePosts(input?: Omit<In<"content.list">, "object">, object?: string): {
|
|
299
|
+
data: {
|
|
300
|
+
items: PublicPost[];
|
|
301
|
+
nextCursor: string | null;
|
|
302
|
+
} | undefined;
|
|
303
|
+
isPending: boolean;
|
|
304
|
+
error: unknown;
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* One published post by its address. An old address answers `redirect`: send the reader to
|
|
308
|
+
* the post's address now (a 301 on the web).
|
|
309
|
+
*/
|
|
310
|
+
usePost(slug: string | null, object?: string): {
|
|
311
|
+
data: PostAnswer | undefined;
|
|
312
|
+
isPending: boolean;
|
|
313
|
+
error: unknown;
|
|
314
|
+
};
|
|
315
|
+
/** A post's working copy, with a preview link's token. */
|
|
316
|
+
usePreview(slug: string | null, token: string | null, object?: string): {
|
|
317
|
+
data: (PublicPost & {
|
|
318
|
+
kind: "preview";
|
|
319
|
+
}) | undefined;
|
|
320
|
+
isPending: boolean;
|
|
321
|
+
error: unknown;
|
|
322
|
+
};
|
|
323
|
+
/** The categories, by name. */
|
|
324
|
+
useCategories(): {
|
|
325
|
+
data: {
|
|
326
|
+
categories: {
|
|
327
|
+
id: string;
|
|
328
|
+
name: string | null;
|
|
329
|
+
slug: string | null;
|
|
330
|
+
}[];
|
|
331
|
+
} | undefined;
|
|
332
|
+
isPending: boolean;
|
|
333
|
+
error: unknown;
|
|
334
|
+
};
|
|
335
|
+
/** Staff: publish a post now (a person with the publish permission). */
|
|
336
|
+
usePublish(): {
|
|
337
|
+
mutate(input: {
|
|
338
|
+
object: string;
|
|
339
|
+
id: string;
|
|
340
|
+
note?: string;
|
|
341
|
+
}): void;
|
|
342
|
+
mutateAsync(input: {
|
|
343
|
+
object: string;
|
|
344
|
+
id: string;
|
|
345
|
+
note?: string;
|
|
346
|
+
}): Promise<{
|
|
347
|
+
version: number;
|
|
348
|
+
publishedAt: string;
|
|
349
|
+
}>;
|
|
350
|
+
data: {
|
|
351
|
+
version: number;
|
|
352
|
+
publishedAt: string;
|
|
353
|
+
} | undefined;
|
|
354
|
+
isPending: boolean;
|
|
355
|
+
error: unknown;
|
|
356
|
+
};
|
|
357
|
+
/** Staff: publish a post at a time; what is published is the post as it is now. */
|
|
358
|
+
useSchedulePublish(): {
|
|
359
|
+
mutate(input: {
|
|
360
|
+
object: string;
|
|
361
|
+
id: string;
|
|
362
|
+
at: string;
|
|
363
|
+
note?: string;
|
|
364
|
+
}): void;
|
|
365
|
+
mutateAsync(input: {
|
|
366
|
+
object: string;
|
|
367
|
+
id: string;
|
|
368
|
+
at: string;
|
|
369
|
+
note?: string;
|
|
370
|
+
}): Promise<{
|
|
371
|
+
publishAt: string;
|
|
372
|
+
}>;
|
|
373
|
+
data: {
|
|
374
|
+
publishAt: string;
|
|
375
|
+
} | undefined;
|
|
376
|
+
isPending: boolean;
|
|
377
|
+
error: unknown;
|
|
378
|
+
};
|
|
379
|
+
/** Staff: take a post down; its versions are kept. */
|
|
380
|
+
useUnpublish(): {
|
|
381
|
+
mutate(input: {
|
|
382
|
+
object: string;
|
|
383
|
+
id: string;
|
|
384
|
+
}): void;
|
|
385
|
+
mutateAsync(input: {
|
|
386
|
+
object: string;
|
|
387
|
+
id: string;
|
|
388
|
+
}): Promise<{
|
|
389
|
+
unpublished: number;
|
|
390
|
+
}>;
|
|
391
|
+
data: {
|
|
392
|
+
unpublished: number;
|
|
393
|
+
} | undefined;
|
|
394
|
+
isPending: boolean;
|
|
395
|
+
error: unknown;
|
|
396
|
+
};
|
|
397
|
+
}, {
|
|
398
|
+
readonly usePosts: readonly ["content.list"];
|
|
399
|
+
readonly usePost: readonly ["content.get"];
|
|
400
|
+
readonly usePreview: readonly ["content.preview"];
|
|
401
|
+
readonly useCategories: readonly ["x.content.categories"];
|
|
402
|
+
readonly usePublish: readonly ["record.publish"];
|
|
403
|
+
readonly useSchedulePublish: readonly ["record.schedulePublish"];
|
|
404
|
+
readonly useUnpublish: readonly ["record.unpublish"];
|
|
405
|
+
}, import("@composable-os/client").OperationsOf<C>>;
|
|
406
|
+
declare function allContentHooks(react: {
|
|
407
|
+
useQuery: UseQuery;
|
|
408
|
+
useMutation: UseMutation;
|
|
409
|
+
}, api: ContentClient): {
|
|
410
|
+
/** A page of published posts, newest first (or as asked). */
|
|
411
|
+
usePosts(input?: Omit<In<"content.list">, "object">, object?: string): {
|
|
412
|
+
data: {
|
|
413
|
+
items: PublicPost[];
|
|
414
|
+
nextCursor: string | null;
|
|
415
|
+
} | undefined;
|
|
416
|
+
isPending: boolean;
|
|
417
|
+
error: unknown;
|
|
418
|
+
};
|
|
419
|
+
/**
|
|
420
|
+
* One published post by its address. An old address answers `redirect`: send the reader to
|
|
421
|
+
* the post's address now (a 301 on the web).
|
|
422
|
+
*/
|
|
423
|
+
usePost(slug: string | null, object?: string): {
|
|
424
|
+
data: PostAnswer | undefined;
|
|
425
|
+
isPending: boolean;
|
|
426
|
+
error: unknown;
|
|
427
|
+
};
|
|
428
|
+
/** A post's working copy, with a preview link's token. */
|
|
429
|
+
usePreview(slug: string | null, token: string | null, object?: string): {
|
|
430
|
+
data: (PublicPost & {
|
|
431
|
+
kind: "preview";
|
|
432
|
+
}) | undefined;
|
|
433
|
+
isPending: boolean;
|
|
434
|
+
error: unknown;
|
|
435
|
+
};
|
|
436
|
+
/** The categories, by name. */
|
|
437
|
+
useCategories(): {
|
|
438
|
+
data: {
|
|
439
|
+
categories: {
|
|
440
|
+
id: string;
|
|
441
|
+
name: string | null;
|
|
442
|
+
slug: string | null;
|
|
443
|
+
}[];
|
|
444
|
+
} | undefined;
|
|
445
|
+
isPending: boolean;
|
|
446
|
+
error: unknown;
|
|
447
|
+
};
|
|
448
|
+
/** Staff: publish a post now (a person with the publish permission). */
|
|
449
|
+
usePublish(): {
|
|
450
|
+
mutate(input: {
|
|
451
|
+
object: string;
|
|
452
|
+
id: string;
|
|
453
|
+
note?: string;
|
|
454
|
+
}): void;
|
|
455
|
+
mutateAsync(input: {
|
|
456
|
+
object: string;
|
|
457
|
+
id: string;
|
|
458
|
+
note?: string;
|
|
459
|
+
}): Promise<{
|
|
460
|
+
version: number;
|
|
461
|
+
publishedAt: string;
|
|
462
|
+
}>;
|
|
463
|
+
data: {
|
|
464
|
+
version: number;
|
|
465
|
+
publishedAt: string;
|
|
466
|
+
} | undefined;
|
|
467
|
+
isPending: boolean;
|
|
468
|
+
error: unknown;
|
|
469
|
+
};
|
|
470
|
+
/** Staff: publish a post at a time; what is published is the post as it is now. */
|
|
471
|
+
useSchedulePublish(): {
|
|
472
|
+
mutate(input: {
|
|
473
|
+
object: string;
|
|
474
|
+
id: string;
|
|
475
|
+
at: string;
|
|
476
|
+
note?: string;
|
|
477
|
+
}): void;
|
|
478
|
+
mutateAsync(input: {
|
|
479
|
+
object: string;
|
|
480
|
+
id: string;
|
|
481
|
+
at: string;
|
|
482
|
+
note?: string;
|
|
483
|
+
}): Promise<{
|
|
484
|
+
publishAt: string;
|
|
485
|
+
}>;
|
|
486
|
+
data: {
|
|
487
|
+
publishAt: string;
|
|
488
|
+
} | undefined;
|
|
489
|
+
isPending: boolean;
|
|
490
|
+
error: unknown;
|
|
491
|
+
};
|
|
492
|
+
/** Staff: take a post down; its versions are kept. */
|
|
493
|
+
useUnpublish(): {
|
|
494
|
+
mutate(input: {
|
|
495
|
+
object: string;
|
|
496
|
+
id: string;
|
|
497
|
+
}): void;
|
|
498
|
+
mutateAsync(input: {
|
|
499
|
+
object: string;
|
|
500
|
+
id: string;
|
|
501
|
+
}): Promise<{
|
|
502
|
+
unpublished: number;
|
|
503
|
+
}>;
|
|
504
|
+
data: {
|
|
505
|
+
unpublished: number;
|
|
506
|
+
} | undefined;
|
|
507
|
+
isPending: boolean;
|
|
508
|
+
error: unknown;
|
|
509
|
+
};
|
|
510
|
+
};
|
|
511
|
+
/** Every hook of this entry. A block takes the ones it calls: `Pick<ContentHooks, "usePost">`. */
|
|
512
|
+
export type ContentHooks = ReturnType<typeof allContentHooks>;
|
|
513
|
+
export {};
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// The Content pack's headless entry (stage 28, step 6; ADRs 0418 and 0453): functions and hooks
|
|
2
|
+
// over the typed client, no markup. A client app imports it as `@composable-os/pack-content/client`
|
|
3
|
+
// and hands it its generated client; the content blocks render what it returns.
|
|
4
|
+
//
|
|
5
|
+
// The public reads posts with the host's own `content.get`, `content.list` and `content.preview`
|
|
6
|
+
// (ADR 0442): only published versions, only the fields the post declares public. Nothing here
|
|
7
|
+
// decides what is published or who may read it. Rich text arrives as structured data (ADR 0440);
|
|
8
|
+
// the renderers draw it block by block with the app's own components and never as HTML.
|
|
9
|
+
import { packHooks } from "@composable-os/client";
|
|
10
|
+
/** The post object, as the host names it. */
|
|
11
|
+
export const POST = "content.post";
|
|
12
|
+
// --- Plain functions ------------------------------------------------------------------------------
|
|
13
|
+
/**
|
|
14
|
+
* Where a picture is, at the width asked for: `/api/media/<id>?w=640` on a site's own hostname,
|
|
15
|
+
* which the client template's media route forwards to the host (issue #181). `base` puts another
|
|
16
|
+
* origin or prefix in front (`https://cdn.example.com`).
|
|
17
|
+
*/
|
|
18
|
+
export function mediaPath(id, width, base = "") {
|
|
19
|
+
return `${base.replace(/\/+$/, "")}/api/media/${encodeURIComponent(id)}${width ? `?w=${Math.round(width)}` : ""}`;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Where a phone asks the host itself for a picture: its public media route beside the HTTP
|
|
23
|
+
* surface, `https://<host>/api/v1/media/<id>?w=<width>` (bypassed by Cloudflare Access with the
|
|
24
|
+
* rest of /api/v1). For the native blocks' `mediaUrl`: `hostMediaUrl(hostUrl)`.
|
|
25
|
+
*/
|
|
26
|
+
export function hostMediaUrl(hostUrl) {
|
|
27
|
+
const host = hostUrl.replace(/\/+$/, "");
|
|
28
|
+
return (id, width) => `${host}/api/v1/media/${encodeURIComponent(id)}${width ? `?w=${Math.round(width)}` : ""}`;
|
|
29
|
+
}
|
|
30
|
+
/** The text of inline runs, without marks: for excerpts, alt text and screen readers. */
|
|
31
|
+
export function inlineText(content) {
|
|
32
|
+
return (content ?? []).map((run) => (run.type === "text" ? run.text : "\n")).join("");
|
|
33
|
+
}
|
|
34
|
+
/** A document's words, block by block, for a summary or a search box. */
|
|
35
|
+
export function plainText(value) {
|
|
36
|
+
const out = [];
|
|
37
|
+
const list = (body) => {
|
|
38
|
+
for (const item of body.items) {
|
|
39
|
+
out.push(inlineText(item.content));
|
|
40
|
+
if (item.list)
|
|
41
|
+
list(item.list);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
for (const block of value?.blocks ?? []) {
|
|
45
|
+
switch (block.type) {
|
|
46
|
+
case "paragraph":
|
|
47
|
+
case "heading":
|
|
48
|
+
case "quote":
|
|
49
|
+
case "callout":
|
|
50
|
+
out.push(inlineText(block.content));
|
|
51
|
+
break;
|
|
52
|
+
case "list":
|
|
53
|
+
list(block);
|
|
54
|
+
break;
|
|
55
|
+
case "image":
|
|
56
|
+
if (block.caption)
|
|
57
|
+
out.push(inlineText(block.caption));
|
|
58
|
+
break;
|
|
59
|
+
case "embed":
|
|
60
|
+
if (block.caption)
|
|
61
|
+
out.push(block.caption);
|
|
62
|
+
break;
|
|
63
|
+
case "button":
|
|
64
|
+
out.push(block.label);
|
|
65
|
+
break;
|
|
66
|
+
case "divider":
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return out.filter(Boolean).join("\n");
|
|
71
|
+
}
|
|
72
|
+
/** What an embed's address is: a YouTube or Vimeo video the renderer can show, or a link. */
|
|
73
|
+
export function embedKind(url) {
|
|
74
|
+
const youtube = /^https:\/\/(?:www\.|m\.)?youtube\.com\/watch\?(?:.*&)?v=([A-Za-z0-9_-]{6,20})/.exec(url) ??
|
|
75
|
+
/^https:\/\/youtu\.be\/([A-Za-z0-9_-]{6,20})/.exec(url) ??
|
|
76
|
+
/^https:\/\/(?:www\.)?youtube\.com\/(?:embed|shorts)\/([A-Za-z0-9_-]{6,20})/.exec(url);
|
|
77
|
+
if (youtube?.[1])
|
|
78
|
+
return { kind: "youtube", id: youtube[1] };
|
|
79
|
+
const vimeo = /^https:\/\/(?:www\.|player\.)?vimeo\.com\/(?:video\/)?([0-9]{4,15})/.exec(url);
|
|
80
|
+
if (vimeo?.[1])
|
|
81
|
+
return { kind: "vimeo", id: vimeo[1] };
|
|
82
|
+
return { kind: "link" };
|
|
83
|
+
}
|
|
84
|
+
/** The player address for a video embed, from the id `embedKind` found (never the raw URL). */
|
|
85
|
+
export function embedPlayer(kind) {
|
|
86
|
+
return kind.kind === "youtube"
|
|
87
|
+
? `https://www.youtube-nocookie.com/embed/${encodeURIComponent(kind.id)}`
|
|
88
|
+
: `https://player.vimeo.com/video/${encodeURIComponent(kind.id)}`;
|
|
89
|
+
}
|
|
90
|
+
/** A post's date as a reader sees it (`21 Nov 2026`), in their own locale. Display only. */
|
|
91
|
+
export function postDate(post, locale) {
|
|
92
|
+
const iso = post.values.date ?? post.values.publishedAt;
|
|
93
|
+
if (!iso)
|
|
94
|
+
return "";
|
|
95
|
+
const at = new Date(iso);
|
|
96
|
+
if (Number.isNaN(at.getTime()))
|
|
97
|
+
return "";
|
|
98
|
+
try {
|
|
99
|
+
return new Intl.DateTimeFormat(locale, { dateStyle: "medium" }).format(at);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return iso.slice(0, 10);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The head of a post's page, from its published values (ADR 0470): its search title (else its
|
|
107
|
+
* title, with ` · <siteName>`), its search description (else its excerpt, else the start of its
|
|
108
|
+
* body), its share picture (else its cover), and whether to ask search engines not to list it.
|
|
109
|
+
* Plain data, so any page can render it: React 19's hoisted <title>/<meta> (the `PostHead` block),
|
|
110
|
+
* TanStack Start's route `head()`, or an Astro page's <head>.
|
|
111
|
+
*/
|
|
112
|
+
export function postHead(post, options = {}) {
|
|
113
|
+
const v = post.values;
|
|
114
|
+
const base = v.seoTitle || v.title || "";
|
|
115
|
+
const title = options.siteName && !v.seoTitle ? `${base} · ${options.siteName}` : base;
|
|
116
|
+
const words = v.seoDescription || v.excerpt || plainText(v.body).slice(0, 200).trim();
|
|
117
|
+
const picture = v.seoImage ?? v.cover ?? null;
|
|
118
|
+
const toUrl = options.mediaUrl ?? ((id, width) => mediaPath(id, width, options.origin ?? ""));
|
|
119
|
+
return {
|
|
120
|
+
title,
|
|
121
|
+
description: words || null,
|
|
122
|
+
image: picture ? { url: toUrl(picture.media, 1200), alt: picture.alt } : null,
|
|
123
|
+
robots: v.noindex ? "noindex" : null,
|
|
124
|
+
canonical: options.url ?? null,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The operations each hook calls (ADR 0453). A customer's client and the public's get the
|
|
129
|
+
* reading hooks; the staff client gets the publish controls too.
|
|
130
|
+
*
|
|
131
|
+
* Criterion 12 (an AI actor's publish queued for a person) is undecided: `usePublish` calls
|
|
132
|
+
* `record.publish`, which is a person's only. Whatever is decided changes this map and that one
|
|
133
|
+
* hook, not the blocks.
|
|
134
|
+
*/
|
|
135
|
+
export const CONTENT_HOOKS_CALL = {
|
|
136
|
+
usePosts: ["content.list"],
|
|
137
|
+
usePost: ["content.get"],
|
|
138
|
+
usePreview: ["content.preview"],
|
|
139
|
+
useCategories: ["x.content.categories"],
|
|
140
|
+
usePublish: ["record.publish"],
|
|
141
|
+
useSchedulePublish: ["record.schedulePublish"],
|
|
142
|
+
useUnpublish: ["record.unpublish"],
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Hooks for a React or React Native app:
|
|
146
|
+
* `const content = createContentHooks({ useQuery, useMutation }, api)`. `api` is any generated
|
|
147
|
+
* client: the customer's, the public's (the same customer client, signed out) or the staff one.
|
|
148
|
+
* The hooks it cannot back are not there, and calling one does not compile.
|
|
149
|
+
*/
|
|
150
|
+
export function createContentHooks(react, client) {
|
|
151
|
+
return packHooks(client, CONTENT_HOOKS_CALL, (api) => allContentHooks(react, api));
|
|
152
|
+
}
|
|
153
|
+
function allContentHooks(react, api) {
|
|
154
|
+
const { useQuery, useMutation } = react;
|
|
155
|
+
return {
|
|
156
|
+
/** A page of published posts, newest first (or as asked). */
|
|
157
|
+
usePosts(input = {}, object = POST) {
|
|
158
|
+
return useQuery(api.query("content.list", { object, ...input }));
|
|
159
|
+
},
|
|
160
|
+
/**
|
|
161
|
+
* One published post by its address. An old address answers `redirect`: send the reader to
|
|
162
|
+
* the post's address now (a 301 on the web).
|
|
163
|
+
*/
|
|
164
|
+
usePost(slug, object = POST) {
|
|
165
|
+
const options = slug
|
|
166
|
+
? api.query("content.get", { object, slug })
|
|
167
|
+
: { queryKey: ["content.get", "none"], queryFn: async () => undefined };
|
|
168
|
+
return useQuery({ ...options, enabled: slug !== null });
|
|
169
|
+
},
|
|
170
|
+
/** A post's working copy, with a preview link's token. */
|
|
171
|
+
usePreview(slug, token, object = POST) {
|
|
172
|
+
const options = slug && token
|
|
173
|
+
? api.query("content.preview", { object, slug, token })
|
|
174
|
+
: { queryKey: ["content.preview", "none"], queryFn: async () => undefined };
|
|
175
|
+
return useQuery({
|
|
176
|
+
...options,
|
|
177
|
+
enabled: Boolean(slug && token),
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
/** The categories, by name. */
|
|
181
|
+
useCategories() {
|
|
182
|
+
return useQuery(api.query("x.content.categories", {}));
|
|
183
|
+
},
|
|
184
|
+
/** Staff: publish a post now (a person with the publish permission). */
|
|
185
|
+
usePublish() {
|
|
186
|
+
return useMutation(api.mutation("record.publish"));
|
|
187
|
+
},
|
|
188
|
+
/** Staff: publish a post at a time; what is published is the post as it is now. */
|
|
189
|
+
useSchedulePublish() {
|
|
190
|
+
return useMutation(api.mutation("record.schedulePublish"));
|
|
191
|
+
},
|
|
192
|
+
/** Staff: take a post down; its versions are kept. */
|
|
193
|
+
useUnpublish() {
|
|
194
|
+
return useMutation(api.mutation("record.unpublish"));
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|