bearminion 1.5.0 → 1.7.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.
@@ -3,6 +3,8 @@
3
3
  const API_PREFIX = "/__bearminion/api";
4
4
  const DEFAULT_CONTENT_DIR = "content";
5
5
  const DEFAULT_UPLOADS_DIR = "src/assets/uploads";
6
+ /** Videos are served as they are, so they live in `public/`. */
7
+ const DEFAULT_VIDEOS_DIR = "public/videos";
6
8
  //#endregion
7
9
  //#region src/migrate.ts
8
10
  /** The key that stores a document's version. It is stripped on read and stamped on write; schemas never see it. */
@@ -56,4 +58,4 @@ function createMigrator(versioning = {}) {
56
58
  };
57
59
  }
58
60
  //#endregion
59
- export { DEFAULT_UPLOADS_DIR as a, DEFAULT_CONTENT_DIR as i, createMigrator as n, API_PREFIX as r, SCHEMA_VERSION_KEY as t };
61
+ export { DEFAULT_UPLOADS_DIR as a, DEFAULT_CONTENT_DIR as i, createMigrator as n, DEFAULT_VIDEOS_DIR as o, API_PREFIX as r, SCHEMA_VERSION_KEY as t };
@@ -1,4 +1,4 @@
1
- import { A as SectionRegistry, J as SiteDefinition, M as ThemeDefinition, Yt as ContentMigrator, k as ContentModel } from "./page-B5NoN74Q.js";
1
+ import { A as SectionRegistry, J as SiteDefinition, M as ThemeDefinition, Yt as ContentMigrator, k as ContentModel } from "./page-BfI9cD8g.js";
2
2
  //#region src/images.d.ts
3
3
  type ImageKind = 'png' | 'jpeg' | 'webp' | 'gif' | 'avif';
4
4
  interface SavedImage {
@@ -11,6 +11,20 @@ interface ListedImage {
11
11
  path: string;
12
12
  }
13
13
  //#endregion
14
+ //#region src/videos.d.ts
15
+ type VideoKind = 'mp4' | 'mov' | 'webm' | 'ogv';
16
+ interface SavedVideo {
17
+ filename: string;
18
+ /** Project-relative path of the file. */
19
+ path: string;
20
+ /** What a Media section's `video` field holds: the file's address on the site, such as `/videos/demo-1a2b.mp4`. */
21
+ url: string;
22
+ bytes: number;
23
+ }
24
+ type ListedVideo = Omit<SavedVideo, 'bytes'> & {
25
+ bytes?: number;
26
+ };
27
+ //#endregion
14
28
  //#region src/store.d.ts
15
29
  /**
16
30
  * Where the editor reads and writes content. The editor API talks only to this interface, so the
@@ -37,6 +51,8 @@ interface ContentStore {
37
51
  apply(operations: readonly StoreOperation[]): Promise<void>;
38
52
  /** Uploaded images. */
39
53
  readonly uploads: UploadStore;
54
+ /** Uploaded videos, served from `public/`. Absent in stores that only hold images. */
55
+ readonly videos?: VideoStore;
40
56
  /**
41
57
  * Whether the image file an image field points at (`src/assets/...`) exists. Saves that point at a missing file
42
58
  * fail validation. A store without it skips that check.
@@ -62,6 +78,15 @@ interface UploadStore {
62
78
  /** Delete one upload by file name. False when there is no such upload. */
63
79
  remove(filename: string): Promise<boolean>;
64
80
  }
81
+ interface VideoStore {
82
+ list(): Promise<ListedVideo[]>;
83
+ save(input: {
84
+ originalName: string;
85
+ bytes: Uint8Array;
86
+ kind: VideoKind;
87
+ }): Promise<SavedVideo>;
88
+ remove(filename: string): Promise<boolean>;
89
+ }
65
90
  /** A key that is empty, absolute or leaves the content directory. */
