@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
package/src/cli.ts
ADDED
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { basename, join } from "node:path";
|
|
5
|
+
import { createInterface } from "node:readline/promises";
|
|
6
|
+
import { pathToFileURL } from "node:url";
|
|
7
|
+
import { Writable } from "node:stream";
|
|
8
|
+
import type { Role } from "./auth/accounts.ts";
|
|
9
|
+
import { ROLES } from "./auth/permissions.ts";
|
|
10
|
+
import { checkSite, formatReport } from "./check.ts";
|
|
11
|
+
import { normalizePagePath, pageScope, siteScope } from "./content/ids.ts";
|
|
12
|
+
import { MediaStore } from "./content/media.ts";
|
|
13
|
+
import { init, initReport } from "./init.ts";
|
|
14
|
+
import { resolveDatabase } from "./config.ts";
|
|
15
|
+
import { type Brim, connect } from "./runtime/connect.ts";
|
|
16
|
+
import {
|
|
17
|
+
type SeedTarget,
|
|
18
|
+
contentFindings,
|
|
19
|
+
exportSeedFiles,
|
|
20
|
+
formatContentFindings,
|
|
21
|
+
isLocalSite,
|
|
22
|
+
runSeed,
|
|
23
|
+
} from "./seed-cli.ts";
|
|
24
|
+
|
|
25
|
+
const USAGE = `Usage:
|
|
26
|
+
brim init [--brim <version>] [--lang <code>] [--no-git]
|
|
27
|
+
Set up Brim.js in an Astro project that already exists: the
|
|
28
|
+
dependency, the integration, the site's language (--lang, else the
|
|
29
|
+
layout's <html lang>, else en), the conversion guide for AI agents,
|
|
30
|
+
and git (git init, nothing committed) unless it is in git already.
|
|
31
|
+
brim start Run the site built by \`astro build\`: pages, /admin and forms.
|
|
32
|
+
Listens on HOST and PORT (default 0.0.0.0:4321).
|
|
33
|
+
brim check [folder] [--json] Read the site's source for duplicate or positional nodeIds and files
|
|
34
|
+
left unchanged from the starter. Exits with 1 when there are errors.
|
|
35
|
+
brim check --content Also read the running site's content: values left from testing
|
|
36
|
+
(lorem ipsum, example.com, 0000000), terms nothing uses, and
|
|
37
|
+
dynamic pages missing from the sitemap. Warnings only.
|
|
38
|
+
brim info [--json] What the site's database holds: users, pages, items, media.
|
|
39
|
+
brim seed [folder] [--dry-run] Add the site's starting content from seed/<type>.json (pictures in
|
|
40
|
+
seed/images/) to the running dev server. Only adds what the site
|
|
41
|
+
does not have yet; never changes or deletes anything. Everything is
|
|
42
|
+
checked first, and one mistake adds nothing.
|
|
43
|
+
brim seed --export [folder] [--force] Write the site's items into seed/<type>.json, with their pictures,
|
|
44
|
+
to keep in git as its starting content.
|
|
45
|
+
Both take --url <site> --token <token> for a site elsewhere.
|
|
46
|
+
brim content [path] [--json] What the owner saved on a page (/ by default) and on the whole
|
|
47
|
+
site (header, footer, settings): each element's nodeId, kind and
|
|
48
|
+
value. Read it before changing a page's code. On Brimsites:
|
|
49
|
+
brimsites content <site> [path].
|
|
50
|
+
brim content --all [--json] Every place with saved content (pages, templates of item pages,
|
|
51
|
+
the site), with how many elements each.
|
|
52
|
+
brim media add <file>... [--item <type>/<id>] Put images into the site's media library (public/uploads), where
|
|
53
|
+
the owner finds them, and print their /uploads addresses.
|
|
54
|
+
brim open [email] [--page /path] [--url <site>]
|
|
55
|
+
Open the editor in the browser, already signed in (a one-time link),
|
|
56
|
+
on the dev server (http://localhost:4321) unless --url names the site.
|
|
57
|
+
brim user add <email> [--role <role>] Create a user who can sign in at /admin.
|
|
58
|
+
The password is asked for, or read from stdin.
|
|
59
|
+
brim user list [--json] List users with their role and number of tokens.
|
|
60
|
+
brim user role <email> <role> Change a user's role: owner (everything) or editor (writes, publishes,
|
|
61
|
+
adds pages, reads forms).
|
|
62
|
+
brim user password <email> Set a new password (asked for, or read from stdin).
|
|
63
|
+
Ends their sessions and access tokens.
|
|
64
|
+
brim user link <email> Print a link that signs the user in once, within two minutes,
|
|
65
|
+
without their password.
|
|
66
|
+
brim user remove <email> Delete a user with their sessions and tokens.
|
|
67
|
+
brim token add <email> [--name <name>] Create an access token for the site's MCP (/admin/mcp).
|
|
68
|
+
It acts as that user and is shown only once.
|
|
69
|
+
brim token list <email> List a user's tokens.
|
|
70
|
+
brim token revoke <email> <name> Delete a token; clients using it stop working.`;
|
|
71
|
+
|
|
72
|
+
async function main(args: string[]): Promise<void> {
|
|
73
|
+
const [group, command] = args;
|
|
74
|
+
// The email is the first word after the command that is neither an option nor an option's value, so
|
|
75
|
+
// `brim user add --role editor ana@example.com` works as well as the order the help shows.
|
|
76
|
+
const takesValue = (arg: string | undefined) => !!arg?.startsWith("--") && arg !== "--json";
|
|
77
|
+
const email = args.slice(2).find((arg, index, rest) => !arg.startsWith("--") && !takesValue(rest[index - 1]));
|
|
78
|
+
const json = args.includes("--json");
|
|
79
|
+
if (group === "start") return start();
|
|
80
|
+
if (group === "init") return setUp(option(args, "--brim"), !args.includes("--no-git"), option(args, "--lang"));
|
|
81
|
+
if (group === "check") return check(command && !command.startsWith("--") ? command : ".", json, args);
|
|
82
|
+
if (group === "info") return info(json);
|
|
83
|
+
if (group === "content") return content(command && !command.startsWith("--") ? command : "/", args, json);
|
|
84
|
+
if (group === "user" && command === "list") return listUsers(json);
|
|
85
|
+
if (group === "media" && command === "add") return addMedia(args.slice(2), json);
|
|
86
|
+
if (group === "open") return openEditor(args.slice(1));
|
|
87
|
+
if (group === "seed") return seed(args.slice(1));
|
|
88
|
+
if (!email) throw new Error(USAGE);
|
|
89
|
+
if (group === "user" && command === "add") return addUser(email, args);
|
|
90
|
+
if (group === "user" && command === "role" && args[3]) return setRole(email, args[3]);
|
|
91
|
+
if (group === "user" && command === "password") return setPassword(email);
|
|
92
|
+
if (group === "user" && command === "link") return signInLink(email);
|
|
93
|
+
if (group === "user" && command === "remove") return removeUser(email);
|
|
94
|
+
if (group === "token" && command === "add") return addToken(email, option(args, "--name") ?? "mcp");
|
|
95
|
+
if (group === "token" && command === "list") return listTokens(email);
|
|
96
|
+
if (group === "token" && command === "revoke" && args[3]) return revokeToken(email, args[3]);
|
|
97
|
+
throw new Error(USAGE);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** `brim init`: Brim.js in an Astro project that was not started from the template. */
|
|
101
|
+
async function setUp(brim: string | undefined, git: boolean, lang: string | undefined): Promise<void> {
|
|
102
|
+
const agent = process.env.npm_config_user_agent ?? "";
|
|
103
|
+
const runner = agent.startsWith("pnpm")
|
|
104
|
+
? "pnpm"
|
|
105
|
+
: agent.startsWith("yarn")
|
|
106
|
+
? "yarn"
|
|
107
|
+
: agent.startsWith("bun")
|
|
108
|
+
? "bun"
|
|
109
|
+
: "npm";
|
|
110
|
+
console.log(initReport(await init(process.cwd(), { brim, git, lang }), runner));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The server `astro build` wrote: one Node process for the pages, the editor, the site's MCP and forms. */
|
|
114
|
+
async function start(): Promise<void> {
|
|
115
|
+
const entry = join(process.cwd(), "dist", "server", "entry.mjs");
|
|
116
|
+
if (!existsSync(entry)) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
"There is no built server in dist/server. Run `astro build` first; a static build (BRIM_OUTPUT=static) has no server to start.",
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
process.env.HOST ??= "0.0.0.0";
|
|
122
|
+
await import(pathToFileURL(entry).href);
|
|
123
|
+
console.log(`Editor: http://localhost:${process.env.PORT ?? 4321}/admin/edit (or npx brim open, already signed in)`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Reads the source only, so it works before installing, building or creating a database. */
|
|
127
|
+
async function check(folder: string, json: boolean, args: string[]): Promise<void> {
|
|
128
|
+
const report = checkSite(folder);
|
|
129
|
+
if (!args.includes("--content")) {
|
|
130
|
+
console.log(json ? JSON.stringify(report, null, 2) : formatReport(report));
|
|
131
|
+
if (report.errors > 0) process.exitCode = 1;
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// The content is read from the running site, which knows the content types, the settings and the routes.
|
|
135
|
+
const url = (option(args, "--url") ?? `http://localhost:${process.env.PORT ?? 4321}`).replace(/\/+$/, "");
|
|
136
|
+
await asSiteOwner(args, url, option(args, "--token") ?? process.env.BRIM_TOKEN, "check --content", async (target) => {
|
|
137
|
+
const content = await contentFindings(target);
|
|
138
|
+
if (json) {
|
|
139
|
+
console.log(JSON.stringify({ ...report, content }, null, 2));
|
|
140
|
+
} else {
|
|
141
|
+
console.log(formatReport(report));
|
|
142
|
+
console.log("");
|
|
143
|
+
console.log(formatContentFindings(content));
|
|
144
|
+
}
|
|
145
|
+
if (report.errors > 0) process.exitCode = 1;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** A summary of the database, e.g. for a host deciding whether to keep an imported site's content. */
|
|
150
|
+
async function info(json: boolean): Promise<void> {
|
|
151
|
+
await withSite(async ({ db, accounts }) => {
|
|
152
|
+
const count = async (table: string) => Number((await db.query(`SELECT COUNT(*) AS n FROM ${table}`))[0]?.n ?? 0);
|
|
153
|
+
const items = await db.query("SELECT type, COUNT(*) AS n FROM brim_items GROUP BY type ORDER BY type");
|
|
154
|
+
const summary = {
|
|
155
|
+
users: (await accounts.listUsers()).map(({ email, role, createdAt, tokens }) => ({
|
|
156
|
+
email,
|
|
157
|
+
role,
|
|
158
|
+
createdAt,
|
|
159
|
+
tokens,
|
|
160
|
+
})),
|
|
161
|
+
pages: await count("brim_pages"),
|
|
162
|
+
editedElements: await count("brim_nodes"),
|
|
163
|
+
items: Object.fromEntries(items.map((row) => [String(row.type), Number(row.n)])),
|
|
164
|
+
media: await count("brim_media"),
|
|
165
|
+
formResponses: await count("brim_form_responses"),
|
|
166
|
+
};
|
|
167
|
+
if (json) return console.log(JSON.stringify(summary, null, 2));
|
|
168
|
+
console.log(`Users ${summary.users.map((user) => `${user.email} (${user.role})`).join(", ") || "none"}`);
|
|
169
|
+
console.log(`Pages ${summary.pages}`);
|
|
170
|
+
console.log(`Edited elements ${summary.editedElements}`);
|
|
171
|
+
console.log(
|
|
172
|
+
`Items ${
|
|
173
|
+
Object.entries(summary.items)
|
|
174
|
+
.map(([type, n]) => `${type} ${n}`)
|
|
175
|
+
.join(", ") || "none"
|
|
176
|
+
}`,
|
|
177
|
+
);
|
|
178
|
+
console.log(`Uploaded images ${summary.media}`);
|
|
179
|
+
console.log(`Form responses ${summary.formResponses}`);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* What the owner saved: on one page and the whole site, or (`--all`) where anything is saved at all. Elements the
|
|
185
|
+
* editor never touched show the code's own words, so they are not here.
|
|
186
|
+
*/
|
|
187
|
+
async function content(path: string, args: string[], json: boolean): Promise<void> {
|
|
188
|
+
await withSite(async (brim) => {
|
|
189
|
+
if (args.includes("--all")) {
|
|
190
|
+
const rows = await brim.db.query("SELECT scope, COUNT(*) AS n FROM brim_nodes GROUP BY scope ORDER BY scope");
|
|
191
|
+
const places = rows.map((row) => ({ scope: String(row.scope), elements: Number(row.n) }));
|
|
192
|
+
if (json) return console.log(JSON.stringify(places, null, 2));
|
|
193
|
+
if (places.length === 0) return console.log("Nothing saved in the editor yet: every page shows its code's words.");
|
|
194
|
+
for (const { scope, elements } of places) console.log(`${scope.padEnd(48)} ${elements}`);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const page = pageScope(path);
|
|
198
|
+
const nodes = await brim.content.loadNodes([page, siteScope()]);
|
|
199
|
+
const of = (scope: string) =>
|
|
200
|
+
nodes
|
|
201
|
+
.filter((node) => node.scope === scope)
|
|
202
|
+
.map(({ nodeId, kind, value }) => ({ nodeId, kind, value }))
|
|
203
|
+
.sort((a, b) => a.nodeId.localeCompare(b.nodeId));
|
|
204
|
+
const saved = { path: normalizePagePath(path), page: of(page), site: of(siteScope()) };
|
|
205
|
+
if (json) return console.log(JSON.stringify(saved, null, 2));
|
|
206
|
+
const show = (value: unknown) => {
|
|
207
|
+
const text = typeof value === "string" ? value : JSON.stringify(value);
|
|
208
|
+
return text.length > 100 ? `${text.slice(0, 97)}…` : text;
|
|
209
|
+
};
|
|
210
|
+
for (const [title, list] of [[`Page ${saved.path}`, saved.page], ["Whole site", saved.site]] as const) {
|
|
211
|
+
console.log(`${title}: ${list.length === 0 ? "nothing saved" : `${list.length} saved`}`);
|
|
212
|
+
for (const node of list) console.log(` ${node.nodeId.padEnd(32)} ${node.kind.padEnd(10)} ${show(node.value)}`);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function listUsers(json: boolean): Promise<void> {
|
|
218
|
+
await withSite(async ({ accounts }) => {
|
|
219
|
+
const users = await accounts.listUsers();
|
|
220
|
+
if (json)
|
|
221
|
+
return console.log(
|
|
222
|
+
JSON.stringify(
|
|
223
|
+
users.map(({ id: _id, ...user }) => user),
|
|
224
|
+
null,
|
|
225
|
+
2,
|
|
226
|
+
),
|
|
227
|
+
);
|
|
228
|
+
if (users.length === 0) return console.log("No users yet. Add one with: brim user add <email>");
|
|
229
|
+
for (const user of users) {
|
|
230
|
+
console.log(
|
|
231
|
+
`${user.email.padEnd(32)} ${user.role.padEnd(7)} tokens ${user.tokens} since ${user.createdAt.slice(0, 10)}`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function setRole(email: string, role: string): Promise<void> {
|
|
238
|
+
if (!ROLES.includes(role as Role)) throw new Error(USAGE);
|
|
239
|
+
await withSite(async ({ accounts }) => {
|
|
240
|
+
const user = await accounts.setRole(email, role as Role);
|
|
241
|
+
console.log(`${user.email} is now ${user.role}.`);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async function signInLink(email: string): Promise<void> {
|
|
246
|
+
await withSite(async ({ accounts }) => {
|
|
247
|
+
const { token, maxAgeSeconds } = await accounts.createSignInToken(email);
|
|
248
|
+
console.log(`Open this on the site within ${maxAgeSeconds / 60} minutes; it works once:`);
|
|
249
|
+
console.log(`/admin/api/session/link?token=${token}`);
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async function setPassword(email: string): Promise<void> {
|
|
254
|
+
const password = await readPassword(); // betterleaks:allow
|
|
255
|
+
await withSite(async ({ accounts }) => {
|
|
256
|
+
await accounts.setPassword(email, password);
|
|
257
|
+
console.log(
|
|
258
|
+
`Changed the password of ${email}; their open sessions were signed out and their access tokens revoked.`,
|
|
259
|
+
);
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function removeUser(email: string): Promise<void> {
|
|
264
|
+
await withSite(async ({ accounts }) => {
|
|
265
|
+
await accounts.removeUser(email);
|
|
266
|
+
console.log(`Removed ${email} with their sessions and tokens.`);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async function addUser(email: string, args: string[]): Promise<void> {
|
|
271
|
+
const role = option(args, "--role") ?? "owner";
|
|
272
|
+
if (!ROLES.includes(role as Role)) throw new Error(USAGE);
|
|
273
|
+
const password = await readPassword(); // betterleaks:allow
|
|
274
|
+
await withSite(async ({ accounts }) => {
|
|
275
|
+
const user = await accounts.createUser(email, password, role as Role);
|
|
276
|
+
console.log(`Created ${user.role} ${user.email}.`);
|
|
277
|
+
// A password written down travels with the folder; a one-time link signs someone in without one.
|
|
278
|
+
console.log(`Open the editor, signed in: npx brim open ${user.email} (with the dev server running)`);
|
|
279
|
+
console.log(`To let them in without passing the password around: npx brim user link ${user.email}`);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async function addToken(email: string, name: string): Promise<void> {
|
|
284
|
+
await withSite(async ({ accounts }) => {
|
|
285
|
+
const token = await accounts.createToken(email, name);
|
|
286
|
+
console.log(`Created the token "${name}" for ${email}. It is shown only this once:
|
|
287
|
+
|
|
288
|
+
${token}
|
|
289
|
+
|
|
290
|
+
Give it to an MCP client as a bearer token for <site>/admin/mcp, for example:
|
|
291
|
+
|
|
292
|
+
claude mcp add --transport http my-site http://localhost:4321/admin/mcp \\
|
|
293
|
+
--header "Authorization: Bearer ${token}"`);
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
async function listTokens(email: string): Promise<void> {
|
|
298
|
+
await withSite(async ({ accounts }) => {
|
|
299
|
+
const tokens = await accounts.listTokens(email);
|
|
300
|
+
if (tokens.length === 0) return console.log(`${email} has no tokens.`);
|
|
301
|
+
for (const token of tokens) {
|
|
302
|
+
console.log(`${token.name} created ${token.createdAt} last used ${token.lastUsedAt ?? "never"}`);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async function revokeToken(email: string, name: string): Promise<void> {
|
|
308
|
+
await withSite(async ({ accounts }) => {
|
|
309
|
+
const removed = await accounts.revokeToken(email, name);
|
|
310
|
+
console.log(removed ? `Revoked the token "${name}".` : `${email} has no token named "${name}".`);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* `brim media add`: images from this computer into the media library, as the editor uploads them, for pictures that
|
|
316
|
+
* are content (an AI agent's photos, a migration's downloads) rather than files in the code's public/ folder.
|
|
317
|
+
*/
|
|
318
|
+
async function addMedia(args: string[], json: boolean): Promise<void> {
|
|
319
|
+
const folder = option(args, "--item") ?? "";
|
|
320
|
+
const files = args.filter((arg, index) => !arg.startsWith("--") && args[index - 1] !== "--item");
|
|
321
|
+
if (files.length === 0) throw new Error(USAGE);
|
|
322
|
+
await withSite(async (brim) => {
|
|
323
|
+
if (folder) {
|
|
324
|
+
const [type, id, ...rest] = folder.split("/");
|
|
325
|
+
const item = id && rest.length === 0 ? await brim.items.get(id) : undefined;
|
|
326
|
+
if (!item || item.type !== type) throw new Error(`There is no ${type ?? "item"} with the id ${id ?? folder}.`);
|
|
327
|
+
}
|
|
328
|
+
const store = new MediaStore(brim.db, pathToFileURL(join(process.cwd(), "public", "uploads") + "/"));
|
|
329
|
+
const added = [];
|
|
330
|
+
for (const file of files) {
|
|
331
|
+
const uploaded = await store.upload(new Uint8Array(await readFile(file)), basename(file), folder);
|
|
332
|
+
added.push({ file, url: uploaded.url });
|
|
333
|
+
if (!json) console.log(`${file} → ${uploaded.url}`);
|
|
334
|
+
}
|
|
335
|
+
if (json) console.log(JSON.stringify(added, null, 2));
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* `brim open`: the editor in the person's browser, signed in with a one-time link, on a page if asked. Without an
|
|
341
|
+
* email it signs in as the site's first owner. The address is printed too, for a browser on another machine.
|
|
342
|
+
*/
|
|
343
|
+
async function openEditor(args: string[]): Promise<void> {
|
|
344
|
+
const page = option(args, "--page") ?? "/";
|
|
345
|
+
const base = (option(args, "--url") ?? `http://localhost:${process.env.PORT ?? 4321}`).replace(/\/+$/, "");
|
|
346
|
+
const given = args.find(
|
|
347
|
+
(arg, index) => !arg.startsWith("--") && !["--page", "--url"].includes(args[index - 1] ?? ""),
|
|
348
|
+
);
|
|
349
|
+
await withSite(async ({ accounts }) => {
|
|
350
|
+
const email = given ?? (await accounts.listUsers()).find((user) => user.role === "owner")?.email;
|
|
351
|
+
if (!email) throw new Error("The site has no user yet: npx brim user add you@example.com, then npx brim open.");
|
|
352
|
+
const { token } = await accounts.createSignInToken(email);
|
|
353
|
+
const editor = `/admin/edit${page === "/" ? "" : `/${page.replace(/^\/+/, "")}`}`;
|
|
354
|
+
const link = `${base}/admin/api/session/link?token=${token}&to=${encodeURIComponent(editor)}`;
|
|
355
|
+
console.log(`Opening the editor as ${email}. If no browser opens, open this within 2 minutes (it works once):`);
|
|
356
|
+
console.log(link);
|
|
357
|
+
openInBrowser(link);
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* `brim seed`: the starting content in seed/ added to the running site, or, with --export, the site's content
|
|
363
|
+
* written there. On this computer it signs in with an access token it creates for the site's first owner and deletes
|
|
364
|
+
* afterwards; a site elsewhere needs --url and --token, so content never reaches it by accident.
|
|
365
|
+
*/
|
|
366
|
+
async function seed(args: string[]): Promise<void> {
|
|
367
|
+
const values = ["--url", "--token"];
|
|
368
|
+
const folder = args.find((arg, index) => !arg.startsWith("--") && !values.includes(args[index - 1] ?? "")) ?? "seed";
|
|
369
|
+
const url = (option(args, "--url") ?? `http://localhost:${process.env.PORT ?? 4321}`).replace(/\/+$/, "");
|
|
370
|
+
const token = option(args, "--token") ?? process.env.BRIM_TOKEN;
|
|
371
|
+
const exporting = args.includes("--export");
|
|
372
|
+
|
|
373
|
+
const work = async (target: SeedTarget) => {
|
|
374
|
+
if (exporting) {
|
|
375
|
+
const done = await exportSeedFiles(folder, target, args.includes("--force"));
|
|
376
|
+
console.log(
|
|
377
|
+
`Wrote ${done.items} item(s) into ${done.files.map((file) => `${folder}/${file}`).join(", ") || "nothing"}, ` +
|
|
378
|
+
`with ${done.images} picture(s) in ${folder}/images/.`,
|
|
379
|
+
);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const dryRun = args.includes("--dry-run");
|
|
383
|
+
const result = await runSeed(folder, target, dryRun);
|
|
384
|
+
const label = (entry: { type: string; slug: string; locale: string }) =>
|
|
385
|
+
`${entry.type}/${entry.slug}${entry.locale ? ` (${entry.locale})` : ""}`;
|
|
386
|
+
if (result.create.length === 0) {
|
|
387
|
+
console.log(`Nothing to add: the site already has all ${result.existing.length} item(s) of ${folder}/.`);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
console.log(
|
|
391
|
+
`${dryRun ? "Would add" : "Added"} ${result.create.length} item(s)` +
|
|
392
|
+
(dryRun ? "" : `, with ${result.uploaded} picture(s)`) +
|
|
393
|
+
`; ${result.existing.length} already there, left as they are.`,
|
|
394
|
+
);
|
|
395
|
+
for (const entry of result.create) console.log(` + ${label(entry)} ${entry.title}`);
|
|
396
|
+
if (dryRun) console.log("Nothing was written. Run it without --dry-run to add them.");
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
await asSiteOwner(args, url, token, "seed", work);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Runs `work` signed in to the site at `url`: with the token given, or, for a site on this computer, with an access
|
|
404
|
+
* token made for its first owner in the local database and deleted afterwards. Refuses a site elsewhere without a
|
|
405
|
+
* token, and a database set up to be elsewhere without --remote, so a command never reaches the live site by accident.
|
|
406
|
+
*/
|
|
407
|
+
async function asSiteOwner(
|
|
408
|
+
args: string[],
|
|
409
|
+
url: string,
|
|
410
|
+
token: string | undefined,
|
|
411
|
+
command: string,
|
|
412
|
+
work: (target: SeedTarget) => Promise<void>,
|
|
413
|
+
): Promise<void> {
|
|
414
|
+
if (token) return work({ url, token });
|
|
415
|
+
if (!isLocalSite(url)) {
|
|
416
|
+
throw new Error(
|
|
417
|
+
`${url} is not on this computer: give a token for it with --token (npx brim token add <email> on that site).`,
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
const database = resolveDatabase({}, process.cwd());
|
|
421
|
+
if (!database.databaseUrl.startsWith("file:") && !args.includes("--remote")) {
|
|
422
|
+
throw new Error(
|
|
423
|
+
"The database set up here (BRIM_DATABASE_URL) is not on this computer, so it may be the live site's. " +
|
|
424
|
+
`Add --remote if you mean brim ${command} to use it.`,
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
await withSite(async ({ accounts }) => {
|
|
428
|
+
const owner = (await accounts.listUsers()).find((user) => user.role === "owner");
|
|
429
|
+
if (!owner)
|
|
430
|
+
throw new Error(`The site has no owner yet: npx brim user add you@example.com, then npx brim ${command}.`);
|
|
431
|
+
const name = `brim ${command} ${Date.now()}`;
|
|
432
|
+
const created = await accounts.createToken(owner.email, name);
|
|
433
|
+
try {
|
|
434
|
+
await work({ url, token: created });
|
|
435
|
+
} finally {
|
|
436
|
+
await accounts.revokeToken(owner.email, name);
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** Hands an address to the system's browser; quietly does nothing where there is none (a server, a container). */
|
|
442
|
+
function openInBrowser(address: string): void {
|
|
443
|
+
const [command, ...args] =
|
|
444
|
+
process.platform === "darwin"
|
|
445
|
+
? ["open", address]
|
|
446
|
+
: process.platform === "win32"
|
|
447
|
+
? ["cmd", "/c", "start", "", address]
|
|
448
|
+
: ["xdg-open", address];
|
|
449
|
+
try {
|
|
450
|
+
spawn(command!, args, { stdio: "ignore", detached: true })
|
|
451
|
+
.on("error", () => undefined)
|
|
452
|
+
.unref();
|
|
453
|
+
} catch {
|
|
454
|
+
// No browser here: the address was printed.
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** Opens the database of the site in the current folder for the length of one command. */
|
|
459
|
+
async function withSite(run: (brim: Brim) => Promise<void>): Promise<void> {
|
|
460
|
+
const database = resolveDatabase({}, process.cwd());
|
|
461
|
+
const brim = await connect(database.databaseUrl, database.authToken);
|
|
462
|
+
try {
|
|
463
|
+
await run(brim);
|
|
464
|
+
} finally {
|
|
465
|
+
brim.db.close();
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function option(args: string[], name: string): string | undefined {
|
|
470
|
+
const index = args.indexOf(name);
|
|
471
|
+
return index === -1 ? undefined : args[index + 1];
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
async function readPassword(): Promise<string> {
|
|
475
|
+
if (!process.stdin.isTTY) {
|
|
476
|
+
let piped = "";
|
|
477
|
+
for await (const chunk of process.stdin) piped += chunk;
|
|
478
|
+
return piped.replace(/\r?\n$/, "");
|
|
479
|
+
}
|
|
480
|
+
// Typed characters are not echoed back to the terminal.
|
|
481
|
+
const silent = new Writable({ write: (_chunk, _encoding, done) => done() });
|
|
482
|
+
const prompt = createInterface({ input: process.stdin, output: silent, terminal: true });
|
|
483
|
+
process.stdout.write("Password: ");
|
|
484
|
+
const password = await prompt.question(""); // betterleaks:allow
|
|
485
|
+
prompt.close();
|
|
486
|
+
process.stdout.write("\n");
|
|
487
|
+
return password;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
main(process.argv.slice(2)).catch((error: unknown) => {
|
|
491
|
+
console.error(error instanceof Error ? error.message : error);
|
|
492
|
+
process.exitCode = 1;
|
|
493
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
---
|
|
2
|
+
import Accordion from "../blocks/accordion/Accordion.astro";
|
|
3
|
+
import Button from "../blocks/button/Button.astro";
|
|
4
|
+
import Carousel from "../blocks/carousel/Carousel.astro";
|
|
5
|
+
import Divider from "../blocks/divider/Divider.astro";
|
|
6
|
+
import ImageGallery from "../blocks/gallery/ImageGallery.astro";
|
|
7
|
+
import Image from "../blocks/image/Image.astro";
|
|
8
|
+
import Map from "../blocks/map/Map.astro";
|
|
9
|
+
import Video from "../blocks/video/Video.astro";
|
|
10
|
+
import { blockSizes } from "../content/block-layout.ts";
|
|
11
|
+
import type { BlockType } from "../content/content-types.ts";
|
|
12
|
+
import "./blocks.css";
|
|
13
|
+
import type { Item } from "../content/items.ts";
|
|
14
|
+
import RichText from "./RichText.astro";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The blocks the owner stacked in a `blocks` field of an item (text, images, galleries, videos…), in their order.
|
|
18
|
+
* On the item's page the owner adds, moves, changes and removes them, and chooses how wide each is and to which side
|
|
19
|
+
* (`.brim-block`, `data-width`, `data-align`; the site sets the widths, see blocks.css).
|
|
20
|
+
*/
|
|
21
|
+
interface Props {
|
|
22
|
+
nodeId: string;
|
|
23
|
+
item: Item;
|
|
24
|
+
field: string;
|
|
25
|
+
[attribute: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const COMPONENTS: Record<BlockType, (props: Record<string, unknown>) => unknown> = {
|
|
29
|
+
richtext: RichText,
|
|
30
|
+
image: Image,
|
|
31
|
+
"image-gallery": ImageGallery,
|
|
32
|
+
carousel: Carousel,
|
|
33
|
+
video: Video,
|
|
34
|
+
button: Button,
|
|
35
|
+
accordion: Accordion,
|
|
36
|
+
divider: Divider,
|
|
37
|
+
map: Map,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// `class` is given once, through class:list: spread with the other attributes it would be written twice.
|
|
41
|
+
const { nodeId, item, field, class: className, ...attributes } = Astro.props;
|
|
42
|
+
const blocks = Astro.locals.brim.blocks(item, field);
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
<div
|
|
46
|
+
{...attributes}
|
|
47
|
+
class:list={["brim-blocks", className as string | undefined]}
|
|
48
|
+
{...Astro.locals.brim.marker("blocks", nodeId, "page", undefined, { item, field })}
|
|
49
|
+
>
|
|
50
|
+
{blocks.map(({ nodeId: id, kind, scope, layout }) => {
|
|
51
|
+
// A block the field no longer knows is left out.
|
|
52
|
+
const Block = COMPONENTS[kind as BlockType];
|
|
53
|
+
// Pictures are downloaded at the width their block is drawn at.
|
|
54
|
+
const sized = kind === "image" || kind === "image-gallery" || kind === "carousel";
|
|
55
|
+
return (
|
|
56
|
+
Block && (
|
|
57
|
+
<div
|
|
58
|
+
class="brim-block"
|
|
59
|
+
data-width={layout.width}
|
|
60
|
+
data-align={layout.align}
|
|
61
|
+
data-space={layout.space === "normal" ? undefined : layout.space}
|
|
62
|
+
>
|
|
63
|
+
<Block nodeId={id} scope={scope} {...(sized && { sizes: blockSizes(layout.width) })} />
|
|
64
|
+
</div>
|
|
65
|
+
)
|
|
66
|
+
);
|
|
67
|
+
})}
|
|
68
|
+
</div>
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
---
|
|
2
|
+
import config from "virtual:brim/config";
|
|
3
|
+
import { FIELD_KIND, type ItemBinding, METADATA_FIELDS, fieldText } from "../content/item-fields.ts";
|
|
4
|
+
import type { Item } from "../content/items.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One of an item's picked values on its page: a date, a number, a choice, yes or no, a link or an email. The owner
|
|
8
|
+
* clicks it and sets it in the side panel with the same widget as in the item form; the page shows the new value.
|
|
9
|
+
*
|
|
10
|
+
* By default a date and a number are written in the item's language, a yes as the field's label, and a link or an
|
|
11
|
+
* email is a link. Children that are a function write it themselves: `{(value, text) => …}`, `value` as saved
|
|
12
|
+
* (`undefined` when empty) and `text` as it would be written by default.
|
|
13
|
+
*/
|
|
14
|
+
interface Props {
|
|
15
|
+
nodeId: string;
|
|
16
|
+
item: Item;
|
|
17
|
+
field: string;
|
|
18
|
+
/** The element; by default `time` for a date, `a` for a link or an email that is set, else `span`. */
|
|
19
|
+
as?: string;
|
|
20
|
+
[attribute: string]: unknown;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const { nodeId, item, field, as, ...attributes } = Astro.props;
|
|
24
|
+
const declared = config.contentTypes
|
|
25
|
+
.find((type) => type.key === item.type)
|
|
26
|
+
?.fields.find((candidate) => candidate.name === field);
|
|
27
|
+
if (!declared || !METADATA_FIELDS.includes(declared.type)) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`<Field nodeId="${nodeId}"> shows a ${METADATA_FIELDS.join(", ")} field, and "${item.type}" has no such field "${field}". ` +
|
|
30
|
+
"Text, rich text and images have <Text>, <RichText> and <Image>.",
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const binding: ItemBinding = { item, field };
|
|
35
|
+
const value = item.data[field];
|
|
36
|
+
const locale = item.locale || config.locales?.defaultLocale || Astro.currentLocale;
|
|
37
|
+
const text = fieldText(declared, value, locale);
|
|
38
|
+
// What the site's code writes is markup; the default text is the owner's words, escaped like any text.
|
|
39
|
+
const custom = Astro.slots.has("default") ? await Astro.slots.render("default", [value, text]) : undefined;
|
|
40
|
+
|
|
41
|
+
const href =
|
|
42
|
+
typeof value === "string" && value !== ""
|
|
43
|
+
? declared.type === "url"
|
|
44
|
+
? value
|
|
45
|
+
: declared.type === "email"
|
|
46
|
+
? `mailto:${value}`
|
|
47
|
+
: undefined
|
|
48
|
+
: undefined;
|
|
49
|
+
const Tag = as ?? (declared.type === "date" ? "time" : href ? "a" : "span");
|
|
50
|
+
const { editing } = Astro.locals.brim;
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
<Tag
|
|
54
|
+
{...(Tag === "time" && typeof value === "string" && value !== "" && { datetime: value })}
|
|
55
|
+
{...(Tag === "a" && href && { href })}
|
|
56
|
+
{...attributes}
|
|
57
|
+
{...Astro.locals.brim.marker(FIELD_KIND, nodeId, "page", value === undefined ? undefined : { value }, binding)}
|
|
58
|
+
data-brim-label={editing ? declared.label : undefined}
|
|
59
|
+
>
|
|
60
|
+
{custom === undefined ? text : <Fragment set:html={custom} />}
|
|
61
|
+
</Tag>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
---
|
|
2
|
+
import config from "virtual:brim/config";
|
|
3
|
+
import type { FooterGroup, FooterValue, ImageValue } from "../content/types.ts";
|
|
4
|
+
import { expandFooter, navSources } from "../content/menus.ts";
|
|
5
|
+
import { itemLinks } from "../runtime/nav.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Holds the footer's brand and link groups, nothing else. The markup is yours:
|
|
9
|
+
*
|
|
10
|
+
* <Footer nodeId="footer" logoText="Acme" groups={[{ title: "Company", items: [...] }]}>
|
|
11
|
+
* {({ logoText, groups }) => <div>…</div>}
|
|
12
|
+
* </Footer>
|
|
13
|
+
*
|
|
14
|
+
* A group with `from: "<content type>"` lists that type's items (`{ title: "Categories", items: [], from:
|
|
15
|
+
* "category" }`), in the owner's order when the type is `ordered`.
|
|
16
|
+
*/
|
|
17
|
+
interface Props {
|
|
18
|
+
nodeId: string;
|
|
19
|
+
logo?: ImageValue;
|
|
20
|
+
logoText?: string;
|
|
21
|
+
groups: FooterGroup[];
|
|
22
|
+
as?: "footer" | "div";
|
|
23
|
+
[attribute: string]: unknown;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const { nodeId, logo, logoText, groups, as: Tag = "footer", ...attributes } = Astro.props;
|
|
27
|
+
const source: FooterValue = { groups, ...(logo && { logo }), ...(logoText !== undefined && { logoText }) };
|
|
28
|
+
const stored = Astro.locals.brim.read("footer", nodeId, "site") ?? source;
|
|
29
|
+
const footer = await expandFooter(stored, itemLinks);
|
|
30
|
+
const content = await Astro.slots.render("default", [footer]);
|
|
31
|
+
const sources = Astro.locals.brim.editing ? { "data-brim-nav-sources": navSources(config.contentTypes) } : {};
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
<Tag {...attributes} {...Astro.locals.brim.marker("footer", nodeId, "site", stored)} {...sources}>
|
|
35
|
+
<Fragment set:html={content} />
|
|
36
|
+
</Tag>
|