@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.
- package/LICENSE +110 -0
- package/README.md +22 -2
- package/bin/brim.js +5 -0
- package/dist/cli.js +3802 -0
- package/editor-dist/editor.css +1 -0
- package/editor-dist/editor.js +91 -0
- package/package.json +71 -3
- package/skills/brim-convert/SKILL.md +256 -0
- package/skills/brim-hosting/SKILL.md +221 -0
- package/src/astro-shim.d.ts +5 -0
- package/src/auth/accounts.ts +341 -0
- package/src/auth/password.ts +34 -0
- package/src/auth/permissions.ts +44 -0
- package/src/blocks/accordion/Accordion.astro +48 -0
- package/src/blocks/accordion/accordion.css +63 -0
- package/src/blocks/analytics/Analytics.astro +45 -0
- package/src/blocks/analytics/ConsentBanner.astro +41 -0
- package/src/blocks/analytics/analytics.client.ts +125 -0
- package/src/blocks/analytics/analytics.css +51 -0
- package/src/blocks/analytics/consent.ts +51 -0
- package/src/blocks/button/Button.astro +64 -0
- package/src/blocks/button/button.css +100 -0
- package/src/blocks/carousel/Carousel.astro +140 -0
- package/src/blocks/carousel/carousel.client.ts +153 -0
- package/src/blocks/carousel/carousel.css +181 -0
- package/src/blocks/data/icons.ts +285 -0
- package/src/blocks/data/image-shapes.ts +91 -0
- package/src/blocks/data/social-icons.ts +41 -0
- package/src/blocks/data/social-networks.ts +256 -0
- package/src/blocks/divider/Divider.astro +27 -0
- package/src/blocks/divider/divider.css +17 -0
- package/src/blocks/form/Captcha.astro +27 -0
- package/src/blocks/form/Form.astro +250 -0
- package/src/blocks/form/FormField.astro +138 -0
- package/src/blocks/form/Newsletter.astro +80 -0
- package/src/blocks/form/form.client.ts +339 -0
- package/src/blocks/form/form.css +271 -0
- package/src/blocks/gallery/ImageGallery.astro +126 -0
- package/src/blocks/gallery/gallery.css +43 -0
- package/src/blocks/gallery/lightbox.client.ts +178 -0
- package/src/blocks/gallery/lightbox.css +134 -0
- package/src/blocks/icon/Icon.astro +41 -0
- package/src/blocks/icon/icon.css +16 -0
- package/src/blocks/image/Image.astro +158 -0
- package/src/blocks/image/image.css +42 -0
- package/src/blocks/images.ts +64 -0
- package/src/blocks/in-editor.client.ts +8 -0
- package/src/blocks/listing/Listing.astro +288 -0
- package/src/blocks/listing/ListingCards.astro +48 -0
- package/src/blocks/listing/listing.css +136 -0
- package/src/blocks/listing/query.ts +363 -0
- package/src/blocks/map/Map.astro +75 -0
- package/src/blocks/map/map.css +32 -0
- package/src/blocks/schemas/accordion.ts +118 -0
- package/src/blocks/schemas/button.ts +78 -0
- package/src/blocks/schemas/carousel.ts +133 -0
- package/src/blocks/schemas/divider.ts +29 -0
- package/src/blocks/schemas/form-field-types.ts +29 -0
- package/src/blocks/schemas/form.ts +350 -0
- package/src/blocks/schemas/icon.ts +24 -0
- package/src/blocks/schemas/image-gallery.ts +54 -0
- package/src/blocks/schemas/image.ts +87 -0
- package/src/blocks/schemas/index.ts +33 -0
- package/src/blocks/schemas/listing.ts +14 -0
- package/src/blocks/schemas/map.ts +81 -0
- package/src/blocks/schemas/newsletter.ts +324 -0
- package/src/blocks/schemas/social-media.ts +348 -0
- package/src/blocks/schemas/text.ts +151 -0
- package/src/blocks/schemas/video.ts +57 -0
- package/src/blocks/search/Search.astro +147 -0
- package/src/blocks/search/search.css +69 -0
- package/src/blocks/search/state.ts +28 -0
- package/src/blocks/settings.ts +94 -0
- package/src/blocks/social/SocialMedia.astro +102 -0
- package/src/blocks/social/social-links.ts +16 -0
- package/src/blocks/social/social.css +31 -0
- package/src/blocks/theme.ts +35 -0
- package/src/blocks/types.ts +52 -0
- package/src/blocks/video/Video.astro +90 -0
- package/src/blocks/video/video-source.ts +77 -0
- package/src/blocks/video/video.client.ts +43 -0
- package/src/blocks/video/video.css +29 -0
- package/src/check.ts +1119 -0
- package/src/cli.ts +493 -0
- package/src/components/Blocks.astro +68 -0
- package/src/components/Field.astro +61 -0
- package/src/components/Footer.astro +36 -0
- package/src/components/Link.astro +18 -0
- package/src/components/MobileMenu.astro +77 -0
- package/src/components/Navbar.astro +42 -0
- package/src/components/RichText.astro +32 -0
- package/src/components/Seo.astro +202 -0
- package/src/components/Setting.astro +46 -0
- package/src/components/Shared.astro +17 -0
- package/src/components/Text.astro +91 -0
- package/src/components/blocks.css +86 -0
- package/src/components/index.ts +27 -0
- package/src/components/mobile-menu.client.ts +21 -0
- package/src/components/mobile-menu.css +52 -0
- package/src/components/text.css +48 -0
- package/src/config.ts +413 -0
- package/src/content/block-layout.ts +63 -0
- package/src/content/block-values.ts +48 -0
- package/src/content/content-check.ts +174 -0
- package/src/content/content-types.ts +408 -0
- package/src/content/drafts.ts +115 -0
- package/src/content/feed.ts +237 -0
- package/src/content/field-types.ts +107 -0
- package/src/content/history.ts +140 -0
- package/src/content/ids.ts +50 -0
- package/src/content/image-size.ts +212 -0
- package/src/content/image-sizes.ts +126 -0
- package/src/content/item-fields.ts +110 -0
- package/src/content/item-head.ts +126 -0
- package/src/content/item-image.ts +26 -0
- package/src/content/item-path.ts +122 -0
- package/src/content/items.ts +580 -0
- package/src/content/language-scopes.ts +40 -0
- package/src/content/media.ts +271 -0
- package/src/content/menus.ts +51 -0
- package/src/content/page-templates.ts +52 -0
- package/src/content/redirects.ts +112 -0
- package/src/content/references.ts +165 -0
- package/src/content/rich-text.ts +63 -0
- package/src/content/search.ts +167 -0
- package/src/content/seed.ts +229 -0
- package/src/content/site-settings.ts +111 -0
- package/src/content/sitemap.ts +143 -0
- package/src/content/store.ts +225 -0
- package/src/content/tags.ts +67 -0
- package/src/content/text-default.ts +139 -0
- package/src/content/types.ts +137 -0
- package/src/content/validate.ts +253 -0
- package/src/content/validation-error.ts +2 -0
- package/src/db/cached.ts +140 -0
- package/src/db/database.ts +15 -0
- package/src/db/libsql.ts +29 -0
- package/src/db/schema.ts +189 -0
- package/src/db/url.ts +44 -0
- package/src/editor/client/BlockSettings.tsx +1063 -0
- package/src/editor/client/Chrome.tsx +284 -0
- package/src/editor/client/ContentBrowser.tsx +1299 -0
- package/src/editor/client/Editor.tsx +1756 -0
- package/src/editor/client/HistoryPanel.tsx +114 -0
- package/src/editor/client/ImagesWidget.tsx +113 -0
- package/src/editor/client/Inspector.tsx +127 -0
- package/src/editor/client/ItemForm.tsx +535 -0
- package/src/editor/client/LinkWidget.tsx +440 -0
- package/src/editor/client/ListingPanel.tsx +99 -0
- package/src/editor/client/Login.tsx +338 -0
- package/src/editor/client/Menu.tsx +64 -0
- package/src/editor/client/PageSettings.tsx +270 -0
- package/src/editor/client/RedirectsSettings.tsx +130 -0
- package/src/editor/client/Resize.tsx +204 -0
- package/src/editor/client/Sidebar.tsx +51 -0
- package/src/editor/client/SiteMap.tsx +568 -0
- package/src/editor/client/SiteSettings.tsx +409 -0
- package/src/editor/client/TextTools.tsx +391 -0
- package/src/editor/client/Toolbar.tsx +449 -0
- package/src/editor/client/VideoUpload.tsx +251 -0
- package/src/editor/client/api.ts +417 -0
- package/src/editor/client/brand.ts +20 -0
- package/src/editor/client/connection.ts +98 -0
- package/src/editor/client/context.ts +19 -0
- package/src/editor/client/css.d.ts +2 -0
- package/src/editor/client/editor.css +4656 -0
- package/src/editor/client/fields.tsx +812 -0
- package/src/editor/client/icons.tsx +174 -0
- package/src/editor/client/keys.ts +15 -0
- package/src/editor/client/kinds.ts +289 -0
- package/src/editor/client/languages.ts +32 -0
- package/src/editor/client/main.tsx +100 -0
- package/src/editor/client/responses.ts +26 -0
- package/src/editor/client/rich-text.ts +326 -0
- package/src/editor/client/site-tree.ts +239 -0
- package/src/editor/client/theme.ts +55 -0
- package/src/editor/client/tsconfig.json +18 -0
- package/src/editor/client/tus.ts +137 -0
- package/src/editor/client/use-rect.ts +50 -0
- package/src/editor/client/widgets.tsx +1218 -0
- package/src/forms/captcha.ts +83 -0
- package/src/forms/define.ts +166 -0
- package/src/forms/fields.ts +197 -0
- package/src/forms/file-types.ts +108 -0
- package/src/forms/files.ts +216 -0
- package/src/forms/form-page.ts +9 -0
- package/src/forms/handler.ts +622 -0
- package/src/forms/spec.ts +89 -0
- package/src/forms/store.ts +96 -0
- package/src/forms/work.ts +50 -0
- package/src/icons/library.ts +144 -0
- package/src/icons/load.ts +63 -0
- package/src/index.ts +99 -0
- package/src/init.ts +257 -0
- package/src/integration.ts +469 -0
- package/src/mail/mailer.ts +197 -0
- package/src/mail/notify.ts +272 -0
- package/src/mail/settings.ts +173 -0
- package/src/mail/templates.ts +191 -0
- package/src/mcp/page-nodes.ts +209 -0
- package/src/mcp/server.ts +111 -0
- package/src/mcp/tools.ts +637 -0
- package/src/runtime/TemplatePage.astro +39 -0
- package/src/runtime/TemplatePageStatic.astro +13 -0
- package/src/runtime/admin-route.ts +80 -0
- package/src/runtime/connect.ts +53 -0
- package/src/runtime/content.ts +188 -0
- package/src/runtime/context.ts +131 -0
- package/src/runtime/form-files.ts +18 -0
- package/src/runtime/forms-route.ts +33 -0
- package/src/runtime/forms.ts +28 -0
- package/src/runtime/icons.ts +5 -0
- package/src/runtime/media.ts +175 -0
- package/src/runtime/middleware.ts +186 -0
- package/src/runtime/nav.ts +13 -0
- package/src/runtime/query-reads.ts +49 -0
- package/src/runtime/query-store.ts +16 -0
- package/src/runtime/request-state.ts +69 -0
- package/src/runtime/robots.ts +29 -0
- package/src/runtime/rss-static.ts +16 -0
- package/src/runtime/rss.ts +29 -0
- package/src/runtime/server.ts +29 -0
- package/src/runtime/sitemap.ts +34 -0
- package/src/runtime/template-paths.ts +34 -0
- package/src/runtime/uploads-route.ts +13 -0
- package/src/seed-cli.ts +253 -0
- package/src/server/auth.ts +31 -0
- package/src/server/fetch-image.ts +219 -0
- package/src/server/handler.ts +2106 -0
- package/src/server/http.ts +162 -0
- package/src/server/locales.ts +73 -0
- package/src/server/node.ts +57 -0
- package/src/server/query-params.ts +249 -0
- package/src/server/rate-limit.ts +72 -0
- package/src/server/video-host.ts +162 -0
- package/src/starter.ts +70 -0
- package/src/version.ts +4 -0
- package/src/virtual.d.ts +65 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/* How a mobile menu opens over the page. What is inside it, and from which width it shows, is the site's CSS. */
|
|
2
|
+
/* In the `brim` layer: any CSS of the site wins over it, whatever its selectors. */
|
|
3
|
+
@layer brim {
|
|
4
|
+
.brim-mobile-menu__toggle,
|
|
5
|
+
.brim-mobile-menu__close {
|
|
6
|
+
display: grid;
|
|
7
|
+
place-items: center;
|
|
8
|
+
width: 2.75rem;
|
|
9
|
+
height: 2.75rem;
|
|
10
|
+
padding: 0;
|
|
11
|
+
border: 0;
|
|
12
|
+
background: none;
|
|
13
|
+
color: inherit;
|
|
14
|
+
cursor: pointer;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.brim-mobile-menu__close {
|
|
18
|
+
margin-inline-start: auto;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@supports selector(:popover-open) {
|
|
22
|
+
.brim-mobile-menu__panel {
|
|
23
|
+
inset: 0;
|
|
24
|
+
width: auto;
|
|
25
|
+
height: auto;
|
|
26
|
+
margin: 0;
|
|
27
|
+
padding: 1rem;
|
|
28
|
+
border: 0;
|
|
29
|
+
background: var(--brim-theme-background, Canvas);
|
|
30
|
+
color: inherit;
|
|
31
|
+
overflow-y: auto;
|
|
32
|
+
overscroll-behavior: contain;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/* Important, so a `display` the site gives the open panel does not show the closed one. */
|
|
36
|
+
.brim-mobile-menu__panel:not(:popover-open) {
|
|
37
|
+
display: none !important;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
html:has(.brim-mobile-menu__panel:popover-open) {
|
|
41
|
+
overflow: hidden;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/* A browser without popovers shows the links in place, with nothing to open. */
|
|
46
|
+
@supports not selector(:popover-open) {
|
|
47
|
+
.brim-mobile-menu__toggle,
|
|
48
|
+
.brim-mobile-menu__close {
|
|
49
|
+
display: none;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/* Presentation choices of a text: drop cap, quotes, oversized. Applied only when the owner turns one on. */
|
|
2
|
+
/* In the `brim` layer: any CSS of the site wins over it, whatever its selectors. */
|
|
3
|
+
@layer brim {
|
|
4
|
+
[data-brim-text][data-drop-cap]::first-letter {
|
|
5
|
+
float: left;
|
|
6
|
+
margin: 0.05em 0.08em 0 0;
|
|
7
|
+
color: var(--brim-text-drop-cap-color, currentColor);
|
|
8
|
+
font-size: 3.4em;
|
|
9
|
+
font-weight: 700;
|
|
10
|
+
line-height: 0.8;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
[data-brim-text][data-oversized] {
|
|
14
|
+
font-size: clamp(2.5rem, 8vw, 7rem);
|
|
15
|
+
line-height: 1.05;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
[data-brim-text][data-quote="classic"] {
|
|
19
|
+
padding-inline-start: 1em;
|
|
20
|
+
border-inline-start: 4px solid var(--brim-text-quote-color, currentColor);
|
|
21
|
+
font-style: italic;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
[data-brim-text][data-quote="pull"] {
|
|
25
|
+
padding-block: 0.6em;
|
|
26
|
+
border-block: 2px solid var(--brim-text-quote-color, currentColor);
|
|
27
|
+
font-size: 1.5em;
|
|
28
|
+
font-weight: 600;
|
|
29
|
+
line-height: 1.3;
|
|
30
|
+
text-align: left;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
[data-brim-text][data-quote="pull"][data-quote-size="small"] {
|
|
34
|
+
font-size: 1.25em;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
[data-brim-text][data-quote="pull"][data-quote-size="large"] {
|
|
38
|
+
font-size: 2em;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
[data-brim-text][data-quote="pull"][data-quote-align="center"] {
|
|
42
|
+
text-align: center;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
[data-brim-text][data-quote="pull"][data-quote-align="right"] {
|
|
46
|
+
text-align: right;
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import { mkdirSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { splitDatabaseUrl } from "./db/url.ts";
|
|
4
|
+
import type { IconsOptions } from "./icons/library.ts";
|
|
5
|
+
|
|
6
|
+
export interface DatabaseOptions {
|
|
7
|
+
/** SQLite database URL. Defaults to `BRIM_DATABASE_URL`, then to a file in `.brim/` inside the project. */
|
|
8
|
+
database?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Token for a remote database while developing and building. Defaults to `BRIM_DATABASE_TOKEN`, which the built
|
|
11
|
+
* server reads where it runs: a token written here is never put into the build.
|
|
12
|
+
*/
|
|
13
|
+
authToken?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* What `astro build` makes:
|
|
18
|
+
* - `"server"` (default): a Node server that renders pages from the database on each request and answers
|
|
19
|
+
* `/admin` (editor, API, MCP) and forms. Start it with `brim start`.
|
|
20
|
+
* - `"static"`: plain files for any static host, as the content is at build time. Nothing answers `/admin`.
|
|
21
|
+
*/
|
|
22
|
+
export type OutputMode = "server" | "static";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A video host: a service that takes the videos uploaded in the editor, stores them and says where they play.
|
|
26
|
+
* Brim only knows its address and speaks the small contract in the README; without one, videos are pasted links.
|
|
27
|
+
*/
|
|
28
|
+
export interface VideoUploadsOptions {
|
|
29
|
+
/** HTTPS endpoint of the video host. `BRIM_VIDEO_UPLOADS_URL` wins over it. */
|
|
30
|
+
url?: string | undefined;
|
|
31
|
+
/** Sent to the host as `Authorization: Bearer <token>`. `BRIM_VIDEO_UPLOADS_TOKEN` wins over it. */
|
|
32
|
+
token?: string | undefined; // betterleaks:allow
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* How the site sends email (the notice the owner gets when someone writes through a form). Either an SMTP
|
|
37
|
+
* server, or a mail service that speaks the small contract in the README. The owner can also set their own SMTP
|
|
38
|
+
* server in the editor's Site Settings, which wins over both.
|
|
39
|
+
*/
|
|
40
|
+
export interface MailOptions {
|
|
41
|
+
/** `smtps://user:password@host:465` or `smtp://…:587`. `BRIM_SMTP_URL` wins over it. */ // betterleaks:allow
|
|
42
|
+
smtp?: string | undefined;
|
|
43
|
+
/** The sender, `Name <address>` or an address. `BRIM_MAIL_FROM` wins over it. Needed with `smtp`. */
|
|
44
|
+
from?: string | undefined;
|
|
45
|
+
/** HTTPS endpoint of a mail service, used when there is no SMTP server. `BRIM_MAIL_URL` wins over it. */
|
|
46
|
+
url?: string | undefined;
|
|
47
|
+
/** Sent to the mail service as `Authorization: Bearer <token>`. `BRIM_MAIL_TOKEN` wins over it. */
|
|
48
|
+
token?: string | undefined; // betterleaks:allow
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A color of the site's brand: offered first wherever the editor picks a color, never changed there. */
|
|
52
|
+
export interface BrandColor {
|
|
53
|
+
/** Shown when hovering the swatch, e.g. "Primary". */
|
|
54
|
+
name: string;
|
|
55
|
+
/** A hex color: `#rgb` or `#rrggbb`. */
|
|
56
|
+
value: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const BRAND_HEX = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The brand colors as the config names them, as a list or as `{ name: "#hex" }`. A color that is not `#rgb` or
|
|
63
|
+
* `#rrggbb` is an error, so a typo shows when the site starts instead of as a missing swatch.
|
|
64
|
+
*/
|
|
65
|
+
export function resolveBrandColors(input: BrandColor[] | Record<string, string> | undefined): BrandColor[] {
|
|
66
|
+
if (!input) return [];
|
|
67
|
+
const list = Array.isArray(input) ? input : Object.entries(input).map(([name, value]) => ({ name, value }));
|
|
68
|
+
if (list.length > 24) throw new Error("brim: brandColors has more than 24 colors.");
|
|
69
|
+
return list.map(({ name, value }) => {
|
|
70
|
+
const clean = String(value ?? "").trim();
|
|
71
|
+
if (!BRAND_HEX.test(clean)) throw new Error(`brim: brand color "${name}" is "${value}"; use #rgb or #rrggbb.`);
|
|
72
|
+
return {
|
|
73
|
+
name:
|
|
74
|
+
String(name ?? "")
|
|
75
|
+
.trim()
|
|
76
|
+
.slice(0, 40) || clean,
|
|
77
|
+
value: clean.toLowerCase(),
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Where uploaded images are kept and how they reach visitors. Everything is optional: by default images are files in
|
|
84
|
+
* `public/uploads`, served by the site's own server at `/uploads/…`, at their full size.
|
|
85
|
+
*
|
|
86
|
+
* media: {
|
|
87
|
+
* storage: { url: "https://storage.example.com/my-site", headers: { Authorization: `Bearer ${process.env.STORAGE_TOKEN}` } },
|
|
88
|
+
* publicUrl: "https://cdn.example.com/my-site",
|
|
89
|
+
* resize: "{url}?width={width}",
|
|
90
|
+
* }
|
|
91
|
+
*/
|
|
92
|
+
export interface MediaOptions {
|
|
93
|
+
/**
|
|
94
|
+
* Keep uploads in a storage reached over HTTP instead of on the server's disk: each image is sent with
|
|
95
|
+
* `PUT <url>/uploads/<file>` and the headers given (for the storage's key). The database lists the images as
|
|
96
|
+
* before, under their `/uploads/…` address, so content does not change when the storage does.
|
|
97
|
+
* `BRIM_MEDIA_STORAGE_URL` and `BRIM_MEDIA_STORAGE_TOKEN` (sent as `Authorization: Bearer`) override it.
|
|
98
|
+
*/
|
|
99
|
+
storage?: { url: string; headers?: Record<string, string> };
|
|
100
|
+
/**
|
|
101
|
+
* Where visitors get the images: `<publicUrl>/uploads/<file>`, e.g. a CDN in front of the storage. Pages then
|
|
102
|
+
* point at it, and the server sends any request for `/uploads/…` there. `BRIM_MEDIA_PUBLIC_URL` overrides it.
|
|
103
|
+
*/
|
|
104
|
+
publicUrl?: string;
|
|
105
|
+
/**
|
|
106
|
+
* How the CDN in front of the images makes smaller copies, as an address with `{url}` and `{width}`:
|
|
107
|
+
* `"{url}?width={width}"`, or `"/cdn-cgi/image/width={width}/{url}"`. Brim's images then offer the browser a
|
|
108
|
+
* `srcset` of those widths, so a phone downloads a small copy. `BRIM_MEDIA_RESIZE` overrides it.
|
|
109
|
+
*/
|
|
110
|
+
resize?: string;
|
|
111
|
+
/** The widths of the `srcset` when `resize` is set; by default 320, 640, 960, 1280, 1920 and 2560. */
|
|
112
|
+
widths?: number[];
|
|
113
|
+
/**
|
|
114
|
+
* Where visitors get the pictures of the site's own code (`public/images/hero.jpg` at `<siteUrl>/images/hero.jpg`),
|
|
115
|
+
* for a host that copies `public/` to its CDN at each deploy. With `resize`, they then get a `srcset` from that CDN
|
|
116
|
+
* as uploads do; without it, a server makes their copies itself (Astro's `/_image`). `BRIM_MEDIA_SITE_URL`
|
|
117
|
+
* overrides it.
|
|
118
|
+
*/
|
|
119
|
+
siteUrl?: string;
|
|
120
|
+
/**
|
|
121
|
+
* Have the site's own server make the smaller copies of the pictures in `public/` with `sharp` (Astro's `/_image`),
|
|
122
|
+
* for a site with no CDN that makes them. Off unless asked for: a site behind a CDN that resizes needs neither the
|
|
123
|
+
* package nor the work. The site then depends on `sharp` itself (`npm install sharp`). `BRIM_MEDIA_SHARP` (`1` or
|
|
124
|
+
* `0`) overrides it.
|
|
125
|
+
*/
|
|
126
|
+
sharp?: boolean;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** What `media` resolves to when the server starts: the environment wins, so a host can set it without the code. */
|
|
130
|
+
export interface MediaConfig {
|
|
131
|
+
storage?: { url: string; headers: Record<string, string> };
|
|
132
|
+
publicUrl?: string;
|
|
133
|
+
resize?: string;
|
|
134
|
+
widths: number[];
|
|
135
|
+
siteUrl?: string;
|
|
136
|
+
sharp?: true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export const DEFAULT_IMAGE_WIDTHS = [320, 640, 960, 1280, 1920, 2560];
|
|
140
|
+
|
|
141
|
+
export function resolveMedia(options: MediaOptions = {}): MediaConfig {
|
|
142
|
+
const pick = (name: string, value: string | undefined): string | undefined =>
|
|
143
|
+
(process.env[name] ?? value ?? "").trim() || undefined;
|
|
144
|
+
const envStorage = pick("BRIM_MEDIA_STORAGE_URL", undefined);
|
|
145
|
+
const token = pick("BRIM_MEDIA_STORAGE_TOKEN", undefined); // betterleaks:allow
|
|
146
|
+
const storage = envStorage
|
|
147
|
+
? { url: envStorage, headers: token ? { authorization: `Bearer ${token}` } : {} }
|
|
148
|
+
: options.storage?.url
|
|
149
|
+
? { url: options.storage.url, headers: options.storage.headers ?? {} }
|
|
150
|
+
: undefined;
|
|
151
|
+
const publicUrl = pick("BRIM_MEDIA_PUBLIC_URL", options.publicUrl);
|
|
152
|
+
const resize = pick("BRIM_MEDIA_RESIZE", options.resize);
|
|
153
|
+
const siteUrl = pick("BRIM_MEDIA_SITE_URL", options.siteUrl);
|
|
154
|
+
const asked = pick("BRIM_MEDIA_SHARP", undefined)?.toLowerCase();
|
|
155
|
+
const sharp = asked ? ["1", "true", "yes", "on"].includes(asked) : options.sharp === true;
|
|
156
|
+
const widths = (options.widths ?? DEFAULT_IMAGE_WIDTHS).filter((width) => Number.isInteger(width) && width > 0);
|
|
157
|
+
return {
|
|
158
|
+
...(storage && { storage: { ...storage, url: storage.url.replace(/\/+$/, "") } }),
|
|
159
|
+
...(publicUrl && { publicUrl: publicUrl.replace(/\/+$/, "") }),
|
|
160
|
+
...(resize && { resize }),
|
|
161
|
+
...(siteUrl && { siteUrl: siteUrl.replace(/\/+$/, "") }),
|
|
162
|
+
...(sharp && { sharp: true as const }),
|
|
163
|
+
widths: widths.length > 0 ? [...new Set(widths)].sort((a, b) => a - b) : DEFAULT_IMAGE_WIDTHS,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Checks `media` when the config is read, so a mistake fails at startup rather than on the first upload. */
|
|
168
|
+
export function checkMedia(options: MediaOptions | undefined): void {
|
|
169
|
+
if (!options) return;
|
|
170
|
+
const address = (value: string, name: string) => {
|
|
171
|
+
if (!/^https?:\/\//.test(value)) throw new Error(`brim({ media }): ${name} must be an http or https address.`);
|
|
172
|
+
};
|
|
173
|
+
if (options.storage) address(options.storage.url, "storage.url");
|
|
174
|
+
if (options.publicUrl) address(options.publicUrl, "publicUrl");
|
|
175
|
+
if (options.siteUrl) address(options.siteUrl, "siteUrl");
|
|
176
|
+
if (options.resize && !(options.resize.includes("{url}") && options.resize.includes("{width}")))
|
|
177
|
+
throw new Error('brim({ media }): resize must hold {url} and {width}, e.g. "{url}?width={width}".');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Content types are not an option: they live in `src/content-types/index.ts`, exported as `contentTypes`. */
|
|
181
|
+
export interface BrimOptions extends DatabaseOptions {
|
|
182
|
+
/** Defaults to `BRIM_OUTPUT`, then to `"server"`. */
|
|
183
|
+
output?: OutputMode;
|
|
184
|
+
videoUploads?: VideoUploadsOptions;
|
|
185
|
+
/** Where uploaded images are kept and how they reach visitors: see `MediaOptions`. */
|
|
186
|
+
media?: MediaOptions;
|
|
187
|
+
mail?: MailOptions;
|
|
188
|
+
/** Which icons the editor offers: Brim's own set, some of it, and packs you add. */
|
|
189
|
+
icons?: IconsOptions;
|
|
190
|
+
/**
|
|
191
|
+
* The brand's colors, offered first in the editor's color pickers (text color, highlight, color fields). They
|
|
192
|
+
* are set here, in code, so editors use them but cannot change them. `{ Primary: "#ff7138" }` or a list.
|
|
193
|
+
*/
|
|
194
|
+
brandColors?: BrandColor[] | Record<string, string>;
|
|
195
|
+
/**
|
|
196
|
+
* Who the site is, set in code: its name, how page titles are written, what a page without its own
|
|
197
|
+
* description or share image falls back to, its theme color and its favicon. See `IdentityOptions`.
|
|
198
|
+
*/
|
|
199
|
+
identity?: IdentityOptions;
|
|
200
|
+
/** The site's forms: see `FormsConfigOptions`. */
|
|
201
|
+
forms?: FormsConfigOptions;
|
|
202
|
+
/** @deprecated Brim.js no longer has an AI assistant: ignored, with a warning when the site starts. */
|
|
203
|
+
assistant?: unknown;
|
|
204
|
+
/**
|
|
205
|
+
* The query parameters pages and routes of the site's own code read, for a host that keeps pages in a CDN
|
|
206
|
+
* (Brimsites): a path, or a pattern where `*` stands for any characters, → the names, or `"all"`.
|
|
207
|
+
* `{ "/api/products": ["sort", "id"], "/shop/*": ["color"], "/legacy/*": "all" }`. Brim's own blocks declare
|
|
208
|
+
* what they read by themselves; a page can also say it with `readsQuery()` from `@brimsites/brimjs/server`.
|
|
209
|
+
*/
|
|
210
|
+
query?: Record<string, readonly string[] | "all">;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface FormsConfigOptions {
|
|
214
|
+
/**
|
|
215
|
+
* A challenge every form asks visitors to pass, from Cloudflare Turnstile or hCaptcha:
|
|
216
|
+
* `{ provider: "turnstile", siteKey: "0x4AAA…" }`. The secret key goes in the server's `BRIM_CAPTCHA_SECRET`.
|
|
217
|
+
*/
|
|
218
|
+
captcha?: import("./forms/captcha.ts").CaptchaOptions;
|
|
219
|
+
/**
|
|
220
|
+
* Where the files visitors attach are kept, for a site that keeps its uploads in a storage (`media.storage`):
|
|
221
|
+
* `{ storage: { url, headers } }` names a private one, reached with `PUT`, `GET` and `DELETE
|
|
222
|
+
* <url>/form-files/<message>/<file>`. Nothing under it may be public. `BRIM_PRIVATE_STORAGE_URL` and
|
|
223
|
+
* `BRIM_PRIVATE_STORAGE_TOKEN` (sent as `Authorization: Bearer`) override it. A site that keeps uploads on its
|
|
224
|
+
* own disk needs nothing: the files go to `.brim/form-files`, which nothing serves.
|
|
225
|
+
*/
|
|
226
|
+
files?: { storage?: { url: string; headers?: Record<string, string> } };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export interface IdentityOptions {
|
|
230
|
+
/** The site's name: `og:site_name`, the title of a page without one, the editor's tab ("Edit · Acme"). */
|
|
231
|
+
name?: string;
|
|
232
|
+
/**
|
|
233
|
+
* Other ways people write the name, the preferred one first: `["AcmeStudio", "acme.com"]`. Search engines read
|
|
234
|
+
* them in the home page's structured data; visitors never see them.
|
|
235
|
+
*/
|
|
236
|
+
alternateName?: string | readonly string[];
|
|
237
|
+
/** How a page's title is written, `%s` being the page's own: `"%s — Acme Studio"`. */
|
|
238
|
+
titleTemplate?: string;
|
|
239
|
+
/** For pages without a description of their own. */
|
|
240
|
+
description?: string;
|
|
241
|
+
/** Shown when a page without its own image is shared: a file of `public` or an address. */
|
|
242
|
+
image?: string;
|
|
243
|
+
/** The browser's color for the site's bar on phones: `#rrggbb`. */
|
|
244
|
+
themeColor?: string;
|
|
245
|
+
/** The icon in browser tabs, also the editor's. Without it, `public/favicon.svg`, `.ico` or `.png` when there. */
|
|
246
|
+
favicon?: string;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** The identity as pages and the editor use it, checked when the site starts. */
|
|
250
|
+
export interface SiteIdentity {
|
|
251
|
+
name?: string | undefined;
|
|
252
|
+
alternateName?: string[] | undefined;
|
|
253
|
+
titleTemplate?: string | undefined;
|
|
254
|
+
description?: string | undefined;
|
|
255
|
+
image?: string | undefined;
|
|
256
|
+
themeColor?: string | undefined;
|
|
257
|
+
favicon?: string | undefined;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const FAVICON_FILES = ["favicon.svg", "favicon.ico", "favicon.png"];
|
|
261
|
+
|
|
262
|
+
/** The favicon to use: the one the config names, else the first favicon file in the site's `public` folder. */
|
|
263
|
+
export function resolveFavicon(
|
|
264
|
+
favicon: string | undefined,
|
|
265
|
+
publicDir: string,
|
|
266
|
+
exists: (path: string) => boolean,
|
|
267
|
+
): string | undefined {
|
|
268
|
+
const named = favicon?.trim();
|
|
269
|
+
if (named) {
|
|
270
|
+
if (!/^(\/|https:\/\/)/.test(named))
|
|
271
|
+
throw new Error(`brim: identity.favicon "${named}" must start with "/" (a file of public/) or https://.`);
|
|
272
|
+
return named;
|
|
273
|
+
}
|
|
274
|
+
const found = FAVICON_FILES.find((file) => exists(`${publicDir.replace(/\/$/, "")}/${file}`));
|
|
275
|
+
return found ? `/${found}` : undefined;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** The `type` of a favicon's link tag, from its file extension. */
|
|
279
|
+
export function faviconType(href: string): string | undefined {
|
|
280
|
+
const extension = href.split("?")[0]?.split(".").pop()?.toLowerCase();
|
|
281
|
+
return { svg: "image/svg+xml", ico: "image/x-icon", png: "image/png" }[extension ?? ""];
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* The video host to use, read when the server starts: the environment wins, so a host can turn uploads on for
|
|
286
|
+
* a site without touching its code. `undefined` (no address) leaves the feature off.
|
|
287
|
+
*/
|
|
288
|
+
export function resolveVideoUploads(options: VideoUploadsOptions = {}): VideoUploadsOptions | undefined {
|
|
289
|
+
const url = (process.env.BRIM_VIDEO_UPLOADS_URL ?? options.url ?? "").trim();
|
|
290
|
+
if (!url) return undefined;
|
|
291
|
+
const token = process.env.BRIM_VIDEO_UPLOADS_TOKEN ?? options.token; // betterleaks:allow
|
|
292
|
+
return { url, token };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* A host's own sign-in, offered as a second way in on the editor's sign-in form: a host that already knows its
|
|
297
|
+
* people (Brimsites) checks who they are and sends them back through a sign-in link. Set by the host in the
|
|
298
|
+
* environment only, never in the site's code; `undefined` leaves the form as it was.
|
|
299
|
+
*/
|
|
300
|
+
export interface HostSignIn {
|
|
301
|
+
/** Where the button leads, on the host. */
|
|
302
|
+
url: string;
|
|
303
|
+
/** Who the button names: "Continue with {name}". */
|
|
304
|
+
name: string;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function resolveHostSignIn(): HostSignIn | undefined {
|
|
308
|
+
const url = (process.env.BRIM_HOST_SIGN_IN_URL ?? "").trim();
|
|
309
|
+
if (!/^https?:\/\//i.test(url)) return undefined;
|
|
310
|
+
return { url, name: (process.env.BRIM_HOST_SIGN_IN_NAME ?? "").trim() || "your host" };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** How the server sends email, read when it starts: the environment wins, like for the video host. */
|
|
314
|
+
export function resolveMail(options: MailOptions = {}): MailOptions {
|
|
315
|
+
const pick = (name: string, value: string | undefined): string | undefined =>
|
|
316
|
+
(process.env[name] ?? value ?? "").trim() || undefined;
|
|
317
|
+
return {
|
|
318
|
+
smtp: pick("BRIM_SMTP_URL", options.smtp),
|
|
319
|
+
from: pick("BRIM_MAIL_FROM", options.from),
|
|
320
|
+
url: pick("BRIM_MAIL_URL", options.url),
|
|
321
|
+
token: pick("BRIM_MAIL_TOKEN", options.token), // betterleaks:allow
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export interface DatabaseConfig {
|
|
326
|
+
databaseUrl: string; // betterleaks:allow
|
|
327
|
+
authToken: string | undefined; // betterleaks:allow
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function resolveDatabase(options: DatabaseOptions, projectRoot: string): DatabaseConfig {
|
|
331
|
+
const { url, authToken } = splitDatabaseUrl(
|
|
332
|
+
options.database ?? process.env.BRIM_DATABASE_URL ?? localDatabaseUrl(projectRoot),
|
|
333
|
+
);
|
|
334
|
+
return {
|
|
335
|
+
databaseUrl: url, // betterleaks:allow
|
|
336
|
+
authToken: options.authToken ?? authToken ?? process.env.BRIM_DATABASE_TOKEN, // betterleaks:allow
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** The environment wins, so one project can be built both ways without touching its config. */
|
|
341
|
+
export function resolveOutput(options: BrimOptions): OutputMode {
|
|
342
|
+
const chosen = process.env.BRIM_OUTPUT ?? options.output ?? "server";
|
|
343
|
+
if (chosen !== "server" && chosen !== "static") {
|
|
344
|
+
throw new Error(`Brim's output must be "server" or "static", not "${chosen}".`);
|
|
345
|
+
}
|
|
346
|
+
return chosen;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function localDatabaseUrl(projectRoot: string): string {
|
|
350
|
+
const directory = join(projectRoot, ".brim");
|
|
351
|
+
mkdirSync(directory, { recursive: true });
|
|
352
|
+
return `file:${join(directory, "data.db")}`;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const ADDRESS = /^(\/|https:\/\/)/;
|
|
356
|
+
|
|
357
|
+
/** Checks the identity the config names; a mistake stops the site from starting and says which setting. */
|
|
358
|
+
export function resolveIdentity(
|
|
359
|
+
identity: IdentityOptions | undefined,
|
|
360
|
+
publicDir: string,
|
|
361
|
+
exists: (path: string) => boolean,
|
|
362
|
+
): SiteIdentity {
|
|
363
|
+
const text = (value: string | undefined, max: number): string | undefined => value?.trim().slice(0, max) || undefined;
|
|
364
|
+
const titleTemplate = text(identity?.titleTemplate, 200);
|
|
365
|
+
if (titleTemplate && !titleTemplate.includes("%s")) {
|
|
366
|
+
throw new Error(`brim: identity.titleTemplate "${titleTemplate}" needs %s where the page's title goes.`);
|
|
367
|
+
}
|
|
368
|
+
const image = text(identity?.image, 500);
|
|
369
|
+
if (image && !ADDRESS.test(image))
|
|
370
|
+
throw new Error(`brim: identity.image "${image}" must start with "/" or https://.`);
|
|
371
|
+
const themeColor = text(identity?.themeColor, 20);
|
|
372
|
+
if (themeColor && !/^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i.test(themeColor)) {
|
|
373
|
+
throw new Error(`brim: identity.themeColor "${themeColor}" must be #rgb or #rrggbb.`);
|
|
374
|
+
}
|
|
375
|
+
const name = text(identity?.name, 120);
|
|
376
|
+
const alternateName = resolveAlternateNames(identity?.alternateName, name);
|
|
377
|
+
return {
|
|
378
|
+
name,
|
|
379
|
+
...(alternateName.length > 0 && { alternateName }),
|
|
380
|
+
titleTemplate,
|
|
381
|
+
description: text(identity?.description, 500),
|
|
382
|
+
image,
|
|
383
|
+
themeColor,
|
|
384
|
+
favicon: resolveFavicon(identity?.favicon, publicDir, exists),
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** The other names of the site, each once and without the name itself, as it is written already. */
|
|
389
|
+
function resolveAlternateNames(value: unknown, name: string | undefined): string[] {
|
|
390
|
+
if (value === undefined) return [];
|
|
391
|
+
const given = typeof value === "string" ? [value] : value;
|
|
392
|
+
if (!Array.isArray(given) || given.some((other) => typeof other !== "string")) {
|
|
393
|
+
throw new Error('brim: identity.alternateName must be a text or a list of texts: ["AcmeStudio", "acme.com"].');
|
|
394
|
+
}
|
|
395
|
+
const names = given.map((other: string) => other.trim().slice(0, 120)).filter((other) => other && other !== name);
|
|
396
|
+
return [...new Set(names)].slice(0, 10);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** The addresses of `sameAs` that can be published: full web addresses, each once; empty settings are left out. */
|
|
400
|
+
export function profileAddresses(values: readonly unknown[] | undefined): string[] {
|
|
401
|
+
const addresses = (values ?? [])
|
|
402
|
+
.map((value) => (typeof value === "string" ? value.trim() : ""))
|
|
403
|
+
.filter((value) => /^https?:\/\//i.test(value) && URL.canParse(value));
|
|
404
|
+
return [...new Set(addresses)];
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** A page's title through the template: `About` → `About — Acme`; the site's name alone stays as it is. */
|
|
408
|
+
export function pageTitleOf(title: string | undefined, identity: SiteIdentity): string {
|
|
409
|
+
const own = title?.trim();
|
|
410
|
+
if (!own) return identity.name ?? "";
|
|
411
|
+
if (!identity.titleTemplate || own === identity.name) return own;
|
|
412
|
+
return identity.titleTemplate.replace("%s", own);
|
|
413
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a block sits in a `blocks` field, where no code of the site places it: how wide (`narrow`, `content`, `wide`,
|
|
3
|
+
* `full`, whose widths the site sets with `--brim-block-narrow`, `--brim-block-content` and `--brim-block-wide`),
|
|
4
|
+
* when narrower than its place, to which side, and how much space is above it, in steps of the site's rhythm. The same two choices for every kind of block; a block the site's code
|
|
5
|
+
* places itself has neither, since the code decides.
|
|
6
|
+
*/
|
|
7
|
+
export const BLOCK_WIDTHS = ["narrow", "content", "wide", "full"] as const;
|
|
8
|
+
export const BLOCK_ALIGNS = ["left", "center", "right"] as const;
|
|
9
|
+
/** The space above a block, in steps of the site's rhythm (`--brim-block-gap`): 0, ½, 1 or 2 times it. */
|
|
10
|
+
export const BLOCK_SPACES = ["none", "small", "normal", "large"] as const;
|
|
11
|
+
|
|
12
|
+
export type BlockWidth = (typeof BLOCK_WIDTHS)[number];
|
|
13
|
+
export type BlockAlign = (typeof BLOCK_ALIGNS)[number];
|
|
14
|
+
export type BlockSpace = (typeof BLOCK_SPACES)[number];
|
|
15
|
+
|
|
16
|
+
export interface BlockLayout {
|
|
17
|
+
width: BlockWidth;
|
|
18
|
+
align: BlockAlign;
|
|
19
|
+
space: BlockSpace;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const DEFAULT_BLOCK_LAYOUT: BlockLayout = { width: "content", align: "center", space: "normal" };
|
|
23
|
+
|
|
24
|
+
/** How wide each width is drawn by default, for the `sizes` of its pictures; the site's CSS may set others. */
|
|
25
|
+
const DRAWN: Record<BlockWidth, string> = {
|
|
26
|
+
narrow: "(max-width: 480px) 100vw, 480px",
|
|
27
|
+
content: "(max-width: 760px) 100vw, 760px",
|
|
28
|
+
wide: "(max-width: 1200px) 100vw, 1200px",
|
|
29
|
+
full: "100vw",
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export function blockSizes(width: BlockWidth): string {
|
|
33
|
+
return DRAWN[width];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A block's layout: the one saved with it, or, for a block saved before blocks had one, what its own settings said
|
|
38
|
+
* (an image's or a video's `size`, a button's `inlineAlignment`), so it keeps looking as it did.
|
|
39
|
+
*/
|
|
40
|
+
export function layoutOf(type: string, layout: unknown, value: Record<string, unknown> = {}): BlockLayout {
|
|
41
|
+
if (typeof layout === "object" && layout !== null) {
|
|
42
|
+
const { width, align, space } = layout as Record<string, unknown>;
|
|
43
|
+
return {
|
|
44
|
+
width: BLOCK_WIDTHS.includes(width as BlockWidth) ? (width as BlockWidth) : DEFAULT_BLOCK_LAYOUT.width,
|
|
45
|
+
align: BLOCK_ALIGNS.includes(align as BlockAlign) ? (align as BlockAlign) : DEFAULT_BLOCK_LAYOUT.align,
|
|
46
|
+
space: BLOCK_SPACES.includes(space as BlockSpace) ? (space as BlockSpace) : DEFAULT_BLOCK_LAYOUT.space,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const size = value.size;
|
|
50
|
+
const width: BlockWidth =
|
|
51
|
+
(type === "image" || type === "video" || type === "map") && typeof size === "string"
|
|
52
|
+
? size === "sm" || size === "md" || size === "custom"
|
|
53
|
+
? "narrow"
|
|
54
|
+
: size === "lg"
|
|
55
|
+
? "wide"
|
|
56
|
+
: "content"
|
|
57
|
+
: DEFAULT_BLOCK_LAYOUT.width;
|
|
58
|
+
const alignment = value.inlineAlignment ?? value.objectPosition;
|
|
59
|
+
const align: BlockAlign = BLOCK_ALIGNS.includes(alignment as BlockAlign)
|
|
60
|
+
? (alignment as BlockAlign)
|
|
61
|
+
: DEFAULT_BLOCK_LAYOUT.align;
|
|
62
|
+
return { width, align, space: DEFAULT_BLOCK_LAYOUT.space };
|
|
63
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Database } from "../db/database.ts";
|
|
2
|
+
import type { BlockEntry, ContentType } from "./content-types.ts";
|
|
3
|
+
|
|
4
|
+
/** The id a text saved before its field held blocks takes as the field's one block. */
|
|
5
|
+
export const FORMER_TEXT_ID = "text";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The blocks of a `blocks` field as saved. A field declared `richtext` before keeps its text as it was saved
|
|
9
|
+
* (`{ blocks: [...] }`): it reads as one text block, so changing a field from `richtext` to `blocks` loses nothing,
|
|
10
|
+
* and the owner can add blocks after it at once. Anything else that is not a list reads as no blocks.
|
|
11
|
+
*/
|
|
12
|
+
export function blocksOf(value: unknown): BlockEntry[] {
|
|
13
|
+
if (Array.isArray(value)) return value as BlockEntry[];
|
|
14
|
+
if (isRichText(value)) return [{ id: FORMER_TEXT_ID, type: "richtext", value }];
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isRichText(value: unknown): value is Record<string, unknown> & { blocks: unknown[] } {
|
|
19
|
+
return typeof value === "object" && value !== null && Array.isArray((value as { blocks?: unknown }).blocks);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const upgraded = new WeakSet<Database>();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Writes the texts of fields that became `blocks` as blocks, in the database itself, once per server: one query per
|
|
26
|
+
* `blocks` field, which finds nothing once they are written. A site that changes an article's body from `richtext`
|
|
27
|
+
* to `blocks` then only deploys; reading them as blocks meanwhile is `blocksOf`'s.
|
|
28
|
+
*/
|
|
29
|
+
export async function upgradeFieldValues(db: Database, contentTypes: ContentType[]): Promise<void> {
|
|
30
|
+
if (upgraded.has(db)) return;
|
|
31
|
+
upgraded.add(db);
|
|
32
|
+
for (const type of contentTypes) {
|
|
33
|
+
for (const field of type.fields) {
|
|
34
|
+
if (field.type !== "blocks" || !/^[A-Za-z_][\w]*$/.test(field.name)) continue;
|
|
35
|
+
const path = `$.${field.name}`;
|
|
36
|
+
await db
|
|
37
|
+
.query(
|
|
38
|
+
`UPDATE brim_items SET data = json_set(data, ?, json_array(json_object('id', ?, 'type', 'richtext', 'value', json(json_extract(data, ?)))))
|
|
39
|
+
WHERE type = ? AND json_type(data, ?) = 'object' AND json_type(data, ?) = 'array'`,
|
|
40
|
+
[path, FORMER_TEXT_ID, path, type.key, path, `${path}.blocks`],
|
|
41
|
+
)
|
|
42
|
+
.catch((error: unknown) => {
|
|
43
|
+
upgraded.delete(db);
|
|
44
|
+
console.warn(`[brim] The texts of "${type.key}.${field.name}" could not be written as blocks:`, error);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|