66
91
  declare class StoreKeyError extends Error {
67
92
  constructor(key: string);
@@ -71,9 +96,11 @@ interface FileStoreOptions {
71
96
  root: string;
72
97
  contentDir: string;
73
98
  uploadsDir: string;
99
+ /** Where uploaded videos go. Without it the store takes no video uploads. */
100
+ videosDir?: string;
74
101
  }
75
102
  /** The content store for a local project: content as JSON files, uploads in a folder. */
76
- declare function createFileStore({ root, contentDir, uploadsDir }: FileStoreOptions): ContentStore;
103
+ declare function createFileStore({ root, contentDir, uploadsDir, videosDir }: FileStoreOptions): ContentStore;
77
104
  //#endregion
78
105
  //#region src/options.d.ts
79
106
  interface BearminionOptions extends ContentModel {
@@ -81,11 +108,25 @@ interface BearminionOptions extends ContentModel {
81
108
  contentDir?: string;
82
109
  /** Directory for uploaded images. Default `src/assets/uploads`. */
83
110
  uploadsDir?: string;
111
+ /** Directory for uploaded videos. It must be inside `public/`, which serves them as they are. Default `public/videos`. */
112
+ videosDir?: string;
113
+ /** Largest video the editor accepts, in bytes. Default 50 MB. */
114
+ maxVideoBytes?: number;
84
115
  /**
85
116
  * Vite module id loaded by the dev server to render a draft page.
86
117
  * Default `/src/lib/render-preview.ts`. Preview never writes to disk.
87
118
  */
88
119
  previewModule?: string;
120
+ /**
121
+ * The dev route for the forms in your site. A file `src/forms/contact.ts` that default-exports `defineForm(...)`
122
+ * answers at `/api/contact` during `astro dev`, as a dry run unless `BEARMINION_FORMS_LIVE=on`. `dir` is the folder
123
+ * (default `src/forms`) and `prefix` the path (default `/api`). `false` turns the route off, for a site whose
124
+ * Astro adapter serves the endpoint itself.
125
+ */
126
+ forms?: false | {
127
+ dir?: string;
128
+ prefix?: string;
129
+ };
89
130
  /**
90
131
  * Show Commit changes in the editor, which commits the changed content and uploads to the local git repository.
91
132
  * Off by default. It never pushes, and it does nothing outside a git work tree.
@@ -102,6 +143,8 @@ interface EditorContext {
102
143
  root: string;
103
144
  contentDir: string;
104
145
  uploadsDir: string;
146
+ videosDir: string;
147
+ maxVideoBytes: number;
105
148
  sections: SectionRegistry;
106
149
  site: SiteDefinition;
107
150
  theme: ThemeDefinition;
@@ -110,6 +153,8 @@ interface EditorContext {
110
153
  migrator: ContentMigrator;
111
154
  /** Commit changes is enabled. */
112
155
  git: boolean;
156
+ /** The folder with the site's form handlers, or `null` when the `forms` option turns them off. */
157
+ formsDir?: string | null;
113
158
  }
114
159
  //#endregion
115
160
  export { StoreKeyError as a, createFileStore as c, FileStoreOptions as i, EditorContext as n, StoreOperation as o, ContentStore as r, UploadStore as s, BearminionOptions as t };
@@ -172,6 +172,7 @@ declare function defineWireframe(wireframe: Wireframe): Wireframe;
172
172
  * - `wireframe` is the thumbnail in the Add section picker.
173
173
  * - `choices` lists every string enum in `props` (derived from the schema); the sidebar offers them as choices.
174
174
  * - `icons` draws those choices as icons in the sidebar's picker.
175
+ * - `form` names the handler in `src/forms` that receives what the section's form sends. The sidebar says whether it exists.
175
176
  * - `showWhen` hides props in the sidebar while the section's options make them unused.
176
177
  */
177
178
  interface SectionDefinition<TType extends string = string, TSchema extends z.ZodType = z.ZodType, TVariant extends string = string> {
@@ -190,6 +191,7 @@ interface SectionDefinition<TType extends string = string, TSchema extends z.Zod
190
191
  readonly choices: FieldChoices;
191
192
  readonly icons: FieldIcons;
192
193
  readonly showWhen: ShowWhen;
194
+ readonly form?: string;
193
195
  /** Cross-field rules the schema cannot express, such as a variant that needs an exact item count. */
194
196
  refine?(section: {
195
197
  variant: TVariant;
@@ -220,6 +222,8 @@ interface SectionConfig<TType extends string, TSchema extends z.ZodType, TVarian
220
222
  * means any of its values; `variant`, `tone` and `motion` count as options. A hidden prop keeps its value in the file.
221
223
  */
222
224
  showWhen?: ShowWhen;
225
+ /** The form handler this section posts to: the file `src/forms/<form>.ts`, answering at `/api/<form>`. */
226
+ form?: string;
223
227
  refine?(section: {
224
228
  variant: TVariants[number];
225
229
  props: z.infer<TSchema>;
@@ -1,21 +1,30 @@
1
1
  //#region src/rich-text.d.ts
2
2
  /**
3
3
  * The subset the editor toolbar writes: paragraphs, line breaks, bold, italic, inline code (backticks),
4
- * links, and bulleted or numbered lists. Headings and images are not supported.
4
+ * links, and bulleted or numbered lists. Images are not supported. Headings are not either, unless a field asks
5
+ * for them (`{ headings: true }`): then `## ` and `### ` lines are h2 and h3, and nothing else is a heading.
5
6
  * Stored as markdown. Rendered as a fixed set of tags, with text escaped.
6
7
  *
7
- * The output is always block content (`<p>`, `<ul>`, `<ol>`), so the element that holds it must accept blocks:
8
- * a `div` or a `blockquote`, never a `<p>` or a heading.
8
+ * The output is always block content (`<p>`, `<ul>`, `<ol>`, with headings `<h2>`, `<h3>`), so the element that
9
+ * holds it must accept blocks: a `div` or a `blockquote`, never a `<p>` or a heading.
9
10
  */
10
- export declare function renderRichText(markdown: string): string;
11
+ /** What a rich field allows beyond the base subset. */
12
+ export interface RichOptions {
13
+ /** Allow h2 and h3 as `## ` and `### ` lines. Page titles are h1, so nothing above h2 exists, and nothing below h3. */
14
+ headings?: boolean;
15
+ }
16
+ export declare function renderRichText(markdown: string, options?: RichOptions): string;
11
17
  /**
12
18
  * Pasted HTML, reduced to what a rich field can hold. The HTML is parsed (never inserted), read back as the
13
19
  * markdown subset, and rendered again, so only the renderer's own tags survive: no attributes, handlers or
14
20
  * elements from the clipboard reach the page. Headings and images are dropped. Browser only.
15
21
  */
16
- export declare function sanitizeRichHtml(html: string): string;
17
- /** Drop markdown headings and images from pasted plain text. */
18
- export declare function stripPlainRich(text: string): string;
22
+ export declare function sanitizeRichHtml(html: string, options?: RichOptions): string;
23
+ /**
24
+ * Drop markdown images from pasted plain text, and headings too unless the field allows them. Then a heading of any
25
+ * level becomes the nearest one the field has: `#` is `##`, and `####` and deeper are `###`.
26
+ */
27
+ export declare function stripPlainRich(text: string, options?: RichOptions): string;
19
28
  /** True when a link URL is safe to put in an href. */
20
29
  export declare function isSafeHref(url: string): boolean;
21
30
  export declare function escapeHtml(value: string): string;
@@ -23,7 +32,7 @@ export declare function escapeHtml(value: string): string;
23
32
  * Turn the formatted DOM of a rich field back into the stored markdown subset. The HTML is parsed with
24
33
  * `DOMParser`, which never runs scripts or loads resources. Browser only (the editor overlay).
25
34
  */
26
- export declare function markdownFromHtml(html: string): string;
35
+ export declare function markdownFromHtml(html: string, options?: RichOptions): string;
27
36
  export interface RichLinkSpan {
28
37
  label: string;
29
38
  href: string;
package/dist/rich-text.js CHANGED
@@ -1,27 +1,35 @@
1
1
  //#region src/rich-text.ts
2
- /**
3
- * The subset the editor toolbar writes: paragraphs, line breaks, bold, italic, inline code (backticks),
4
- * links, and bulleted or numbered lists. Headings and images are not supported.
5
- * Stored as markdown. Rendered as a fixed set of tags, with text escaped.
6
- *
7
- * The output is always block content (`<p>`, `<ul>`, `<ol>`), so the element that holds it must accept blocks:
8
- * a `div` or a `blockquote`, never a `<p>` or a heading.
9
- */
2
+ const HEADING_LINE = /^(#{2,3})[ \t]+(\S.*)$/;
10
3
  const BLOCKED_PROTOCOL = /^(?:javascript|data|vbscript):/i;
11
- function renderRichText(markdown) {
12
- return markdown.replace(/\r\n/g, "\n").split(/\n{2,}/).filter((block) => block.trim() !== "").map((block) => renderBlock(block)).map((html) => isListHtml(html) ? html : `<p>${html}</p>`).join("");
4
+ function renderRichText(markdown, options = {}) {
5
+ const headings = options.headings === true;
6
+ const text = markdown.replace(/\r\n/g, "\n");
7
+ return (headings ? text.split("\n").map((line) => HEADING_LINE.test(line.trim()) ? `\n\n${line.trim()}\n\n` : line).join("\n") : text).split(/\n{2,}/).filter((block) => block.trim() !== "").map((block) => {
8
+ const heading = headings ? HEADING_LINE.exec(block.trim()) : null;
9
+ if (heading) {
10
+ const tag = heading[1]?.length === 2 ? "h2" : "h3";
11
+ return `<${tag}>${renderInline((heading[2] ?? "").trim())}</${tag}>`;
12
+ }
13
+ const html = renderBlock(block);
14
+ return isListHtml(html) ? html : `<p>${html}</p>`;
15
+ }).join("");
13
16
  }
14
17
  /**
15
18
  * Pasted HTML, reduced to what a rich field can hold. The HTML is parsed (never inserted), read back as the
16
19
  * markdown subset, and rendered again, so only the renderer's own tags survive: no attributes, handlers or
17
20
  * elements from the clipboard reach the page. Headings and images are dropped. Browser only.
18
21
  */
19
- function sanitizeRichHtml(html) {
20
- return renderRichText(markdownFromHtml(html));
22
+ function sanitizeRichHtml(html, options = {}) {
23
+ return renderRichText(markdownFromHtml(html, options), options);
21
24
  }
22
- /** Drop markdown headings and images from pasted plain text. */
23
- function stripPlainRich(text) {
24
- return text.replace(/!\[[^\]]*\]\([^)\n]*\)/g, "").replace(/^#{1,6}[ \t]+.*$/gm, "");
25
+ /**
26
+ * Drop markdown images from pasted plain text, and headings too unless the field allows them. Then a heading of any
27
+ * level becomes the nearest one the field has: `#` is `##`, and `####` and deeper are `###`.
28
+ */
29
+ function stripPlainRich(text, options = {}) {
30
+ const withoutImages = text.replace(/!\[[^\]]*\]\([^)\n]*\)/g, "");
31
+ if (options.headings !== true) return withoutImages.replace(/^#{1,6}[ \t]+.*$/gm, "");
32
+ return withoutImages.replace(/^#{4,6}([ \t]+)/gm, "###$1").replace(/^#([ \t]+)/gm, "##$1");
25
33
  }
26
34
  function renderBlock(block) {
27
35
  const lines = block.split("\n");
@@ -155,8 +163,8 @@ function escapeAttr(value) {
155
163
  * Turn the formatted DOM of a rich field back into the stored markdown subset. The HTML is parsed with
156
164
  * `DOMParser`, which never runs scripts or loads resources. Browser only (the editor overlay).
157
165
  */
158
- function markdownFromHtml(html) {
159
- return cleanup(serialize(parseHtml(html)));
166
+ function markdownFromHtml(html, options = {}) {
167
+ return cleanup(serialize(parseHtml(html, options.headings === true)));
160
168
  }
161
169
  /** Elements whose content is dropped with them. */
162
170
  const DROPPED = /* @__PURE__ */ new Set([
@@ -171,7 +179,10 @@ const DROPPED = /* @__PURE__ */ new Set([
171
179
  "img",
172
180
  "picture",
173
181
  "video",
174
- "audio",
182
+ "audio"
183
+ ]);
184
+ /** Headings a field without them drops, with their text. */
185
+ const HEADING_TAGS = /* @__PURE__ */ new Set([
175
186
  "h1",
176
187
  "h2",
177
188
  "h3",
@@ -179,16 +190,16 @@ const DROPPED = /* @__PURE__ */ new Set([
179
190
  "h5",
180
191
  "h6"
181
192
  ]);
182
- function parseHtml(html) {
193
+ function parseHtml(html, headings) {
183
194
  const root = {
184
195
  mark: "root",
185
196
  children: []
186
197
  };
187
198
  const body = new DOMParser().parseFromString(`<!doctype html><body>${html}`, "text/html").body;
188
- appendChildren(body, root);
199
+ appendChildren(body, root, headings);
189
200
  return root;
190
201
  }
191
- function appendChildren(parent, into) {
202
+ function appendChildren(parent, into, headings) {
192
203
  for (const child of Array.from(parent.childNodes)) {
193
204
  if (child.nodeType === TEXT_NODE) {
194
205
  into.children.push({
@@ -202,6 +213,7 @@ function appendChildren(parent, into) {
202
213
  const element = child;
203
214
  const name = element.tagName.toLowerCase();
204
215
  if (DROPPED.has(name)) continue;
216
+ if (HEADING_TAGS.has(name) && !headings) continue;
205
217
  if (name === "br") {
206
218
  into.children.push({
207
219
  mark: "text",
@@ -212,7 +224,7 @@ function appendChildren(parent, into) {
212
224
  }
213
225
  const mark = blockOf(name) ?? markOf(name, element.getAttribute("style") ?? "");
214
226
  if (!mark) {
215
- appendChildren(element, into);
227
+ appendChildren(element, into, headings);
216
228
  continue;
217
229
  }
218
230
  const tab = mark === "link" ? element.getAttribute("data-new-tab") : null;
@@ -223,12 +235,14 @@ function appendChildren(parent, into) {
223
235
  newTab: tab === "true" ? "new" : tab === "false" ? "same" : void 0
224
236
  };
225
237
  into.children.push(node);
226
- appendChildren(element, node);
238
+ appendChildren(element, node, headings);
227
239
  }
228
240
  }
229
241
  const ELEMENT_NODE = 1;
230
242
  const TEXT_NODE = 3;
231
243
  function blockOf(name) {
244
+ if (name === "h1" || name === "h2") return "h2";
245
+ if (name === "h3" || name === "h4" || name === "h5" || name === "h6") return "h3";
232
246
  if (name === "ul") return "ul";
233
247
  if (name === "ol") return "ol";
234
248
  if (name === "li") return "li";
@@ -260,6 +274,13 @@ function serializeFlow(nodes) {
260
274
  out += `${list}\n\n`;
261
275
  continue;
262
276
  }
277
+ if (node.mark === "h2" || node.mark === "h3") {
278
+ const text = node.children.map((child) => serializeInline(child)).join("").replace(/\s*\n+\s*/g, " ").trim();
279
+ if (text === "") continue;
280
+ if (out.trim() !== "" && !out.endsWith("\n\n")) out = out.replace(/\n*$/, "\n\n");
281
+ out += `${node.mark === "h2" ? "##" : "###"} ${text}\n\n`;
282
+ continue;
283
+ }
263
284
  if (node.mark === "block") {
264
285
  const inner = serializeFlow(node.children).replace(/\n+$/, "");
265
286
  if (inner === "") continue;
@@ -291,7 +312,7 @@ function serializeInline(node) {
291
312
  return text.trim() === "" ? text : `\`${text}\``;
292
313
  }
293
314
  if (node.mark === "ul" || node.mark === "ol") return serializeList(node);
294
- if (node.mark === "li" || node.mark === "block" || node.mark === "root") return serializeFlow(node.children);
315
+ if (node.mark === "li" || node.mark === "block" || node.mark === "root" || node.mark === "h2" || node.mark === "h3") return serializeFlow(node.mark === "h2" || node.mark === "h3" ? [node] : node.children);
295
316
  const inner = node.children.map((child) => serializeInline(child)).join("");
296
317
  if (!inner) return "";
297
318
  if (node.mark === "strong") return `**${inner}**`;
@@ -1,3 +1,33 @@
1
+ //#region src/media-source.ts
2
+ const YOUTUBE = /^https?:\/\/(?:(?:www|m)\.)?(?:youtube\.com\/(?:watch\?(?:[^#\s]*&)?v=|embed\/|shorts\/|live\/)|youtube-nocookie\.com\/embed\/|youtu\.be\/)([\w-]{6,})(?:[?&#][^\s]*)?$/i;
3
+ const VIMEO = /^https?:\/\/(?:(?:www|player)\.)?vimeo\.com\/(?:video\/)?(\d+)(?:[/?#][^\s]*)?$/i;
4
+ /** A path in `public/` (`/videos/demo.webm`) or an `https://` link to a video file. */
5
+ const VIDEO_FILE = /^(?:\/(?!\/)|https?:\/\/)\S+\.(?:mp4|webm|m4v|mov|ogv)(?:[?#]\S*)?$/i;
6
+ function parseMediaSource(value) {
7
+ const src = value.trim();
8
+ const youtube = YOUTUBE.exec(src);
9
+ if (youtube?.[1]) {
10
+ const id = youtube[1];
11
+ return {
12
+ kind: "youtube",
13
+ id,
14
+ embed: `https://www.youtube-nocookie.com/embed/${id}?autoplay=1&rel=0`,
15
+ thumbnail: `https://i.ytimg.com/vi/${id}/hqdefault.jpg`
16
+ };
17
+ }
18
+ const vimeo = VIMEO.exec(src);
19
+ if (vimeo?.[1]) return {
20
+ kind: "vimeo",
21
+ id: vimeo[1],
22
+ embed: `https://player.vimeo.com/video/${vimeo[1]}?autoplay=1&dnt=1`
23
+ };
24
+ if (VIDEO_FILE.test(src)) return {
25
+ kind: "file",
26
+ src
27
+ };
28
+ return null;
29
+ }
30
+ //#endregion
1
31
  //#region src/consent.ts
2
32
  /** Longest copy the consent banner takes, in characters. The editor shows the same limits. */
3
33
  const CONSENT_LIMITS = {
@@ -478,4 +508,4 @@ function contrastChecks(colors) {
478
508
  return checks;
479
509
  }
480
510
  //#endregion
481
- export { consentCopy as _, contrastRatio as a, hasThemeColors as c, tokenToHex as d, CONSENT_COPY as f, CONSENT_REOPEN as g, CONSENT_LIMITS as h, contrastChecks as i, hexToToken as l, CONSENT_HREF_MESSAGE as m, HEX_COLOR as n, derivePalette as o, CONSENT_HREF as p, OVERRIDE_TOKENS as r, hasOverrides as s, DEFAULT_BRAND as t, themeCss as u, consentTextIssues as v };
511
+ export { consentCopy as _, contrastRatio as a, parseMediaSource as b, hasThemeColors as c, tokenToHex as d, CONSENT_COPY as f, CONSENT_REOPEN as g, CONSENT_LIMITS as h, contrastChecks as i, hexToToken as l, CONSENT_HREF_MESSAGE as m, HEX_COLOR as n, derivePalette as o, CONSENT_HREF as p, OVERRIDE_TOKENS as r, hasOverrides as s, DEFAULT_BRAND as t, themeCss as u, consentTextIssues as v, VIDEO_FILE as y };