@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,115 @@
|
|
|
1
|
+
import type { Database } from "../db/database.ts";
|
|
2
|
+
import type { Item, ItemDraft } from "./items.ts";
|
|
3
|
+
import type { SeoSettings, StoredNode } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What a draft holds back from the live site: an element of a page, an item's content, a page's SEO settings.
|
|
7
|
+
* The key is the element's `scope\nnodeId`, the item's id, or the page's path.
|
|
8
|
+
*/
|
|
9
|
+
export type DraftTarget = "node" | "item" | "seo";
|
|
10
|
+
|
|
11
|
+
/** An element's draft: a new value, or `reset` to fall back to what the site's code says once saved. */
|
|
12
|
+
export type NodeDraft = { kind: string; value: unknown } | { reset: true };
|
|
13
|
+
/** An item's draft: its content. Who can see the item is never drafted; it changes at once. */
|
|
14
|
+
export type ItemContentDraft = Omit<ItemDraft, "visibility">;
|
|
15
|
+
|
|
16
|
+
export interface DraftRow {
|
|
17
|
+
target: DraftTarget;
|
|
18
|
+
key: string;
|
|
19
|
+
value: unknown;
|
|
20
|
+
/** The page it was made on, so the editor can say where the unsaved changes are. */
|
|
21
|
+
page: string;
|
|
22
|
+
updatedAt: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Changes made in the editor that visitors do not see yet. The editor's preview draws the site with them; saving
|
|
27
|
+
* writes them to the content visitors see, discarding throws them away. One draft for the whole site, shared by
|
|
28
|
+
* everyone who edits it.
|
|
29
|
+
*/
|
|
30
|
+
export class DraftStore {
|
|
31
|
+
readonly #db: Database;
|
|
32
|
+
|
|
33
|
+
constructor(db: Database) {
|
|
34
|
+
this.#db = db;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async all(): Promise<DraftRow[]> {
|
|
38
|
+
const rows = await this.#db.query("SELECT * FROM brim_drafts ORDER BY updated_at");
|
|
39
|
+
return rows.map((row) => ({
|
|
40
|
+
target: String(row.target) as DraftTarget,
|
|
41
|
+
key: String(row.key),
|
|
42
|
+
value: JSON.parse(String(row.value)) as unknown,
|
|
43
|
+
page: String(row.page),
|
|
44
|
+
updatedAt: String(row.updated_at),
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async count(): Promise<number> {
|
|
49
|
+
return Number((await this.#db.query("SELECT COUNT(*) AS n FROM brim_drafts"))[0]?.n ?? 0);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The draft of one thing; `undefined` when it has none, so the live content stands. */
|
|
53
|
+
async get(target: DraftTarget, key: string): Promise<unknown> {
|
|
54
|
+
const rows = await this.#db.query("SELECT value FROM brim_drafts WHERE target = ? AND key = ?", [target, key]);
|
|
55
|
+
return rows[0] ? (JSON.parse(String(rows[0].value)) as unknown) : undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** `undefined` removes the draft. The value must already be validated. */
|
|
59
|
+
async set(target: DraftTarget, key: string, value: unknown, page: string): Promise<void> {
|
|
60
|
+
if (value === undefined) {
|
|
61
|
+
await this.#db.query("DELETE FROM brim_drafts WHERE target = ? AND key = ?", [target, key]);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
await this.#db.query(
|
|
65
|
+
`INSERT INTO brim_drafts (target, key, value, page, updated_at) VALUES (?, ?, ?, ?, ?)
|
|
66
|
+
ON CONFLICT (target, key) DO UPDATE SET value = excluded.value, page = excluded.page,
|
|
67
|
+
updated_at = excluded.updated_at`,
|
|
68
|
+
[target, key, JSON.stringify(value), page, new Date().toISOString()],
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async clear(): Promise<void> {
|
|
73
|
+
await this.#db.query("DELETE FROM brim_drafts");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The elements of these scopes as the preview shows them: saved values with the drafts over them. */
|
|
77
|
+
async overlayNodes(nodes: StoredNode[], scopes: string[]): Promise<StoredNode[]> {
|
|
78
|
+
const drafts = (await this.#rows("node")).filter((row) => scopes.includes(row.key.split("\n")[0] ?? ""));
|
|
79
|
+
if (drafts.length === 0) return nodes;
|
|
80
|
+
const byKey = new Map(nodes.map((node) => [`${node.scope}\n${node.nodeId}`, node]));
|
|
81
|
+
for (const { key, value } of drafts) {
|
|
82
|
+
const [scope = "", nodeId = ""] = key.split("\n");
|
|
83
|
+
const draft = value as NodeDraft;
|
|
84
|
+
if ("reset" in draft) byKey.delete(key);
|
|
85
|
+
else byKey.set(key, { scope, nodeId, kind: draft.kind, value: draft.value });
|
|
86
|
+
}
|
|
87
|
+
return [...byKey.values()];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Items as the preview shows them: their drafted content over what is saved. */
|
|
91
|
+
async overlayItems<T extends Item>(items: T[]): Promise<T[]> {
|
|
92
|
+
if (items.length === 0) return items;
|
|
93
|
+
const drafts = new Map((await this.#rows("item")).map((row) => [row.key, row.value as ItemContentDraft]));
|
|
94
|
+
if (drafts.size === 0) return items;
|
|
95
|
+
return items.map((item) => {
|
|
96
|
+
const draft = drafts.get(item.id);
|
|
97
|
+
return draft ? { ...item, slug: draft.slug, title: draft.title, data: draft.data } : item;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async overlayItem<T extends Item>(item: T): Promise<T> {
|
|
102
|
+
return (await this.overlayItems([item]))[0] ?? item;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A page's SEO settings as the preview shows them. */
|
|
106
|
+
async overlaySeo(path: string, seo: SeoSettings): Promise<SeoSettings> {
|
|
107
|
+
const draft = await this.get("seo", path);
|
|
108
|
+
return draft === undefined ? seo : (draft as SeoSettings);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async #rows(target: DraftTarget): Promise<{ key: string; value: unknown }[]> {
|
|
112
|
+
const rows = await this.#db.query("SELECT key, value FROM brim_drafts WHERE target = ?", [target]);
|
|
113
|
+
return rows.map((row) => ({ key: String(row.key), value: JSON.parse(String(row.value)) as unknown }));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { type LocaleConfig, itemPagePath, storedLocale } from "../server/locales.ts";
|
|
2
|
+
import { type ContentType, type FeedOptions, feedOptionsOf, standardField } from "./content-types.ts";
|
|
3
|
+
import { itemAuthors } from "./item-head.ts";
|
|
4
|
+
import { itemImage } from "./item-image.ts";
|
|
5
|
+
import type { Item, ItemStore } from "./items.ts";
|
|
6
|
+
import { type SiteRoutes, escapeXml, itemHasPage } from "./sitemap.ts";
|
|
7
|
+
import type { ContentStore } from "./store.ts";
|
|
8
|
+
import type { InlineNode, RichTextValue } from "./types.ts";
|
|
9
|
+
|
|
10
|
+
/** Where feeds live: `/rss/<slug>.xml`, and `/rss/<locale>/<slug>.xml` for a language other than the default. */
|
|
11
|
+
export const FEED_PREFIX = "/rss/";
|
|
12
|
+
const DEFAULT_LIMIT = 20;
|
|
13
|
+
const SUMMARY_LENGTH = 300;
|
|
14
|
+
|
|
15
|
+
/** One feed: a content type that has one, in one of the site's languages. */
|
|
16
|
+
export interface FeedAddress {
|
|
17
|
+
type: ContentType;
|
|
18
|
+
/** The language's code, or "" for the default one (and on a site with one language). */
|
|
19
|
+
locale: string;
|
|
20
|
+
/** `/rss/blog.xml`, `/rss/ro/blog.xml`. */
|
|
21
|
+
path: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface FeedItem {
|
|
25
|
+
title: string;
|
|
26
|
+
/** The item's page, from the site's root: `/blog/spring`. */
|
|
27
|
+
path: string;
|
|
28
|
+
/** Stays the same when the item's address changes, so readers do not show it again. */
|
|
29
|
+
id: string;
|
|
30
|
+
published: Date;
|
|
31
|
+
updated: Date;
|
|
32
|
+
summary?: string;
|
|
33
|
+
/** The item's own picture, as stored: an address on the site or a full one. */
|
|
34
|
+
image?: string;
|
|
35
|
+
/** The names of who wrote it. */
|
|
36
|
+
authors: string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface Feed {
|
|
40
|
+
title: string;
|
|
41
|
+
description?: string;
|
|
42
|
+
/** The page the feed is about: the type's listing page when the code has one at `/<slug>`, else the home page. */
|
|
43
|
+
homePath: string;
|
|
44
|
+
/** The feed's own address, from the site's root. */
|
|
45
|
+
path: string;
|
|
46
|
+
/** The feed's language code, on a site with languages. */
|
|
47
|
+
language?: string;
|
|
48
|
+
items: FeedItem[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The path of a type's feed in a language ("" or the default code for the default one). */
|
|
52
|
+
export function feedPath(type: ContentType, locale: string, locales: LocaleConfig | undefined): string {
|
|
53
|
+
const stored = storedLocale(locale, locales) ?? "";
|
|
54
|
+
return `${FEED_PREFIX}${stored ? `${stored}/` : ""}${type.slug}.xml`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The feed's title: the one its options give, else the type's label after the site's name. */
|
|
58
|
+
export function feedTitle(type: ContentType, siteName: string | undefined): string {
|
|
59
|
+
const chosen = feedOptionsOf(type)?.title;
|
|
60
|
+
return chosen ?? (siteName ? `${siteName} · ${type.label}` : type.label);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Every feed the site has: each type with `feed` set, in each of the site's languages. */
|
|
64
|
+
export function feedAddresses(contentTypes: ContentType[], locales: LocaleConfig | undefined): FeedAddress[] {
|
|
65
|
+
const languages = locales ? ["", ...locales.locales.filter((code) => code !== locales.defaultLocale)] : [""];
|
|
66
|
+
return contentTypes
|
|
67
|
+
.filter((type) => feedOptionsOf(type))
|
|
68
|
+
.flatMap((type) => languages.map((locale) => ({ type, locale, path: feedPath(type, locale, locales) })));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The feed at a path under `/rss/`, written without the prefix (`blog.xml`, `ro/blog.xml`); `undefined` if none. */
|
|
72
|
+
export function feedAt(
|
|
73
|
+
rest: string,
|
|
74
|
+
contentTypes: ContentType[],
|
|
75
|
+
locales: LocaleConfig | undefined,
|
|
76
|
+
): FeedAddress | undefined {
|
|
77
|
+
const path = `${FEED_PREFIX}${rest.replace(/^\/+/, "")}`;
|
|
78
|
+
return feedAddresses(contentTypes, locales).find((address) => address.path === path);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface Sources {
|
|
82
|
+
/** Every type of the site, for the authors an item points at. */
|
|
83
|
+
types: ContentType[];
|
|
84
|
+
content: ContentStore;
|
|
85
|
+
items: ItemStore;
|
|
86
|
+
routes: SiteRoutes;
|
|
87
|
+
locales?: LocaleConfig | undefined;
|
|
88
|
+
/** The site's name, put before the type's label in the feed's title. */
|
|
89
|
+
siteName?: string | undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A feed's newest published items, those with a page a visitor can open: an item that is private or has no page is
|
|
94
|
+
* left out. Read when asked, so an item just published is in the next feed.
|
|
95
|
+
*/
|
|
96
|
+
export async function buildFeed(
|
|
97
|
+
{ type, locale, path }: FeedAddress,
|
|
98
|
+
{ types, content, items, routes, locales, siteName }: Sources,
|
|
99
|
+
): Promise<Feed> {
|
|
100
|
+
const options: FeedOptions = feedOptionsOf(type) ?? {};
|
|
101
|
+
const listed = await items.list(type.key, {
|
|
102
|
+
publishedOnly: true,
|
|
103
|
+
sort: "newest",
|
|
104
|
+
...(locales && { locale }),
|
|
105
|
+
});
|
|
106
|
+
const dateField =
|
|
107
|
+
options.date ?? (standardField(type, "published") ?? type.fields.find((field) => field.type === "date"))?.name;
|
|
108
|
+
const publishedItems = (key: string) => items.listCached(key, { publishedOnly: true });
|
|
109
|
+
const candidates = listed
|
|
110
|
+
.map((item) => ({ item, path: itemPagePath(type, item, locales) }))
|
|
111
|
+
.filter(({ item, path }) => itemHasPage(type, item, path, routes))
|
|
112
|
+
.map((entry) => ({ ...entry, published: publishedAt(entry.item, dateField) }))
|
|
113
|
+
.sort((a, b) => b.published.getTime() - a.published.getTime())
|
|
114
|
+
.slice(0, options.limit ?? DEFAULT_LIMIT);
|
|
115
|
+
|
|
116
|
+
const feedItems = await Promise.all(
|
|
117
|
+
candidates.map(async ({ item, path, published }): Promise<FeedItem> => {
|
|
118
|
+
const summary = await summaryOf(type, item, options.summary, () => content.page(path));
|
|
119
|
+
const image = itemImage(type, item);
|
|
120
|
+
const authors = await itemAuthors(type, item, { types, publishedItems, locales });
|
|
121
|
+
return {
|
|
122
|
+
title: item.title,
|
|
123
|
+
path,
|
|
124
|
+
id: item.id,
|
|
125
|
+
published,
|
|
126
|
+
updated: new Date(item.updatedAt),
|
|
127
|
+
...(summary && { summary }),
|
|
128
|
+
...(image && { image }),
|
|
129
|
+
authors: authors.map(({ name }) => name),
|
|
130
|
+
};
|
|
131
|
+
}),
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const language = locales ? locale || locales.defaultLocale : undefined;
|
|
135
|
+
const prefix = locales && (locale || locales.prefixDefaultLocale) ? `/${language}` : "";
|
|
136
|
+
const listing = `${prefix}/${type.slug}`;
|
|
137
|
+
return {
|
|
138
|
+
title: feedTitle(type, siteName),
|
|
139
|
+
...(options.description && { description: options.description }),
|
|
140
|
+
homePath: routes.pages.includes(listing) ? listing : `${prefix}/`,
|
|
141
|
+
path,
|
|
142
|
+
...(language && { language }),
|
|
143
|
+
items: feedItems,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The item's date field when it holds a date, else when it was created. */
|
|
148
|
+
function publishedAt(item: Item, field: string | undefined): Date {
|
|
149
|
+
const value = field ? item.data[field] : undefined;
|
|
150
|
+
if (typeof value === "string" && /^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
|
151
|
+
const date = new Date(`${value}T00:00:00Z`);
|
|
152
|
+
if (!Number.isNaN(date.getTime())) return date;
|
|
153
|
+
}
|
|
154
|
+
return new Date(item.createdAt);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function summaryOf(
|
|
158
|
+
type: ContentType,
|
|
159
|
+
item: Item,
|
|
160
|
+
chosen: string | undefined,
|
|
161
|
+
page: () => Promise<{ seo: { description?: string } }>,
|
|
162
|
+
): Promise<string | undefined> {
|
|
163
|
+
const read = (name: string | undefined): string | undefined => {
|
|
164
|
+
const value = name ? item.data[name] : undefined;
|
|
165
|
+
if (typeof value === "string") return value.trim() || undefined;
|
|
166
|
+
if (value && typeof value === "object" && Array.isArray((value as RichTextValue).blocks)) {
|
|
167
|
+
return shorten(plainText(value as RichTextValue));
|
|
168
|
+
}
|
|
169
|
+
return undefined;
|
|
170
|
+
};
|
|
171
|
+
if (chosen) return read(chosen);
|
|
172
|
+
const main = standardField(type, "description") ?? type.fields.find((field) => field.type === "textarea");
|
|
173
|
+
return (
|
|
174
|
+
read(main?.name) ??
|
|
175
|
+
((await page()).seo.description?.trim() || undefined) ??
|
|
176
|
+
read(type.fields.find((field) => field.type === "richtext")?.name)
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The words of rich text, a block per line. */
|
|
181
|
+
export function plainText(value: RichTextValue): string {
|
|
182
|
+
const inline = (nodes: InlineNode[]): string =>
|
|
183
|
+
nodes.map((node) => (typeof node === "string" ? node : node.tag === "br" ? " " : inline(node.children))).join("");
|
|
184
|
+
return value.blocks
|
|
185
|
+
.map((block) => ("items" in block ? block.items.map(inline).join(" ") : inline(block.children)))
|
|
186
|
+
.map((text) => text.trim())
|
|
187
|
+
.filter(Boolean)
|
|
188
|
+
.join("\n");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function shorten(text: string): string | undefined {
|
|
192
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
193
|
+
if (!flat) return undefined;
|
|
194
|
+
if (flat.length <= SUMMARY_LENGTH) return flat;
|
|
195
|
+
const cut = flat.slice(0, SUMMARY_LENGTH);
|
|
196
|
+
return `${cut.slice(0, cut.lastIndexOf(" ") > 0 ? cut.lastIndexOf(" ") : SUMMARY_LENGTH).trimEnd()}…`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** RSS 2.0 (rssboard.org), with the feed's own address as `atom:link` and pictures as `media:content`. */
|
|
200
|
+
export function rssXml(feed: Feed, site: string | URL): string {
|
|
201
|
+
const absolute = (address: string) => escapeXml(new URL(address, site).href);
|
|
202
|
+
const tag = (name: string, value: string) => `<${name}>${escapeXml(value)}</${name}>`;
|
|
203
|
+
const newest = feed.items.reduce<Date | undefined>(
|
|
204
|
+
(latest, item) => (!latest || item.updated > latest ? item.updated : latest),
|
|
205
|
+
undefined,
|
|
206
|
+
);
|
|
207
|
+
const items = feed.items.map((item) =>
|
|
208
|
+
[
|
|
209
|
+
" <item>",
|
|
210
|
+
` ${tag("title", item.title)}`,
|
|
211
|
+
` <link>${absolute(item.path)}</link>`,
|
|
212
|
+
` <guid isPermaLink="false">${escapeXml(item.id)}</guid>`,
|
|
213
|
+
` <pubDate>${item.published.toUTCString()}</pubDate>`,
|
|
214
|
+
...item.authors.map((author) => ` ${tag("dc:creator", author)}`),
|
|
215
|
+
...(item.summary ? [` ${tag("description", item.summary)}`] : []),
|
|
216
|
+
...(item.image ? [` <media:content url="${absolute(item.image)}" medium="image" />`] : []),
|
|
217
|
+
" </item>",
|
|
218
|
+
].join("\n"),
|
|
219
|
+
);
|
|
220
|
+
return [
|
|
221
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
222
|
+
'<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:dc="http://purl.org/dc/elements/1.1/">',
|
|
223
|
+
" <channel>",
|
|
224
|
+
` ${tag("title", feed.title)}`,
|
|
225
|
+
` <link>${absolute(feed.homePath)}</link>`,
|
|
226
|
+
// RSS needs a description; a feed without its own says what it is.
|
|
227
|
+
` ${tag("description", feed.description ?? feed.title)}`,
|
|
228
|
+
` <atom:link href="${absolute(feed.path)}" rel="self" type="application/rss+xml" />`,
|
|
229
|
+
...(feed.language ? [` ${tag("language", feed.language)}`] : []),
|
|
230
|
+
...(newest ? [` <lastBuildDate>${newest.toUTCString()}</lastBuildDate>`] : []),
|
|
231
|
+
" <generator>Brim.js</generator>",
|
|
232
|
+
...items,
|
|
233
|
+
" </channel>",
|
|
234
|
+
"</rss>",
|
|
235
|
+
"",
|
|
236
|
+
].join("\n");
|
|
237
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The TypeScript types of items, inferred from the fields a content type declares, so code reads `item.data.price`
|
|
3
|
+
* as a number and a misspelt field fails at build time instead of showing nothing on the site:
|
|
4
|
+
*
|
|
5
|
+
* export const product = defineContentType({ key: "product", …, fields: [
|
|
6
|
+
* { name: "price", label: "Price", type: "number", required: true },
|
|
7
|
+
* { name: "category", label: "Category", vocabulary: "category" },
|
|
8
|
+
* ] });
|
|
9
|
+
* type Product = ItemOf<typeof product>; // data: { price: number; category?: string; … }
|
|
10
|
+
* const products = await getItems(product); // Product[]
|
|
11
|
+
*
|
|
12
|
+
* Types only: nothing here runs, so the editor and the server share it.
|
|
13
|
+
*/
|
|
14
|
+
import type { ImageValue, RichTextValue } from "./types.ts";
|
|
15
|
+
|
|
16
|
+
/** One row of a `keyvalue` field. */
|
|
17
|
+
export interface KeyValueRow {
|
|
18
|
+
label: string;
|
|
19
|
+
value: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** One block of a `blocks` field. */
|
|
23
|
+
export interface BlockRow {
|
|
24
|
+
id: string;
|
|
25
|
+
type: string;
|
|
26
|
+
value: Record<string, unknown>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The shape of a field as declared, as far as its type goes. */
|
|
30
|
+
interface DeclaredField {
|
|
31
|
+
readonly name: string;
|
|
32
|
+
readonly type?: string;
|
|
33
|
+
readonly required?: boolean;
|
|
34
|
+
readonly multiple?: boolean;
|
|
35
|
+
readonly vocabulary?: string;
|
|
36
|
+
readonly options?: readonly unknown[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** What a select field saves: the values of its choices, plain strings or `{ value, label }`. */
|
|
40
|
+
type ChoiceValue<O> = O extends string ? O : O extends { readonly value: infer V extends string } ? V : string;
|
|
41
|
+
|
|
42
|
+
/** The value a field holds, from its type. */
|
|
43
|
+
export type FieldValue<F extends DeclaredField> = F extends { readonly vocabulary: string }
|
|
44
|
+
? F extends { readonly multiple: true }
|
|
45
|
+
? string[]
|
|
46
|
+
: string
|
|
47
|
+
: F extends { readonly type: "reference" }
|
|
48
|
+
? F extends { readonly multiple: true }
|
|
49
|
+
? string[]
|
|
50
|
+
: string
|
|
51
|
+
: F extends { readonly type: "text" | "textarea" | "email" | "url" | "date" }
|
|
52
|
+
? string
|
|
53
|
+
: F extends { readonly type: "number" }
|
|
54
|
+
? number
|
|
55
|
+
: F extends { readonly type: "boolean" }
|
|
56
|
+
? boolean
|
|
57
|
+
: F extends { readonly type: "select"; readonly options: readonly (infer O)[] }
|
|
58
|
+
? ChoiceValue<O>
|
|
59
|
+
: F extends { readonly type: "tags" }
|
|
60
|
+
? string[]
|
|
61
|
+
: F extends { readonly type: "keyvalue" }
|
|
62
|
+
? KeyValueRow[]
|
|
63
|
+
: F extends { readonly type: "image" }
|
|
64
|
+
? ImageValue
|
|
65
|
+
: F extends { readonly type: "gallery" }
|
|
66
|
+
? ImageValue[]
|
|
67
|
+
: F extends { readonly type: "richtext" }
|
|
68
|
+
? RichTextValue
|
|
69
|
+
: F extends { readonly type: "blocks" }
|
|
70
|
+
? BlockRow[]
|
|
71
|
+
: unknown;
|
|
72
|
+
|
|
73
|
+
type Simplify<T> = { [K in keyof T]: T[K] } & {};
|
|
74
|
+
|
|
75
|
+
/** The fields Brim adds to a type, see `defineContentType`. */
|
|
76
|
+
interface AddedFields {
|
|
77
|
+
description?: string;
|
|
78
|
+
image?: ImageValue;
|
|
79
|
+
published?: string;
|
|
80
|
+
author?: string;
|
|
81
|
+
tags?: string[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* An item's `data` for these fields: required ones always there, the others optional. The fields Brim adds unless a
|
|
86
|
+
* type leaves them out (`description`, `image`, `published`, `author`, `tags`) are typed here as optional; one the
|
|
87
|
+
* type declares itself has the type it declares.
|
|
88
|
+
*/
|
|
89
|
+
export type FieldsData<Fields extends readonly DeclaredField[]> = Simplify<
|
|
90
|
+
{ [F in Fields[number] as F extends { readonly required: true } ? F["name"] : never]: FieldValue<F> } & {
|
|
91
|
+
[F in Fields[number] as F extends { readonly required: true } ? never : F["name"]]?: FieldValue<F>;
|
|
92
|
+
} & Omit<AddedFields, Fields[number]["name"]>
|
|
93
|
+
>;
|
|
94
|
+
|
|
95
|
+
/** The site settings' values for these fields: each one there unless empty, when it is `null` or missing. */
|
|
96
|
+
export type SettingsData<Fields extends readonly DeclaredField[]> = Simplify<{
|
|
97
|
+
[F in Fields[number] as F["name"]]?: FieldValue<F> | null;
|
|
98
|
+
}>;
|
|
99
|
+
|
|
100
|
+
/** A content type that knows the type of its items' data, as `defineContentType` returns it. */
|
|
101
|
+
export interface Typed<Data> {
|
|
102
|
+
/** Never set: it carries the type of the items' data for `ItemOf`, `getItems` and `ItemPageProps`. */
|
|
103
|
+
readonly __data?: Data;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The data of a typed content type's items, or an open record for one declared without its fields' types. */
|
|
107
|
+
export type DataOf<T> = T extends Typed<infer Data> ? (unknown extends Data ? Record<string, unknown> : Data) : Record<string, unknown>;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { Database, Row } from "../db/database.ts";
|
|
3
|
+
|
|
4
|
+
/** What was changed: an element of a page, an item, or a page added from a template. */
|
|
5
|
+
export type HistoryTarget = "node" | "item" | "page";
|
|
6
|
+
/** `reset` is an element put back to what the site's code says. */
|
|
7
|
+
export type HistoryAction = "changed" | "reset" | "deleted";
|
|
8
|
+
|
|
9
|
+
/** A change as the editor lists it. */
|
|
10
|
+
export interface HistoryEntry {
|
|
11
|
+
id: string;
|
|
12
|
+
target: HistoryTarget;
|
|
13
|
+
action: HistoryAction;
|
|
14
|
+
/** The page it was made on, "" when it was made outside a page (an item's form). */
|
|
15
|
+
page: string;
|
|
16
|
+
/** What changed, in words: an element's nodeId, an item's or a page's title. */
|
|
17
|
+
title: string;
|
|
18
|
+
/** Email of who made the change. */
|
|
19
|
+
user: string;
|
|
20
|
+
at: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A change with what is needed to take it back. */
|
|
24
|
+
export interface HistoryRecord extends HistoryEntry {
|
|
25
|
+
key: string;
|
|
26
|
+
/** The content as it was before the change; `null` when there was none (the page showed the code's default). */
|
|
27
|
+
before: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface NewHistory {
|
|
31
|
+
target: HistoryTarget;
|
|
32
|
+
key: string;
|
|
33
|
+
action: HistoryAction;
|
|
34
|
+
page: string;
|
|
35
|
+
title: string;
|
|
36
|
+
before: unknown;
|
|
37
|
+
user: string;
|
|
38
|
+
/** Kept even right after another change of the same thing by the same user (see `record`). */
|
|
39
|
+
always?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Changes of the same thing by the same person within this time count as one: typing saves many times. */
|
|
43
|
+
const SAME_EDIT_SECONDS = 10 * 60;
|
|
44
|
+
const KEEP_DAYS = 90;
|
|
45
|
+
const KEEP_ENTRIES = 5000;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* What the site's content was before each change, so a change can be taken back: an element's saved value, an
|
|
49
|
+
* item, a deleted page with what was written on it. Kept for 90 days, the newest 5000 changes at most.
|
|
50
|
+
*/
|
|
51
|
+
export class HistoryStore {
|
|
52
|
+
readonly #db: Database;
|
|
53
|
+
|
|
54
|
+
constructor(db: Database) {
|
|
55
|
+
this.#db = db;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Call before the change is made, with the content as it is now. */
|
|
59
|
+
async record(entry: NewHistory): Promise<void> {
|
|
60
|
+
const now = new Date();
|
|
61
|
+
if (!entry.always && entry.action === "changed") {
|
|
62
|
+
const rows = await this.#db.query(
|
|
63
|
+
"SELECT user_email, action, created_at FROM brim_history WHERE target = ? AND key = ? ORDER BY created_at DESC LIMIT 1",
|
|
64
|
+
[entry.target, entry.key],
|
|
65
|
+
);
|
|
66
|
+
const last = rows[0];
|
|
67
|
+
if (
|
|
68
|
+
last &&
|
|
69
|
+
last.action === "changed" &&
|
|
70
|
+
last.user_email === entry.user &&
|
|
71
|
+
now.getTime() - Date.parse(String(last.created_at)) < SAME_EDIT_SECONDS * 1000
|
|
72
|
+
) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
await this.#db.batch([
|
|
77
|
+
{
|
|
78
|
+
sql: `INSERT INTO brim_history (id, target, key, action, page, title, before, user_email, created_at)
|
|
79
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
80
|
+
args: [
|
|
81
|
+
randomUUID(),
|
|
82
|
+
entry.target,
|
|
83
|
+
entry.key,
|
|
84
|
+
entry.action,
|
|
85
|
+
entry.page,
|
|
86
|
+
entry.title,
|
|
87
|
+
entry.before === null || entry.before === undefined ? null : JSON.stringify(entry.before),
|
|
88
|
+
entry.user,
|
|
89
|
+
now.toISOString(),
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
sql: "DELETE FROM brim_history WHERE created_at < ?",
|
|
94
|
+
args: [new Date(now.getTime() - KEEP_DAYS * 24 * 60 * 60 * 1000).toISOString()],
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
sql: `DELETE FROM brim_history WHERE id IN (SELECT id FROM brim_history ORDER BY created_at DESC LIMIT -1 OFFSET ${KEEP_ENTRIES})`,
|
|
98
|
+
},
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Newest first; `page` narrows the list to the changes made on one page. */
|
|
103
|
+
async list(options: { page?: string | undefined; limit?: number | undefined } = {}): Promise<HistoryEntry[]> {
|
|
104
|
+
const limit = Math.min(Math.max(Math.trunc(options.limit ?? 100) || 100, 1), 500);
|
|
105
|
+
const rows =
|
|
106
|
+
options.page === undefined
|
|
107
|
+
? await this.#db.query(`${SELECT} ORDER BY created_at DESC, id LIMIT ${limit}`)
|
|
108
|
+
: await this.#db.query(`${SELECT} WHERE page = ? ORDER BY created_at DESC, id LIMIT ${limit}`, [options.page]);
|
|
109
|
+
return rows.map(toEntry);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async get(id: string): Promise<HistoryRecord | undefined> {
|
|
113
|
+
const rows = await this.#db.query("SELECT * FROM brim_history WHERE id = ?", [id]);
|
|
114
|
+
const row = rows[0];
|
|
115
|
+
if (!row) return undefined;
|
|
116
|
+
return {
|
|
117
|
+
...toEntry(row),
|
|
118
|
+
key: String(row.key),
|
|
119
|
+
before: row.before === null ? null : (JSON.parse(String(row.before)) as unknown),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async remove(id: string): Promise<void> {
|
|
124
|
+
await this.#db.query("DELETE FROM brim_history WHERE id = ?", [id]);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const SELECT = "SELECT id, target, action, page, title, user_email, created_at FROM brim_history";
|
|
129
|
+
|
|
130
|
+
function toEntry(row: Row): HistoryEntry {
|
|
131
|
+
return {
|
|
132
|
+
id: String(row.id),
|
|
133
|
+
target: String(row.target) as HistoryTarget,
|
|
134
|
+
action: String(row.action) as HistoryAction,
|
|
135
|
+
page: String(row.page),
|
|
136
|
+
title: String(row.title),
|
|
137
|
+
user: String(row.user_email),
|
|
138
|
+
at: String(row.created_at),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scope of nodes shared by every page (navbar, footer, anything marked `scope="site"`).
|
|
3
|
+
* A multilingual site keeps one set per language.
|
|
4
|
+
*/
|
|
5
|
+
export function siteScope(locale?: string): string {
|
|
6
|
+
return locale ? `site:${locale}` : "site";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const NODE_ID = /^[a-z][a-z0-9-]{0,63}$/;
|
|
10
|
+
|
|
11
|
+
export function isNodeId(value: string): boolean {
|
|
12
|
+
return NODE_ID.test(value);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function assertNodeId(value: string): void {
|
|
16
|
+
if (!isNodeId(value)) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`Invalid nodeId "${value}": use lowercase letters, digits and dashes, starting with a letter (max 64 characters).`,
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** `/about/`, `/about` and `/about/index.html` are the same page. */
|
|
24
|
+
export function normalizePagePath(pathname: string): string {
|
|
25
|
+
const withoutIndex = pathname.replace(/\/index\.html$/, "/");
|
|
26
|
+
const trimmed = withoutIndex.replace(/\/+$/, "");
|
|
27
|
+
return trimmed === "" ? "/" : trimmed;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The page a visitor's address asks for. Astro draws the page at `/about` for `/about.html` too (always where the
|
|
32
|
+
* route has a parameter, everywhere with `build.format: "file"`), so what is kept by address, such as whether the
|
|
33
|
+
* page is private, is asked by the page's own. A route with `.html` in its pattern is at the address it says.
|
|
34
|
+
*/
|
|
35
|
+
export function requestedPage(pathname: string, routePattern: string): string {
|
|
36
|
+
const path = normalizePagePath(pathname);
|
|
37
|
+
return routePattern.includes(".html") ? path : normalizePagePath(path.replace(/\.html$/, ""));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function pageScope(pathname: string): string {
|
|
41
|
+
return `page:${normalizePagePath(pathname)}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Scope of what the owner changed on a content type's template, shared by the pages of all its items.
|
|
46
|
+
* A multilingual site keeps one per language, like the site's own.
|
|
47
|
+
*/
|
|
48
|
+
export function typeScope(key: string, locale?: string): string {
|
|
49
|
+
return locale ? `type:${key}:${locale}` : `type:${key}`;
|
|
50
|
+
}
|