@velocity-astro/cms-astro 0.0.0-stage → 0.1.0-beta.2
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 +21 -0
- package/README.md +12 -2
- package/bin/velocity-cms.mjs +6 -0
- package/dist/admin-files.d.ts +21 -0
- package/dist/admin-files.d.ts.map +1 -0
- package/dist/admin-files.js +77 -0
- package/dist/admin-files.js.map +1 -0
- package/dist/cli.d.ts +9 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +75 -0
- package/dist/cli.js.map +1 -0
- package/dist/connection.d.ts +25 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +62 -0
- package/dist/connection.js.map +1 -0
- package/dist/editor.d.ts +65 -0
- package/dist/editor.d.ts.map +1 -0
- package/dist/editor.js +186 -0
- package/dist/editor.js.map +1 -0
- package/dist/entry-page.d.ts +37 -0
- package/dist/entry-page.d.ts.map +1 -0
- package/dist/entry-page.js +56 -0
- package/dist/entry-page.js.map +1 -0
- package/dist/form-post.d.ts +106 -0
- package/dist/form-post.d.ts.map +1 -0
- package/dist/form-post.js +291 -0
- package/dist/form-post.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +33 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +125 -0
- package/dist/init.js.map +1 -0
- package/dist/integration.d.ts +63 -0
- package/dist/integration.d.ts.map +1 -0
- package/dist/integration.js +129 -0
- package/dist/integration.js.map +1 -0
- package/dist/link.d.ts +32 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +76 -0
- package/dist/link.js.map +1 -0
- package/dist/live-loader.d.ts +40 -0
- package/dist/live-loader.d.ts.map +1 -0
- package/dist/live-loader.js +92 -0
- package/dist/live-loader.js.map +1 -0
- package/dist/live-pages.d.ts +69 -0
- package/dist/live-pages.d.ts.map +1 -0
- package/dist/live-pages.js +94 -0
- package/dist/live-pages.js.map +1 -0
- package/dist/loader.d.ts +22 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +62 -0
- package/dist/loader.js.map +1 -0
- package/dist/media-store.d.ts +46 -0
- package/dist/media-store.d.ts.map +1 -0
- package/dist/media-store.js +154 -0
- package/dist/media-store.js.map +1 -0
- package/dist/menu.d.ts +51 -0
- package/dist/menu.d.ts.map +1 -0
- package/dist/menu.js +81 -0
- package/dist/menu.js.map +1 -0
- package/dist/page-head.d.ts +23 -0
- package/dist/page-head.d.ts.map +1 -0
- package/dist/page-head.js +66 -0
- package/dist/page-head.js.map +1 -0
- package/dist/preview.d.ts +60 -0
- package/dist/preview.d.ts.map +1 -0
- package/dist/preview.js +126 -0
- package/dist/preview.js.map +1 -0
- package/dist/resend-mailer.d.ts +46 -0
- package/dist/resend-mailer.d.ts.map +1 -0
- package/dist/resend-mailer.js +95 -0
- package/dist/resend-mailer.js.map +1 -0
- package/dist/secret.d.ts +8 -0
- package/dist/secret.d.ts.map +1 -0
- package/dist/secret.js +16 -0
- package/dist/secret.js.map +1 -0
- package/dist/site-editor.d.ts +41 -0
- package/dist/site-editor.d.ts.map +1 -0
- package/dist/site-editor.js +110 -0
- package/dist/site-editor.js.map +1 -0
- package/dist/site-forms.d.ts +43 -0
- package/dist/site-forms.d.ts.map +1 -0
- package/dist/site-forms.js +82 -0
- package/dist/site-forms.js.map +1 -0
- package/dist/site-images.d.ts +17 -0
- package/dist/site-images.d.ts.map +1 -0
- package/dist/site-images.js +22 -0
- package/dist/site-images.js.map +1 -0
- package/dist/site-media.d.ts +18 -0
- package/dist/site-media.d.ts.map +1 -0
- package/dist/site-media.js +43 -0
- package/dist/site-media.js.map +1 -0
- package/dist/site-plugins.d.ts +10 -0
- package/dist/site-plugins.d.ts.map +1 -0
- package/dist/site-plugins.js +23 -0
- package/dist/site-plugins.js.map +1 -0
- package/dist/site-search.d.ts +12 -0
- package/dist/site-search.d.ts.map +1 -0
- package/dist/site-search.js +79 -0
- package/dist/site-search.js.map +1 -0
- package/package.json +53 -3
- package/routes/admin.ts +10 -0
- package/routes/api.ts +15 -0
- package/routes/auth.ts +10 -0
- package/routes/due.ts +12 -0
- package/routes/editor.ts +48 -0
- package/routes/form-client.js.ts +105 -0
- package/routes/media.ts +10 -0
- package/routes/pages.ts +13 -0
- package/routes/plugins.ts +10 -0
- package/routes/preview-client.js.ts +63 -0
- package/routes/published.ts +29 -0
- package/routes/schedule.ts +20 -0
- package/routes/search-client.js.ts +33 -0
- package/routes/search.json.ts +10 -0
- package/routes/share-image.png.ts +33 -0
- package/routes/sitemap-live.xml.ts +13 -0
- package/routes/submit.ts +22 -0
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@velocity-astro/cms-astro",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-beta.2",
|
|
4
|
+
"description": "The Astro side of Velocity CMS: a live loader for pages rendered on demand, a Content Layer loader for static builds, and the integration.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Velocity-Astro/velocity-cms.git",
|
|
10
|
+
"directory": "packages/cms-astro"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://docs.deployvelocity.com/cms/",
|
|
13
|
+
"bugs": "https://github.com/Velocity-Astro/velocity-cms/issues",
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=22.23.4"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./middleware": {
|
|
26
|
+
"types": "./dist/preview.d.ts",
|
|
27
|
+
"default": "./dist/preview.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"routes",
|
|
33
|
+
"bin",
|
|
34
|
+
"!dist/**/*.test.*"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@velocity-astro/cms-admin": "0.1.0-beta.2",
|
|
38
|
+
"@velocity-astro/cms-api": "0.1.0-beta.2",
|
|
39
|
+
"@velocity-astro/cms-auth": "0.1.0-beta.2",
|
|
40
|
+
"@velocity-astro/cms-core": "0.1.0-beta.2",
|
|
41
|
+
"@velocity-astro/cms-db": "0.1.0-beta.2",
|
|
42
|
+
"@velocity-astro/cms-plugins": "0.1.0-beta.2"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"astro": "^7.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"astro": "7.4.5"
|
|
49
|
+
},
|
|
50
|
+
"astro": {
|
|
51
|
+
"external": true
|
|
52
|
+
},
|
|
53
|
+
"bin": {
|
|
54
|
+
"velocity-cms": "bin/velocity-cms.mjs"
|
|
55
|
+
}
|
|
6
56
|
}
|
package/routes/admin.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
import { editor } from './editor.ts';
|
|
3
|
+
|
|
4
|
+
export const prerender = false;
|
|
5
|
+
|
|
6
|
+
/** The editor's page and the files it loads. */
|
|
7
|
+
export const ALL: APIRoute = ({ request, cache }) => {
|
|
8
|
+
cache.set(false);
|
|
9
|
+
return editor(request, (host) => host.admin(request));
|
|
10
|
+
};
|
package/routes/api.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
import { editor, ownOrigin, showPublished } from './editor.ts';
|
|
3
|
+
|
|
4
|
+
export const prerender = false;
|
|
5
|
+
|
|
6
|
+
/** The CMS over HTTP, for the editor. Every rule is in the API and the services behind it. */
|
|
7
|
+
export const ALL: APIRoute = ({ request, cache }) => {
|
|
8
|
+
// The API serves drafts. Nothing it answers may be kept.
|
|
9
|
+
cache.set(false);
|
|
10
|
+
return editor(request, async (host) => {
|
|
11
|
+
const response = await host.api(request);
|
|
12
|
+
await showPublished(host, cache, ownOrigin(request));
|
|
13
|
+
return response;
|
|
14
|
+
});
|
|
15
|
+
};
|
package/routes/auth.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
import { editor } from './editor.ts';
|
|
3
|
+
|
|
4
|
+
export const prerender = false;
|
|
5
|
+
|
|
6
|
+
/** Sign-in: email links and passkeys. */
|
|
7
|
+
export const ALL: APIRoute = ({ request, cache }) => {
|
|
8
|
+
cache.set(false);
|
|
9
|
+
return editor(request, (host) => host.signIn(request));
|
|
10
|
+
};
|
package/routes/due.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Looks, on a request for a page, for entries whose time to be published has come. The host
|
|
3
|
+
* looks at most every half minute, so nearly every request passes straight through; the one that
|
|
4
|
+
* finds something waits for it, and so is the first to see the new page.
|
|
5
|
+
*/
|
|
6
|
+
import type { MiddlewareHandler } from 'astro';
|
|
7
|
+
import { publishDue } from './editor.ts';
|
|
8
|
+
|
|
9
|
+
export const onRequest: MiddlewareHandler = async (context, next) => {
|
|
10
|
+
if (!context.isPrerendered) await publishDue(context.request, context.cache).catch(() => undefined);
|
|
11
|
+
return next();
|
|
12
|
+
};
|
package/routes/editor.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the editor's three routes share. Compiled with the site, like every file here, so it can
|
|
3
|
+
* read the settings the integration wrote and the site's own mailer.
|
|
4
|
+
*/
|
|
5
|
+
import { indexPages, pagesOrigin, siteEditorHost, withEditor, type EditorHost, type SiteEditorSettings } from '@velocity-astro/cms-astro';
|
|
6
|
+
import images from 'virtual:velocity-cms/images';
|
|
7
|
+
import mailer from 'virtual:velocity-cms/mailer';
|
|
8
|
+
import options from 'virtual:velocity-cms/options';
|
|
9
|
+
|
|
10
|
+
const settings: SiteEditorSettings = { ...options.editor, dev: import.meta.env.DEV, mailer, images };
|
|
11
|
+
|
|
12
|
+
export const editor = (request: Request, answer: (host: EditorHost) => Response | Promise<Response>) =>
|
|
13
|
+
withEditor(request, settings, answer);
|
|
14
|
+
|
|
15
|
+
/** What a request can clear kept pages with. */
|
|
16
|
+
interface PageCache {
|
|
17
|
+
invalidate(options: { tags: string[] }): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Where the site reads its own pages from: never an address a request merely claims. `null` when it cannot tell. */
|
|
21
|
+
export const ownOrigin = (request: Request): string | null => pagesOrigin(request, settings);
|
|
22
|
+
|
|
23
|
+
/** A publish must be seen at once: drops the kept pages, then reads the changed ones for search. */
|
|
24
|
+
export async function showPublished(host: EditorHost, cache: PageCache, origin: string | null): Promise<void> {
|
|
25
|
+
const changed = host.takePublished();
|
|
26
|
+
if (!changed) return;
|
|
27
|
+
await cache.invalidate({ tags: ['cms'] });
|
|
28
|
+
if (origin === null) return;
|
|
29
|
+
await indexPages(origin, changed).catch((error: unknown) => console.error('[velocity-cms] Search was not updated.', error));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** When the editor was last found not to be set up, so a site without one is not asked on every request. */
|
|
33
|
+
let notSetUpAt = 0;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Publishes what was set to go out by now, and shows it. A site whose editor is not set up has
|
|
37
|
+
* nothing scheduled, so that is passed over in silence.
|
|
38
|
+
*/
|
|
39
|
+
export async function publishDue(request: Request, cache: PageCache, options: { now?: boolean } = {}): Promise<void> {
|
|
40
|
+
if (Date.now() - notSetUpAt < 30_000) return;
|
|
41
|
+
const host = await siteEditorHost(request, settings).catch(() => null);
|
|
42
|
+
if (!host) {
|
|
43
|
+
notSetUpAt = Date.now();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
await host.publishDue(options);
|
|
47
|
+
await showPublished(host, cache, ownOrigin(request));
|
|
48
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
|
|
3
|
+
export const prerender = false;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What a page with a form of the CMS runs, so the form is sent without leaving the page. Without
|
|
7
|
+
* it (or without scripts at all) the form still posts, and the site answers with a page.
|
|
8
|
+
*
|
|
9
|
+
* A template marks its form and the places for words:
|
|
10
|
+
*
|
|
11
|
+
* - `<form data-cms-form>`: the form. `data-cms-form-failed` holds what to say when the site
|
|
12
|
+
* cannot be reached. It gains `data-cms-form-state` (`sending`, `sent` or `failed`) to style by.
|
|
13
|
+
* - `[data-cms-form-status]`: where the answer is written, usually a `role="status"`.
|
|
14
|
+
* - `[data-cms-form-error="<field key>"]`: where a problem with that field is written.
|
|
15
|
+
* - a submit button may carry `data-cms-form-sending`, its words while the form is on its way.
|
|
16
|
+
* They replace the button's words, or those of the `[data-cms-form-label]` inside it.
|
|
17
|
+
*
|
|
18
|
+
* Every word is written as text. Nothing the visitor typed comes back from the site.
|
|
19
|
+
*/
|
|
20
|
+
const SCRIPT = `
|
|
21
|
+
const fieldsOf = (form, key) => [...form.elements].filter((element) => element.name === key);
|
|
22
|
+
const errorOf = (form, key) => [...form.querySelectorAll('[data-cms-form-error]')].find((place) => place.dataset.cmsFormError === key);
|
|
23
|
+
|
|
24
|
+
function clear(form, key) {
|
|
25
|
+
const place = errorOf(form, key);
|
|
26
|
+
if (place) {
|
|
27
|
+
place.textContent = '';
|
|
28
|
+
place.hidden = true;
|
|
29
|
+
}
|
|
30
|
+
for (const field of fieldsOf(form, key)) field.removeAttribute('aria-invalid');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function say(form, state, message) {
|
|
34
|
+
form.dataset.cmsFormState = state;
|
|
35
|
+
const status = form.querySelector('[data-cms-form-status]');
|
|
36
|
+
if (status) {
|
|
37
|
+
status.textContent = message;
|
|
38
|
+
status.hidden = message === '';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function send(form) {
|
|
43
|
+
const button = form.querySelector('[type="submit"]');
|
|
44
|
+
const words = button?.querySelector('[data-cms-form-label]') ?? button;
|
|
45
|
+
const label = words?.textContent ?? '';
|
|
46
|
+
for (const place of form.querySelectorAll('[data-cms-form-error]')) clear(form, place.dataset.cmsFormError);
|
|
47
|
+
say(form, 'sending', '');
|
|
48
|
+
if (button) {
|
|
49
|
+
button.disabled = true;
|
|
50
|
+
if (button.dataset.cmsFormSending) words.textContent = button.dataset.cmsFormSending;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const response = await fetch(form.action, { method: 'POST', headers: { accept: 'application/json' }, body: new URLSearchParams(new FormData(form)) });
|
|
54
|
+
const answer = await response.json().catch(() => null);
|
|
55
|
+
if (response.ok && answer?.ok) {
|
|
56
|
+
form.reset();
|
|
57
|
+
say(form, 'sent', String(answer.message ?? ''));
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const issues = Array.isArray(answer?.error?.issues) ? answer.error.issues : [];
|
|
61
|
+
let first = null;
|
|
62
|
+
for (const issue of issues) {
|
|
63
|
+
const key = String(issue?.path?.[0] ?? '');
|
|
64
|
+
const place = errorOf(form, key);
|
|
65
|
+
if (!place) continue;
|
|
66
|
+
place.textContent = String(issue.message ?? '');
|
|
67
|
+
place.hidden = false;
|
|
68
|
+
for (const field of fieldsOf(form, key)) {
|
|
69
|
+
field.setAttribute('aria-invalid', 'true');
|
|
70
|
+
first ??= field;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
say(form, 'failed', String(answer?.error?.message ?? form.dataset.cmsFormFailed ?? ''));
|
|
74
|
+
first?.focus();
|
|
75
|
+
} catch {
|
|
76
|
+
say(form, 'failed', form.dataset.cmsFormFailed ?? '');
|
|
77
|
+
} finally {
|
|
78
|
+
if (button) {
|
|
79
|
+
button.disabled = false;
|
|
80
|
+
words.textContent = label;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function start() {
|
|
86
|
+
for (const form of document.querySelectorAll('form[data-cms-form]')) {
|
|
87
|
+
if (form.dataset.cmsFormReady) continue;
|
|
88
|
+
form.dataset.cmsFormReady = 'true';
|
|
89
|
+
form.addEventListener('submit', (event) => {
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
if (form.dataset.cmsFormState !== 'sending') void send(form);
|
|
92
|
+
});
|
|
93
|
+
// A problem is taken away as soon as its field is touched again.
|
|
94
|
+
form.addEventListener('input', (event) => {
|
|
95
|
+
if (event.target?.name) clear(form, event.target.name);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
start();
|
|
101
|
+
document.addEventListener('astro:after-swap', start);
|
|
102
|
+
`;
|
|
103
|
+
|
|
104
|
+
export const GET: APIRoute = () =>
|
|
105
|
+
new Response(SCRIPT, { headers: { 'Content-Type': 'text/javascript; charset=utf-8', 'Cache-Control': 'public, max-age=3600' } });
|
package/routes/media.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
import { uploadedFile } from '@velocity-astro/cms-astro';
|
|
3
|
+
|
|
4
|
+
export const prerender = false;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* An image someone uploaded in the editor. It may be cached, and says so itself: a file never
|
|
8
|
+
* changes once it is kept.
|
|
9
|
+
*/
|
|
10
|
+
export const GET: APIRoute = ({ params }) => uploadedFile(params.file ?? '');
|
package/routes/pages.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The site's own page files, gathered by Vite. This file is compiled with the site, not with the
|
|
3
|
+
* package, which is why it can see them.
|
|
4
|
+
*/
|
|
5
|
+
import { entryPageAddresses, liveAddresses, type PageModule } from '@velocity-astro/cms-astro';
|
|
6
|
+
import options from 'virtual:velocity-cms/options';
|
|
7
|
+
|
|
8
|
+
const localized = import.meta.glob<PageModule>('/src/live-pages/lang/*.astro', { eager: true });
|
|
9
|
+
const detail = import.meta.glob<PageModule>('/src/pages/*/[[]slug[]].astro', { eager: true });
|
|
10
|
+
const fixed = Object.keys(import.meta.glob('/src/pages/*.astro'));
|
|
11
|
+
|
|
12
|
+
export const addresses = async () => liveAddresses({ fixed, detail, localized, entries: await entryPageAddresses(options.entryPages), unlisted: options.unlisted });
|
|
13
|
+
export const clientDir: string = options.clientDir;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hands the site's plugins to the CMS. Compiled with the site, like every file here, so the
|
|
3
|
+
* site's bundler reads the plugin files; it runs when the server starts, before any request.
|
|
4
|
+
*/
|
|
5
|
+
import { registerSitePlugins } from '@velocity-astro/cms-astro';
|
|
6
|
+
import plugins from 'virtual:velocity-cms/plugins';
|
|
7
|
+
|
|
8
|
+
registerSitePlugins(plugins);
|
|
9
|
+
|
|
10
|
+
export const onRequest = (_context: unknown, next: () => Promise<Response>) => next();
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
|
|
3
|
+
export const prerender = false;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What the page beside the editor's form runs. The preview adds it to that page only, and it
|
|
7
|
+
* does nothing unless the page is in a frame of the same site.
|
|
8
|
+
*
|
|
9
|
+
* It tells the editor which section was clicked, shows the section the editor asks for, and
|
|
10
|
+
* keeps the page where it was scrolled to when it is drawn again. It marks a section by setting
|
|
11
|
+
* its style from script, which a page's content rules allow where a style written into the page
|
|
12
|
+
* would not be.
|
|
13
|
+
*/
|
|
14
|
+
const SCRIPT = `
|
|
15
|
+
if (window.parent !== window) {
|
|
16
|
+
const origin = location.origin;
|
|
17
|
+
const say = (message) => window.parent.postMessage({ source: 'velocity-cms', ...message }, origin);
|
|
18
|
+
const sectionOf = (target) => (target instanceof Element ? target.closest('[data-cms-section]') : null);
|
|
19
|
+
|
|
20
|
+
let marked = null;
|
|
21
|
+
const mark = (section) => {
|
|
22
|
+
if (marked === section) return;
|
|
23
|
+
if (marked) marked.style.outline = marked.style.outlineOffset = marked.style.cursor = '';
|
|
24
|
+
marked = section;
|
|
25
|
+
if (!section) return;
|
|
26
|
+
section.style.outline = '2px solid Highlight';
|
|
27
|
+
section.style.outlineOffset = '-2px';
|
|
28
|
+
section.style.cursor = 'pointer';
|
|
29
|
+
};
|
|
30
|
+
document.addEventListener('mouseover', (event) => mark(sectionOf(event.target)));
|
|
31
|
+
document.documentElement.addEventListener('mouseleave', () => mark(null));
|
|
32
|
+
|
|
33
|
+
// Nothing on the page goes anywhere: a click chooses the section it is in.
|
|
34
|
+
document.addEventListener('click', (event) => {
|
|
35
|
+
event.preventDefault();
|
|
36
|
+
const section = sectionOf(event.target);
|
|
37
|
+
if (section) say({ type: 'section', id: section.dataset.cmsSection });
|
|
38
|
+
}, true);
|
|
39
|
+
document.addEventListener('submit', (event) => event.preventDefault(), true);
|
|
40
|
+
|
|
41
|
+
addEventListener('message', (event) => {
|
|
42
|
+
if (event.origin !== origin || event.source !== window.parent || event.data?.source !== 'velocity-cms') return;
|
|
43
|
+
if (event.data.type !== 'show') return;
|
|
44
|
+
const section = [...document.querySelectorAll('[data-cms-section]')].find((candidate) => candidate.dataset.cmsSection === event.data.id);
|
|
45
|
+
if (!section) return;
|
|
46
|
+
// Room for a header that stays at the top of the page.
|
|
47
|
+
section.style.scrollMarginTop = '6rem';
|
|
48
|
+
section.scrollIntoView({ block: 'start' });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const key = 'velocity-cms:scroll:' + location.pathname;
|
|
52
|
+
try {
|
|
53
|
+
const kept = Number(sessionStorage.getItem(key));
|
|
54
|
+
if (kept > 0) scrollTo({ top: kept, behavior: 'instant' });
|
|
55
|
+
addEventListener('pagehide', () => sessionStorage.setItem(key, String(scrollY)));
|
|
56
|
+
} catch {}
|
|
57
|
+
|
|
58
|
+
say({ type: 'ready' });
|
|
59
|
+
}
|
|
60
|
+
`;
|
|
61
|
+
|
|
62
|
+
export const GET: APIRoute = () =>
|
|
63
|
+
new Response(SCRIPT, { headers: { 'Content-Type': 'text/javascript; charset=utf-8', 'Cache-Control': 'public, max-age=3600' } });
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
import { hasSiteSecret, indexPages, indexSite, invalidateLiveContent } from '@velocity-astro/cms-astro';
|
|
3
|
+
import { ownOrigin } from './editor.ts';
|
|
4
|
+
import { addresses, clientDir } from './pages.ts';
|
|
5
|
+
|
|
6
|
+
export const prerender = false;
|
|
7
|
+
|
|
8
|
+
interface Notice {
|
|
9
|
+
/** Pages whose content changed, or `all` after a deploy. The editor will work these out itself. */
|
|
10
|
+
reindex?: string[] | 'all';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Tells the running site that published content changed: pages are fresh on the next request and
|
|
15
|
+
* search follows. Only the holder of `VELOCITY_CMS_SECRET` may call it. Once the editor runs in
|
|
16
|
+
* this same server it will call these functions directly and this address goes away.
|
|
17
|
+
*/
|
|
18
|
+
export const POST: APIRoute = async ({ cache, request }) => {
|
|
19
|
+
if (!hasSiteSecret(request)) return new Response(null, { status: 404 });
|
|
20
|
+
invalidateLiveContent();
|
|
21
|
+
await cache.invalidate({ tags: ['cms'] });
|
|
22
|
+
const { reindex } = (await request.json().catch(() => ({}))) as Notice;
|
|
23
|
+
const origin = ownOrigin(request);
|
|
24
|
+
if (!reindex || origin === null) return Response.json({});
|
|
25
|
+
const started = performance.now();
|
|
26
|
+
const everything = async () => indexSite(origin, clientDir, (await addresses()).map((address) => address.path));
|
|
27
|
+
const pages = reindex === 'all' ? await everything() : await indexPages(origin, reindex);
|
|
28
|
+
return Response.json({ search: { pages, ms: performance.now() - started } });
|
|
29
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
import { hasSiteSecret } from '@velocity-astro/cms-astro';
|
|
3
|
+
import { publishDue } from './editor.ts';
|
|
4
|
+
|
|
5
|
+
export const prerender = false;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* For a scheduled job: publishes what was set to go out by now, whether or not anyone is
|
|
9
|
+
* visiting. Only the holder of `VELOCITY_CMS_SECRET` may call it, and it answers with nothing
|
|
10
|
+
* about the content.
|
|
11
|
+
*/
|
|
12
|
+
const look: APIRoute = async ({ request, cache }) => {
|
|
13
|
+
cache.set(false);
|
|
14
|
+
if (!hasSiteSecret(request)) return new Response(null, { status: 404, headers: { 'cache-control': 'no-store' } });
|
|
15
|
+
await publishDue(request, cache, { now: true });
|
|
16
|
+
return new Response(null, { status: 204, headers: { 'cache-control': 'no-store' } });
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const GET = look;
|
|
20
|
+
export const POST = look;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
|
|
3
|
+
export const prerender = false;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The browser side of search. It offers the two calls Velocity's search page used from Pagefind
|
|
7
|
+
* and asks the site instead of downloading an index, so the search page itself does not change.
|
|
8
|
+
*/
|
|
9
|
+
const SCRIPT = `
|
|
10
|
+
export async function search(query) {
|
|
11
|
+
const address = '/api/cms/search.json?q=' + encodeURIComponent(query) + '&lang=' + encodeURIComponent(document.documentElement.lang);
|
|
12
|
+
const { total, hits } = await (await fetch(address)).json();
|
|
13
|
+
// The page counts results and reads the first ten, so the list is as long as the total.
|
|
14
|
+
const results = Array.from({ length: total }, (_, position) => ({
|
|
15
|
+
id: hits[position]?.id,
|
|
16
|
+
data: async () => {
|
|
17
|
+
const hit = hits[position];
|
|
18
|
+
return hit && { url: hit.url, excerpt: hit.excerpt, meta: { title: hit.title } };
|
|
19
|
+
},
|
|
20
|
+
}));
|
|
21
|
+
return { results };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let latest = 0;
|
|
25
|
+
export async function debouncedSearch(query, _options, wait = 200) {
|
|
26
|
+
const mine = ++latest;
|
|
27
|
+
await new Promise((resolve) => setTimeout(resolve, wait));
|
|
28
|
+
return mine === latest ? search(query) : null;
|
|
29
|
+
}
|
|
30
|
+
`;
|
|
31
|
+
|
|
32
|
+
export const GET: APIRoute = () =>
|
|
33
|
+
new Response(SCRIPT, { headers: { 'Content-Type': 'text/javascript; charset=utf-8', 'Cache-Control': 'public, max-age=3600' } });
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
import { searchSite } from '@velocity-astro/cms-astro';
|
|
3
|
+
|
|
4
|
+
export const prerender = false;
|
|
5
|
+
|
|
6
|
+
export const GET: APIRoute = async ({ url, cache }) => {
|
|
7
|
+
// Always the database's answer: the index changes with every publish, and a lookup takes a few milliseconds.
|
|
8
|
+
cache.set(false);
|
|
9
|
+
return Response.json(await searchSite(url.searchParams.get('q') ?? '', url.searchParams.get('lang') ?? undefined));
|
|
10
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
// Velocity's own card drawing, from the site: the live card is the same one the build draws.
|
|
3
|
+
import { readPage, renderCard } from '/integrations/og-images.mjs';
|
|
4
|
+
import { ownOrigin } from './editor.ts';
|
|
5
|
+
|
|
6
|
+
export const prerender = false;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The share image of a live page, drawn the first time someone asks for it and then cached like
|
|
10
|
+
* the page itself. It reads the page's own title and description, as the build step did.
|
|
11
|
+
*/
|
|
12
|
+
export const GET: APIRoute = async ({ params, request, url, site }) => {
|
|
13
|
+
const slug = params.live ?? '';
|
|
14
|
+
const pagePath = slug === 'index' ? '/' : slug.endsWith('/index') ? `/${slug.slice(0, -'index'.length)}` : `/${slug}/`;
|
|
15
|
+
// The page is read from the site itself, never from an address the request chose.
|
|
16
|
+
const origin = ownOrigin(request);
|
|
17
|
+
const target = origin === null ? null : new URL(pagePath, origin);
|
|
18
|
+
if (target === null || target.origin !== origin) return new Response(null, { status: 404 });
|
|
19
|
+
const page = await fetch(target);
|
|
20
|
+
if (!page.ok) return new Response(null, { status: 404 });
|
|
21
|
+
|
|
22
|
+
const details = readPage(await page.text());
|
|
23
|
+
if (!details.image || new URL(details.image).pathname !== url.pathname) return new Response(null, { status: 404 });
|
|
24
|
+
|
|
25
|
+
const png = await renderCard({
|
|
26
|
+
title: details.title,
|
|
27
|
+
description: details.description,
|
|
28
|
+
siteName: details.siteName,
|
|
29
|
+
hostname: (site ?? url).host,
|
|
30
|
+
themeColor: details.themeColor,
|
|
31
|
+
});
|
|
32
|
+
return new Response(new Uint8Array(png), { headers: { 'Content-Type': 'image/png' } });
|
|
33
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
import { addresses } from './pages.ts';
|
|
3
|
+
|
|
4
|
+
export const prerender = false;
|
|
5
|
+
|
|
6
|
+
/** The live pages' share of the sitemap. Point `sitemap-index.xml` here with `customSitemaps`. */
|
|
7
|
+
export const GET: APIRoute = async ({ site, url }) => {
|
|
8
|
+
const urls = (await addresses())
|
|
9
|
+
.filter((address) => address.listed && !address.fixed)
|
|
10
|
+
.map((address) => `<url><loc>${new URL(address.path, site ?? url).href}</loc></url>`);
|
|
11
|
+
const xml = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urls.join('')}</urlset>`;
|
|
12
|
+
return new Response(xml, { headers: { 'Content-Type': 'application/xml; charset=utf-8' } });
|
|
13
|
+
};
|
package/routes/submit.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { APIRoute } from 'astro';
|
|
2
|
+
import { siteFormPosts, type SiteEditorSettings } from '@velocity-astro/cms-astro';
|
|
3
|
+
import images from 'virtual:velocity-cms/images';
|
|
4
|
+
import mailer from 'virtual:velocity-cms/mailer';
|
|
5
|
+
import options from 'virtual:velocity-cms/options';
|
|
6
|
+
|
|
7
|
+
export const prerender = false;
|
|
8
|
+
|
|
9
|
+
const settings: SiteEditorSettings = { ...options.editor, dev: import.meta.env.DEV, mailer, images };
|
|
10
|
+
|
|
11
|
+
/** Where the site's forms post. The one address of the CMS that anyone may send to: every check is in `FormPosts`. */
|
|
12
|
+
export const ALL: APIRoute = async (context) => {
|
|
13
|
+
// An answer to one visitor is never kept for the next.
|
|
14
|
+
context.cache.set(false);
|
|
15
|
+
let address: string | undefined;
|
|
16
|
+
try {
|
|
17
|
+
address = context.clientAddress;
|
|
18
|
+
} catch {
|
|
19
|
+
// Some hosts do not say. The other checks still stand.
|
|
20
|
+
}
|
|
21
|
+
return (await siteFormPosts(settings)).handle(context.request, context.params.form ?? '', address);
|
|
22
|
+
};
|