@brimsites/brimjs 0.0.0-stage → 1.0.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.
Files changed (238) hide show
  1. package/LICENSE +110 -0
  2. package/README.md +22 -2
  3. package/bin/brim.js +5 -0
  4. package/dist/cli.js +3802 -0
  5. package/editor-dist/editor.css +1 -0
  6. package/editor-dist/editor.js +91 -0
  7. package/package.json +71 -3
  8. package/skills/brim-convert/SKILL.md +256 -0
  9. package/skills/brim-hosting/SKILL.md +221 -0
  10. package/src/astro-shim.d.ts +5 -0
  11. package/src/auth/accounts.ts +341 -0
  12. package/src/auth/password.ts +34 -0
  13. package/src/auth/permissions.ts +44 -0
  14. package/src/blocks/accordion/Accordion.astro +48 -0
  15. package/src/blocks/accordion/accordion.css +63 -0
  16. package/src/blocks/analytics/Analytics.astro +45 -0
  17. package/src/blocks/analytics/ConsentBanner.astro +41 -0
  18. package/src/blocks/analytics/analytics.client.ts +125 -0
  19. package/src/blocks/analytics/analytics.css +51 -0
  20. package/src/blocks/analytics/consent.ts +51 -0
  21. package/src/blocks/button/Button.astro +64 -0
  22. package/src/blocks/button/button.css +100 -0
  23. package/src/blocks/carousel/Carousel.astro +140 -0
  24. package/src/blocks/carousel/carousel.client.ts +153 -0
  25. package/src/blocks/carousel/carousel.css +181 -0
  26. package/src/blocks/data/icons.ts +285 -0
  27. package/src/blocks/data/image-shapes.ts +91 -0
  28. package/src/blocks/data/social-icons.ts +41 -0
  29. package/src/blocks/data/social-networks.ts +256 -0
  30. package/src/blocks/divider/Divider.astro +27 -0
  31. package/src/blocks/divider/divider.css +17 -0
  32. package/src/blocks/form/Captcha.astro +27 -0
  33. package/src/blocks/form/Form.astro +250 -0
  34. package/src/blocks/form/FormField.astro +138 -0
  35. package/src/blocks/form/Newsletter.astro +80 -0
  36. package/src/blocks/form/form.client.ts +339 -0
  37. package/src/blocks/form/form.css +271 -0
  38. package/src/blocks/gallery/ImageGallery.astro +126 -0
  39. package/src/blocks/gallery/gallery.css +43 -0
  40. package/src/blocks/gallery/lightbox.client.ts +178 -0
  41. package/src/blocks/gallery/lightbox.css +134 -0
  42. package/src/blocks/icon/Icon.astro +41 -0
  43. package/src/blocks/icon/icon.css +16 -0
  44. package/src/blocks/image/Image.astro +158 -0
  45. package/src/blocks/image/image.css +42 -0
  46. package/src/blocks/images.ts +64 -0
  47. package/src/blocks/in-editor.client.ts +8 -0
  48. package/src/blocks/listing/Listing.astro +288 -0
  49. package/src/blocks/listing/ListingCards.astro +48 -0
  50. package/src/blocks/listing/listing.css +136 -0
  51. package/src/blocks/listing/query.ts +363 -0
  52. package/src/blocks/map/Map.astro +75 -0
  53. package/src/blocks/map/map.css +32 -0
  54. package/src/blocks/schemas/accordion.ts +118 -0
  55. package/src/blocks/schemas/button.ts +78 -0
  56. package/src/blocks/schemas/carousel.ts +133 -0
  57. package/src/blocks/schemas/divider.ts +29 -0
  58. package/src/blocks/schemas/form-field-types.ts +29 -0
  59. package/src/blocks/schemas/form.ts +350 -0
  60. package/src/blocks/schemas/icon.ts +24 -0
  61. package/src/blocks/schemas/image-gallery.ts +54 -0
  62. package/src/blocks/schemas/image.ts +87 -0
  63. package/src/blocks/schemas/index.ts +33 -0
  64. package/src/blocks/schemas/listing.ts +14 -0
  65. package/src/blocks/schemas/map.ts +81 -0
  66. package/src/blocks/schemas/newsletter.ts +324 -0
  67. package/src/blocks/schemas/social-media.ts +348 -0
  68. package/src/blocks/schemas/text.ts +151 -0
  69. package/src/blocks/schemas/video.ts +57 -0
  70. package/src/blocks/search/Search.astro +147 -0
  71. package/src/blocks/search/search.css +69 -0
  72. package/src/blocks/search/state.ts +28 -0
  73. package/src/blocks/settings.ts +94 -0
  74. package/src/blocks/social/SocialMedia.astro +102 -0
  75. package/src/blocks/social/social-links.ts +16 -0
  76. package/src/blocks/social/social.css +31 -0
  77. package/src/blocks/theme.ts +35 -0
  78. package/src/blocks/types.ts +52 -0
  79. package/src/blocks/video/Video.astro +90 -0
  80. package/src/blocks/video/video-source.ts +77 -0
  81. package/src/blocks/video/video.client.ts +43 -0
  82. package/src/blocks/video/video.css +29 -0
  83. package/src/check.ts +1119 -0
  84. package/src/cli.ts +493 -0
  85. package/src/components/Blocks.astro +68 -0
  86. package/src/components/Field.astro +61 -0
  87. package/src/components/Footer.astro +36 -0
  88. package/src/components/Link.astro +18 -0
  89. package/src/components/MobileMenu.astro +77 -0
  90. package/src/components/Navbar.astro +42 -0
  91. package/src/components/RichText.astro +32 -0
  92. package/src/components/Seo.astro +202 -0
  93. package/src/components/Setting.astro +46 -0
  94. package/src/components/Shared.astro +17 -0
  95. package/src/components/Text.astro +91 -0
  96. package/src/components/blocks.css +86 -0
  97. package/src/components/index.ts +27 -0
  98. package/src/components/mobile-menu.client.ts +21 -0
  99. package/src/components/mobile-menu.css +52 -0
  100. package/src/components/text.css +48 -0
  101. package/src/config.ts +413 -0
  102. package/src/content/block-layout.ts +63 -0
  103. package/src/content/block-values.ts +48 -0
  104. package/src/content/content-check.ts +174 -0
  105. package/src/content/content-types.ts +408 -0
  106. package/src/content/drafts.ts +115 -0
  107. package/src/content/feed.ts +237 -0
  108. package/src/content/field-types.ts +107 -0
  109. package/src/content/history.ts +140 -0
  110. package/src/content/ids.ts +50 -0
  111. package/src/content/image-size.ts +212 -0
  112. package/src/content/image-sizes.ts +126 -0
  113. package/src/content/item-fields.ts +110 -0
  114. package/src/content/item-head.ts +126 -0
  115. package/src/content/item-image.ts +26 -0
  116. package/src/content/item-path.ts +122 -0
  117. package/src/content/items.ts +580 -0
  118. package/src/content/language-scopes.ts +40 -0
  119. package/src/content/media.ts +271 -0
  120. package/src/content/menus.ts +51 -0
  121. package/src/content/page-templates.ts +52 -0
  122. package/src/content/redirects.ts +112 -0
  123. package/src/content/references.ts +165 -0
  124. package/src/content/rich-text.ts +63 -0
  125. package/src/content/search.ts +167 -0
  126. package/src/content/seed.ts +229 -0
  127. package/src/content/site-settings.ts +111 -0
  128. package/src/content/sitemap.ts +143 -0
  129. package/src/content/store.ts +225 -0
  130. package/src/content/tags.ts +67 -0
  131. package/src/content/text-default.ts +139 -0
  132. package/src/content/types.ts +137 -0
  133. package/src/content/validate.ts +253 -0
  134. package/src/content/validation-error.ts +2 -0
  135. package/src/db/cached.ts +140 -0
  136. package/src/db/database.ts +15 -0
  137. package/src/db/libsql.ts +29 -0
  138. package/src/db/schema.ts +189 -0
  139. package/src/db/url.ts +44 -0
  140. package/src/editor/client/BlockSettings.tsx +1063 -0
  141. package/src/editor/client/Chrome.tsx +284 -0
  142. package/src/editor/client/ContentBrowser.tsx +1299 -0
  143. package/src/editor/client/Editor.tsx +1756 -0
  144. package/src/editor/client/HistoryPanel.tsx +114 -0
  145. package/src/editor/client/ImagesWidget.tsx +113 -0
  146. package/src/editor/client/Inspector.tsx +127 -0
  147. package/src/editor/client/ItemForm.tsx +535 -0
  148. package/src/editor/client/LinkWidget.tsx +440 -0
  149. package/src/editor/client/ListingPanel.tsx +99 -0
  150. package/src/editor/client/Login.tsx +338 -0
  151. package/src/editor/client/Menu.tsx +64 -0
  152. package/src/editor/client/PageSettings.tsx +270 -0
  153. package/src/editor/client/RedirectsSettings.tsx +130 -0
  154. package/src/editor/client/Resize.tsx +204 -0
  155. package/src/editor/client/Sidebar.tsx +51 -0
  156. package/src/editor/client/SiteMap.tsx +568 -0
  157. package/src/editor/client/SiteSettings.tsx +409 -0
  158. package/src/editor/client/TextTools.tsx +391 -0
  159. package/src/editor/client/Toolbar.tsx +449 -0
  160. package/src/editor/client/VideoUpload.tsx +251 -0
  161. package/src/editor/client/api.ts +417 -0
  162. package/src/editor/client/brand.ts +20 -0
  163. package/src/editor/client/connection.ts +98 -0
  164. package/src/editor/client/context.ts +19 -0
  165. package/src/editor/client/css.d.ts +2 -0
  166. package/src/editor/client/editor.css +4656 -0
  167. package/src/editor/client/fields.tsx +812 -0
  168. package/src/editor/client/icons.tsx +174 -0
  169. package/src/editor/client/keys.ts +15 -0
  170. package/src/editor/client/kinds.ts +289 -0
  171. package/src/editor/client/languages.ts +32 -0
  172. package/src/editor/client/main.tsx +100 -0
  173. package/src/editor/client/responses.ts +26 -0
  174. package/src/editor/client/rich-text.ts +326 -0
  175. package/src/editor/client/site-tree.ts +239 -0
  176. package/src/editor/client/theme.ts +55 -0
  177. package/src/editor/client/tsconfig.json +18 -0
  178. package/src/editor/client/tus.ts +137 -0
  179. package/src/editor/client/use-rect.ts +50 -0
  180. package/src/editor/client/widgets.tsx +1218 -0
  181. package/src/forms/captcha.ts +83 -0
  182. package/src/forms/define.ts +166 -0
  183. package/src/forms/fields.ts +197 -0
  184. package/src/forms/file-types.ts +108 -0
  185. package/src/forms/files.ts +216 -0
  186. package/src/forms/form-page.ts +9 -0
  187. package/src/forms/handler.ts +622 -0
  188. package/src/forms/spec.ts +89 -0
  189. package/src/forms/store.ts +96 -0
  190. package/src/forms/work.ts +50 -0
  191. package/src/icons/library.ts +144 -0
  192. package/src/icons/load.ts +63 -0
  193. package/src/index.ts +99 -0
  194. package/src/init.ts +257 -0
  195. package/src/integration.ts +469 -0
  196. package/src/mail/mailer.ts +197 -0
  197. package/src/mail/notify.ts +272 -0
  198. package/src/mail/settings.ts +173 -0
  199. package/src/mail/templates.ts +191 -0
  200. package/src/mcp/page-nodes.ts +209 -0
  201. package/src/mcp/server.ts +111 -0
  202. package/src/mcp/tools.ts +637 -0
  203. package/src/runtime/TemplatePage.astro +39 -0
  204. package/src/runtime/TemplatePageStatic.astro +13 -0
  205. package/src/runtime/admin-route.ts +80 -0
  206. package/src/runtime/connect.ts +53 -0
  207. package/src/runtime/content.ts +188 -0
  208. package/src/runtime/context.ts +131 -0
  209. package/src/runtime/form-files.ts +18 -0
  210. package/src/runtime/forms-route.ts +33 -0
  211. package/src/runtime/forms.ts +28 -0
  212. package/src/runtime/icons.ts +5 -0
  213. package/src/runtime/media.ts +175 -0
  214. package/src/runtime/middleware.ts +186 -0
  215. package/src/runtime/nav.ts +13 -0
  216. package/src/runtime/query-reads.ts +49 -0
  217. package/src/runtime/query-store.ts +16 -0
  218. package/src/runtime/request-state.ts +69 -0
  219. package/src/runtime/robots.ts +29 -0
  220. package/src/runtime/rss-static.ts +16 -0
  221. package/src/runtime/rss.ts +29 -0
  222. package/src/runtime/server.ts +29 -0
  223. package/src/runtime/sitemap.ts +34 -0
  224. package/src/runtime/template-paths.ts +34 -0
  225. package/src/runtime/uploads-route.ts +13 -0
  226. package/src/seed-cli.ts +253 -0
  227. package/src/server/auth.ts +31 -0
  228. package/src/server/fetch-image.ts +219 -0
  229. package/src/server/handler.ts +2106 -0
  230. package/src/server/http.ts +162 -0
  231. package/src/server/locales.ts +73 -0
  232. package/src/server/node.ts +57 -0
  233. package/src/server/query-params.ts +249 -0
  234. package/src/server/rate-limit.ts +72 -0
  235. package/src/server/video-host.ts +162 -0
  236. package/src/starter.ts +70 -0
  237. package/src/version.ts +4 -0
  238. package/src/virtual.d.ts +65 -0
