@bettercms-ai/sdk 1.38.0 → 1.40.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/dist/bindings.cjs +146 -0
- package/dist/bindings.cjs.map +1 -0
- package/dist/bindings.d.cts +174 -0
- package/dist/bindings.d.ts +174 -0
- package/dist/bindings.js +109 -0
- package/dist/bindings.js.map +1 -0
- package/dist/draft.cjs +8 -0
- package/dist/draft.cjs.map +1 -1
- package/dist/draft.d.cts +9 -1
- package/dist/draft.d.ts +9 -1
- package/dist/draft.js +6 -0
- package/dist/draft.js.map +1 -1
- package/dist/in-editor-D1vXPgJh.d.cts +63 -0
- package/dist/in-editor-D1vXPgJh.d.ts +63 -0
- package/dist/index.cjs +5 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -218
- package/dist/index.d.ts +6 -218
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/live.cjs +158 -0
- package/dist/live.cjs.map +1 -0
- package/dist/live.d.cts +95 -0
- package/dist/live.d.ts +95 -0
- package/dist/live.js +120 -0
- package/dist/live.js.map +1 -0
- package/dist/read-AYzXKCU-.d.cts +191 -0
- package/dist/read-Bb60mWmW.d.ts +191 -0
- package/dist/{read-client-B1uDVNKk.d.cts → read-client-CnkVzNP0.d.cts} +5 -0
- package/dist/{read-client-B1uDVNKk.d.ts → read-client-CnkVzNP0.d.ts} +5 -0
- package/dist/read.cjs +54 -0
- package/dist/read.cjs.map +1 -1
- package/dist/read.d.cts +2 -39
- package/dist/read.d.ts +2 -39
- package/dist/read.js +49 -0
- package/dist/read.js.map +1 -1
- package/dist/sections.cjs +144 -75
- package/dist/sections.cjs.map +1 -1
- package/dist/sections.d.cts +56 -4
- package/dist/sections.d.ts +56 -4
- package/dist/sections.js +138 -75
- package/dist/sections.js.map +1 -1
- package/package.json +11 -1
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import './read-client-CnkVzNP0.cjs';
|
|
2
|
+
import { RichTextResolved, RichTextEmbeds } from '@bettercms-ai/types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Reading a rich-text field, for any site.
|
|
6
|
+
*
|
|
7
|
+
* A `text` field delivers a bare string. A `richtext` field delivers the canonical envelope
|
|
8
|
+
* `{ format, value, html }`. Those are different shapes, and switching a field's type in the
|
|
9
|
+
* CMS switches which one your site receives — the Visual Editor offers exactly that switch,
|
|
10
|
+
* so it happens on live projects. A template that interpolates the value directly then
|
|
11
|
+
* renders `[object Object]` (or, in React, throws `Objects are not valid as a React child`).
|
|
12
|
+
*
|
|
13
|
+
* No payload can be both structured and safe to interpolate, which is why every headless CMS
|
|
14
|
+
* requires a renderer for rich text. These are ours:
|
|
15
|
+
*
|
|
16
|
+
* plain(v) → text, for <title>, meta descriptions, JSON-LD, alt text, aria-label
|
|
17
|
+
* rich(v) → HTML, for `set:html` / `dangerouslySetInnerHTML`
|
|
18
|
+
*
|
|
19
|
+
* Both accept a bare string, so a field can be switched in the CMS without touching the site.
|
|
20
|
+
*
|
|
21
|
+
* <h1 set:html={rich(page.heroTitle)} /> // Astro
|
|
22
|
+
* <h1 dangerouslySetInnerHTML={{ __html: rich(f.heroTitle) }}/> // React
|
|
23
|
+
* seo({ title: plain(page.heroTitle) })
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A block in a Body field's `doc`. Bounded vocabulary — the server only emits what it can
|
|
28
|
+
* parse back, so an unknown `type` means your SDK is older than the content, not that the
|
|
29
|
+
* content is malformed. Handle the default case rather than exhausting on it.
|
|
30
|
+
*/
|
|
31
|
+
type DocBlock = {
|
|
32
|
+
/**
|
|
33
|
+
* Stable within THIS document only — never a global key. Two different entries both have a
|
|
34
|
+
* block called "0.0". A cross-document anchor is (entryId, fieldKey, id).
|
|
35
|
+
*
|
|
36
|
+
* Ids on content authored before block ids existed are derived from position, so they SHIFT
|
|
37
|
+
* when that document is edited. Do not persist an id as a bookmark unless you know it came
|
|
38
|
+
* from the editor rather than from the positional fallback.
|
|
39
|
+
*/
|
|
40
|
+
readonly id: string;
|
|
41
|
+
readonly type: string;
|
|
42
|
+
readonly [key: string]: unknown;
|
|
43
|
+
};
|
|
44
|
+
/** The `doc` rendition of a Body field — a versioned block array. */
|
|
45
|
+
type DocValue = {
|
|
46
|
+
readonly version: 1;
|
|
47
|
+
readonly blocks: readonly DocBlock[];
|
|
48
|
+
};
|
|
49
|
+
/** The canonical rich-text envelope returned by the Delivery API. */
|
|
50
|
+
interface RichTextValue {
|
|
51
|
+
readonly format?: string;
|
|
52
|
+
readonly value?: unknown;
|
|
53
|
+
/** Server-rendered, sanitized HTML. Computed on write. */
|
|
54
|
+
readonly html?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Structured blocks — present on `document` (Body) fields only, and NOT guaranteed even
|
|
57
|
+
* there: it is derived at write time, so an entry saved before this existed carries none
|
|
58
|
+
* until its next save. There is no backfill. Always branch on its absence; `html` is the
|
|
59
|
+
* field that is always there.
|
|
60
|
+
*/
|
|
61
|
+
readonly doc?: DocValue;
|
|
62
|
+
/**
|
|
63
|
+
* The embeds the server resolved when it built `html`: X cards keyed by canonical url (X's
|
|
64
|
+
* script, when a site opts in, is the page's, never the value's). `html` already holds each card, so `rich()` never needs this; it is
|
|
65
|
+
* for renderers of the STRUCTURE (`<PortableText embeds={…}>`). Absent on a value with no embed
|
|
66
|
+
* and on any envelope written before it existed: read it through `embeds()`.
|
|
67
|
+
*/
|
|
68
|
+
readonly embeds?: RichTextEmbeds;
|
|
69
|
+
/**
|
|
70
|
+
* What the value's related posts and author bios point at, resolved by the server for THIS
|
|
71
|
+
* response from the published entries (never stored). `html` already draws them; this is for
|
|
72
|
+
* renderers of the structure (`<PortableText resolved={…}>`). Absent on a value with neither block.
|
|
73
|
+
* Read it through `resolved()`.
|
|
74
|
+
*/
|
|
75
|
+
readonly resolved?: RichTextResolved;
|
|
76
|
+
}
|
|
77
|
+
/** The marker on a Portable Text envelope. `format` is a plain string, never a union — see below. */
|
|
78
|
+
declare const PORTABLE_TEXT_FORMAT = "portable-text-1";
|
|
79
|
+
/**
|
|
80
|
+
* Structured blocks, when the field stores Portable Text.
|
|
81
|
+
*
|
|
82
|
+
* ── The compatibility guarantee, stated once ────────────────────────────────────────────
|
|
83
|
+
* Portable Text did NOT change this envelope. `format`, `value` and `html` are the same three
|
|
84
|
+
* keys they always were, so `isRichText`, `plain()` and `rich()` keep working untouched — and
|
|
85
|
+
* they have to, because sites compiled months ago read `.html` directly and cannot be
|
|
86
|
+
* rebuilt. `bettercms-starter-modern/lib/cms.ts` even carries its OWN copy of `rich()`, baked
|
|
87
|
+
* into deployments nobody can reach. Storing a bare array instead would have made
|
|
88
|
+
* `isRichText` return false (it rejects arrays), every shipped `rich()` call return "", and
|
|
89
|
+
* every one of those pages go blank at 200 OK.
|
|
90
|
+
*
|
|
91
|
+
* `portableText()` is therefore ADDITIVE: `html` remains the always-present field, and this is
|
|
92
|
+
* the opt-in for consumers that want to render structure with `@portabletext/react` instead.
|
|
93
|
+
*/
|
|
94
|
+
declare function portableText(value: TextOrRich): readonly unknown[] | null;
|
|
95
|
+
/**
|
|
96
|
+
* The value's resolved embeds, for a renderer of its structure (`<PortableText embeds>`), or null:
|
|
97
|
+
* a bare string, a value with no embed, or an envelope written before embeds existed. Additive,
|
|
98
|
+
* like `portableText()`: `rich()` reads `html`, which already holds the cards.
|
|
99
|
+
*/
|
|
100
|
+
declare function embeds(value: TextOrRich): RichTextEmbeds | null;
|
|
101
|
+
/** A delivered entry as far as `resolved()` reads it: its entry-level map. */
|
|
102
|
+
interface ResolvedHolder {
|
|
103
|
+
readonly resolved?: RichTextResolved | null;
|
|
104
|
+
readonly data?: unknown;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* What a rich-text value's related posts and author bios resolved to, for a renderer of its
|
|
108
|
+
* structure (`<PortableText resolved>`), or null when there is nothing.
|
|
109
|
+
*
|
|
110
|
+
* resolved(entry.data.body, entry) // a field value, with the entry it came in
|
|
111
|
+
* resolved(entry) // the entry's merged map
|
|
112
|
+
*
|
|
113
|
+
* An envelope value carries its own (`value.resolved`), which wins. A Body stored as a bare block
|
|
114
|
+
* array has nowhere to carry one, so delivery puts the entry's merged map beside `data`
|
|
115
|
+
* (`entry.resolved`): pass the entry and that is what you get. Additive, like `embeds()`: `rich()`
|
|
116
|
+
* reads `html`, which already draws them.
|
|
117
|
+
*/
|
|
118
|
+
declare function resolved(value: TextOrRich | readonly unknown[] | ResolvedHolder, entry?: ResolvedHolder | null): RichTextResolved | null;
|
|
119
|
+
/**
|
|
120
|
+
* A field that may arrive as either shape. Type author-editable text fields with this and the
|
|
121
|
+
* compiler routes you through `plain()`/`rich()`, so a type switch in the CMS can never reach
|
|
122
|
+
* production as `[object Object]`.
|
|
123
|
+
*/
|
|
124
|
+
type TextOrRich = string | RichTextValue | null | undefined;
|
|
125
|
+
/** True when the value is a rich-text envelope rather than a bare string. */
|
|
126
|
+
declare function isRichText(value: unknown): value is RichTextValue;
|
|
127
|
+
/**
|
|
128
|
+
* Plain text for an attribute context — `<title>`, meta description, JSON-LD, `alt`.
|
|
129
|
+
* Strips tags and decodes the entities that survive stripping, so the result is text the
|
|
130
|
+
* consumer can escape once without double-escaping.
|
|
131
|
+
*/
|
|
132
|
+
declare function plain(value: TextOrRich): string;
|
|
133
|
+
/**
|
|
134
|
+
* Renderable HTML. Rich text keeps its inline marks (the server sanitizes `html` on write);
|
|
135
|
+
* a bare string is escaped, so a plain field can never inject markup.
|
|
136
|
+
*
|
|
137
|
+
* A lone wrapping block is unwrapped: a field switched from `text` stores its value as
|
|
138
|
+
* `<p>…</p>`, but it renders inside the element that already is the block — and
|
|
139
|
+
* `<h1><p>…</p></h1>` is invalid, the parser closes the heading at the `<p>` and the text
|
|
140
|
+
* falls out of it. Values with real block structure (several paragraphs, a list) are left
|
|
141
|
+
* alone, so a body field still renders as a document.
|
|
142
|
+
*/
|
|
143
|
+
declare function rich(value: TextOrRich, fallback?: string): string;
|
|
144
|
+
/**
|
|
145
|
+
* Renderable HTML for a ONE-LINE place: a heading, a button, a caption. `rich()`, except that several
|
|
146
|
+
* paragraphs are that place's LINES, joined with `<br>` (a heading-styled first block counts as one).
|
|
147
|
+
*
|
|
148
|
+
* An author who presses Enter in a heading's editor stores a second paragraph. `rich()` leaves it a
|
|
149
|
+
* paragraph, and `<h1><p>…</p><p>…</p></h1>` is what the build then draws, while a publish between
|
|
150
|
+
* builds writes the same value into the built heading as lines. This draws what the publish draws.
|
|
151
|
+
* A value holding anything else (a list, an image, a table) is returned as `rich()` returns it.
|
|
152
|
+
*/
|
|
153
|
+
declare function lines(value: TextOrRich, fallback?: string): string;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Public site search — the headless surface for the visitor-facing search box.
|
|
157
|
+
*
|
|
158
|
+
* Queries `GET /api/v1/delivery/search?project=…&q=…`, the same key-less public
|
|
159
|
+
* endpoint the embeddable widget uses. Powers <BcmsSearch> in @bettercms-ai/next
|
|
160
|
+
* and @bettercms-ai/astro, so the component and any custom UI never drift.
|
|
161
|
+
*
|
|
162
|
+
* `snippet` is HTML-safe: the server escapes the field content and keeps only the
|
|
163
|
+
* <mark> highlight wrappers, so it can be rendered with dangerouslySetInnerHTML.
|
|
164
|
+
*/
|
|
165
|
+
interface SearchHit {
|
|
166
|
+
title: string;
|
|
167
|
+
slug: string;
|
|
168
|
+
type: "page" | "entry";
|
|
169
|
+
/** HTML-safe snippet with <mark> highlights around matched terms. */
|
|
170
|
+
snippet: string;
|
|
171
|
+
/** Site-relative URL, e.g. "/about". */
|
|
172
|
+
url: string;
|
|
173
|
+
}
|
|
174
|
+
interface SearchOptions {
|
|
175
|
+
/** The project id (from the dashboard's Site Search settings). */
|
|
176
|
+
project: string;
|
|
177
|
+
/** The query string. Calls with fewer than 1 char resolve to []. */
|
|
178
|
+
q: string;
|
|
179
|
+
/** Max hits, 1–20 (default 8). */
|
|
180
|
+
limit?: number;
|
|
181
|
+
/** API origin. Defaults to https://api.bettercms.ai. */
|
|
182
|
+
baseUrl?: string;
|
|
183
|
+
/** Injectable fetch for tests / non-browser runtimes. */
|
|
184
|
+
fetchImpl?: typeof fetch;
|
|
185
|
+
/** AbortSignal so callers can cancel a stale in-flight search. */
|
|
186
|
+
signal?: AbortSignal;
|
|
187
|
+
}
|
|
188
|
+
/** Search a published site. Resolves with hits, or [] on an empty query / network error. */
|
|
189
|
+
declare function search(opts: SearchOptions): Promise<SearchHit[]>;
|
|
190
|
+
|
|
191
|
+
export { PORTABLE_TEXT_FORMAT as P, type ResolvedHolder as R, type SearchHit as S, type TextOrRich as T, type RichTextValue as a, type SearchOptions as b, portableText as c, rich as d, embeds as e, isRichText as i, lines as l, plain as p, resolved as r, search as s };
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import './read-client-CnkVzNP0.js';
|
|
2
|
+
import { RichTextResolved, RichTextEmbeds } from '@bettercms-ai/types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Reading a rich-text field, for any site.
|
|
6
|
+
*
|
|
7
|
+
* A `text` field delivers a bare string. A `richtext` field delivers the canonical envelope
|
|
8
|
+
* `{ format, value, html }`. Those are different shapes, and switching a field's type in the
|
|
9
|
+
* CMS switches which one your site receives — the Visual Editor offers exactly that switch,
|
|
10
|
+
* so it happens on live projects. A template that interpolates the value directly then
|
|
11
|
+
* renders `[object Object]` (or, in React, throws `Objects are not valid as a React child`).
|
|
12
|
+
*
|
|
13
|
+
* No payload can be both structured and safe to interpolate, which is why every headless CMS
|
|
14
|
+
* requires a renderer for rich text. These are ours:
|
|
15
|
+
*
|
|
16
|
+
* plain(v) → text, for <title>, meta descriptions, JSON-LD, alt text, aria-label
|
|
17
|
+
* rich(v) → HTML, for `set:html` / `dangerouslySetInnerHTML`
|
|
18
|
+
*
|
|
19
|
+
* Both accept a bare string, so a field can be switched in the CMS without touching the site.
|
|
20
|
+
*
|
|
21
|
+
* <h1 set:html={rich(page.heroTitle)} /> // Astro
|
|
22
|
+
* <h1 dangerouslySetInnerHTML={{ __html: rich(f.heroTitle) }}/> // React
|
|
23
|
+
* seo({ title: plain(page.heroTitle) })
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A block in a Body field's `doc`. Bounded vocabulary — the server only emits what it can
|
|
28
|
+
* parse back, so an unknown `type` means your SDK is older than the content, not that the
|
|
29
|
+
* content is malformed. Handle the default case rather than exhausting on it.
|
|
30
|
+
*/
|
|
31
|
+
type DocBlock = {
|
|
32
|
+
/**
|
|
33
|
+
* Stable within THIS document only — never a global key. Two different entries both have a
|
|
34
|
+
* block called "0.0". A cross-document anchor is (entryId, fieldKey, id).
|
|
35
|
+
*
|
|
36
|
+
* Ids on content authored before block ids existed are derived from position, so they SHIFT
|
|
37
|
+
* when that document is edited. Do not persist an id as a bookmark unless you know it came
|
|
38
|
+
* from the editor rather than from the positional fallback.
|
|
39
|
+
*/
|
|
40
|
+
readonly id: string;
|
|
41
|
+
readonly type: string;
|
|
42
|
+
readonly [key: string]: unknown;
|
|
43
|
+
};
|
|
44
|
+
/** The `doc` rendition of a Body field — a versioned block array. */
|
|
45
|
+
type DocValue = {
|
|
46
|
+
readonly version: 1;
|
|
47
|
+
readonly blocks: readonly DocBlock[];
|
|
48
|
+
};
|
|
49
|
+
/** The canonical rich-text envelope returned by the Delivery API. */
|
|
50
|
+
interface RichTextValue {
|
|
51
|
+
readonly format?: string;
|
|
52
|
+
readonly value?: unknown;
|
|
53
|
+
/** Server-rendered, sanitized HTML. Computed on write. */
|
|
54
|
+
readonly html?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Structured blocks — present on `document` (Body) fields only, and NOT guaranteed even
|
|
57
|
+
* there: it is derived at write time, so an entry saved before this existed carries none
|
|
58
|
+
* until its next save. There is no backfill. Always branch on its absence; `html` is the
|
|
59
|
+
* field that is always there.
|
|
60
|
+
*/
|
|
61
|
+
readonly doc?: DocValue;
|
|
62
|
+
/**
|
|
63
|
+
* The embeds the server resolved when it built `html`: X cards keyed by canonical url (X's
|
|
64
|
+
* script, when a site opts in, is the page's, never the value's). `html` already holds each card, so `rich()` never needs this; it is
|
|
65
|
+
* for renderers of the STRUCTURE (`<PortableText embeds={…}>`). Absent on a value with no embed
|
|
66
|
+
* and on any envelope written before it existed: read it through `embeds()`.
|
|
67
|
+
*/
|
|
68
|
+
readonly embeds?: RichTextEmbeds;
|
|
69
|
+
/**
|
|
70
|
+
* What the value's related posts and author bios point at, resolved by the server for THIS
|
|
71
|
+
* response from the published entries (never stored). `html` already draws them; this is for
|
|
72
|
+
* renderers of the structure (`<PortableText resolved={…}>`). Absent on a value with neither block.
|
|
73
|
+
* Read it through `resolved()`.
|
|
74
|
+
*/
|
|
75
|
+
readonly resolved?: RichTextResolved;
|
|
76
|
+
}
|
|
77
|
+
/** The marker on a Portable Text envelope. `format` is a plain string, never a union — see below. */
|
|
78
|
+
declare const PORTABLE_TEXT_FORMAT = "portable-text-1";
|
|
79
|
+
/**
|
|
80
|
+
* Structured blocks, when the field stores Portable Text.
|
|
81
|
+
*
|
|
82
|
+
* ── The compatibility guarantee, stated once ────────────────────────────────────────────
|
|
83
|
+
* Portable Text did NOT change this envelope. `format`, `value` and `html` are the same three
|
|
84
|
+
* keys they always were, so `isRichText`, `plain()` and `rich()` keep working untouched — and
|
|
85
|
+
* they have to, because sites compiled months ago read `.html` directly and cannot be
|
|
86
|
+
* rebuilt. `bettercms-starter-modern/lib/cms.ts` even carries its OWN copy of `rich()`, baked
|
|
87
|
+
* into deployments nobody can reach. Storing a bare array instead would have made
|
|
88
|
+
* `isRichText` return false (it rejects arrays), every shipped `rich()` call return "", and
|
|
89
|
+
* every one of those pages go blank at 200 OK.
|
|
90
|
+
*
|
|
91
|
+
* `portableText()` is therefore ADDITIVE: `html` remains the always-present field, and this is
|
|
92
|
+
* the opt-in for consumers that want to render structure with `@portabletext/react` instead.
|
|
93
|
+
*/
|
|
94
|
+
declare function portableText(value: TextOrRich): readonly unknown[] | null;
|
|
95
|
+
/**
|
|
96
|
+
* The value's resolved embeds, for a renderer of its structure (`<PortableText embeds>`), or null:
|
|
97
|
+
* a bare string, a value with no embed, or an envelope written before embeds existed. Additive,
|
|
98
|
+
* like `portableText()`: `rich()` reads `html`, which already holds the cards.
|
|
99
|
+
*/
|
|
100
|
+
declare function embeds(value: TextOrRich): RichTextEmbeds | null;
|
|
101
|
+
/** A delivered entry as far as `resolved()` reads it: its entry-level map. */
|
|
102
|
+
interface ResolvedHolder {
|
|
103
|
+
readonly resolved?: RichTextResolved | null;
|
|
104
|
+
readonly data?: unknown;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* What a rich-text value's related posts and author bios resolved to, for a renderer of its
|
|
108
|
+
* structure (`<PortableText resolved>`), or null when there is nothing.
|
|
109
|
+
*
|
|
110
|
+
* resolved(entry.data.body, entry) // a field value, with the entry it came in
|
|
111
|
+
* resolved(entry) // the entry's merged map
|
|
112
|
+
*
|
|
113
|
+
* An envelope value carries its own (`value.resolved`), which wins. A Body stored as a bare block
|
|
114
|
+
* array has nowhere to carry one, so delivery puts the entry's merged map beside `data`
|
|
115
|
+
* (`entry.resolved`): pass the entry and that is what you get. Additive, like `embeds()`: `rich()`
|
|
116
|
+
* reads `html`, which already draws them.
|
|
117
|
+
*/
|
|
118
|
+
declare function resolved(value: TextOrRich | readonly unknown[] | ResolvedHolder, entry?: ResolvedHolder | null): RichTextResolved | null;
|
|
119
|
+
/**
|
|
120
|
+
* A field that may arrive as either shape. Type author-editable text fields with this and the
|
|
121
|
+
* compiler routes you through `plain()`/`rich()`, so a type switch in the CMS can never reach
|
|
122
|
+
* production as `[object Object]`.
|
|
123
|
+
*/
|
|
124
|
+
type TextOrRich = string | RichTextValue | null | undefined;
|
|
125
|
+
/** True when the value is a rich-text envelope rather than a bare string. */
|
|
126
|
+
declare function isRichText(value: unknown): value is RichTextValue;
|
|
127
|
+
/**
|
|
128
|
+
* Plain text for an attribute context — `<title>`, meta description, JSON-LD, `alt`.
|
|
129
|
+
* Strips tags and decodes the entities that survive stripping, so the result is text the
|
|
130
|
+
* consumer can escape once without double-escaping.
|
|
131
|
+
*/
|
|
132
|
+
declare function plain(value: TextOrRich): string;
|
|
133
|
+
/**
|
|
134
|
+
* Renderable HTML. Rich text keeps its inline marks (the server sanitizes `html` on write);
|
|
135
|
+
* a bare string is escaped, so a plain field can never inject markup.
|
|
136
|
+
*
|
|
137
|
+
* A lone wrapping block is unwrapped: a field switched from `text` stores its value as
|
|
138
|
+
* `<p>…</p>`, but it renders inside the element that already is the block — and
|
|
139
|
+
* `<h1><p>…</p></h1>` is invalid, the parser closes the heading at the `<p>` and the text
|
|
140
|
+
* falls out of it. Values with real block structure (several paragraphs, a list) are left
|
|
141
|
+
* alone, so a body field still renders as a document.
|
|
142
|
+
*/
|
|
143
|
+
declare function rich(value: TextOrRich, fallback?: string): string;
|
|
144
|
+
/**
|
|
145
|
+
* Renderable HTML for a ONE-LINE place: a heading, a button, a caption. `rich()`, except that several
|
|
146
|
+
* paragraphs are that place's LINES, joined with `<br>` (a heading-styled first block counts as one).
|
|
147
|
+
*
|
|
148
|
+
* An author who presses Enter in a heading's editor stores a second paragraph. `rich()` leaves it a
|
|
149
|
+
* paragraph, and `<h1><p>…</p><p>…</p></h1>` is what the build then draws, while a publish between
|
|
150
|
+
* builds writes the same value into the built heading as lines. This draws what the publish draws.
|
|
151
|
+
* A value holding anything else (a list, an image, a table) is returned as `rich()` returns it.
|
|
152
|
+
*/
|
|
153
|
+
declare function lines(value: TextOrRich, fallback?: string): string;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Public site search — the headless surface for the visitor-facing search box.
|
|
157
|
+
*
|
|
158
|
+
* Queries `GET /api/v1/delivery/search?project=…&q=…`, the same key-less public
|
|
159
|
+
* endpoint the embeddable widget uses. Powers <BcmsSearch> in @bettercms-ai/next
|
|
160
|
+
* and @bettercms-ai/astro, so the component and any custom UI never drift.
|
|
161
|
+
*
|
|
162
|
+
* `snippet` is HTML-safe: the server escapes the field content and keeps only the
|
|
163
|
+
* <mark> highlight wrappers, so it can be rendered with dangerouslySetInnerHTML.
|
|
164
|
+
*/
|
|
165
|
+
interface SearchHit {
|
|
166
|
+
title: string;
|
|
167
|
+
slug: string;
|
|
168
|
+
type: "page" | "entry";
|
|
169
|
+
/** HTML-safe snippet with <mark> highlights around matched terms. */
|
|
170
|
+
snippet: string;
|
|
171
|
+
/** Site-relative URL, e.g. "/about". */
|
|
172
|
+
url: string;
|
|
173
|
+
}
|
|
174
|
+
interface SearchOptions {
|
|
175
|
+
/** The project id (from the dashboard's Site Search settings). */
|
|
176
|
+
project: string;
|
|
177
|
+
/** The query string. Calls with fewer than 1 char resolve to []. */
|
|
178
|
+
q: string;
|
|
179
|
+
/** Max hits, 1–20 (default 8). */
|
|
180
|
+
limit?: number;
|
|
181
|
+
/** API origin. Defaults to https://api.bettercms.ai. */
|
|
182
|
+
baseUrl?: string;
|
|
183
|
+
/** Injectable fetch for tests / non-browser runtimes. */
|
|
184
|
+
fetchImpl?: typeof fetch;
|
|
185
|
+
/** AbortSignal so callers can cancel a stale in-flight search. */
|
|
186
|
+
signal?: AbortSignal;
|
|
187
|
+
}
|
|
188
|
+
/** Search a published site. Resolves with hits, or [] on an empty query / network error. */
|
|
189
|
+
declare function search(opts: SearchOptions): Promise<SearchHit[]>;
|
|
190
|
+
|
|
191
|
+
export { PORTABLE_TEXT_FORMAT as P, type ResolvedHolder as R, type SearchHit as S, type TextOrRich as T, type RichTextValue as a, type SearchOptions as b, portableText as c, rich as d, embeds as e, isRichText as i, lines as l, plain as p, resolved as r, search as s };
|
|
@@ -278,6 +278,11 @@ interface CreateClientOptions {
|
|
|
278
278
|
* `projectId`; a branch that is not open answers 404 (BRANCH_NOT_FOUND).
|
|
279
279
|
*/
|
|
280
280
|
branch?: string;
|
|
281
|
+
/**
|
|
282
|
+
* `signBranchRead(branch, BCMS_BRANCH_SECRET)`: on a server BetterCMS hosts, reads the `branch`
|
|
283
|
+
* with no draft key. `@bettercms-ai/astro` signs for you on a branch preview.
|
|
284
|
+
*/
|
|
285
|
+
branchSignature?: string;
|
|
281
286
|
/** `published` (default) or `drafts`. */
|
|
282
287
|
perspective?: Perspective;
|
|
283
288
|
/**
|
|
@@ -278,6 +278,11 @@ interface CreateClientOptions {
|
|
|
278
278
|
* `projectId`; a branch that is not open answers 404 (BRANCH_NOT_FOUND).
|
|
279
279
|
*/
|
|
280
280
|
branch?: string;
|
|
281
|
+
/**
|
|
282
|
+
* `signBranchRead(branch, BCMS_BRANCH_SECRET)`: on a server BetterCMS hosts, reads the `branch`
|
|
283
|
+
* with no draft key. `@bettercms-ai/astro` signs for you on a branch preview.
|
|
284
|
+
*/
|
|
285
|
+
branchSignature?: string;
|
|
281
286
|
/** `published` (default) or `drafts`. */
|
|
282
287
|
perspective?: Perspective;
|
|
283
288
|
/**
|
package/dist/read.cjs
CHANGED
|
@@ -24,6 +24,11 @@ __export(read_exports, {
|
|
|
24
24
|
createClient: () => createClient,
|
|
25
25
|
formInitialValues: () => formInitialValues,
|
|
26
26
|
isMultiValueField: () => isMultiValueField,
|
|
27
|
+
isRichText: () => isRichText,
|
|
28
|
+
lines: () => lines,
|
|
29
|
+
plain: () => plain,
|
|
30
|
+
portableText: () => portableText,
|
|
31
|
+
rich: () => rich,
|
|
27
32
|
search: () => search,
|
|
28
33
|
shouldShowField: () => shouldShowField,
|
|
29
34
|
submitForm: () => submitForm,
|
|
@@ -164,6 +169,10 @@ function backoffMs(attempt, res) {
|
|
|
164
169
|
}
|
|
165
170
|
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
166
171
|
|
|
172
|
+
// src/draft.ts
|
|
173
|
+
var enc = new TextEncoder();
|
|
174
|
+
var BRANCH_SIGNATURE_HEADER = "X-BCMS-Branch-Signature";
|
|
175
|
+
|
|
167
176
|
// src/read-client.ts
|
|
168
177
|
var DEFAULT_TIMEOUT = 1e4;
|
|
169
178
|
function qs(params) {
|
|
@@ -223,6 +232,7 @@ function createClient(options) {
|
|
|
223
232
|
const headers = () => {
|
|
224
233
|
const h = { "Content-Type": "application/json" };
|
|
225
234
|
if (apiKey) h["X-API-Key"] = apiKey;
|
|
235
|
+
if (branch && options.branchSignature) h[BRANCH_SIGNATURE_HEADER] = options.branchSignature;
|
|
226
236
|
return h;
|
|
227
237
|
};
|
|
228
238
|
const collapse = options.cache && perspective === "published" ? createRequestCache() : void 0;
|
|
@@ -449,12 +459,56 @@ async function search(opts) {
|
|
|
449
459
|
return [];
|
|
450
460
|
}
|
|
451
461
|
}
|
|
462
|
+
|
|
463
|
+
// src/richtext.ts
|
|
464
|
+
var PORTABLE_TEXT_FORMAT = "portable-text-1";
|
|
465
|
+
function portableText(value) {
|
|
466
|
+
if (!isRichText(value)) return null;
|
|
467
|
+
const v = value;
|
|
468
|
+
return v.format === PORTABLE_TEXT_FORMAT && Array.isArray(v.value) ? v.value : null;
|
|
469
|
+
}
|
|
470
|
+
function isRichText(value) {
|
|
471
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && ("html" in value || "format" in value);
|
|
472
|
+
}
|
|
473
|
+
function plain(value) {
|
|
474
|
+
if (typeof value === "string") return value;
|
|
475
|
+
if (!isRichText(value) || typeof value.html !== "string") return "";
|
|
476
|
+
return decodeEntities(value.html.replace(/<[^>]+>/g, "")).trim();
|
|
477
|
+
}
|
|
478
|
+
function rich(value, fallback = "") {
|
|
479
|
+
const html = (typeof value === "string" ? escapeHtml(value) : isRichText(value) ? value.html ?? "" : "").trim();
|
|
480
|
+
return html ? unwrapLoneBlock(html) : escapeHtml(fallback);
|
|
481
|
+
}
|
|
482
|
+
function lines(value, fallback = "") {
|
|
483
|
+
const html = rich(value, fallback);
|
|
484
|
+
const block = /<(p|h[1-6])(?:\s[^>]*)?>([\s\S]*?)<\/\1>/gi;
|
|
485
|
+
if (html.length > 1e4) return html;
|
|
486
|
+
const found = [...html.matchAll(block)];
|
|
487
|
+
if (found.length < 2 || html.replace(block, "").trim() !== "") return html;
|
|
488
|
+
const shown = found.map((m) => m[2].trim()).filter((line) => line.replace(/<br\s*\/?>/gi, "").trim() !== "");
|
|
489
|
+
return shown.length ? shown.join("<br>") : html;
|
|
490
|
+
}
|
|
491
|
+
function unwrapLoneBlock(html) {
|
|
492
|
+
const m = html.match(/^<(p|div|h[1-6])(?:\s[^>]*)?>([\s\S]*)<\/\1>$/i);
|
|
493
|
+
return m && !new RegExp(`</${m[1]}>`, "i").test(m[2]) ? m[2] : html;
|
|
494
|
+
}
|
|
495
|
+
function escapeHtml(s) {
|
|
496
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
497
|
+
}
|
|
498
|
+
function decodeEntities(s) {
|
|
499
|
+
return s.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/�?39;/g, "'").replace(/ /g, " ").replace(/&/g, "&");
|
|
500
|
+
}
|
|
452
501
|
// Annotate the CommonJS export names for ESM import in node:
|
|
453
502
|
0 && (module.exports = {
|
|
454
503
|
asStringArray,
|
|
455
504
|
createClient,
|
|
456
505
|
formInitialValues,
|
|
457
506
|
isMultiValueField,
|
|
507
|
+
isRichText,
|
|
508
|
+
lines,
|
|
509
|
+
plain,
|
|
510
|
+
portableText,
|
|
511
|
+
rich,
|
|
458
512
|
search,
|
|
459
513
|
shouldShowField,
|
|
460
514
|
submitForm,
|