@composable-os/pack-content 0.1.0-beta.11 → 0.1.0-beta.14

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.
@@ -1,7 +1,8 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { postDate } from "@composable-os/pack-content/client";
3
- import { Image, ScrollView, StyleSheet, Text, View } from "react-native";
3
+ import { Image, ScrollView, StyleSheet, View } from "react-native";
4
4
  import { RichTextView } from "../content-native-rich-text/rich-text-view.js";
5
+ import { Text } from "../native-base/text.js";
5
6
  import { theme } from "../native-base/theme.js";
6
7
  export function PostScreen({ content, slug, openUrl, mediaUrl, onRedirect, width, }) {
7
8
  const { data, isPending, error } = content.usePost(slug);
@@ -4,6 +4,8 @@ export type NativeBlockRenderer<T extends Block["type"]> = (props: {
4
4
  block: Extract<Block, {
5
5
  type: T;
6
6
  }>;
7
+ /** A heading's anchor (ADR 0700), as the web block's `id`: give it as the view's `nativeID`. */
8
+ anchor?: string;
7
9
  }) => ReactNode;
8
10
  export interface RichTextViewProps {
9
11
  value: RichTextValue | null | undefined;
@@ -19,6 +21,11 @@ export interface RichTextViewProps {
19
21
  components?: {
20
22
  [T in Block["type"]]?: NativeBlockRenderer<T>;
21
23
  };
24
+ /**
25
+ * Where each heading is laid out, by its anchor (ADR 0700): scroll to `y` to open a search
26
+ * result or a citation at its section.
27
+ */
28
+ onAnchorLayout?: (anchor: string, y: number) => void;
22
29
  }
23
30
  /** A rich text document. An empty or missing one draws nothing. */
24
- export declare function RichTextView({ value, openUrl, mediaUrl, width, components, }: RichTextViewProps): import("react").JSX.Element | null;
31
+ export declare function RichTextView({ value, openUrl, mediaUrl, width, components, onAnchorLayout, }: RichTextViewProps): import("react").JSX.Element | null;
@@ -6,8 +6,9 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
6
6
  // in-app browser) for links, buttons and videos.
7
7
  //
8
8
  // No web view: an embed is a tappable card that opens the video (or the link) outside the app.
9
- import { embedKind, } from "@composable-os/pack-content/client";
10
- import { Image, Pressable, StyleSheet, Text, View } from "react-native";
9
+ import { embedKind, headingAnchors, } from "@composable-os/pack-content/client";
10
+ import { Image, Pressable, StyleSheet, View } from "react-native";
11
+ import { Text } from "../native-base/text.js";
11
12
  import { theme } from "../native-base/theme.js";
12
13
  function Runs({ content, openUrl, style, }) {
13
14
  return (_jsx(Text, { style: [styles.body, style], children: (content ?? []).map((run, i) => {
@@ -36,17 +37,17 @@ function ListView({ body, openUrl, depth, }) {
36
37
  return (_jsx(View, { testID: `rt-block-list-${body.style}`, style: { paddingLeft: depth * theme.space.md }, children: body.items.map((item, i) => (_jsxs(View, { children: [_jsxs(View, { style: styles.listRow, children: [_jsx(Text, { style: styles.body, children: body.style === "ordered" ? `${i + 1}.` : "•" }), _jsx(Runs, { content: item.content, openUrl: openUrl, style: styles.listText })] }), item.list ? _jsx(ListView, { body: item.list, openUrl: openUrl, depth: depth + 1 }) : null] }, i))) }));
37
38
  }
38
39
  const TONE_COLOR = {
39
- info: "#0ea5e9",
40
+ info: theme.color.info,
40
41
  note: theme.color.muted,
41
- success: "#10b981",
42
- warning: "#f59e0b",
42
+ success: theme.color.success,
43
+ warning: theme.color.warning,
43
44
  };
44
- function DefaultBlock({ block, openUrl, mediaUrl, width, }) {
45
+ function DefaultBlock({ block, openUrl, mediaUrl, width, anchor, }) {
45
46
  switch (block.type) {
46
47
  case "paragraph":
47
48
  return (_jsx(View, { testID: "rt-block-paragraph", children: _jsx(Runs, { content: block.content, openUrl: openUrl }) }));
48
49
  case "heading":
49
- return (_jsx(View, { testID: "rt-block-heading", accessibilityRole: "header", children: _jsx(Runs, { content: block.content, openUrl: openUrl, style: {
50
+ return (_jsx(View, { testID: "rt-block-heading", accessibilityRole: "header", nativeID: anchor, children: _jsx(Runs, { content: block.content, openUrl: openUrl, style: {
50
51
  fontSize: Math.max(theme.font.title - (block.level - 1) * 2, theme.font.body),
51
52
  fontWeight: "700",
52
53
  } }) }));
@@ -71,13 +72,23 @@ function DefaultBlock({ block, openUrl, mediaUrl, width, }) {
71
72
  }
72
73
  }
73
74
  /** A rich text document. An empty or missing one draws nothing. */
74
- export function RichTextView({ value, openUrl, mediaUrl, width = 960, components = {}, }) {
75
+ export function RichTextView({ value, openUrl, mediaUrl, width = 960, components = {}, onAnchorLayout, }) {
75
76
  const blocks = value?.blocks ?? [];
76
77
  if (!blocks.length)
77
78
  return null;
79
+ // Each heading's anchor, made by the host's own rule: search results and citations name it.
80
+ const anchors = headingAnchors(value);
81
+ let heading = 0;
78
82
  return (_jsx(View, { style: styles.document, testID: "rt-document", children: blocks.map((block, i) => {
79
83
  const custom = components[block.type];
80
- return (_jsx(View, { children: custom ? (custom({ block })) : (_jsx(DefaultBlock, { block: block, openUrl: openUrl, mediaUrl: mediaUrl, width: width })) }, i));
84
+ const anchor = block.type === "heading" ? anchors[heading++] : undefined;
85
+ return (_jsx(View
86
+ // biome-ignore lint/suspicious/noArrayIndexKey: blocks are positional in a document.
87
+ , { ...(anchor && onAnchorLayout
88
+ ? {
89
+ onLayout: (e) => onAnchorLayout(anchor, e.nativeEvent.layout.y),
90
+ }
91
+ : {}), children: custom ? (custom({ block, ...(anchor ? { anchor } : {}) })) : (_jsx(DefaultBlock, { block: block, openUrl: openUrl, mediaUrl: mediaUrl, width: width, anchor: anchor })) }, i));
81
92
  }) }));
82
93
  }
83
94
  const styles = StyleSheet.create({
@@ -6,6 +6,8 @@ export type BlockRenderer<T extends Block["type"]> = (props: {
6
6
  type: T;
7
7
  }>;
8
8
  children?: ReactNode;
9
+ /** A heading's anchor (ADR 0700): give it as the element's `id`, so `#<anchor>` lands on it. */
10
+ anchor?: string;
9
11
  }) => ReactNode;
10
12
  export interface RichTextProps {
11
13
  value: RichTextValue | null | undefined;
@@ -24,5 +26,3 @@ export interface RichTextProps {
24
26
  }
25
27
  /** A rich text document. An empty or missing one draws nothing. */
26
28
  export declare function RichText({ value, mediaBase, mediaUrl, components, className, }: RichTextProps): import("react").JSX.Element | null;
27
- /** A document's first paragraph as text, for a summary where the post has no excerpt. */
28
- export declare function firstParagraph(value: RichTextValue | null | undefined): string;
@@ -7,8 +7,14 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
7
7
  // host's checked one, a picture is the host's media route, and a video is played from YouTube's
8
8
  // or Vimeo's own player address built from the id, never from the raw URL. An embed that is
9
9
  // neither is a link.
10
- import { embedKind, embedPlayer, inlineText, mediaPath, } from "@composable-os/pack-content/client";
10
+ import { embedKind, embedPlayer, headingAnchors, mediaPath, } from "@composable-os/pack-content/client";
11
11
  import { Button } from "../ui-button/button.js";
12
+ /** A body heading's size by its level: larger than the text, smaller than the post's title. */
13
+ const HEADING_SIZE = {
14
+ 2: "text-2xl",
15
+ 3: "text-xl",
16
+ 4: "text-lg",
17
+ };
12
18
  const WIDTHS = [640, 960, 1280];
13
19
  /** One run of text with its marks, as nested elements. */
14
20
  function Run({ run }) {
@@ -40,7 +46,7 @@ const TONES = {
40
46
  success: "border-emerald-500",
41
47
  warning: "border-amber-500",
42
48
  };
43
- function DefaultBlock({ block, mediaUrl, }) {
49
+ function DefaultBlock({ block, mediaUrl, anchor, }) {
44
50
  switch (block.type) {
45
51
  case "paragraph":
46
52
  return (_jsx("p", { "data-block": "paragraph", children: _jsx(Runs, { content: block.content }) }));
@@ -48,7 +54,7 @@ function DefaultBlock({ block, mediaUrl, }) {
48
54
  // A post's title is its h1: the body's headings start one below it.
49
55
  const level = Math.min(Math.max(block.level + 1, 2), 6);
50
56
  const Tag = `h${level}`;
51
- return (_jsx(Tag, { "data-block": "heading", "data-level": block.level, className: "font-semibold", children: _jsx(Runs, { content: block.content }) }));
57
+ return (_jsx(Tag, { id: anchor, "data-block": "heading", "data-level": block.level, className: `mt-2 font-semibold ${HEADING_SIZE[level] ?? "text-base"}`, children: _jsx(Runs, { content: block.content }) }));
52
58
  }
53
59
  case "list":
54
60
  return _jsx(List, { body: block });
@@ -73,13 +79,12 @@ export function RichText({ value, mediaBase = "", mediaUrl = (id, width) => medi
73
79
  const blocks = value?.blocks ?? [];
74
80
  if (!blocks.length)
75
81
  return null;
82
+ // Each heading's anchor, made by the host's own rule: search results and citations link to it.
83
+ const anchors = headingAnchors(value);
84
+ let heading = 0;
76
85
  return (_jsx("div", { className: className ?? "grid gap-4", "data-rich-text": "", children: blocks.map((block, i) => {
77
86
  const custom = components[block.type];
78
- return (_jsx("div", { className: "contents", children: custom ? (custom({ block })) : (_jsx(DefaultBlock, { block: block, mediaUrl: mediaUrl })) }, i));
87
+ const anchor = block.type === "heading" ? anchors[heading++] : undefined;
88
+ return (_jsx("div", { className: "contents", children: custom ? (custom({ block, ...(anchor ? { anchor } : {}) })) : (_jsx(DefaultBlock, { block: block, mediaUrl: mediaUrl, anchor: anchor })) }, i));
79
89
  }) }));
80
90
  }
81
- /** A document's first paragraph as text, for a summary where the post has no excerpt. */
82
- export function firstParagraph(value) {
83
- const first = value?.blocks.find((b) => b.type === "paragraph");
84
- return first && first.type === "paragraph" ? inlineText(first.content) : "";
85
- }
@@ -1,4 +1,4 @@
1
- import { type ClientHolding, type PackClient } from "@composable-os/client";
1
+ import { type ClientHolding, type PackClient, type UseMutation, type UseQuery } from "@composable-os/client";
2
2
  import type { z } from "@composable-os/sdk";
3
3
  import type { categories } from "../tools.ts";
4
4
  type Io<T extends {
@@ -196,6 +196,11 @@ export declare function mediaPath(id: string, width?: number, base?: string): st
196
196
  * rest of /api/v1). For the native blocks' `mediaUrl`: `hostMediaUrl(hostUrl)`.
197
197
  */
198
198
  export declare function hostMediaUrl(hostUrl: string): (id: string, width: number) => string;
199
+ /**
200
+ * Each heading's anchor, in document order (ADR 0700): the host's own rule, so a renderer's
201
+ * heading ids are the anchors `content.search` returns.
202
+ */
203
+ export { headingAnchors } from "@composable-os/sdk/rich-text";
199
204
  /** The text of inline runs, without marks: for excerpts, alt text and screen readers. */
200
205
  export declare function inlineText(content: readonly Inline[] | undefined): string;
201
206
  /** A document's words, block by block, for a summary or a search box. */
@@ -246,27 +251,7 @@ export declare function postHead(post: PublicPost, options?: {
246
251
  origin?: string;
247
252
  mediaUrl?: (id: string, width: number) => string;
248
253
  }): 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
- };
254
+ export type { UseMutation, UseQuery } from "@composable-os/client";
270
255
  /**
271
256
  * The operations each hook calls (ADR 0453). A customer's client and the public's get the
272
257
  * reading hooks; the staff client gets the publish controls too.
@@ -303,6 +288,7 @@ export declare function createContentHooks<C extends ClientHolding>(react: {
303
288
  } | undefined;
304
289
  isPending: boolean;
305
290
  error: unknown;
291
+ refetch?: () => unknown;
306
292
  };
307
293
  /**
308
294
  * One published post by its address. An old address answers `redirect`: send the reader to
@@ -312,6 +298,7 @@ export declare function createContentHooks<C extends ClientHolding>(react: {
312
298
  data: PostAnswer | undefined;
313
299
  isPending: boolean;
314
300
  error: unknown;
301
+ refetch?: () => unknown;
315
302
  };
316
303
  /** A post's working copy, with a preview link's token. */
317
304
  usePreview(slug: string | null, token: string | null, object?: string): {
@@ -320,6 +307,7 @@ export declare function createContentHooks<C extends ClientHolding>(react: {
320
307
  }) | undefined;
321
308
  isPending: boolean;
322
309
  error: unknown;
310
+ refetch?: () => unknown;
323
311
  };
324
312
  /** The categories, by name. */
325
313
  useCategories(): {
@@ -332,6 +320,7 @@ export declare function createContentHooks<C extends ClientHolding>(react: {
332
320
  } | undefined;
333
321
  isPending: boolean;
334
322
  error: unknown;
323
+ refetch?: () => unknown;
335
324
  };
336
325
  /** Staff: publish a post now (a person with the publish permission). */
337
326
  usePublish(): {
@@ -354,6 +343,7 @@ export declare function createContentHooks<C extends ClientHolding>(react: {
354
343
  } | undefined;
355
344
  isPending: boolean;
356
345
  error: unknown;
346
+ reset?: () => void;
357
347
  };
358
348
  /** Staff: publish a post at a time; what is published is the post as it is now. */
359
349
  useSchedulePublish(): {
@@ -376,6 +366,7 @@ export declare function createContentHooks<C extends ClientHolding>(react: {
376
366
  } | undefined;
377
367
  isPending: boolean;
378
368
  error: unknown;
369
+ reset?: () => void;
379
370
  };
380
371
  /** Staff: take a post down; its versions are kept. */
381
372
  useUnpublish(): {
@@ -394,6 +385,7 @@ export declare function createContentHooks<C extends ClientHolding>(react: {
394
385
  } | undefined;
395
386
  isPending: boolean;
396
387
  error: unknown;
388
+ reset?: () => void;
397
389
  };
398
390
  }, {
399
391
  readonly usePosts: readonly ["content.list"];
@@ -416,6 +408,7 @@ declare function allContentHooks(react: {
416
408
  } | undefined;
417
409
  isPending: boolean;
418
410
  error: unknown;
411
+ refetch?: () => unknown;
419
412
  };
420
413
  /**
421
414
  * One published post by its address. An old address answers `redirect`: send the reader to
@@ -425,6 +418,7 @@ declare function allContentHooks(react: {
425
418
  data: PostAnswer | undefined;
426
419
  isPending: boolean;
427
420
  error: unknown;
421
+ refetch?: () => unknown;
428
422
  };
429
423
  /** A post's working copy, with a preview link's token. */
430
424
  usePreview(slug: string | null, token: string | null, object?: string): {
@@ -433,6 +427,7 @@ declare function allContentHooks(react: {
433
427
  }) | undefined;
434
428
  isPending: boolean;
435
429
  error: unknown;
430
+ refetch?: () => unknown;
436
431
  };
437
432
  /** The categories, by name. */
438
433
  useCategories(): {
@@ -445,6 +440,7 @@ declare function allContentHooks(react: {
445
440
  } | undefined;
446
441
  isPending: boolean;
447
442
  error: unknown;
443
+ refetch?: () => unknown;
448
444
  };
449
445
  /** Staff: publish a post now (a person with the publish permission). */
450
446
  usePublish(): {
@@ -467,6 +463,7 @@ declare function allContentHooks(react: {
467
463
  } | undefined;
468
464
  isPending: boolean;
469
465
  error: unknown;
466
+ reset?: () => void;
470
467
  };
471
468
  /** Staff: publish a post at a time; what is published is the post as it is now. */
472
469
  useSchedulePublish(): {
@@ -489,6 +486,7 @@ declare function allContentHooks(react: {
489
486
  } | undefined;
490
487
  isPending: boolean;
491
488
  error: unknown;
489
+ reset?: () => void;
492
490
  };
493
491
  /** Staff: take a post down; its versions are kept. */
494
492
  useUnpublish(): {
@@ -507,8 +505,8 @@ declare function allContentHooks(react: {
507
505
  } | undefined;
508
506
  isPending: boolean;
509
507
  error: unknown;
508
+ reset?: () => void;
510
509
  };
511
510
  };
512
511
  /** Every hook of this entry. A block takes the ones it calls: `Pick<ContentHooks, "usePost">`. */
513
512
  export type ContentHooks = ReturnType<typeof allContentHooks>;
514
- export {};
@@ -6,7 +6,7 @@
6
6
  // (ADR 0442): only published versions, only the fields the post declares public. Nothing here
7
7
  // decides what is published or who may read it. Rich text arrives as structured data (ADR 0440);
8
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";
9
+ import { packHooks, } from "@composable-os/client";
10
10
  /** The post object, as the host names it. */
11
11
  export const POST = "content.post";
12
12
  // --- Plain functions ------------------------------------------------------------------------------
@@ -27,6 +27,11 @@ export function hostMediaUrl(hostUrl) {
27
27
  const host = hostUrl.replace(/\/+$/, "");
28
28
  return (id, width) => `${host}/api/v1/media/${encodeURIComponent(id)}${width ? `?w=${Math.round(width)}` : ""}`;
29
29
  }
30
+ /**
31
+ * Each heading's anchor, in document order (ADR 0700): the host's own rule, so a renderer's
32
+ * heading ids are the anchors `content.search` returns.
33
+ */
34
+ export { headingAnchors } from "@composable-os/sdk/rich-text";
30
35
  /** The text of inline runs, without marks: for excerpts, alt text and screen readers. */
31
36
  export function inlineText(content) {
32
37
  return (content ?? []).map((run) => (run.type === "text" ? run.text : "\n")).join("");
package/dist/tools.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { z } from "@composable-os/sdk";
2
- export declare const CATEGORY = "content.category";
3
2
  export declare const categories: import("@composable-os/sdk").ToolSpec<z.ZodObject<{
4
3
  limit: z.ZodDefault<z.ZodNumber>;
5
4
  }, z.core.$strip>, z.ZodObject<{
package/dist/tools.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // host's `content.get` and `content.list`, which serve published versions only. This one lists
3
3
  // the categories for a site's menu, through `public.list`, so the seam decides what is shown.
4
4
  import { defineTool, z } from "@composable-os/sdk";
5
- export const CATEGORY = "content.category";
5
+ const CATEGORY = "content.category";
6
6
  const text = (value) => (typeof value === "string" ? value : null);
7
7
  export const categories = defineTool({
8
8
  description: "Every category of posts, by name: its id, name and address (slug), for a site's menu or " +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@composable-os/pack-content",
3
- "version": "0.1.0-beta.11",
3
+ "version": "0.1.0-beta.14",
4
4
  "license": "FSL-1.1-ALv2",
5
5
  "type": "module",
6
6
  "repository": {
@@ -32,7 +32,7 @@
32
32
  "description": "Blog posts staff write, schedule and publish, read by the public on your site and in your app."
33
33
  },
34
34
  "dependencies": {
35
- "@composable-os/client": "0.1.0-beta.11",
36
- "@composable-os/sdk": "0.1.0-beta.11"
35
+ "@composable-os/client": "0.1.0-beta.14",
36
+ "@composable-os/sdk": "0.1.0-beta.14"
37
37
  }
38
38
  }