@@ -0,0 +1,363 @@
1
+ import { type ContentField, optionLabel, optionValues } from "../../content/content-types.ts";
2
+ import type { Item } from "../../content/items.ts";
3
+ import { textOf } from "../../content/search.ts";
4
+ import { noteQueryReads } from "../../runtime/query-reads.ts";
5
+
6
+ /** A field's value, or several: an item matches when its field has the value, or one of them. */
7
+ export type FilterValue = string | number | boolean | (string | number | boolean)[];
8
+
9
+ /** Which items a listing shows, and in what order. The site's code says it; the owner edits the items. */
10
+ export interface ListingQuery {
11
+ /**
12
+ * Only items whose fields have these values, text whatever its case: `{ section: "Guides" }`, `{ featured: true }`.
13
+ */
14
+ filter?: Record<string, FilterValue> | undefined;
15
+ /**
16
+ * A field of the type, or `title`, `created`, `updated`, `position` (the order the owner gives the items of an
17
+ * `ordered` type). By default the newest first.
18
+ */
19
+ sort?: string | undefined;
20
+ /** By default descending for `created` and `updated`, ascending for anything else. */
21
+ order?: "ascending" | "descending" | undefined;
22
+ limit?: number | undefined;
23
+ /** Items left out, by id: the item whose page this is, for a list of related items. */
24
+ exclude?: string | string[] | undefined;
25
+ }
26
+
27
+ /** The items a listing shows, from all the items of its type. */
28
+ export function queryItems<T extends Item>(items: T[], query: ListingQuery): T[] {
29
+ const excluded = new Set([query.exclude ?? []].flat());
30
+ const filters = Object.entries(query.filter ?? {});
31
+ const matching = items.filter(
32
+ (item) =>
33
+ !excluded.has(item.id) && filters.every(([field, wanted]) => matches(valueOf(item, field), [wanted].flat())),
34
+ );
35
+ const sort = query.sort ?? "created";
36
+ const order = query.order ?? (sort === "created" || sort === "updated" ? "descending" : "ascending");
37
+ const direction = order === "descending" ? -1 : 1;
38
+ const sorted = [...matching].sort((a, b) => compare(valueOf(a, sort), valueOf(b, sort), direction));
39
+ return query.limit && query.limit > 0 ? sorted.slice(0, query.limit) : sorted;
40
+ }
41
+
42
+ /**
43
+ * What a new item added from the listing starts with, so it lands in the list it was added from: the fields the
44
+ * filter asks one value of. A filter with several values says nothing about which one. A field of several values
45
+ * (`tags`) starts with the one asked for.
46
+ */
47
+ export function prefillOf(
48
+ filter: ListingQuery["filter"],
49
+ fields: readonly Pick<ContentField, "name" | "type" | "options">[] = [],
50
+ ): Record<string, string | number | boolean | (string | number | boolean)[]> {
51
+ return Object.fromEntries(
52
+ Object.entries(filter ?? {})
53
+ .filter((entry): entry is [string, string | number | boolean] => !Array.isArray(entry[1]))
54
+ .map(([field, value]) => {
55
+ const definition = fields.find((candidate) => candidate.name === field);
56
+ // Filtering ignores case, but select validation requires the declared spelling.
57
+ // Keep the editor's initial value valid for both selects and constrained tags.
58
+ const canonical =
59
+ (definition?.type === "select" || definition?.type === "tags") && typeof value === "string"
60
+ ? (optionValues(definition).find((option) => same(option, value)) ?? value)
61
+ : value;
62
+ return [field, definition?.type === "tags" ? [canonical] : canonical];
63
+ }),
64
+ );
65
+ }
66
+
67
+ function valueOf(item: Item, field: string): unknown {
68
+ if (field === "title") return item.title;
69
+ if (field === "created") return item.createdAt;
70
+ if (field === "updated") return item.updatedAt;
71
+ if (field === "position") return item.position;
72
+ return item.data[field];
73
+ }
74
+
75
+ function matches(value: unknown, wanted: (string | number | boolean)[]): boolean {
76
+ // A field holding several values (tags) matches when any of them is wanted; an empty one is false, like an
77
+ // unchecked box.
78
+ const values = Array.isArray(value) ? value : [value];
79
+ return values.some((one) => wanted.some((candidate) => same(one ?? false, candidate)));
80
+ }
81
+
82
+ /** Text is the same whatever its case: `{ tags: "garden" }` finds the items tagged "Garden". */
83
+ function same(value: unknown, wanted: string | number | boolean): boolean {
84
+ return typeof value === "string" && typeof wanted === "string"
85
+ ? value.toLocaleLowerCase() === wanted.toLocaleLowerCase()
86
+ : value === wanted;
87
+ }
88
+
89
+ /** Empty values go last whatever the order; numbers compare as numbers, the rest as text in the reader's way. */
90
+ function compare(a: unknown, b: unknown, direction: number): number {
91
+ const emptyA = a === undefined || a === null || a === "";
92
+ const emptyB = b === undefined || b === null || b === "";
93
+ if (emptyA || emptyB) return emptyA === emptyB ? 0 : emptyA ? 1 : -1;
94
+ if (typeof a === "number" && typeof b === "number") return (a - b) * direction;
95
+ return String(a).localeCompare(String(b), undefined, { numeric: true }) * direction;
96
+ }
97
+
98
+ /** What a visitor can narrow a listing by: fields to pick values of, words to search for, pages to turn. */
99
+ export interface BrowseOptions {
100
+ /** Fields the visitor picks values of (`select`, `text`, `tags`, `number`, `boolean`), in the order shown. */
101
+ facets?: readonly string[] | undefined;
102
+ /** `true` searches the title and the type's text-like fields; a list names the fields (and `title`) to search. */
103
+ search?: boolean | readonly string[] | undefined;
104
+ /** How many items one page shows; every item on one page when left out. */
105
+ pageSize?: number | undefined;
106
+ /** Put before every parameter in the address, for a page with several listings that can be browsed. */
107
+ paramPrefix?: string | undefined;
108
+ /**
109
+ * What a value of a field is called, for `reference` fields: the titles of the items they point at, by slug
110
+ * (`{ category: { ceramica: "Ceramică" } }`). Facets show them and the search finds them.
111
+ */
112
+ labels?: Record<string, Record<string, string>> | undefined;
113
+ }
114
+
115
+ export interface FacetOption {
116
+ value: string;
117
+ /** What the visitor reads: a select choice's label, or the value itself. */
118
+ label: string;
119
+ /** How many items the visitor would see with this value picked, given the rest of what they picked. */
120
+ count: number;
121
+ selected: boolean;
122
+ }
123
+
124
+ export interface Facet {
125
+ field: string;
126
+ label: string;
127
+ /** The address parameter this facet is sent as. */
128
+ param: string;
129
+ options: FacetOption[];
130
+ }
131
+
132
+ /** Everything a listing's markup needs to show one page of what the visitor asked for. */
133
+ export interface BrowseState<T> {
134
+ /** The items of the current page. */
135
+ items: T[];
136
+ /** How many items match, over every page. */
137
+ total: number;
138
+ facets: Facet[];
139
+ /** The words searched for, as typed. */
140
+ query: string;
141
+ /** Whether the listing can be searched, and the address parameter of the words. */
142
+ searchable: boolean;
143
+ searchParam: string;
144
+ page: number;
145
+ pageCount: number;
146
+ pageParam: string;
147
+ /** Whether the visitor narrowed the list at all, to offer a way back to everything. */
148
+ narrowed: boolean;
149
+ /** The address of a page of these results, keeping everything else the visitor picked: `?brand=Marazzi&page=2`. */
150
+ pageHref(page: number): string;
151
+ /** The address of the listing with nothing picked and nothing searched. */
152
+ resetHref: string;
153
+ /** Parameters of the address that are not this listing's, for a form to send along. */
154
+ otherParams: [string, string][];
155
+ }
156
+
157
+ const FACET_TYPES = new Set(["select", "text", "tags", "number", "boolean", "reference"]);
158
+ const SEARCH_TYPES = new Set([
159
+ "text",
160
+ "textarea",
161
+ "richtext",
162
+ "blocks",
163
+ "select",
164
+ "tags",
165
+ "keyvalue",
166
+ "email",
167
+ "url",
168
+ "reference",
169
+ ]);
170
+ const MAX_QUERY = 200;
171
+
172
+ /**
173
+ * Narrows the items a listing shows (already filtered and sorted by the site's code) by what the visitor asked for
174
+ * in the address: values picked for each facet (any of the values of one facet, all of the facets), words searched
175
+ * for (each of them, whatever their case and accents), and the page.
176
+ */
177
+ export function browseItems<T extends Item>(
178
+ items: T[],
179
+ fields: readonly Pick<ContentField, "name" | "label" | "type" | "options">[],
180
+ options: BrowseOptions,
181
+ params: URLSearchParams,
182
+ ): BrowseState<T> {
183
+ const prefix = options.paramPrefix ?? "";
184
+ const facetFields = (options.facets ?? []).map((name) => {
185
+ const field = fields.find((candidate) => candidate.name === name);
186
+ if (!field || !FACET_TYPES.has(field.type)) {
187
+ throw new Error(
188
+ `Listing facet "${name}" must be a select, text, tags, number, boolean or reference field of the type.`,
189
+ );
190
+ }
191
+ return field;
192
+ });
193
+ const searchFields = searchFieldsOf(fields, options.search);
194
+ const searchParam = `${prefix}q`;
195
+ const pageParam = `${prefix}page`;
196
+ // What the page reads of its address, for a host that caches pages by the parameters they read.
197
+ noteQueryReads([
198
+ ...(searchFields.length > 0 ? [searchParam] : []),
199
+ pageParam,
200
+ ...facetFields.map((field) => `${prefix}${field.name}`),
201
+ ]);
202
+ const query = searchFields.length > 0 ? (params.get(searchParam) ?? "").trim().slice(0, MAX_QUERY) : "";
203
+ const words = fold(query).split(/\s+/).filter(Boolean);
204
+ const picked = new Map(
205
+ facetFields.map((field) => [
206
+ field.name,
207
+ params
208
+ .getAll(`${prefix}${field.name}`)
209
+ .map((v) => v.trim())
210
+ .filter(Boolean),
211
+ ]),
212
+ );
213
+
214
+ const labels = options.labels ?? {};
215
+ const found = words.length === 0 ? items : items.filter((item) => matchesWords(item, searchFields, words, labels));
216
+ // An item counts for a facet's value when it fits everything else picked: the other facets, and the words.
217
+ const fitsFacets = (item: T, skip?: string) =>
218
+ facetFields.every((field) => {
219
+ const wanted = picked.get(field.name) ?? [];
220
+ return (
221
+ field.name === skip ||
222
+ wanted.length === 0 ||
223
+ facetValues(item, field.name).some((v) => wanted.some((w) => sameValue(v, w)))
224
+ );
225
+ });
226
+ const matched = found.filter((item) => fitsFacets(item));
227
+
228
+ const facets: Facet[] = facetFields.map((field) => {
229
+ const wanted = picked.get(field.name) ?? [];
230
+ const pool = found.filter((item) => fitsFacets(item, field.name));
231
+ const values = new Map<string, string>();
232
+ // Every value any item of the listing has, so a value does not vanish once another is picked.
233
+ for (const item of items)
234
+ for (const value of facetValues(item, field.name)) if (!values.has(fold(value))) values.set(fold(value), value);
235
+ const choices = field.type === "select" && field.options ? optionValues(field) : undefined;
236
+ const ordered = [...values.values()].sort((a, b) =>
237
+ choices
238
+ ? choices.indexOf(a) - choices.indexOf(b)
239
+ : String(a).localeCompare(String(b), undefined, { numeric: true, sensitivity: "base" }),
240
+ );
241
+ return {
242
+ field: field.name,
243
+ label: field.label,
244
+ param: `${prefix}${field.name}`,
245
+ options: ordered.map((value) => ({
246
+ value,
247
+ // The page gives labels in its language; without them a choice shows its label as declared.
248
+ label: labels[field.name]?.[value] ?? (field.type === "select" ? optionLabel(field, value) : value),
249
+ count: pool.filter((item) => facetValues(item, field.name).some((v) => sameValue(v, value))).length,
250
+ selected: wanted.some((w) => sameValue(w, value)),
251
+ })),
252
+ };
253
+ });
254
+
255
+ const size = options.pageSize && options.pageSize > 0 ? Math.floor(options.pageSize) : matched.length || 1;
256
+ const pageCount = Math.max(1, Math.ceil(matched.length / size));
257
+ const asked = Number.parseInt(params.get(pageParam) ?? "1", 10);
258
+ const page = Math.min(pageCount, Math.max(1, Number.isFinite(asked) ? asked : 1));
259
+
260
+ const ours = (key: string) =>
261
+ key === searchParam || key === pageParam || facetFields.some((f) => key === `${prefix}${f.name}`);
262
+ const others = [...params.entries()].filter(([key]) => !ours(key));
263
+ const build = (entries: [string, string][]) => {
264
+ const search = new URLSearchParams(entries).toString();
265
+ return search ? `?${search}` : "?";
266
+ };
267
+ const kept: [string, string][] = [
268
+ ...others,
269
+ ...(query ? [[searchParam, query] as [string, string]] : []),
270
+ ...facetFields.flatMap((field) =>
271
+ (picked.get(field.name) ?? []).map((v) => [`${prefix}${field.name}`, v] as [string, string]),
272
+ ),
273
+ ];
274
+
275
+ return {
276
+ items: matched.slice((page - 1) * size, page * size),
277
+ total: matched.length,
278
+ facets,
279
+ query,
280
+ searchable: searchFields.length > 0,
281
+ searchParam,
282
+ page,
283
+ pageCount,
284
+ pageParam,
285
+ narrowed: query !== "" || [...picked.values()].some((values) => values.length > 0),
286
+ pageHref: (target) => build(target > 1 ? [...kept, [pageParam, String(target)]] : kept),
287
+ resetHref: build(others),
288
+ otherParams: others,
289
+ };
290
+ }
291
+
292
+ function searchFieldsOf(
293
+ fields: readonly Pick<ContentField, "name" | "type" | "options">[],
294
+ search: BrowseOptions["search"],
295
+ ): { name: string; field?: Pick<ContentField, "name" | "type" | "options"> }[] {
296
+ if (!search) return [];
297
+ if (search === true) {
298
+ return [
299
+ { name: "title" },
300
+ ...fields.filter((field) => SEARCH_TYPES.has(field.type)).map((field) => ({ name: field.name, field })),
301
+ ];
302
+ }
303
+ return search.map((name) => {
304
+ if (name === "title") return { name };
305
+ const field = fields.find((candidate) => candidate.name === name);
306
+ if (!field) throw new Error(`Listing searches "${name}", which is not a field of the type.`);
307
+ return { name, field };
308
+ });
309
+ }
310
+
311
+ /** The values of a field as text, one per value: a tag list gives each tag, an empty field gives none. */
312
+ function facetValues(item: Item, field: string): string[] {
313
+ const value = valueOf(item, field);
314
+ const values = Array.isArray(value) ? value : [value];
315
+ return values
316
+ .filter((one) => one !== undefined && one !== null && one !== "")
317
+ .map((one) => String(one).trim())
318
+ .filter(Boolean);
319
+ }
320
+
321
+ function matchesWords(
322
+ item: Item,
323
+ fields: { name: string; field?: Pick<ContentField, "name" | "type" | "options"> }[],
324
+ words: string[],
325
+ labels: Record<string, Record<string, string>> = {},
326
+ ): boolean {
327
+ const text = fold(
328
+ fields
329
+ .map(({ name, field }) => {
330
+ const value = valueOf(item, name);
331
+ // A reference is found by the title of what it points at too: "Ceramică" finds the products in ceramica.
332
+ const named = labels[name];
333
+ const titles = named ? (Array.isArray(value) ? value : [value]).map((slug) => named[String(slug)] ?? "") : [];
334
+ return `${searchableText(value, field)} ${titles.join(" ")}`;
335
+ })
336
+ .join(" "),
337
+ );
338
+ return words.every((word) => text.includes(word));
339
+ }
340
+
341
+ /** The words a value holds: a select gives its label too, key–value rows give both sides, rich text its words. */
342
+ function searchableText(value: unknown, field?: Pick<ContentField, "name" | "type" | "options">): string {
343
+ if (value === undefined || value === null) return "";
344
+ // Rich text and blocks by the words a visitor reads, not their tags and settings.
345
+ if (field?.type === "richtext" || field?.type === "blocks") return textOf(value);
346
+ if (field?.type === "select" && typeof value === "string") return `${value} ${optionLabel(field, value)}`;
347
+ if (Array.isArray(value)) return value.map((one) => searchableText(one)).join(" ");
348
+ if (typeof value === "object")
349
+ return Object.values(value as Record<string, unknown>)
350
+ .map((one) => searchableText(one))
351
+ .join(" ");
352
+ return String(value);
353
+ }
354
+
355
+ /** Text as compared when searching: lowercase, without accents, so "ceramica" finds "Ceramică". */
356
+ function fold(text: string): string {
357
+ return text.normalize("NFD").replace(/[̀-ͯ]/g, "").toLocaleLowerCase();
358
+ }
359
+
360
+ /** Two values are the same value of a facet whatever their case and accents. */
361
+ function sameValue(a: string, b: string): boolean {
362
+ return fold(a) === fold(b);
363
+ }
@@ -0,0 +1,75 @@
1
+ ---
2
+ import { defaultScope } from "../../runtime/request-state.ts";
3
+ import "./map.css";
4
+ import type { NodeScope } from "../../runtime/context.ts";
5
+ import { map } from "../schemas/map.ts";
6
+
7
+ export interface MapPoint {
8
+ lat: number;
9
+ lng: number;
10
+ title?: string;
11
+ address?: string;
12
+ details?: string;
13
+ hours?: string;
14
+ phone?: string;
15
+ directionsUrl?: string;
16
+ }
17
+
18
+ /**
19
+ * A map of the first location, with every location listed beside it.
20
+ * The map is OpenStreetMap's own embed, so the page needs no script. Its size is the site's (or, in a `blocks`
21
+ * field, the block's width); `class` goes on `.brim-map`.
22
+ */
23
+ interface Props {
24
+ nodeId: string;
25
+ scope?: NodeScope;
26
+ points?: MapPoint[];
27
+ showMarker?: boolean;
28
+ class?: string;
29
+ }
30
+
31
+ const { nodeId, scope = defaultScope(), class: className, ...source } = Astro.props;
32
+ const settings = Astro.locals.brim.settings(map, nodeId, scope, source);
33
+
34
+ const points = (Array.isArray(settings.points) ? settings.points : []).filter(
35
+ (point): point is MapPoint => Number.isFinite(point?.lat) && Number.isFinite(point?.lng),
36
+ );
37
+ const center = points[0];
38
+ // Roughly a neighbourhood around the location.
39
+ const SPAN = 0.01;
40
+ const embed = center
41
+ ? `https://www.openstreetmap.org/export/embed.html?${new URLSearchParams({
42
+ bbox: [center.lng - SPAN, center.lat - SPAN, center.lng + SPAN, center.lat + SPAN]
43
+ .map((value) => value.toFixed(5))
44
+ .join(","),
45
+ layer: "mapnik",
46
+ ...(settings.showMarker !== false && { marker: `${center.lat},${center.lng}` }),
47
+ })}`
48
+ : undefined;
49
+ const listed = points.filter((point) => point.title || point.address);
50
+ const directions = (url: unknown): string | undefined =>
51
+ typeof url === "string" && /^https?:\/\//i.test(url) ? url : undefined;
52
+ ---
53
+
54
+ <div class:list={["brim-map", className]} {...Astro.locals.brim.marker("map", nodeId, scope, settings)}>
55
+ {embed && <iframe class="brim-map__frame" src={embed} title={center?.title || "Map"} loading="lazy" />}
56
+ {!embed && Astro.locals.brim.editing && <p>No location yet. Select this block to add one.</p>}
57
+ {listed.length > 0 && (
58
+ <ul class="brim-map__points">
59
+ {listed.map((point) => (
60
+ <li>
61
+ {point.title && <strong>{point.title}</strong>}
62
+ {point.address && <span>{point.address}</span>}
63
+ {point.details && <span>{point.details}</span>}
64
+ {point.hours && <span>{point.hours}</span>}
65
+ {point.phone && <a href={`tel:${point.phone.replace(/[^\d+]/g, "")}`}>{point.phone}</a>}
66
+ {directions(point.directionsUrl) && (
67
+ <a href={directions(point.directionsUrl)} target="_blank" rel="noopener noreferrer">
68
+ Directions
69
+ </a>
70
+ )}
71
+ </li>
72
+ ))}
73
+ </ul>
74
+ )}
75
+ </div>
@@ -0,0 +1,32 @@
1
+ /* In the `brim` layer: any CSS of the site wins over it, whatever its selectors. */
2
+ @layer brim {
3
+ .brim-map {
4
+ --brim-map-height: 360px;
5
+
6
+ display: flex;
7
+ flex-direction: column;
8
+ gap: 1rem;
9
+ }
10
+
11
+ .brim-map__frame {
12
+ width: 100%;
13
+ height: var(--brim-map-height);
14
+ border: 0;
15
+ border-radius: var(--brim-theme-radius, 0.5rem);
16
+ }
17
+
18
+ .brim-map__points {
19
+ display: grid;
20
+ grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
21
+ gap: 1rem;
22
+ margin: 0;
23
+ padding: 0;
24
+ list-style: none;
25
+ }
26
+
27
+ .brim-map__points li {
28
+ display: flex;
29
+ flex-direction: column;
30
+ gap: 0.125rem;
31
+ }
32
+ }
@@ -0,0 +1,118 @@
1
+ // Settings of the "accordion" block, from BrimCMS. Only the questions: a heading or an intro above them is the
2
+ // site's own markup (a `Text`), so an accordion never brings words the site did not write.
3
+ import type { BlockDefinition } from "../types.ts";
4
+
5
+ export const accordion: BlockDefinition = {
6
+ type: "accordion",
7
+ label: "Accordion",
8
+ description: "Collapsible content panels for FAQs and details",
9
+ schema: {
10
+ fieldsets: [
11
+ {
12
+ id: "content",
13
+ title: "Content",
14
+ fields: ["items"],
15
+ },
16
+ {
17
+ id: "behavior",
18
+ title: "Behavior",
19
+ fields: ["allowMultiple", "icon"],
20
+ },
21
+ ],
22
+ properties: {
23
+ items: {
24
+ title: "Accordion Items",
25
+ type: "array",
26
+ widget: "accordion-items",
27
+ },
28
+ allowMultiple: {
29
+ title: "Allow Multiple Open",
30
+ type: "boolean",
31
+ widget: "checkbox",
32
+ default: true,
33
+ },
34
+ icon: {
35
+ title: "Indicator",
36
+ type: "string",
37
+ widget: "select",
38
+ options: [
39
+ { value: "chevron", label: "Chevron" },
40
+ { value: "plus", label: "Plus" },
41
+ ],
42
+ default: "chevron",
43
+ },
44
+ gap: {
45
+ title: "Gap",
46
+ type: "number",
47
+ widget: "range",
48
+ min: 0,
49
+ max: 48,
50
+ step: 1,
51
+ default: 10,
52
+ description: "Spacing between accordion items in pixels",
53
+ },
54
+ radius: {
55
+ title: "Radius",
56
+ type: "number",
57
+ widget: "range",
58
+ min: 0,
59
+ max: 48,
60
+ step: 1,
61
+ default: 14,
62
+ description: "Corner radius for accordion items in pixels",
63
+ },
64
+ itemTitleTypographyStyle: {
65
+ title: "Item Title Text Style",
66
+ type: "string",
67
+ default: "p1",
68
+ description: "Semantic heading or paragraph style: h1, h2, h3, h4, p1, p2, p3, or custom:<substyle-id>",
69
+ },
70
+ customClassName: {
71
+ title: "Custom Class",
72
+ type: "string",
73
+ widget: "text",
74
+ placeholder: "my-accordion",
75
+ default: "",
76
+ },
77
+ },
78
+ },
79
+ defaults: {
80
+ "@type": "accordion",
81
+ presetId: "default-theme",
82
+ items: [
83
+ {
84
+ id: "item_1",
85
+ title: "How long does setup take?",
86
+ content:
87
+ "<p>Most projects are live in a few hours. Advanced integrations can take a bit longer depending on complexity.</p>",
88
+ },
89
+ {
90
+ id: "item_2",
91
+ title: "Can I change the design later?",
92
+ content:
93
+ "<p>Yes. You can update content, layout, and colors at any time without rebuilding the page from scratch.</p>",
94
+ },
95
+ {
96
+ id: "item_3",
97
+ title: "Do you support custom integrations?",
98
+ content: "<p>Yes. We can connect with CRMs, email platforms, analytics tools, and internal APIs.</p>",
99
+ },
100
+ {
101
+ id: "item_4",
102
+ title: "What happens after launch?",
103
+ content:
104
+ "<p>You keep full editing control, and your team can continue publishing without developer involvement.</p>",
105
+ },
106
+ ],
107
+ allowMultiple: true,
108
+ icon: "chevron",
109
+ gap: 10,
110
+ radius: 14,
111
+ customClassName: "",
112
+ partStyles: {
113
+ root: {
114
+ desktop: { verticalAlign: "top" },
115
+ },
116
+ },
117
+ },
118
+ };
@@ -0,0 +1,78 @@
1
+ // Settings of the "button" block: its words, its link, which of the site's button styles and sizes, and an icon.
2
+ // Where it sits on the page is the site's layout.
3
+ import type { BlockDefinition } from "../types.ts";
4
+
5
+ export const button: BlockDefinition = {
6
+ type: "button",
7
+ label: "Button",
8
+ description: "Interactive button",
9
+ schema: {
10
+ fieldsets: [
11
+ { id: "default", title: "Default", fields: ["text", "url"] },
12
+ { id: "block_layout", title: "Style", fields: ["variant", "size", "showIcon", "icon"] },
13
+ ],
14
+ properties: {
15
+ text: {
16
+ title: "Button Text",
17
+ type: "string",
18
+ widget: "text",
19
+ placeholder: "Click me",
20
+ default: "Click me",
21
+ },
22
+ url: {
23
+ title: "URL",
24
+ type: "string",
25
+ widget: "url",
26
+ placeholder: "https://example.com",
27
+ default: "",
28
+ newTabField: "openInNewTab",
29
+ allowFormTarget: true,
30
+ },
31
+ variant: {
32
+ title: "Style",
33
+ type: "string",
34
+ widget: "button-variant",
35
+ default: "block",
36
+ description:
37
+ "Visual variants: light-1, light-2, light-3, accent, block (Default), dark-1, dark-2, ghost (Outline). Legacy primary and secondary remain supported for existing content. Prefer block for a new default button.",
38
+ },
39
+ size: {
40
+ title: "Size",
41
+ type: "string",
42
+ widget: "size",
43
+ options: [
44
+ { value: "sm", label: "Small (compact)" },
45
+ { value: "md", label: "Medium (default)" },
46
+ { value: "lg", label: "Large (prominent)" },
47
+ ],
48
+ default: "md",
49
+ description: "Button scale for inline layout",
50
+ editContext: "inline",
51
+ },
52
+ showIcon: {
53
+ title: "Show Icon",
54
+ type: "boolean",
55
+ widget: "checkbox",
56
+ default: false,
57
+ },
58
+ icon: {
59
+ title: "Icon",
60
+ type: "string",
61
+ widget: "icon-picker",
62
+ default: "none",
63
+ showWhen: { showIcon: true },
64
+ },
65
+ },
66
+ },
67
+ defaults: {
68
+ "@type": "button",
69
+ variant: "block",
70
+ text: "Click me",
71
+ url: "",
72
+ openInNewTab: false,
73
+ size: "md",
74
+ showIcon: false,
75
+ icon: "none",
76
+ presetId: "default-theme",
77
+ },
78
+ };