@pirateandfox/pagestack 0.0.0-stage → 0.1.0-beta.11
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/README.md +431 -2
- package/bin/invite.mjs +26 -0
- package/index.mjs +234 -0
- package/live.mjs +30 -0
- package/package.json +60 -4
- package/src/client/auth.ts +35 -0
- package/src/client/browse.ts +129 -0
- package/src/client/collection.ts +37 -0
- package/src/client/editor.ts +967 -0
- package/src/client/form.ts +219 -0
- package/src/client/global.ts +33 -0
- package/src/client/icons.ts +27 -0
- package/src/client/inline.ts +107 -0
- package/src/client/inserter.ts +59 -0
- package/src/client/invite.ts +17 -0
- package/src/client/item.ts +404 -0
- package/src/client/live.ts +92 -0
- package/src/client/media.ts +81 -0
- package/src/client/pages.ts +89 -0
- package/src/client/picker.ts +109 -0
- package/src/client/preview.css +64 -0
- package/src/client/rich.ts +232 -0
- package/src/client/shell.css +363 -0
- package/src/client/state.ts +90 -0
- package/src/client/tomarkdown.ts +56 -0
- package/src/client/tree.ts +69 -0
- package/src/client/ui.ts +122 -0
- package/src/client/upload.ts +25 -0
- package/src/client/users.ts +40 -0
- package/src/core/auth.ts +87 -0
- package/src/core/frontmatter.ts +29 -0
- package/src/core/insert.ts +70 -0
- package/src/core/markdown.ts +127 -0
- package/src/core/media.ts +37 -0
- package/src/core/pages.ts +56 -0
- package/src/core/patch.ts +127 -0
- package/src/core/ratelimit.ts +13 -0
- package/src/core/sanitize.ts +325 -0
- package/src/core/schema.ts +111 -0
- package/src/core/storage-github.ts +278 -0
- package/src/core/storage-local.ts +71 -0
- package/src/core/storage.ts +102 -0
- package/src/core/synced.ts +19 -0
- package/src/core/transform.ts +66 -0
- package/src/core/users.ts +127 -0
- package/src/routes/Nav.astro +24 -0
- package/src/routes/api/auth.ts +56 -0
- package/src/routes/api/browse.ts +24 -0
- package/src/routes/api/collection.ts +111 -0
- package/src/routes/api/global.ts +33 -0
- package/src/routes/api/images.ts +65 -0
- package/src/routes/api/live.ts +27 -0
- package/src/routes/api/page.ts +78 -0
- package/src/routes/api/pages.ts +42 -0
- package/src/routes/api/patterns.ts +68 -0
- package/src/routes/api/users.ts +46 -0
- package/src/routes/edit-collection.astro +47 -0
- package/src/routes/edit-global.astro +39 -0
- package/src/routes/edit-index.astro +64 -0
- package/src/routes/edit-item.astro +69 -0
- package/src/routes/edit-media.astro +30 -0
- package/src/routes/edit-page.astro +72 -0
- package/src/routes/edit-site.astro +42 -0
- package/src/routes/edit-users.astro +43 -0
- package/src/routes/enroll.astro +46 -0
- package/src/routes/login.astro +32 -0
- package/src/routes/preview-item.astro +34 -0
- package/src/routes/preview.astro +37 -0
- package/src/routes/uploaded.ts +29 -0
- package/src/runtime.ts +189 -0
- package/src/server/browse.ts +80 -0
- package/src/server/context.ts +199 -0
- package/src/server/live.ts +67 -0
- package/src/server/middleware.ts +131 -0
- package/src/server/webauthn.ts +91 -0
- package/src/virtual.d.ts +13 -0
package/README.md
CHANGED
|
@@ -1,3 +1,432 @@
|
|
|
1
|
-
#
|
|
1
|
+
# PageStack
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A visual editor for Astro sites where pages are a stack of sections. Git-only: saving is a commit,
|
|
4
|
+
the public site stays static, and nothing lives outside the repo.
|
|
5
|
+
|
|
6
|
+
- The public site stays **fully static**. `astro build` is unchanged.
|
|
7
|
+
- The editor (`/edit/*`, `/api/editor/*`) is on-demand rendered and only exists when asked for:
|
|
8
|
+
`EDITOR=dev astro dev`, or a build made with `EDITOR=1` (which adds `@astrojs/node` unless the site has an adapter).
|
|
9
|
+
- Pages are JSON documents in the repo (`src/content/pages/<slug>.json`). Saving writes a file
|
|
10
|
+
(dev) or makes **one commit** through the GitHub API (production). Nothing is stored anywhere else.
|
|
11
|
+
- Login is **passkeys** (WebAuthn). Users' public keys live in a committed `editor-users.json`;
|
|
12
|
+
the only server secret is `EDITOR_SECRET`. No database, no session store.
|
|
13
|
+
- Live preview renders the real page with the site's own components, in the browser's iframe.
|
|
14
|
+
|
|
15
|
+
Status: **early beta** (`0.1.0-beta.x`). The API will move; pin a version. See [ROADMAP.md](ROADMAP.md)
|
|
16
|
+
and [CHANGELOG.md](CHANGELOG.md).
|
|
17
|
+
|
|
18
|
+
## Versions
|
|
19
|
+
|
|
20
|
+
Published on npm as **`@pirateandfox/pagestack`** (betas under the `beta` dist-tag):
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @pirateandfox/pagestack@beta
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or install from a git tag and bump the tag to upgrade:
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
"dependencies": { "@pirateandfox/pagestack": "github:pirateandfox/pagestack#v0.1.0-beta.4" }
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install # after changing the tag
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Releases are tags `vX.Y.Z[-beta.N]` on `main`; each one has a CHANGELOG entry.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
// astro.config.mjs
|
|
42
|
+
import pagestack from '@pirateandfox/pagestack';
|
|
43
|
+
|
|
44
|
+
export default defineConfig({
|
|
45
|
+
integrations: [
|
|
46
|
+
pagestack({
|
|
47
|
+
renderer: './src/components/SectionPage.astro', // renders { page, editing }
|
|
48
|
+
sections: './src/sections/index.ts', // exports { schemas, presets }
|
|
49
|
+
siteName: 'Acme site editor',
|
|
50
|
+
interactiveSelector: 'summary, .tabs__title, .map__load', // preview elements that keep their own click
|
|
51
|
+
reservedSlugs: blogSlugs, // URLs the site uses some other way
|
|
52
|
+
auth: 'passkey', // 'passkey' | 'none-behind-proxy' | 'none' (dev only)
|
|
53
|
+
storage: process.env.EDITOR === '1' ? 'github' : 'local',
|
|
54
|
+
github: { repo: 'acme/site', branch: 'main', pullRequest: true },
|
|
55
|
+
}),
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
EDITOR=dev astro dev # editor at http://localhost:4321/edit, saves to the working tree
|
|
62
|
+
astro dev # the site alone: no editor, nothing can write content
|
|
63
|
+
EDITOR=1 astro build # dist/server (node) + dist/client — the editor for a host
|
|
64
|
+
astro build # plain static site, no editor code
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## What the site provides
|
|
68
|
+
|
|
69
|
+
### 1. A content model: `src/content/pages/<slug>.json`
|
|
70
|
+
|
|
71
|
+
```jsonc
|
|
72
|
+
{
|
|
73
|
+
"title": "Home",
|
|
74
|
+
"description": "…",
|
|
75
|
+
"layout": { "footer": "big", "css": ["post-9.css"] }, // opaque to the editor
|
|
76
|
+
"sections": [
|
|
77
|
+
{ "id": "c8da6f4", "type": "container", "settings": { "backgroundColor": "#fff" },
|
|
78
|
+
"blocks": [
|
|
79
|
+
{ "id": "1a2b3c4", "type": "heading", "settings": { "text": "Hello", "tag": "h1" } },
|
|
80
|
+
{ "id": "5d6e7f8", "type": "text", "settings": { "html": "<p>…</p>" } }
|
|
81
|
+
] }
|
|
82
|
+
]
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
A page is a tree of nodes. Top-level nodes are *sections*; anything can nest (`blocks`).
|
|
87
|
+
`attrs` is a passthrough for element attributes the editor never shows (class, aria-*).
|
|
88
|
+
`"status": "draft"` keeps a page out of the build; without it the page is published.
|
|
89
|
+
|
|
90
|
+
To let editors add pages, render them from one catch-all route. A new JSON file is then a new page,
|
|
91
|
+
and drafts are left out:
|
|
92
|
+
|
|
93
|
+
```astro
|
|
94
|
+
---
|
|
95
|
+
// src/pages/[...page].astro
|
|
96
|
+
import { staticPages } from '@pirateandfox/pagestack/runtime';
|
|
97
|
+
import SectionPage from '../components/SectionPage.astro';
|
|
98
|
+
export const getStaticPaths = () =>
|
|
99
|
+
staticPages(import.meta.glob('../content/pages/**/*.json', { eager: true }));
|
|
100
|
+
const { page } = Astro.props;
|
|
101
|
+
---
|
|
102
|
+
<SectionPage page={page} />
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
A site with its own `.astro` file per page keeps working, but pages added in the editor have no
|
|
106
|
+
route until the catch-all exists.
|
|
107
|
+
|
|
108
|
+
### 2. A block registry: `src/sections/index.ts`
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import type { BlockSchema, Preset } from '@pirateandfox/pagestack/runtime';
|
|
112
|
+
export const schemas = {
|
|
113
|
+
heading: {
|
|
114
|
+
label: 'Heading',
|
|
115
|
+
fields: {
|
|
116
|
+
text: { type: 'text', label: 'Text', inline: true, default: 'Heading' },
|
|
117
|
+
tag: { type: 'select', label: 'HTML tag', options: [...], default: 'h2', group: 'advanced' },
|
|
118
|
+
color: { type: 'color', label: 'Text colour', group: 'style' },
|
|
119
|
+
},
|
|
120
|
+
component: Heading, // the Astro component
|
|
121
|
+
},
|
|
122
|
+
container: { label: 'Container', fields: {...}, blocks: '*', component: Container },
|
|
123
|
+
gallery: { label: 'Gallery', fields: {}, blocks: ['gallery-item'], component: Gallery },
|
|
124
|
+
'gallery-item': { label: 'Image', fields: {...}, hidden: true, component: GalleryItem },
|
|
125
|
+
} satisfies Record<string, BlockSchema & { component: any }>;
|
|
126
|
+
|
|
127
|
+
export const presets: Preset[] = [
|
|
128
|
+
{ label: 'Hero', description: 'Full-width image with a heading and two buttons', create: () => ({ ... }) },
|
|
129
|
+
];
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Field types: `text`, `richtext`, `image`, `link`, `color`, `select`, `number`, `boolean`,
|
|
133
|
+
`textarea`, `code`, `svg`. `inline: true` on `text` / `richtext` / `image` makes the field
|
|
134
|
+
editable by clicking it in the preview. The schema drives the sidebar form, validation on save,
|
|
135
|
+
the add-block menus and the tree labels. **Presets are the section library.**
|
|
136
|
+
|
|
137
|
+
### 3. Components that mark what they render
|
|
138
|
+
|
|
139
|
+
Each block component receives `{ node, edit }` and spreads two helpers onto its markup:
|
|
140
|
+
|
|
141
|
+
```astro
|
|
142
|
+
---
|
|
143
|
+
import { nodeAttrs, fieldAttrs } from '@pirateandfox/pagestack/runtime';
|
|
144
|
+
const { node, edit } = Astro.props;
|
|
145
|
+
---
|
|
146
|
+
<div {...nodeAttrs(edit, node)}>
|
|
147
|
+
<h2 {...fieldAttrs(edit, 'text', 'text')} set:html={node.settings.text} />
|
|
148
|
+
</div>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
`edit` is `undefined` on the public site, so nothing leaks into the static output. A container
|
|
152
|
+
passes `child(edit, i)` to each child. The `renderer` component takes `{ page, editing }` and
|
|
153
|
+
renders the page inside the site's normal layout.
|
|
154
|
+
|
|
155
|
+
## Editing
|
|
156
|
+
|
|
157
|
+
Coming from the WordPress block editor:
|
|
158
|
+
|
|
159
|
+
| WordPress | PageStack |
|
|
160
|
+
|---|---|
|
|
161
|
+
| Pages list | `/edit`: search, status, add, copy, delete |
|
|
162
|
+
| Block inserter, Patterns | `+` or `/`: searchable blocks by category, Patterns tab, `+` between sections |
|
|
163
|
+
| List View | Same, with drag to reorder and to move between containers |
|
|
164
|
+
| Block toolbar, ⋮ menu | Move, parent, add, duplicate, delete, copy, save as pattern |
|
|
165
|
+
| Settings sidebar, Page / Block tabs | Same, generated from the schema |
|
|
166
|
+
| Rich text toolbar, links | Same; `⌘B`, `⌘I`, `⌘K`, and an HTML view |
|
|
167
|
+
| Status (Draft / Published), Publish, Preview | A **Ready to publish** switch (**Published** without `github.live`) that saves when flipped; Publish is **Publish changes** in the top bar with `github.live`; Preview opens the unsaved draft in a new tab |
|
|
168
|
+
| Revisions | The page's git history |
|
|
169
|
+
| Permalink | Change URL, which also adds a redirect |
|
|
170
|
+
| Menus, site options | Global content, at `/edit/global/<id>` |
|
|
171
|
+
| Patterns, synced patterns | Saved patterns; synced ones update every copy in one commit |
|
|
172
|
+
| Block transforms | ⋮ → Turn into… |
|
|
173
|
+
| `/` in an empty block | Same |
|
|
174
|
+
| Posts | `/edit/collection/<id>`: details form, Markdown with live preview |
|
|
175
|
+
| Media Library | `/edit/media` |
|
|
176
|
+
| Users, roles | `/edit/users`: Admin and Editor |
|
|
177
|
+
|
|
178
|
+
- **Preview**: the page renders in a frame at real device widths, scaled to fit. Every change POSTs
|
|
179
|
+
the draft to `/edit/preview/<slug>`; the server renders it and only changed sections are swapped.
|
|
180
|
+
- **Inline**: click text to edit it in place; click an image to open the picker (alt text required).
|
|
181
|
+
- **Drafts** are kept in `localStorage` until saved; undo/redo is per session.
|
|
182
|
+
- **Save** sanitises rich text, validates the document, and writes the file (local) or commits
|
|
183
|
+
(github). A hash of what you loaded is sent along; if someone saved first you get a 409 and a
|
|
184
|
+
reload prompt.
|
|
185
|
+
|
|
186
|
+
Optional exports from the `sections` module:
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
// Fields on the Page tab, stored in page.layout
|
|
190
|
+
export const pageFields = {
|
|
191
|
+
footer: { type: 'select', label: 'Footer', options: [{ value: 'big', label: 'Big' }, { value: 'small', label: 'Small' }] },
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// Starting points offered by Add page
|
|
195
|
+
export const templates = [{ label: 'Standard page', create: ({ title }) => ({ title, sections: [/* … */] }) }];
|
|
196
|
+
|
|
197
|
+
// JSON files edited at /edit/global/<id>
|
|
198
|
+
// Markdown folders edited at /edit/collection/<id>
|
|
199
|
+
export const collections = {
|
|
200
|
+
blog: {
|
|
201
|
+
label: 'Posts', singular: 'post', dir: 'src/content/blog', url: (slug) => `/${slug}/`,
|
|
202
|
+
fields: { title: { type: 'text', label: 'Title' }, draft: { type: 'boolean', label: 'Draft' }, date: { type: 'date', label: 'Date' }, tags: { type: 'tags', label: 'Tags' } },
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export const globals = {
|
|
207
|
+
menus: {
|
|
208
|
+
label: 'Menus',
|
|
209
|
+
file: 'src/data/menus.json',
|
|
210
|
+
fields: {
|
|
211
|
+
nav: { type: 'list', label: 'Main menu', itemLabel: 'label', fields: { label: { type: 'text', label: 'Label' } } },
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### Editing a collection item on its own page
|
|
218
|
+
|
|
219
|
+
Give a collection a layout in `previews` (an option on the integration) and its items open on that
|
|
220
|
+
page in the editor: click the title, a paragraph, a heading, a list or a quote and type; click an
|
|
221
|
+
image to pick another; tables, code and nested lists open the Markdown with that block selected. The
|
|
222
|
+
**+** beside a block, or `/` in an empty one, adds a paragraph, heading, list, quote, image or table
|
|
223
|
+
there. **Page** and **Markdown** switch views, and Preview ↗ shows the unsaved item in a tab. An
|
|
224
|
+
edited block is turned back into Markdown and replaces only its own lines, so the rest of the file
|
|
225
|
+
stays byte-for-byte as it was.
|
|
226
|
+
|
|
227
|
+
The ⋮ menu has **Change URL** (moves the file; with the collection's `url` and a redirects file, the
|
|
228
|
+
old address redirects in the same commit), **Revisions** (the file's git history; Restore loads one
|
|
229
|
+
as unsaved changes, covering the body and the fields the editor shows) and **Delete**.
|
|
230
|
+
|
|
231
|
+
```js
|
|
232
|
+
pagestack({ /* … */ previews: { blog: './src/editor/PostPreview.astro' } })
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
The layout gets `{ item: { id, collection, data, body, html, headings }, editing }`. `html` is the
|
|
236
|
+
sanitised body (render it with `set:html`), blocks already marked; `data` is the frontmatter as
|
|
237
|
+
written, so apply the defaults and date parsing your content schema would. Mark the item's own
|
|
238
|
+
fields with `itemField(editing, 'title')` from the runtime, or write the same two attributes
|
|
239
|
+
(`data-ps-field`, `data-ps-field-type`) yourself. Sharing one component between the real page
|
|
240
|
+
and the preview keeps them identical:
|
|
241
|
+
|
|
242
|
+
```astro
|
|
243
|
+
---
|
|
244
|
+
// src/editor/PostPreview.astro
|
|
245
|
+
import PostPage from '../components/PostPage.astro';
|
|
246
|
+
const { item, editing } = Astro.props;
|
|
247
|
+
---
|
|
248
|
+
<PostPage data={{ ...item.data, date: new Date(item.data.date) }} headings={item.headings} editing={editing}>
|
|
249
|
+
<Fragment set:html={item.html} />
|
|
250
|
+
</PostPage>
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
The preview renders on the editor's server (a Worker on Cloudflare), so whatever the layout imports
|
|
254
|
+
must run there: no `node:fs` reads of the project, no native modules (sharp, resvg), and no import of
|
|
255
|
+
`content.config.ts` (its loaders only run in a build). `getCollection` works. The page's own scripts
|
|
256
|
+
are left out of the editor's frame.
|
|
257
|
+
|
|
258
|
+
In the block editor's preview, Browse and live list pages, the site's own scripts do run (tabs,
|
|
259
|
+
accordions), but scripts from other origins are refused, so analytics, captchas and chat widgets
|
|
260
|
+
stay out of the editor.
|
|
261
|
+
`previewScripts: 'all'` lifts that for a site whose preview needs an outside script.
|
|
262
|
+
|
|
263
|
+
An image field stored as a bare URL can name its alt-text field, `{ type: 'image', asUrl: true,
|
|
264
|
+
altField: 'imageAlt' }`, and the picker fills it in.
|
|
265
|
+
|
|
266
|
+
Blocks can set `category` and `keywords` for the inserter. A `list` field is a repeatable group of
|
|
267
|
+
`fields`, with `itemLabel` naming the field that titles each row.
|
|
268
|
+
|
|
269
|
+
## Browsing the site
|
|
270
|
+
|
|
271
|
+
**Browse site** in the editor's top bar (`/edit/site`) shows the site as visitors see it, with a
|
|
272
|
+
slim bar on top. Click around as you would on the real site; **Edit this page** opens the editor
|
|
273
|
+
for whatever is showing, and **View on site** in the editor comes back to it. On a deployed editor,
|
|
274
|
+
opening a live page directly (below) lands in Browse too.
|
|
275
|
+
|
|
276
|
+
What each page shows:
|
|
277
|
+
|
|
278
|
+
- **Pages** and **collection items with a `previews` layout**: the latest saved version, through
|
|
279
|
+
their previews. A new item shows before the editor has been rebuilt.
|
|
280
|
+
- **List pages named in `liveRoutes`**: rendered on request from what has been saved (see below).
|
|
281
|
+
- **Anything else**: the copy built into this editor deployment. When something has been saved
|
|
282
|
+
since that build, the bar says so ("As of Nov 12, 4:28 PM. May not show changes saved since.").
|
|
283
|
+
|
|
284
|
+
### Live list pages (`liveRoutes`)
|
|
285
|
+
|
|
286
|
+
On a deployed editor, list pages (the home page, `/blog/`) are frozen when the editor is built, so a
|
|
287
|
+
new post wouldn't appear in them until the next deploy. Name them in `liveRoutes` and, in the
|
|
288
|
+
editor's build only, they render on request from storage instead:
|
|
289
|
+
|
|
290
|
+
```js
|
|
291
|
+
pagestack({ liveRoutes: ['/', '/blog'], /* … */ })
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
The page reads its collections through `liveEntries`, which gives the saved entries on the editor's
|
|
295
|
+
server and `null` everywhere else, so the public build is unchanged and stays fully static:
|
|
296
|
+
|
|
297
|
+
```ts
|
|
298
|
+
// src/lib/content.ts
|
|
299
|
+
import { getCollection } from 'astro:content';
|
|
300
|
+
import { liveEntries } from '@pirateandfox/pagestack/live';
|
|
301
|
+
import { postSchema } from './schemas'; // the same zod schema content.config.ts uses
|
|
302
|
+
|
|
303
|
+
export const posts = async () => (await liveEntries('posts', postSchema)) ?? (await getCollection('posts'));
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
The collection name is the editor's collection id. Pass the schema so dates and defaults come out as
|
|
307
|
+
they do at build time; an entry the schema rejects is left out of the list. These pages need to be
|
|
308
|
+
able to render where the editor runs (on Workers: no `node:fs`, and nothing from
|
|
309
|
+
`content.config.ts`, whose loaders only run at build time). On GitHub storage, entries are cached
|
|
310
|
+
until the branch moves, so a list costs one request to GitHub once the first one has warmed up.
|
|
311
|
+
These pages show unpublished work, so they need a signed-in editor.
|
|
312
|
+
|
|
313
|
+
## Security
|
|
314
|
+
|
|
315
|
+
Everything an editor sends is treated as untrusted. On save, every field is cleaned by its type,
|
|
316
|
+
and anything outside the schema is cleaned as HTML, so components can render fields with
|
|
317
|
+
`set:html`. Only admins can put raw HTML in a `code` field. Uploads are checked by their bytes and
|
|
318
|
+
SVGs are sanitised. Set `EDITOR_ORIGIN` to the public https origin in production.
|
|
319
|
+
|
|
320
|
+
With `auth: "none-behind-proxy"`, set `EDITOR_PROXY_SECRET` if the app can be reached without going
|
|
321
|
+
through the proxy: requests must then carry the same value in `x-editor-proxy-secret`. Otherwise the
|
|
322
|
+
identity headers could be forged by anyone who reaches the app directly.
|
|
323
|
+
|
|
324
|
+
The login rate limit is held in memory per server instance, so on Workers it slows guessing rather
|
|
325
|
+
than setting a hard limit. Passkeys are what actually stops guessing.
|
|
326
|
+
|
|
327
|
+
## Storage
|
|
328
|
+
|
|
329
|
+
| `storage` | Reads | Saves | Uploads |
|
|
330
|
+
|---|---|---|---|
|
|
331
|
+
| `local` (dev default) | working tree | writes the JSON file (you commit) | written to `public/images/uploads/` |
|
|
332
|
+
| `github` (build default) | Contents API on `github.branch` | one commit via the Git Data API: blobs → tree → commit → ref; with `pullRequest: true` a new `site-editor/<timestamp>-<sha>` branch + PR instead | wait on a `pagestack-uploads` branch until the next save brings them along; the editor serves them, and anything committed since it was built, to signed-in editors |
|
|
333
|
+
|
|
334
|
+
### Staging and Go live
|
|
335
|
+
|
|
336
|
+
With `github.live`, saves stay on `github.branch` (staging) and the public site builds from `live`.
|
|
337
|
+
Every screen gets a **Publish changes** button (Go live) counting the saved changes the live site doesn't have yet;
|
|
338
|
+
it lists them, links to `github.stagingUrl` if you give one, and puts them all on `live` in one step.
|
|
339
|
+
That is a fast-forward when nobody committed to `live` directly. When someone did, it makes a merge
|
|
340
|
+
commit and moves staging to it; when their change conflicts, it refuses and says so, and a person
|
|
341
|
+
merges in GitHub. A burst of saves becomes one production build. Drafts (the **Ready to publish**
|
|
342
|
+
switch off) stay in the files but out of the built site, whatever is published.
|
|
343
|
+
|
|
344
|
+
```js
|
|
345
|
+
github: { repo: 'acme/site', branch: 'develop', live: 'main', stagingUrl: 'https://develop.acme.pages.dev/' }
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
Point the host's production deploy at `live` and its preview deploys at the staging branch.
|
|
349
|
+
|
|
350
|
+
The GitHub token (`GITHUB_TOKEN` or `EDITOR_GITHUB_TOKEN`) is read from the environment on the
|
|
351
|
+
server only. A fine-grained token with *Contents: write* (and *Pull requests: write* for PR mode)
|
|
352
|
+
on the one repo is enough. The host that runs the editor rebuilds and deploys from the commit
|
|
353
|
+
like any other push.
|
|
354
|
+
|
|
355
|
+
## Login
|
|
356
|
+
|
|
357
|
+
### `auth: "passkey"` (default)
|
|
358
|
+
|
|
359
|
+
- `editor-users.json` (committed, path configurable with `usersFile`) holds users and the
|
|
360
|
+
**public keys** of their passkeys. Nothing in it is secret.
|
|
361
|
+
- `EDITOR_SECRET` (≥ 16 chars, environment only) signs three kinds of short-lived tokens:
|
|
362
|
+
WebAuthn challenges (5 min), the session cookie (`ps_session`, HttpOnly, SameSite=Lax, 12 h) and
|
|
363
|
+
invite links (3 days). Tokens are `base64url(json).base64url(hmac-sha256)` with the expiry inside,
|
|
364
|
+
so there is no server state.
|
|
365
|
+
- **Invites** are single-use: the invite id is recorded on the credential it created. Make the first
|
|
366
|
+
one from the CLI, later ones from the "Invite another editor" panel on `/edit`:
|
|
367
|
+
|
|
368
|
+
```bash
|
|
369
|
+
EDITOR_SECRET=… npx pagestack-invite --name "Jane Doe" --email jane@example.com --origin https://example.com
|
|
370
|
+
```
|
|
371
|
+
- `/edit/enroll?invite=…` creates the passkey (Touch ID / Face ID / Windows Hello / security key);
|
|
372
|
+
`/edit/login` signs in with it. Several passkeys per person are fine (enrol again with a new invite).
|
|
373
|
+
- Set `EDITOR_ORIGIN` when a proxy rewrites `Host`; WebAuthn binds credentials to the origin the
|
|
374
|
+
browser sees.
|
|
375
|
+
- The signature counter is not persisted (it would mean a commit per login; synced passkeys report 0).
|
|
376
|
+
|
|
377
|
+
### `auth: "none-behind-proxy"`
|
|
378
|
+
|
|
379
|
+
Use when something in front of the editor already authenticates people, e.g. **Cloudflare Access**:
|
|
380
|
+
put the editor host behind an Access application for `/edit/*` and `/api/editor/*`, allow the
|
|
381
|
+
client's email addresses, and the editor trusts the proxy. It reads the identity from
|
|
382
|
+
`Cf-Access-Authenticated-User-Email` (or `X-Forwarded-User`) for commit attribution. This is a
|
|
383
|
+
good fit when the site already sits on Cloudflare and the client prefers email one-time codes over
|
|
384
|
+
passkeys. (A production deployment should also verify the Access JWT on the editor host; that's not
|
|
385
|
+
implemented here.)
|
|
386
|
+
|
|
387
|
+
### `auth: "none"`
|
|
388
|
+
|
|
389
|
+
Dev only. A build with `auth: "none"` is refused.
|
|
390
|
+
|
|
391
|
+
## Deploying the editor
|
|
392
|
+
|
|
393
|
+
Requires Node 22.23.4+ and Astro 7.2.1+. On Cloudflare, add `@astrojs/cloudflare` as the host's
|
|
394
|
+
adapter and set `EDITOR_SECRET`, `EDITOR_ORIGIN` and `GITHUB_TOKEN` as secrets.
|
|
395
|
+
|
|
396
|
+
The editor server is a normal `@astrojs/node` standalone build. Run `node dist/server/entry.mjs`
|
|
397
|
+
on any small Node host (Fly, Railway, a VPS…) with `EDITOR_SECRET`, `GITHUB_TOKEN` and
|
|
398
|
+
`EDITOR_ORIGIN` set, behind HTTPS. It serves the editor routes and the static pages it was built
|
|
399
|
+
with; the public site is still deployed separately from the same repo as static files. Every
|
|
400
|
+
editor response is `noindex` and `no-store`.
|
|
401
|
+
|
|
402
|
+
## Options
|
|
403
|
+
|
|
404
|
+
| option | default | |
|
|
405
|
+
|---|---|---|
|
|
406
|
+
| `renderer` | required, unless `pages: false` | Astro component rendering `{ page, editing }` |
|
|
407
|
+
| `pages` | `true` | `false` for a site with no block pages: only collections, globals and media |
|
|
408
|
+
| `previews` | `{}` | per collection id, an Astro layout for editing items on their page (see above) |
|
|
409
|
+
| `sections` | required | module exporting `{ schemas, presets }` |
|
|
410
|
+
| `pagesDir` | `src/content/pages` | |
|
|
411
|
+
| `imagesDir` / `uploadsDir` / `publicDir` | `public/images` / `public/images/uploads` / `public` | |
|
|
412
|
+
| `usersFile` | `editor-users.json` | |
|
|
413
|
+
| `auth` | `passkey` | `passkey` \| `none-behind-proxy` \| `none` |
|
|
414
|
+
| `storage` | `local` in dev, `github` in build | |
|
|
415
|
+
| `github` | — | `{ repo, branch, live?, stagingUrl?, pullRequest? }`: see Staging and Go live |
|
|
416
|
+
| `liveRoutes` | `[]` | site paths rendered on request in the editor's build, from saved content (see Live list pages) |
|
|
417
|
+
| `previewScripts` | `'site'` | block-editor preview runs the site's own scripts only; `'all'` allows other origins too |
|
|
418
|
+
| `enabled` | `EDITOR=dev` (dev) or `EDITOR=1` | Off otherwise, so plain `astro dev` never writes content |
|
|
419
|
+
| `siteName`, `secretEnv` | `Site editor`, `EDITOR_SECRET` | |
|
|
420
|
+
| `reservedSlugs` | `[]` | URLs Add page must refuse, on top of the editor's own and `src/pages` |
|
|
421
|
+
| `redirectsFile` | `public/_redirects` | Change URL appends here, only if the file exists |
|
|
422
|
+
| `patternsFile` | `src/content/pagestack-patterns.json` | where saved patterns live |
|
|
423
|
+
| `clientIpHeader` | none | header with the client IP behind a proxy, for login rate limits, e.g. `cf-connecting-ip` |
|
|
424
|
+
|
|
425
|
+
## Open-source pieces used
|
|
426
|
+
|
|
427
|
+
- [SortableJS](https://github.com/SortableJS/Sortable) (MIT) for the tree drag-and-drop.
|
|
428
|
+
- [@simplewebauthn/server + browser](https://simplewebauthn.dev) (MIT) for WebAuthn.
|
|
429
|
+
- [@astrojs/node](https://docs.astro.build/en/guides/integrations-guide/node/) (MIT) as the editor's adapter.
|
|
430
|
+
- [Puck](https://puckeditor.com) was used as a design reference for JSON page editing only; no code.
|
|
431
|
+
|
|
432
|
+
MIT.
|
package/bin/invite.mjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Create the first invite link without a running editor (later invites are made at /edit/users; CLI invites create admins).
|
|
3
|
+
//
|
|
4
|
+
// EDITOR_SECRET=… node packages/pagestack/bin/invite.mjs --name "Jane Doe" --email jane@example.com --origin https://example.com [--days 3]
|
|
5
|
+
//
|
|
6
|
+
// Same token format as src/core/auth.ts (HMAC-SHA256, base64url(json).base64url(mac)) so the
|
|
7
|
+
// running site can verify it. Plain JS so it runs on any Node without a TypeScript step.
|
|
8
|
+
import { createHmac, randomBytes } from 'node:crypto';
|
|
9
|
+
|
|
10
|
+
const args = Object.fromEntries(process.argv.slice(2).map((a, i, all) => a.startsWith('--') ? [a.slice(2), all[i + 1]?.startsWith('--') || all[i + 1] === undefined ? 'true' : all[i + 1]] : []).filter((p) => p.length));
|
|
11
|
+
const secret = process.env[args.secretEnv || 'EDITOR_SECRET'] || '';
|
|
12
|
+
const { name, email, origin, days = '3' } = args;
|
|
13
|
+
if (!name || !email || !origin || args.help) {
|
|
14
|
+
console.error('usage: EDITOR_SECRET=… invite.mjs --name "Jane Doe" --email jane@example.com --origin https://example.com [--days 3]');
|
|
15
|
+
process.exit(args.help ? 0 : 2);
|
|
16
|
+
}
|
|
17
|
+
if (secret.length < 16) { console.error('EDITOR_SECRET must be set in the environment (>= 16 characters)'); process.exit(2); }
|
|
18
|
+
|
|
19
|
+
const b64url = (buf) => Buffer.from(buf).toString('base64url');
|
|
20
|
+
const ttl = Math.round(Number(days) * 86400);
|
|
21
|
+
const payload = { kind: 'invite', id: b64url(randomBytes(12)), name: name.trim(), email: email.trim().toLowerCase() };
|
|
22
|
+
const body = b64url(Buffer.from(JSON.stringify({ payload, exp: Math.floor(Date.now() / 1000) + ttl })));
|
|
23
|
+
const mac = b64url(createHmac('sha256', secret).update(body).digest());
|
|
24
|
+
const token = `${body}.${mac}`;
|
|
25
|
+
console.log(`${origin.replace(/\/$/, '')}/edit/enroll?invite=${encodeURIComponent(token)}`);
|
|
26
|
+
console.error(`invite for ${payload.name} <${payload.email}>, valid ${days} day(s), single use`);
|