ferst-core 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/NOTICE +63 -31
  2. package/README.md +13 -4
  3. package/bin/ferst-media.mjs +96 -0
  4. package/bin/ferst-modules.mjs +17 -2
  5. package/components/BlockRenderer.astro +131 -129
  6. package/components/Card.astro +53 -51
  7. package/components/MembersAccount.astro +370 -0
  8. package/components/PostCard.astro +138 -136
  9. package/components/SiteHeader.astro +32 -14
  10. package/components/blocks/Grid.astro +49 -48
  11. package/components/blocks/Heading.astro +4 -1
  12. package/components/islands/BookMonth.tsx +95 -0
  13. package/components/islands/MembersAccount.tsx +392 -0
  14. package/components/recipes/Banner.astro +146 -146
  15. package/components/recipes/Book.astro +1147 -926
  16. package/components/recipes/FeatureCards.astro +112 -118
  17. package/components/recipes/Gallery.astro +57 -59
  18. package/components/recipes/Hero.astro +112 -10
  19. package/components/recipes/LatestPosts.astro +295 -292
  20. package/components/recipes/Locations.astro +146 -143
  21. package/components/recipes/Marquee.astro +75 -75
  22. package/components/recipes/MediaCards.astro +213 -219
  23. package/components/recipes/MembersContent.astro +176 -0
  24. package/components/recipes/Pricing.astro +149 -150
  25. package/components/recipes/Stats.astro +150 -157
  26. package/components/recipes/Tiles.astro +342 -296
  27. package/content/blocks.ts +1091 -1024
  28. package/content/cookies.ts +14 -0
  29. package/content/imageSlots.mjs +542 -0
  30. package/content/recipe-questions.ts +81 -80
  31. package/content/schemas.ts +13 -11
  32. package/examples/recipes.ts +5 -0
  33. package/fonts/OFL.txt +1332 -287
  34. package/fonts/anton-latin-400-normal.woff2 +0 -0
  35. package/fonts/bangers-latin-400-normal.woff2 +0 -0
  36. package/fonts/bebas-neue-latin-400-normal.woff2 +0 -0
  37. package/fonts/bowlby-one-latin-400-normal.woff2 +0 -0
  38. package/fonts/fredoka-latin-400-normal.woff2 +0 -0
  39. package/fonts/fredoka-latin-600-normal.woff2 +0 -0
  40. package/fonts/fredoka-latin-700-normal.woff2 +0 -0
  41. package/fonts/knewave-latin-400-normal.woff2 +0 -0
  42. package/fonts/lora-latin-400-normal.woff2 +0 -0
  43. package/fonts/lora-latin-600-normal.woff2 +0 -0
  44. package/fonts/lora-latin-700-normal.woff2 +0 -0
  45. package/fonts/merriweather-latin-400-normal.woff2 +0 -0
  46. package/fonts/merriweather-latin-600-normal.woff2 +0 -0
  47. package/fonts/merriweather-latin-700-normal.woff2 +0 -0
  48. package/fonts/nunito-latin-400-normal.woff2 +0 -0
  49. package/fonts/nunito-latin-600-normal.woff2 +0 -0
  50. package/fonts/nunito-latin-700-normal.woff2 +0 -0
  51. package/fonts/oswald-latin-400-normal.woff2 +0 -0
  52. package/fonts/oswald-latin-600-normal.woff2 +0 -0
  53. package/fonts/oswald-latin-700-normal.woff2 +0 -0
  54. package/fonts/rubik-dirt-latin-400-normal.woff2 +0 -0
  55. package/integration.mjs +319 -263
  56. package/layouts/Base.astro +8 -16
  57. package/lib/book.ts +416 -378
  58. package/lib/fonts.ts +130 -0
  59. package/lib/gridRows.ts +197 -0
  60. package/lib/image-fields.ts +140 -0
  61. package/lib/image-size.mjs +108 -73
  62. package/lib/image-sizes.ts +16 -0
  63. package/lib/logo-header.ts +46 -0
  64. package/lib/media-check.mjs +524 -0
  65. package/lib/members-account.ts +276 -0
  66. package/lib/members.ts +115 -0
  67. package/lib/theme-preview.ts +203 -140
  68. package/lib/themeTokens.ts +235 -220
  69. package/package.json +80 -71
  70. package/routes/account.astro +16 -0
  71. package/routes/cookie-policy.astro +28 -28
  72. package/styles/fonts.css +239 -76
  73. package/styles/theme.css +228 -160
  74. package/lib/gridColumns.ts +0 -22
