ferst-core 0.17.0 → 0.18.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/lib/members.ts CHANGED
@@ -4,10 +4,9 @@
4
4
  * Function the site template carries), so the session is a first-party cookie and nothing here
5
5
  * needs CORS. Every call is same-origin, `credentials: 'same-origin'`, JSON.
6
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.
7
+ * Nothing a member reads is ever in the build.
9
8
  *
10
- * The browser scripts (MembersContent, the header's account link) are thin; the decisions are
9
+ * The browser scripts (the header's account link, the account page) are thin; the decisions are
11
10
  * here, pure, so they're unit-tested.
12
11
  */
13
12
 
@@ -18,14 +17,6 @@ export const MEMBERS_ME = `${MEMBERS_API}/me`;
18
17
  /** The members' account page: signing in, and the member's own details. */
19
18
  export const ACCOUNT_PATH = '/account';
20
19
 
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
20
  /** The account page, coming back here once signed in. */
30
21
  export function signInHref(here: string): string {
31
22
  return `${ACCOUNT_PATH}?next=${encodeURIComponent(here)}`;
@@ -52,43 +43,6 @@ export async function readMembersAnswer(res: Response): Promise<MembersAnswer> {
52
43
  return { status: res.status, body };
53
44
  }
54
45
 
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
46
  /** The first word of a person's name, for the header ("Sam" for "Sam Hill"). */
93
47
  export function firstName(displayName: unknown): string {
94
48
  if (typeof displayName !== 'string') return '';
@@ -109,6 +63,25 @@ export function accountLinkText(answer: MembersAnswer | null): string | null {
109
63
  return firstName(member.display_name) || 'Your account';
110
64
  }
111
65
 
66
+ /**
67
+ * The header's account link without a flicker (0.17.1): the build says "Sign in" (most visitors
68
+ * aren't signed in), and a tiny inline script puts back the label this browser last saw before the
69
+ * page paints, under this key. The answer from `/me` still has the last word, and is remembered.
70
+ */
71
+ export const ACCOUNT_LABEL_KEY = 'ferst:account-label';
72
+
73
+ /** A remembered label worth showing: one short line of plain text, as accountLinkText makes them. */
74
+ export function rememberedLabel(v: unknown): string | null {
75
+ if (typeof v !== 'string') return null;
76
+ const t = v.trim();
77
+ return t && t.length <= 40 && !/[\n\r<>]/.test(t) ? t : null;
78
+ }
79
+
80
+ /** Remember the link's label for the next page (nothing, where the browser keeps no storage). */
81
+ export function rememberAccountLabel(text: string): void {
82
+ try { localStorage.setItem(ACCOUNT_LABEL_KEY, text); } catch { /* private mode, storage off */ }
83
+ }
84
+
112
85
  /** True on the author's own computer, where `/ferst-api` isn't there (no Pages Functions). */
113
86
  export function isLocalHost(hostname: string): boolean {
114
87
  return /^(localhost|127\.0\.0\.1|\[::1\]|::1)$/.test(hostname) || hostname.endsWith('.localhost');
package/package.json CHANGED
@@ -1,80 +1,80 @@
1
- {
2
- "name": "ferst-core",
3
- "version": "0.17.0",
4
- "type": "module",
5
- "description": "Ferst Core — the shared, brand-agnostic client-site system: Astro components, layouts, content schemas, the layered config resolver, and a neutral styling architecture that every client overrides.",
6
- "license": "BUSL-1.1",
7
- "author": "MyAI4 Ltd (https://ferst.co.uk)",
8
- "homepage": "https://ferst.co.uk",
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/MyAI4WebDev/myai4-ferst-core.git",
12
- "directory": "packages/core"
13
- },
14
- "bugs": {
15
- "url": "https://github.com/MyAI4WebDev/myai4-ferst-core/issues"
16
- },
17
- "keywords": [
18
- "astro",
19
- "astro-component",
20
- "cms",
21
- "sveltia",
22
- "website",
23
- "design-tokens",
24
- "ferst"
25
- ],
26
- "scripts": {
27
- "test": "vitest run",
28
- "test:watch": "vitest"
29
- },
30
- "sideEffects": [
31
- "**/*.css"
32
- ],
33
- "files": [
34
- "bin",
35
- "cms",
36
- "components",
37
- "integration.mjs",
38
- "layouts",
39
- "content",
40
- "examples",
41
- "lib",
42
- "routes",
43
- "utils",
44
- "styles",
45
- "fonts",
46
- "README.md",
47
- "NOTICE",
48
- "LICENSE",
49
- "LICENSE-Apache-2.0"
50
- ],
51
- "bin": {
52
- "ferst-check-thin": "bin/check-thin.mjs",
53
- "ferst-check-pages": "bin/check-pages.mjs",
54
- "ferst-set-cms-branch": "bin/set-cms-branch.mjs",
55
- "ferst-cms-config": "bin/cms-config.mjs",
56
- "ferst-update": "bin/ferst-update.mjs",
57
- "ferst-modules": "bin/ferst-modules.mjs",
58
- "ferst-media": "bin/ferst-media.mjs"
59
- },
60
- "dependencies": {
61
- "@astrojs/react": "^4.4.2",
62
- "@internationalized/date": "^3.12.4",
63
- "react": "^19.3.1",
64
- "react-aria": "^3.53.1",
65
- "react-aria-components": "^1.22.1",
66
- "react-dom": "^19.3.1",
67
- "zod": "^3.25.0"
68
- },
69
- "devDependencies": {
70
- "@types/react": "^19.3.3",
71
- "@types/react-dom": "^19.3.0",
72
- "vitest": "^3.0.0"
73
- },
74
- "peerDependencies": {
75
- "astro": "^5.0.0"
76
- },
77
- "publishConfig": {
78
- "access": "public"
79
- }
80
- }
1
+ {
2
+ "name": "ferst-core",
3
+ "version": "0.18.0",
4
+ "type": "module",
5
+ "description": "Ferst Core — the shared, brand-agnostic client-site system: Astro components, layouts, content schemas, the layered config resolver, and a neutral styling architecture that every client overrides.",
6
+ "license": "BUSL-1.1",
7
+ "author": "MyAI4 Ltd (https://ferst.co.uk)",
8
+ "homepage": "https://ferst.co.uk",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/MyAI4WebDev/myai4-ferst-core.git",
12
+ "directory": "packages/core"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/MyAI4WebDev/myai4-ferst-core/issues"
16
+ },
17
+ "keywords": [
18
+ "astro",
19
+ "astro-component",
20
+ "cms",
21
+ "sveltia",
22
+ "website",
23
+ "design-tokens",
24
+ "ferst"
25
+ ],
26
+ "scripts": {
27
+ "test": "vitest run",
28
+ "test:watch": "vitest"
29
+ },
30
+ "sideEffects": [
31
+ "**/*.css"
32
+ ],
33
+ "files": [
34
+ "bin",
35
+ "cms",
36
+ "components",
37
+ "integration.mjs",
38
+ "layouts",
39
+ "content",
40
+ "examples",
41
+ "lib",
42
+ "routes",
43
+ "utils",
44
+ "styles",
45
+ "fonts",
46
+ "README.md",
47
+ "NOTICE",
48
+ "LICENSE",
49
+ "LICENSE-Apache-2.0"
50
+ ],
51
+ "bin": {
52
+ "ferst-check-thin": "bin/check-thin.mjs",
53
+ "ferst-check-pages": "bin/check-pages.mjs",
54
+ "ferst-set-cms-branch": "bin/set-cms-branch.mjs",
55
+ "ferst-cms-config": "bin/cms-config.mjs",
56
+ "ferst-update": "bin/ferst-update.mjs",
57
+ "ferst-modules": "bin/ferst-modules.mjs",
58
+ "ferst-media": "bin/ferst-media.mjs"
59
+ },
60
+ "dependencies": {
61
+ "@astrojs/react": "^4.4.2",
62
+ "@internationalized/date": "^3.12.4",
63
+ "react": "^19.3.1",
64
+ "react-aria": "^3.53.1",
65
+ "react-aria-components": "^1.22.1",
66
+ "react-dom": "^19.3.1",
67
+ "zod": "^3.25.0"
68
+ },
69
+ "devDependencies": {
70
+ "@types/react": "^19.3.3",
71
+ "@types/react-dom": "^19.3.0",
72
+ "vitest": "^3.0.0"
73
+ },
74
+ "peerDependencies": {
75
+ "astro": "^5.0.0"
76
+ },
77
+ "publishConfig": {
78
+ "access": "public"
79
+ }
80
+ }
@@ -1,176 +0,0 @@
1
- ---
2
- /**
3
- * MembersContent — a members' page from the Ferst Members module, written in the portal. The
4
- * build renders only a placeholder (and the block's title, when it has one): what members read is
5
- * never in the build. In the visitor's browser the block asks the site for the page
6
- * (`/ferst-api/members/pages/<id>`, same-origin, the session cookie going with it), and the
7
- * platform checks who is signed in. Then it shows the page, a sign-in prompt (401), "not included"
8
- * (403) or "can't be shown" (anything else). On the author's own computer (`astro dev` /
9
- * `astro preview`, no Pages Functions) it says sign-in works on the published site. lib/members
10
- * holds the decisions.
11
- */
12
- import Heading from '../blocks/Heading.astro';
13
- import { MEMBERS_PAGE_TEXT, membersPageUrl } from '../../lib/members';
14
-
15
- interface Props {
16
- page: string;
17
- title?: string;
18
- }
19
- const { page, title } = Astro.props;
20
- const { align: blockAlign, headingAlign } = Astro.props as { align?: 'start' | 'center' | 'end'; headingAlign?: 'start' | 'center' | 'end' };
21
- const headAlign = headingAlign ?? blockAlign;
22
- const signIn = MEMBERS_PAGE_TEXT.signIn;
23
- ---
24
-
25
- <section class="b-members" data-members-page={membersPageUrl(page)} data-members-own-title={title ? undefined : ''} aria-busy="true">
26
- <div class="b-members__head" data-recipe-head data-content-align={headAlign} hidden={!title}>
27
- <Heading text={title ?? ''} level={2} />
28
- </div>
29
- <div class="b-members__skeleton" data-members-skeleton aria-hidden="true">
30
- <span></span><span></span><span></span>
31
- </div>
32
- <div class="b-members__body" data-members-body hidden></div>
33
- <p class="b-members__notice" role="status" data-members-notice="sign-in" hidden>
34
- {signIn.before} <a href="/account" data-members-sign-in>{signIn.link}</a> {signIn.after}
35
- </p>
36
- <p class="b-members__notice" role="status" data-members-notice="not-included" hidden>{MEMBERS_PAGE_TEXT.notIncluded}</p>
37
- <p class="b-members__notice" role="status" data-members-notice="unavailable" hidden>{MEMBERS_PAGE_TEXT.unavailable}</p>
38
- <p class="b-members__notice" role="status" data-members-notice="local" hidden>{MEMBERS_PAGE_TEXT.local}</p>
39
- </section>
40
-
41
- <script>
42
- import { isLocalHost, membersPageView, readMembersAnswer, signInHref, type MembersAnswer } from '../../lib/members';
43
-
44
- async function ask(url: string): Promise<MembersAnswer | null> {
45
- try {
46
- const res = await fetch(url, { credentials: 'same-origin', headers: { Accept: 'application/json' } });
47
- return await readMembersAnswer(res);
48
- } catch {
49
- return null;
50
- }
51
- }
52
-
53
- async function setUp(block: HTMLElement) {
54
- const url = block.dataset.membersPage;
55
- if (!url) return;
56
- const view = membersPageView(await ask(url), import.meta.env.DEV || isLocalHost(location.hostname));
57
- block.querySelector<HTMLElement>('[data-members-skeleton]')?.remove();
58
- block.removeAttribute('aria-busy');
59
- if (view.kind === 'page') {
60
- // The page's own title, unless the block gives its own.
61
- const head = block.querySelector<HTMLElement>('.b-members__head');
62
- if (head && block.dataset.membersOwnTitle !== undefined && view.title) {
63
- const heading = head.querySelector<HTMLElement>('.b-heading');
64
- if (heading) heading.textContent = view.title;
65
- head.hidden = false;
66
- }
67
- const body = block.querySelector<HTMLElement>('[data-members-body]');
68
- if (body) {
69
- // The platform renders the page's Markdown and allows only plain tags (no scripts, no styles).
70
- body.innerHTML = view.html;
71
- body.hidden = false;
72
- }
73
- return;
74
- }
75
- if (view.kind === 'sign-in') {
76
- const link = block.querySelector<HTMLAnchorElement>('[data-members-sign-in]');
77
- if (link) link.href = signInHref(location.pathname + location.search);
78
- }
79
- const notice = block.querySelector<HTMLElement>(`[data-members-notice="${view.kind}"]`);
80
- if (notice) notice.hidden = false;
81
- }
82
-
83
- document.querySelectorAll<HTMLElement>('[data-members-page]').forEach((b) => { void setUp(b); });
84
- </script>
85
-
86
- <style>
87
- .b-members {
88
- display: flex;
89
- flex-direction: column;
90
- gap: 1rem;
91
- max-width: var(--max);
92
- margin-inline: auto;
93
- padding-inline: 1rem;
94
- color: var(--fg);
95
- }
96
- .b-members__head[hidden],
97
- .b-members__body[hidden],
98
- .b-members__notice[hidden] {
99
- display: none;
100
- }
101
-
102
- /* The placeholder while the page loads: three quiet bars, still under reduced motion. */
103
- .b-members__skeleton {
104
- display: flex;
105
- flex-direction: column;
106
- gap: 0.75rem;
107
- }
108
- .b-members__skeleton span {
109
- display: block;
110
- height: 1rem;
111
- border-radius: var(--radius-sm);
112
- background: var(--bg-section);
113
- animation: b-members-pulse 1.6s ease-in-out infinite;
114
- }
115
- .b-members__skeleton span:nth-child(2) { width: 92%; }
116
- .b-members__skeleton span:nth-child(3) { width: 64%; }
117
- @keyframes b-members-pulse {
118
- 50% { opacity: 0.55; }
119
- }
120
- @media (prefers-reduced-motion: reduce) {
121
- .b-members__skeleton span { animation: none; }
122
- }
123
-
124
- /* The page itself: plain Markdown from the portal, read like a Prose block. */
125
- .b-members__body :global(:is(p, ul, ol, blockquote, pre, table)) {
126
- margin: 0 0 1rem;
127
- line-height: 1.7;
128
- }
129
- .b-members__body :global(:is(ul, ol)) {
130
- padding-inline-start: 1.4em;
131
- }
132
- .b-members__body :global(li + li) {
133
- margin-top: 0.25rem;
134
- }
135
- .b-members__body :global(:is(h1, h2, h3, h4, h5, h6)) {
136
- margin: 1.5rem 0 0.75rem;
137
- line-height: 1.25;
138
- color: var(--fg);
139
- }
140
- .b-members__body :global(a) {
141
- color: var(--accent);
142
- text-decoration: underline;
143
- text-underline-offset: 0.15em;
144
- }
145
- .b-members__body :global(img) {
146
- max-width: 100%;
147
- height: auto;
148
- border-radius: var(--radius-sm);
149
- }
150
- .b-members__body :global(blockquote) {
151
- padding-inline-start: 1rem;
152
- border-inline-start: 3px solid var(--border);
153
- color: var(--muted);
154
- }
155
- .b-members__body > :global(:first-child) {
156
- margin-top: 0;
157
- }
158
- .b-members__body > :global(:last-child) {
159
- margin-bottom: 0;
160
- }
161
-
162
- .b-members__notice {
163
- margin: 0;
164
- padding: 0.85rem 1rem;
165
- border: 1px solid var(--border);
166
- border-radius: var(--radius-sm);
167
- background: var(--bg-section);
168
- line-height: 1.5;
169
- }
170
- .b-members__notice a {
171
- color: var(--accent);
172
- font-weight: 600;
173
- text-decoration: underline;
174
- text-underline-offset: 0.15em;
175
- }
176
- </style>