@@ -0,0 +1,276 @@
1
+ /**
2
+ * The members' account page (`/account`): signing in with an emailed code, and the member's own
3
+ * details. The page is a React island (components/islands/MembersAccount.tsx); every decision it
4
+ * makes from the members service's answers is here, pure, so it's unit-tested.
5
+ *
6
+ * The service is the site's own `/ferst-api/members/…` (lib/members), same-origin, JSON. There is no
7
+ * password, ever: a code is sent to the visitor's email, and the same code signs them up when
8
+ * they're new and the organisation lets anyone join.
9
+ */
10
+ import { ACCOUNT_PATH, MEMBERS_API, MEMBERS_ME, type MembersAnswer } from './members';
11
+
12
+ export const MEMBERS_SIGN_IN = `${MEMBERS_API}/sign-in`;
13
+ export const MEMBERS_START = `${MEMBERS_API}/sign-in/start`;
14
+ export const MEMBERS_VERIFY = `${MEMBERS_API}/sign-in/verify`;
15
+ export const MEMBERS_SIGN_OUT = `${MEMBERS_API}/sign-out`;
16
+ export const MEMBERS_JOIN = `${MEMBERS_API}/join`;
17
+ export { MEMBERS_ME };
18
+
19
+ /** Who can join the organisation: anyone, only people it invites, or anyone it approves. */
20
+ export type Joining = 'open' | 'invite' | 'approval';
21
+ /** A member's standing. A guest booked without joining; pending waits for the organisation's yes. */
22
+ export type MemberStatus = 'active' | 'pending' | 'guest' | 'invited' | 'suspended' | 'removed';
23
+
24
+ export interface Member {
25
+ id: string;
26
+ email: string;
27
+ displayName: string | null;
28
+ status: MemberStatus;
29
+ }
30
+
31
+ /** What the sign-in card shows before anyone signs in. */
32
+ export interface SignInCard {
33
+ org: string;
34
+ joining: Joining;
35
+ termsUrl: string | null;
36
+ privacyUrl: string | null;
37
+ }
38
+
39
+ const JOININGS: readonly Joining[] = ['open', 'invite', 'approval'];
40
+ const STATUSES: readonly MemberStatus[] = ['active', 'pending', 'guest', 'invited', 'suspended', 'removed'];
41
+
42
+ const str = (v: unknown): string | null => (typeof v === 'string' && v.trim() ? v : null);
43
+ const joiningOf = (v: unknown): Joining => (JOININGS.includes(v as Joining) ? (v as Joining) : 'open');
44
+ /** Only a link to a web page is shown (the portal accepts https: links alone). */
45
+ const webLink = (v: unknown): string | null => {
46
+ const s = str(v);
47
+ return s && /^https:\/\//i.test(s) ? s : null;
48
+ };
49
+
50
+ /** A member from the service's answer, or null when the answer has none. */
51
+ export function readMember(v: unknown): Member | null {
52
+ if (!v || typeof v !== 'object') return null;
53
+ const m = v as Record<string, unknown>;
54
+ const email = str(m.email);
55
+ if (!email || !STATUSES.includes(m.status as MemberStatus)) return null;
56
+ return { id: str(m.id) ?? '', email, displayName: str(m.display_name)?.trim() ?? null, status: m.status as MemberStatus };
57
+ }
58
+
59
+ /** The organisation's name in an answer (`org: {name}`). */
60
+ const orgOf = (body: Record<string, unknown>): string => str((body.org as { name?: unknown } | undefined)?.name) ?? 'this organisation';
61
+
62
+ /** Where the page is when it can't talk to the service. */
63
+ type Unreachable = { kind: 'local' } | { kind: 'unavailable' };
64
+ /** No JSON answer at all: on the author's own computer the site has no /ferst-api; elsewhere it's down. */
65
+ const unreachable = (local: boolean): Unreachable => (local ? { kind: 'local' } : { kind: 'unavailable' });
66
+
67
+ /** The answer to `GET /members/me`: who is signed in, nobody, or the service can't say. */
68
+ export type MeView =
69
+ | { kind: 'member'; member: Member; org: string; joining: Joining }
70
+ | { kind: 'signed-out' }
71
+ | Unreachable;
72
+
73
+ export function readMe(answer: MembersAnswer | null, local = false): MeView {
74
+ if (!answer || !answer.body) return unreachable(local);
75
+ if (answer.status === 401) return { kind: 'signed-out' };
76
+ const member = answer.status === 200 ? readMember(answer.body.member) : null;
77
+ if (!member) return { kind: 'unavailable' };
78
+ return { kind: 'member', member, org: orgOf(answer.body), joining: joiningOf(answer.body.joining) };
79
+ }
80
+
81
+ /** The answer to `GET /members/sign-in`: the card, or the service can't say (`module_off` included). */
82
+ export type CardView = { kind: 'card'; card: SignInCard } | Unreachable;
83
+
84
+ export function readCard(answer: MembersAnswer | null, local = false): CardView {
85
+ if (!answer || !answer.body) return unreachable(local);
86
+ if (answer.status !== 200) return { kind: 'unavailable' };
87
+ const b = answer.body;
88
+ return { kind: 'card', card: { org: orgOf(b), joining: joiningOf(b.joining), termsUrl: webLink(b.terms_url), privacyUrl: webLink(b.privacy_url) } };
89
+ }
90
+
91
+ /** The organisation asks people who join to agree to its terms or privacy notice. */
92
+ export const hasTerms = (card: SignInCard | null): boolean => Boolean(card && (card.termsUrl || card.privacyUrl));
93
+
94
+ /** The answer to `POST /members/sign-in/start`: always 200 with a sign-in id, whoever asks. */
95
+ export function readStart(answer: MembersAnswer | null): string | null {
96
+ if (!answer || answer.status !== 200 || !answer.body) return null;
97
+ return str(answer.body.signin_id);
98
+ }
99
+
100
+ /** The answer to `POST /members/sign-in/verify`. */
101
+ export type VerifyOutcome =
102
+ | { kind: 'signed-in'; member: Member; welcome: string | null }
103
+ /** A sign-up confirmed, its code having been sent again: a sign-in code follows, same sign-in. */
104
+ | { kind: 'new-code' }
105
+ | { kind: 'wrong-code'; triesLeft: number | null }
106
+ | { kind: 'expired' }
107
+ | { kind: 'try-again' };
108
+
109
+ export function readVerify(answer: MembersAnswer | null): VerifyOutcome {
110
+ if (!answer || !answer.body) return { kind: 'try-again' };
111
+ const { status, body } = answer;
112
+ if (status === 200) {
113
+ const member = readMember(body.member);
114
+ return member ? { kind: 'signed-in', member, welcome: str(body.welcome) } : { kind: 'try-again' };
115
+ }
116
+ if (status === 409 && body.error === 'new_code_sent') return { kind: 'new-code' };
117
+ if (status === 400 && body.error === 'wrong_code') {
118
+ const left = typeof body.tries_left === 'number' && body.tries_left >= 0 ? Math.floor(body.tries_left) : null;
119
+ return { kind: 'wrong-code', triesLeft: left };
120
+ }
121
+ if (status === 410) return { kind: 'expired' };
122
+ return { kind: 'try-again' };
123
+ }
124
+
125
+ /** The answer to `POST /members/join` (a signed-in guest joins). */
126
+ export type JoinOutcome =
127
+ | { kind: 'joined'; member: Member; welcome: string | null }
128
+ | { kind: 'agree' }
129
+ | { kind: 'invite-only' }
130
+ | { kind: 'signed-out' }
131
+ | { kind: 'try-again' };
132
+
133
+ export function readJoin(answer: MembersAnswer | null): JoinOutcome {
134
+ if (!answer || !answer.body) return answer?.status === 401 ? { kind: 'signed-out' } : { kind: 'try-again' };
135
+ const { status, body } = answer;
136
+ if (status === 200) {
137
+ const member = readMember(body.member);
138
+ return member ? { kind: 'joined', member, welcome: str(body.welcome) } : { kind: 'try-again' };
139
+ }
140
+ if (status === 401) return { kind: 'signed-out' };
141
+ if (status === 400 && body.error === 'agree_required') return { kind: 'agree' };
142
+ if (status === 403 && body.error === 'not_for_you') return { kind: 'invite-only' };
143
+ return { kind: 'try-again' };
144
+ }
145
+
146
+ /** The answer to a change with no body to read (`DELETE /members/me`, `POST /members/sign-out`). */
147
+ export function readDone(answer: MembersAnswer | null): 'done' | 'signed-out' | 'try-again' {
148
+ if (!answer) return 'try-again';
149
+ if (answer.status === 204 || answer.status === 200) return 'done';
150
+ if (answer.status === 401) return 'signed-out';
151
+ return 'try-again';
152
+ }
153
+
154
+ /** The answer to `PATCH /members/me`. */
155
+ export function readRename(answer: MembersAnswer | null): { kind: 'saved'; member: Member } | { kind: 'signed-out' } | { kind: 'invalid' } | { kind: 'try-again' } {
156
+ if (!answer) return { kind: 'try-again' };
157
+ if (answer.status === 401) return { kind: 'signed-out' };
158
+ if (answer.status === 400) return { kind: 'invalid' };
159
+ const member = answer.status === 200 ? readMember(answer.body?.member) : null;
160
+ return member ? { kind: 'saved', member } : { kind: 'try-again' };
161
+ }
162
+
163
+ /**
164
+ * Where to go once signed in: `?next=` when it's a page of this same site, else nowhere (stay on the
165
+ * account page). Never another site (`//elsewhere`, `https://…`, `/\elsewhere`), never the account
166
+ * page itself, never the members service.
167
+ */
168
+ export function safeNext(next: string | null | undefined): string | null {
169
+ if (typeof next !== 'string' || !next.startsWith('/') || next.startsWith('//')) return null;
170
+ // Browsers read a backslash as a slash, and drop tabs and newlines: none of them belong here.
171
+ if (/[\\\u0000-\u001f\u007f]/.test(next)) return null;
172
+ const here = 'https://site.invalid';
173
+ let url: URL;
174
+ try {
175
+ url = new URL(next, here);
176
+ } catch {
177
+ return null;
178
+ }
179
+ if (url.origin !== here) return null;
180
+ const path = url.pathname.replace(/\/+$/, '') || '/';
181
+ if (path === ACCOUNT_PATH || path.startsWith(`${ACCOUNT_PATH}/`) || path.startsWith('/ferst-api')) return null;
182
+ return url.pathname + url.search + url.hash;
183
+ }
184
+
185
+ /** An email address worth sending a code to (the service decides the rest). */
186
+ export function looksLikeEmail(value: string): boolean {
187
+ const v = value.trim();
188
+ return v.length <= 254 && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v);
189
+ }
190
+
191
+ /** A code as typed (spaces and dashes dropped), or null when it can't be one: 4 to 8 digits. */
192
+ export function cleanCode(value: string): string | null {
193
+ const v = value.replace(/[\s-]/g, '');
194
+ return /^\d{4,8}$/.test(v) ? v : null;
195
+ }
196
+
197
+ /** A name a member can give themselves: 1 to 60 characters once trimmed. */
198
+ export function cleanName(value: string): string | null {
199
+ const v = value.trim();
200
+ return v.length >= 1 && v.length <= 60 ? v : null;
201
+ }
202
+
203
+ /** A guest may join unless the organisation only lets in people it invites. */
204
+ export const canJoin = (member: Member, joining: Joining): boolean => member.status === 'guest' && joining !== 'invite';
205
+
206
+ /** "Sam Hill (sam@example.com)", or the email alone when they've no name yet. */
207
+ export function signedInAs(member: Member): string {
208
+ return member.displayName ? `${member.displayName} (${member.email})` : member.email;
209
+ }
210
+
211
+ /** The words for the page, in one place: plain and warm, and never about the tools behind it. */
212
+ export const ACCOUNT_TEXT = {
213
+ title: 'Your account',
214
+ loading: 'Opening your account…',
215
+ noScript: 'Signing in needs JavaScript, which is switched off in this browser.',
216
+ local: 'Sign-in works on the published site.',
217
+ unavailable: 'Signing in isn’t available just now. Please try again later.',
218
+ signInTitle: (org: string, joining: Joining) => (joining === 'invite' ? `Sign in to ${org}` : `Sign in or join ${org}`),
219
+ signInIntro: (org: string, joining: Joining) => {
220
+ const base = 'Enter your email and we’ll send you a code to sign in. No password needed.';
221
+ if (joining === 'open') return `${base} New here? The same code makes you a member.`;
222
+ if (joining === 'approval') return `${base} New here? You can ask to join, and ${org} will let you in.`;
223
+ return `${base} Joining ${org} is by invitation.`;
224
+ },
225
+ email: 'Email',
226
+ emailInvalid: 'Enter your email address, like name@example.com.',
227
+ agree: (org: string, card: SignInCard) =>
228
+ `I agree to ${org}’s ${[card.termsUrl && 'terms', card.privacyUrl && 'privacy notice'].filter(Boolean).join(' and ')}`,
229
+ agreeNeeded: 'Please tick the box to agree first.',
230
+ continue: 'Continue',
231
+ codeTitle: 'Check your email',
232
+ codeSent: (email: string) => `We’ve sent a code to ${email}. Enter it here.`,
233
+ codeHint: 'It can take a minute to arrive. If it hasn’t, check your junk folder.',
234
+ code: 'Code',
235
+ codeInvalid: 'Enter the code from the email: numbers only.',
236
+ signIn: 'Sign in',
237
+ newCode: 'Send a new code',
238
+ newCodeSent: 'We’ve sent a new code.',
239
+ otherEmail: 'Use a different email',
240
+ wrongCode: (left: number | null) =>
241
+ left === null ? 'That code isn’t right.' : left > 0 ? `That code isn’t right. ${left} ${left === 1 ? 'try' : 'tries'} left.` : 'That code isn’t right, and there are no tries left. Send a new one.',
242
+ expired: 'That code has expired. Send a new one.',
243
+ newCodeToFinish: 'Thanks. We’ve sent you a new code to finish signing in.',
244
+ tryAgain: 'Signing in isn’t working just now. Please try again in a moment.',
245
+ signedInAs: (member: Member) => `Signed in as ${signedInAs(member)}`,
246
+ status: (member: Member, org: string, joining: Joining) => {
247
+ if (member.status === 'pending') return `Thanks for joining. ${org} will let you in shortly.`;
248
+ if (member.status === 'guest') {
249
+ return joining === 'invite' ? `You’ve booked with ${org}. Joining is by invitation.` : `You’ve booked with ${org}, but haven’t joined.`;
250
+ }
251
+ return `You’re a member of ${org}.`;
252
+ },
253
+ join: (org: string) => `Join ${org}`,
254
+ inviteOnly: 'Joining is by invitation only.',
255
+ back: 'Back to the page you were on',
256
+ nameTitle: 'Your name',
257
+ name: 'Your name',
258
+ nameHint: 'How the site greets you.',
259
+ nameInvalid: 'Your name needs 1 to 60 characters.',
260
+ saveName: 'Save name',
261
+ nameSaved: 'Saved.',
262
+ signOut: 'Sign out',
263
+ signedOut: 'You’ve signed out.',
264
+ signInAgain: 'Please sign in again.',
265
+ deleteTitle: 'Delete my account',
266
+ deleteOpen: 'Delete my account',
267
+ deleteWarning: (org: string) => `This removes your account with ${org}: you’re signed out, and your details are deleted. It can’t be undone.`,
268
+ deleteConfirm: 'Type “delete” to confirm',
269
+ deleteGo: 'Delete my account for good',
270
+ deleteKeep: 'Keep my account',
271
+ deleted: 'Your account has been deleted.',
272
+ deleteLater: 'This can’t be done just now. Please try again later.',
273
+ opensNewTab: '(opens in a new tab)',
274
+ terms: 'Terms',
275
+ privacy: 'Privacy notice',
276
+ } as const;
package/lib/members.ts ADDED
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Members: people who sign in on an organisation's sites (the Ferst Members module). The site
3
+ * reaches the platform's members service only through its own address, `/ferst-api/…` (a Pages
4
+ * Function the site template carries), so the session is a first-party cookie and nothing here
5
+ * needs CORS. Every call is same-origin, `credentials: 'same-origin'`, JSON.
6
+ *
7
+ * Nothing a member reads is ever in the build: a members' page is a placeholder until the visitor's
8
+ * browser asks for it, and the platform decides, per request, whether they may read it.
9
+ *
10
+ * The browser scripts (MembersContent, the header's account link) are thin; the decisions are
11
+ * here, pure, so they're unit-tested.
12
+ */
13
+
14
+ /** Where a site's members service is, on the site's own address. */
15
+ export const MEMBERS_API = '/ferst-api/members';
16
+ /** The signed-in person, or `401 signed_out`. */
17
+ export const MEMBERS_ME = `${MEMBERS_API}/me`;
18
+ /** The members' account page: signing in, and the member's own details. */
19
+ export const ACCOUNT_PATH = '/account';
20
+
21
+ /** A members' page's id, as the portal makes them. */
22
+ export const MEMBERS_PAGE_ID = /^mpg_[0-9a-f]{24}$/;
23
+
24
+ /** Where the browser reads a members' page. */
25
+ export function membersPageUrl(page: string): string {
26
+ return `${MEMBERS_API}/pages/${encodeURIComponent(page)}`;
27
+ }
28
+
29
+ /** The account page, coming back here once signed in. */
30
+ export function signInHref(here: string): string {
31
+ return `${ACCOUNT_PATH}?next=${encodeURIComponent(here)}`;
32
+ }
33
+
34
+ /** An answer from the members service: its status, and its JSON body (null when it isn't JSON). */
35
+ export interface MembersAnswer {
36
+ status: number;
37
+ body: Record<string, unknown> | null;
38
+ }
39
+
40
+ /** Read a response as a MembersAnswer. Anything but JSON (a "page not found" page) has no body. */
41
+ export async function readMembersAnswer(res: Response): Promise<MembersAnswer> {
42
+ const json = /application\/json/i.test(res.headers.get('Content-Type') ?? '');
43
+ let body: Record<string, unknown> | null = null;
44
+ if (json) {
45
+ try {
46
+ const parsed: unknown = await res.json();
47
+ body = parsed && typeof parsed === 'object' ? (parsed as Record<string, unknown>) : null;
48
+ } catch {
49
+ body = null;
50
+ }
51
+ }
52
+ return { status: res.status, body };
53
+ }
54
+
55
+ /** What a membersContent block shows. */
56
+ export type MembersPageView =
57
+ | { kind: 'page'; title: string; html: string }
58
+ /** 401: "This is for members. Sign in to read it". */
59
+ | { kind: 'sign-in' }
60
+ /** 403 not_for_you: a guest, someone waiting for approval, or a page for other members. */
61
+ | { kind: 'not-included' }
62
+ /** Anything else: the page is gone, Members is off for the site, the service is down. */
63
+ | { kind: 'unavailable' }
64
+ /** Running on a computer (`astro dev` / `astro preview`), where the site has no /ferst-api. */
65
+ | { kind: 'local' };
66
+
67
+ /**
68
+ * What to show for the answer to `GET /ferst-api/members/pages/:id`. `local` is true when the site
69
+ * runs on the author's own computer: there the answer is the dev server's "page not found", not the
70
+ * service's, so the block says sign-in works on the published site.
71
+ */
72
+ export function membersPageView(answer: MembersAnswer | null, local = false): MembersPageView {
73
+ if (!answer || !answer.body) return local ? { kind: 'local' } : { kind: 'unavailable' };
74
+ const { status, body } = answer;
75
+ if (status === 200 && typeof body.html === 'string') {
76
+ return { kind: 'page', title: typeof body.title === 'string' ? body.title : '', html: body.html };
77
+ }
78
+ if (status === 401) return { kind: 'sign-in' };
79
+ // 403 is also the answer when Members isn't on for the site (`module_off`): not about them.
80
+ if (status === 403 && body.error === 'not_for_you') return { kind: 'not-included' };
81
+ return { kind: 'unavailable' };
82
+ }
83
+
84
+ /** The words a members' page shows when it can't show the page itself. */
85
+ export const MEMBERS_PAGE_TEXT = {
86
+ signIn: { before: 'This is for members.', link: 'Sign in', after: 'to read it.' },
87
+ notIncluded: 'This isn’t included in your membership.',
88
+ unavailable: 'This can’t be shown right now.',
89
+ local: 'Sign-in works on the published site.',
90
+ } as const;
91
+
92
+ /** The first word of a person's name, for the header ("Sam" for "Sam Hill"). */
93
+ export function firstName(displayName: unknown): string {
94
+ if (typeof displayName !== 'string') return '';
95
+ return displayName.trim().split(/\s+/)[0] ?? '';
96
+ }
97
+
98
+ /**
99
+ * The header's account link, from the answer to `GET /ferst-api/members/me`: the member's first
100
+ * name (or "Your account" when they have none yet), "Sign in" when signed out, and null (leave the
101
+ * build's "Account") when the service can't say.
102
+ */
103
+ export function accountLinkText(answer: MembersAnswer | null): string | null {
104
+ if (!answer || !answer.body) return null;
105
+ if (answer.status === 401) return 'Sign in';
106
+ if (answer.status !== 200) return null;
107
+ const member = answer.body.member as { display_name?: unknown } | undefined;
108
+ if (!member || typeof member !== 'object') return null;
109
+ return firstName(member.display_name) || 'Your account';
110
+ }
111
+
112
+ /** True on the author's own computer, where `/ferst-api` isn't there (no Pages Functions). */
113
+ export function isLocalHost(hostname: string): boolean {
114
+ return /^(localhost|127\.0\.0\.1|\[::1\]|::1)$/.test(hostname) || hostname.endsWith('.localhost');
115
+ }