@velocity-astro/cms-astro 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.
Files changed (154) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +12 -2
  3. package/bin/velocity-cms.mjs +6 -0
  4. package/dist/admin-files.d.ts +21 -0
  5. package/dist/admin-files.d.ts.map +1 -0
  6. package/dist/admin-files.js +83 -0
  7. package/dist/admin-files.js.map +1 -0
  8. package/dist/challenge.d.ts +35 -0
  9. package/dist/challenge.d.ts.map +1 -0
  10. package/dist/challenge.js +84 -0
  11. package/dist/challenge.js.map +1 -0
  12. package/dist/cli.d.ts +9 -0
  13. package/dist/cli.d.ts.map +1 -0
  14. package/dist/cli.js +76 -0
  15. package/dist/cli.js.map +1 -0
  16. package/dist/connection.d.ts +26 -0
  17. package/dist/connection.d.ts.map +1 -0
  18. package/dist/connection.js +87 -0
  19. package/dist/connection.js.map +1 -0
  20. package/dist/editor.d.ts +67 -0
  21. package/dist/editor.d.ts.map +1 -0
  22. package/dist/editor.js +194 -0
  23. package/dist/editor.js.map +1 -0
  24. package/dist/entry-page.d.ts +37 -0
  25. package/dist/entry-page.d.ts.map +1 -0
  26. package/dist/entry-page.js +56 -0
  27. package/dist/entry-page.js.map +1 -0
  28. package/dist/form-post.d.ts +114 -0
  29. package/dist/form-post.d.ts.map +1 -0
  30. package/dist/form-post.js +298 -0
  31. package/dist/form-post.js.map +1 -0
  32. package/dist/hosted.d.ts +76 -0
  33. package/dist/hosted.d.ts.map +1 -0
  34. package/dist/hosted.js +104 -0
  35. package/dist/hosted.js.map +1 -0
  36. package/dist/image-optimizer.d.ts +35 -0
  37. package/dist/image-optimizer.d.ts.map +1 -0
  38. package/dist/image-optimizer.js +76 -0
  39. package/dist/image-optimizer.js.map +1 -0
  40. package/dist/index.d.ts +34 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +34 -0
  43. package/dist/index.js.map +1 -0
  44. package/dist/init.d.ts +35 -0
  45. package/dist/init.d.ts.map +1 -0
  46. package/dist/init.js +140 -0
  47. package/dist/init.js.map +1 -0
  48. package/dist/integration.d.ts +72 -0
  49. package/dist/integration.d.ts.map +1 -0
  50. package/dist/integration.js +143 -0
  51. package/dist/integration.js.map +1 -0
  52. package/dist/link.d.ts +32 -0
  53. package/dist/link.d.ts.map +1 -0
  54. package/dist/link.js +80 -0
  55. package/dist/link.js.map +1 -0
  56. package/dist/live-loader.d.ts +44 -0
  57. package/dist/live-loader.d.ts.map +1 -0
  58. package/dist/live-loader.js +98 -0
  59. package/dist/live-loader.js.map +1 -0
  60. package/dist/live-pages.d.ts +69 -0
  61. package/dist/live-pages.d.ts.map +1 -0
  62. package/dist/live-pages.js +94 -0
  63. package/dist/live-pages.js.map +1 -0
  64. package/dist/loader.d.ts +22 -0
  65. package/dist/loader.d.ts.map +1 -0
  66. package/dist/loader.js +62 -0
  67. package/dist/loader.js.map +1 -0
  68. package/dist/media-store.d.ts +61 -0
  69. package/dist/media-store.d.ts.map +1 -0
  70. package/dist/media-store.js +178 -0
  71. package/dist/media-store.js.map +1 -0
  72. package/dist/menu.d.ts +51 -0
  73. package/dist/menu.d.ts.map +1 -0
  74. package/dist/menu.js +81 -0
  75. package/dist/menu.js.map +1 -0
  76. package/dist/page-head.d.ts +23 -0
  77. package/dist/page-head.d.ts.map +1 -0
  78. package/dist/page-head.js +66 -0
  79. package/dist/page-head.js.map +1 -0
  80. package/dist/page-marks.d.ts +50 -0
  81. package/dist/page-marks.d.ts.map +1 -0
  82. package/dist/page-marks.js +90 -0
  83. package/dist/page-marks.js.map +1 -0
  84. package/dist/preview.d.ts +92 -0
  85. package/dist/preview.d.ts.map +1 -0
  86. package/dist/preview.js +192 -0
  87. package/dist/preview.js.map +1 -0
  88. package/dist/resend-mailer.d.ts +63 -0
  89. package/dist/resend-mailer.d.ts.map +1 -0
  90. package/dist/resend-mailer.js +116 -0
  91. package/dist/resend-mailer.js.map +1 -0
  92. package/dist/secret.d.ts +8 -0
  93. package/dist/secret.d.ts.map +1 -0
  94. package/dist/secret.js +17 -0
  95. package/dist/secret.js.map +1 -0
  96. package/dist/single.d.ts +18 -0
  97. package/dist/single.d.ts.map +1 -0
  98. package/dist/single.js +37 -0
  99. package/dist/single.js.map +1 -0
  100. package/dist/site-editor.d.ts +41 -0
  101. package/dist/site-editor.d.ts.map +1 -0
  102. package/dist/site-editor.js +140 -0
  103. package/dist/site-editor.js.map +1 -0
  104. package/dist/site-forms.d.ts +54 -0
  105. package/dist/site-forms.d.ts.map +1 -0
  106. package/dist/site-forms.js +100 -0
  107. package/dist/site-forms.js.map +1 -0
  108. package/dist/site-images.d.ts +17 -0
  109. package/dist/site-images.d.ts.map +1 -0
  110. package/dist/site-images.js +22 -0
  111. package/dist/site-images.js.map +1 -0
  112. package/dist/site-mail.d.ts +45 -0
  113. package/dist/site-mail.d.ts.map +1 -0
  114. package/dist/site-mail.js +84 -0
  115. package/dist/site-mail.js.map +1 -0
  116. package/dist/site-media.d.ts +22 -0
  117. package/dist/site-media.d.ts.map +1 -0
  118. package/dist/site-media.js +52 -0
  119. package/dist/site-media.js.map +1 -0
  120. package/dist/site-name.d.ts +9 -0
  121. package/dist/site-name.d.ts.map +1 -0
  122. package/dist/site-name.js +21 -0
  123. package/dist/site-name.js.map +1 -0
  124. package/dist/site-plugins.d.ts +10 -0
  125. package/dist/site-plugins.d.ts.map +1 -0
  126. package/dist/site-plugins.js +23 -0
  127. package/dist/site-plugins.js.map +1 -0
  128. package/dist/site-search.d.ts +12 -0
  129. package/dist/site-search.d.ts.map +1 -0
  130. package/dist/site-search.js +79 -0
  131. package/dist/site-search.js.map +1 -0
  132. package/dist/site-storage.d.ts +34 -0
  133. package/dist/site-storage.d.ts.map +1 -0
  134. package/dist/site-storage.js +118 -0
  135. package/dist/site-storage.js.map +1 -0
  136. package/package.json +56 -3
  137. package/routes/admin.ts +10 -0
  138. package/routes/api.ts +15 -0
  139. package/routes/auth.ts +10 -0
  140. package/routes/due.ts +12 -0
  141. package/routes/editor.ts +49 -0
  142. package/routes/form-client.js.ts +173 -0
  143. package/routes/media.ts +10 -0
  144. package/routes/pages.ts +13 -0
  145. package/routes/plugins.ts +10 -0
  146. package/routes/preview-client.js.ts +18 -0
  147. package/routes/published.ts +29 -0
  148. package/routes/schedule.ts +20 -0
  149. package/routes/search-client.js.ts +33 -0
  150. package/routes/search.json.ts +10 -0
  151. package/routes/share-image.png.ts +33 -0
  152. package/routes/sitemap-live.xml.ts +16 -0
  153. package/routes/sites.ts +11 -0
  154. package/routes/submit.ts +22 -0
@@ -0,0 +1,178 @@
1
+ import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ /** A key is one file name the CMS made: nothing that could name a folder or another place. */
4
+ const KEY = /^[a-z0-9]+(?:-[a-z0-9]+)*\.(?:jpg|png|webp)$/;
5
+ const TYPES = { jpg: 'image/jpeg', png: 'image/png', webp: 'image/webp' };
6
+ const typeOf = (key) => TYPES[key.slice(key.lastIndexOf('.') + 1)] ?? 'application/octet-stream';
7
+ function checked(key) {
8
+ if (!KEY.test(key))
9
+ throw new Error(`"${key}" is not a name an uploaded file can be kept under.`);
10
+ return key;
11
+ }
12
+ /** Keeps uploads in a folder on the server's disk. For a site on a server of its own. */
13
+ export class FolderMediaStore {
14
+ directory;
15
+ constructor(directory) {
16
+ this.directory = directory;
17
+ }
18
+ async put(key, file) {
19
+ await mkdir(this.directory, { recursive: true });
20
+ await writeFile(path.join(this.directory, checked(key)), file.bytes);
21
+ }
22
+ async get(key) {
23
+ if (!KEY.test(key))
24
+ return null;
25
+ try {
26
+ return { bytes: new Uint8Array(await readFile(path.join(this.directory, key))), contentType: typeOf(key) };
27
+ }
28
+ catch (error) {
29
+ if (error.code === 'ENOENT')
30
+ return null;
31
+ throw error;
32
+ }
33
+ }
34
+ async remove(key) {
35
+ if (KEY.test(key))
36
+ await rm(path.join(this.directory, key), { force: true });
37
+ }
38
+ }
39
+ /** A prefix is made of plain parts, so it can never climb out of its folder or need encoding. */
40
+ const PREFIX = /^[a-z0-9]+(?:-[a-z0-9]+)*(?:\/[a-z0-9]+(?:-[a-z0-9]+)*)*$/;
41
+ const encoder = new TextEncoder();
42
+ const hex = (bytes) => [...new Uint8Array(bytes)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
43
+ const sha256 = async (bytes) => hex(await crypto.subtle.digest('SHA-256', bytes));
44
+ async function hmac(key, text) {
45
+ const imported = await crypto.subtle.importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
46
+ return crypto.subtle.sign('HMAC', imported, encoder.encode(text));
47
+ }
48
+ /** The bucket answered and refused. `status` is its answer; the message holds its own words, for the server's log. */
49
+ export class StorageRefusedError extends Error {
50
+ status;
51
+ constructor(status, message) {
52
+ super(message);
53
+ this.status = status;
54
+ }
55
+ }
56
+ /**
57
+ * Keeps uploads in a bucket that speaks the S3 protocol: Cloudflare R2, Amazon S3, Backblaze B2,
58
+ * MinIO and others. For a site on a host with no disk of its own. The bucket stays private: the
59
+ * site reads from it with its key and serves the files itself.
60
+ */
61
+ export class S3MediaStore {
62
+ #options;
63
+ #region;
64
+ /** Empty, or the prefix with its closing slash. */
65
+ #folder;
66
+ constructor(options) {
67
+ if (options.prefix !== undefined && !PREFIX.test(options.prefix)) {
68
+ throw new Error(`"${options.prefix}" cannot be a prefix for uploads. Use lowercase letters, digits and hyphens, in parts divided by "/", such as "sites/s-4f2a".`);
69
+ }
70
+ this.#options = options;
71
+ this.#region = options.region ?? 'auto';
72
+ this.#folder = options.prefix ? `${options.prefix}/` : '';
73
+ }
74
+ async put(key, file) {
75
+ const response = await this.#send('PUT', checked(key), file);
76
+ if (!response.ok)
77
+ throw await this.#failure('keep', key, response);
78
+ }
79
+ async get(key) {
80
+ if (!KEY.test(key))
81
+ return null;
82
+ const response = await this.#send('GET', key);
83
+ if (response.status === 404)
84
+ return null;
85
+ if (!response.ok)
86
+ throw await this.#failure('read', key, response);
87
+ return { bytes: new Uint8Array(await response.arrayBuffer()), contentType: response.headers.get('content-type') ?? typeOf(key) };
88
+ }
89
+ async remove(key) {
90
+ if (!KEY.test(key))
91
+ return;
92
+ const response = await this.#send('DELETE', key);
93
+ if (!response.ok && response.status !== 404)
94
+ throw await this.#failure('remove', key, response);
95
+ }
96
+ /** One request, signed the way the protocol asks (Signature Version 4). */
97
+ async #send(method, key, file) {
98
+ const { endpoint, bucket, accessKeyId, secretAccessKey } = this.#options;
99
+ const url = new URL(`${endpoint.replace(/\/+$/, '')}/${encodeURIComponent(bucket)}/${this.#folder}${key}`);
100
+ const stamp = (this.#options.now?.() ?? new Date()).toISOString().replace(/[-:]|\.\d{3}/g, '');
101
+ const day = stamp.slice(0, 8);
102
+ const headers = {
103
+ host: url.host,
104
+ 'x-amz-content-sha256': await sha256(file?.bytes ?? new Uint8Array()),
105
+ 'x-amz-date': stamp,
106
+ ...(file && { 'content-type': file.contentType }),
107
+ };
108
+ const names = Object.keys(headers).sort();
109
+ const request = [
110
+ method,
111
+ url.pathname,
112
+ '',
113
+ ...names.map((name) => `${name}:${headers[name]}`),
114
+ '',
115
+ names.join(';'),
116
+ headers['x-amz-content-sha256'],
117
+ ].join('\n');
118
+ const scope = `${day}/${this.#region}/s3/aws4_request`;
119
+ const toSign = ['AWS4-HMAC-SHA256', stamp, scope, await sha256(encoder.encode(request))].join('\n');
120
+ let signingKey = encoder.encode(`AWS4${secretAccessKey}`);
121
+ for (const part of [day, this.#region, 's3', 'aws4_request'])
122
+ signingKey = await hmac(signingKey, part);
123
+ const signature = hex(await hmac(signingKey, toSign));
124
+ const { host: _host, ...sent } = headers;
125
+ return (this.#options.fetch ?? globalThis.fetch)(url, {
126
+ method,
127
+ headers: {
128
+ ...sent,
129
+ authorization: `AWS4-HMAC-SHA256 Credential=${accessKeyId}/${scope}, SignedHeaders=${names.join(';')}, Signature=${signature}`,
130
+ },
131
+ ...(file && { body: file.bytes }),
132
+ });
133
+ }
134
+ /** What went wrong, in the storage service's words. It goes to the server's log, not to the person. */
135
+ async #failure(doing, key, response) {
136
+ const said = (await response.text().catch(() => '')).replace(/\s+/g, ' ').slice(0, 300);
137
+ return new StorageRefusedError(response.status, `The storage bucket "${this.#options.bucket}" refused to ${doing} "${key}" (${response.status}). ${said}`.trim());
138
+ }
139
+ }
140
+ /**
141
+ * Where this site keeps uploads, from its environment:
142
+ *
143
+ * - `VELOCITY_CMS_S3_BUCKET`, `VELOCITY_CMS_S3_ENDPOINT`, `VELOCITY_CMS_S3_ACCESS_KEY_ID` and
144
+ * `VELOCITY_CMS_S3_SECRET_ACCESS_KEY` (and `VELOCITY_CMS_S3_REGION`): a bucket.
145
+ * - `VELOCITY_CMS_MEDIA_DIR`: a folder.
146
+ * - Neither, with the database in a file: the folder `media` beside the database, since a
147
+ * server that keeps its database on disk has a disk to keep files on.
148
+ * - Neither, with a hosted database: nowhere, and the site takes no uploads.
149
+ */
150
+ export function mediaStoreFromEnv(databaseUrl, env = process.env) {
151
+ return bucketFromEnv(env) ?? folderFromEnv(databaseUrl, env);
152
+ }
153
+ /** The bucket the server's own settings name, or none. Settings that name only part of one are refused. */
154
+ export function bucketFromEnv(env = process.env) {
155
+ const s3 = {
156
+ bucket: env.VELOCITY_CMS_S3_BUCKET,
157
+ endpoint: env.VELOCITY_CMS_S3_ENDPOINT,
158
+ accessKeyId: env.VELOCITY_CMS_S3_ACCESS_KEY_ID,
159
+ secretAccessKey: env.VELOCITY_CMS_S3_SECRET_ACCESS_KEY,
160
+ };
161
+ const given = Object.values(s3).filter(Boolean).length;
162
+ if (s3.bucket && s3.endpoint && s3.accessKeyId && s3.secretAccessKey) {
163
+ return new S3MediaStore({ ...s3, bucket: s3.bucket, endpoint: s3.endpoint, accessKeyId: s3.accessKeyId, secretAccessKey: s3.secretAccessKey, ...(env.VELOCITY_CMS_S3_REGION && { region: env.VELOCITY_CMS_S3_REGION }) });
164
+ }
165
+ if (given > 0) {
166
+ throw new Error('Uploads are set up only in part. A bucket needs all four of VELOCITY_CMS_S3_BUCKET, VELOCITY_CMS_S3_ENDPOINT, VELOCITY_CMS_S3_ACCESS_KEY_ID and VELOCITY_CMS_S3_SECRET_ACCESS_KEY.');
167
+ }
168
+ return undefined;
169
+ }
170
+ /** The folder on the server's disk uploads are kept in: the one its settings name, or `media` beside a database that is a file. */
171
+ export function folderFromEnv(databaseUrl, env = process.env) {
172
+ if (env.VELOCITY_CMS_MEDIA_DIR)
173
+ return new FolderMediaStore(path.resolve(env.VELOCITY_CMS_MEDIA_DIR));
174
+ if (databaseUrl.startsWith('file:'))
175
+ return new FolderMediaStore(path.join(path.dirname(path.resolve(databaseUrl.slice('file:'.length))), 'media'));
176
+ return undefined;
177
+ }
178
+ //# sourceMappingURL=media-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"media-store.js","sourceRoot":"","sources":["../src/media-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,8FAA8F;AAC9F,MAAM,GAAG,GAAG,8CAA8C,CAAC;AAC3D,MAAM,KAAK,GAAqC,EAAE,GAAG,EAAE,YAAY,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,YAAY,EAAE,CAAC;AAC5G,MAAM,MAAM,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,0BAA0B,CAAC;AAEjH,SAAS,OAAO,CAAC,GAAW;IAC1B,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,IAAI,GAAG,qDAAqD,CAAC,CAAC;IAClG,OAAO,GAAG,CAAC;AACb,CAAC;AAED,yFAAyF;AACzF,MAAM,OA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,EAAE,CAAC,QAAQ,IAAI,EAAE,CAAC,WAAW,IAAI,EAAE,CAAC,eAAe,EAAE,CAAC;QACrE,OAAO,IAAI,YAAY,CAAC,EAAE,GAAG,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,CAAC,QAAQ,EAAE,WAAW,EAAE,EAAE,CAAC,WAAW,EAAE,eAAe,EAAE,EAAE,CAAC,eAAe,EAAE,GAAG,CAAC,GAAG,CAAC,sBAAsB,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,sBAAsB,EAAE,CAAC,EAAE,CAAC,CAAC;IAC5N,CAAC;IACD,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CACb,oLAAoL,CACrL,CAAC;IACJ,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,mIAAmI;AACnI,MAAM,UAAU,aAAa,CAAC,WAAmB,EAAE,MAAoD,OAAO,CAAC,GAAG;IAChH,IAAI,GAAG,CAAC,sBAAsB;QAAE,OAAO,IAAI,gBAAgB,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC,CAAC;IACtG,IAAI,WAAW,CAAC,UAAU,CAAC,OAAO,CAAC;QAAE,OAAO,IAAI,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;IACpJ,OAAO,SAAS,CAAC;AACnB,CAAC"}
package/dist/menu.d.ts ADDED
@@ -0,0 +1,51 @@
1
+ import type { Cms } from '@velocity-astro/cms-core';
2
+ import type { DrawnPage } from './page-head.js';
3
+ /** One link of a menu, ready to be written into a page. */
4
+ export interface MenuLink {
5
+ readonly label: string;
6
+ /** A path on this site, or a whole address when the link leaves it. */
7
+ readonly href: string;
8
+ /** True when the link leaves the site. */
9
+ readonly external: boolean;
10
+ /** What opens under the link, when it has anything: a large menu's columns. Left out otherwise. */
11
+ readonly columns?: readonly MenuColumn[];
12
+ }
13
+ /** One column of the menu that opens under a link: a heading and its own links. */
14
+ export interface MenuColumn {
15
+ readonly title: string;
16
+ readonly links: readonly MenuLink[];
17
+ /**
18
+ * What the template is asked to fill the column with, when the item has a `show` choice: the
19
+ * template's own word for it, such as `neighborhoods`. Left out, the column shows its links.
20
+ */
21
+ readonly show?: string;
22
+ }
23
+ export interface MenuOptions {
24
+ /** The key of the content type that holds the site's menus, e.g. `navigation`. Its entry in the page's language is read. */
25
+ readonly type: string;
26
+ /** The key of the list field that is the menu, e.g. `header`. Each item is a group with a label and a link. */
27
+ readonly field: string;
28
+ /** The keys of an item's label and link fields. `label` and `link` unless told otherwise. */
29
+ readonly labelKey?: string;
30
+ readonly linkKey?: string;
31
+ /**
32
+ * The key of an item's list of columns: `columns` unless told otherwise. Each column is a group
33
+ * with a `title`, a list of `links` (label and link, as above) and, if the template has columns
34
+ * it fills itself, a `show` choice.
35
+ */
36
+ readonly columnsKey?: string;
37
+ }
38
+ /** `menuLinks` for a CMS already at hand. `preview` reads the versions being worked on. */
39
+ export declare function menuLinksFrom(cms: Cms, locale: string, options: MenuOptions, preview?: boolean): Promise<MenuLink[]>;
40
+ /**
41
+ * A menu people edit in the CMS, as links ready for the page being drawn:
42
+ *
43
+ * ```astro
44
+ * const links = await menuLinks(Astro, { type: 'navigation', field: 'header' });
45
+ * ```
46
+ *
47
+ * It answers with no links when there is no menu in the page's language yet, and also when anything goes wrong: a page
48
+ * is never lost over its menu, and the template shows its own links instead.
49
+ */
50
+ export declare function menuLinks(page: DrawnPage, options: MenuOptions): Promise<MenuLink[]>;
51
+ //# sourceMappingURL=menu.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"menu.d.ts","sourceRoot":"","sources":["../src/menu.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EA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u.js ADDED
@@ -0,0 +1,81 @@
1
+ import { sharedConnection } from './connection.js';
2
+ import { DEFAULT_LOCALE, entriesOf, linkResolver } from './link.js';
3
+ import { isPreview, withPreviewDraft } from './preview.js';
4
+ const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
5
+ /** `menuLinks` for a CMS already at hand. `preview` reads the versions being worked on. */
6
+ export async function menuLinksFrom(cms, locale, options, preview = false) {
7
+ const types = await cms.schema.list();
8
+ const holder = types.find((type) => type.key === options.type);
9
+ if (!holder)
10
+ return [];
11
+ // A language with no menu of its own has no links here, so the template shows its own, in that
12
+ // language. Another language's menu would put the wrong words on every page.
13
+ const menu = (await entriesOf(cms, holder)).find((entry) => entry.locale === locale && (preview || entry.published !== undefined));
14
+ if (!menu)
15
+ return [];
16
+ const items = cms.content.toPublic(holder, withPreviewDraft(menu), preview ? 'working' : 'published').data[options.field];
17
+ if (!Array.isArray(items))
18
+ return [];
19
+ const resolve = linkResolver(cms, locale, preview);
20
+ /** One item as a link, or nothing when it has no label or leads nowhere a visitor can go. */
21
+ const linkOf = async (item) => {
22
+ if (!isRecord(item))
23
+ return undefined;
24
+ const label = item[options.labelKey ?? 'label'];
25
+ const link = item[options.linkKey ?? 'link'];
26
+ if (typeof label !== 'string' || label.trim() === '' || !isRecord(link))
27
+ return undefined;
28
+ const target = await resolve(link);
29
+ return target && { label, ...target };
30
+ };
31
+ const linksOf = async (list) => {
32
+ const found = [];
33
+ for (const item of Array.isArray(list) ? list : []) {
34
+ const link = await linkOf(item);
35
+ if (link)
36
+ found.push(link);
37
+ }
38
+ return found;
39
+ };
40
+ const links = [];
41
+ for (const item of items) {
42
+ const link = await linkOf(item);
43
+ if (!link || !isRecord(item))
44
+ continue;
45
+ const columns = [];
46
+ const described = item[options.columnsKey ?? 'columns'];
47
+ for (const column of Array.isArray(described) ? described : []) {
48
+ if (!isRecord(column))
49
+ continue;
50
+ const title = typeof column['title'] === 'string' ? column['title'] : '';
51
+ const show = typeof column['show'] === 'string' && column['show'] !== 'links' ? column['show'] : undefined;
52
+ const inside = await linksOf(column['links']);
53
+ // A column with nothing in it, and nothing the template fills it with, is left out.
54
+ if (inside.length > 0 || show !== undefined)
55
+ columns.push({ title, links: inside, ...(show !== undefined && { show }) });
56
+ }
57
+ links.push(columns.length > 0 ? { ...link, columns } : link);
58
+ }
59
+ return links;
60
+ }
61
+ /**
62
+ * A menu people edit in the CMS, as links ready for the page being drawn:
63
+ *
64
+ * ```astro
65
+ * const links = await menuLinks(Astro, { type: 'navigation', field: 'header' });
66
+ * ```
67
+ *
68
+ * It answers with no links when there is no menu in the page's language yet, and also when anything goes wrong: a page
69
+ * is never lost over its menu, and the template shows its own links instead.
70
+ */
71
+ export async function menuLinks(page, options) {
72
+ try {
73
+ const { cms } = await sharedConnection({});
74
+ return await menuLinksFrom(cms, page.currentLocale ?? DEFAULT_LOCALE, options, isPreview());
75
+ }
76
+ catch (error) {
77
+ console.error('[velocity-cms] The menu could not be read, so the template\'s own links are used.', error);
78
+ return [];
79
+ }
80
+ }
81
+ //# sourceMappingURL=menu.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"menu.js","sourceRoot":"","sources":["../src/menu.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEpE,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAwC3D,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAoC,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAE5I,2FAA2F;AAC3F,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,GAAQ,EAAE,MAAc,EAAE,OAAoB,EAAE,OAAO,GAAG,KAAK;IACjG,MAAM,K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H,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { type Cms, type ContentTypeDef, type PageHead, type PageInfo } from '@velocity-astro/cms-core';
2
+ /** What `pageHead` needs of the page being drawn: `Astro` has both. */
3
+ export interface DrawnPage {
4
+ readonly url: URL;
5
+ readonly currentLocale?: string | undefined;
6
+ }
7
+ /** The entry whose own page is at `path`, if any: the published one, or in a preview the one being worked on. */
8
+ export declare function pageAt(cms: Cms, types: readonly ContentTypeDef[], path: string): Promise<Pick<PageInfo, 'entry' | 'type'> & {
9
+ locale?: string;
10
+ }>;
11
+ /**
12
+ * What the page being drawn should say about itself to search engines, after the site's plugins
13
+ * have had their say. A template calls it where it writes its `<title>` and description:
14
+ *
15
+ * ```astro
16
+ * const head = await pageHead(Astro, { title, description, noindex });
17
+ * ```
18
+ *
19
+ * It gives back what it was given when no plugin changes anything, and also when anything goes
20
+ * wrong: a page is never lost over its description.
21
+ */
22
+ export declare function pageHead(page: DrawnPage, head: PageHead): Promise<PageHead>;
23
+ //# sourceMappingURL=page-head.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page-head.d.ts","sourceRoot":"","sources":["../src/page-head.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,KAAK,GAAG,EAAE,KAAK,cAAc,EAAE,KAAK,QAAQ,EAAE,KAAK,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAOtI,uEAAuE;AACvE,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAClB,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC7C;AAmBD,iHAAiH;AACjH,wBAAsB,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,SAAS,cAAc,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,CAAC,GAAG;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAYtJ;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC,CAWjF"}
@@ -0,0 +1,66 @@
1
+ import { HOME_SLUG, isSlug, isSlugPath } from '@velocity-astro/cms-core';
2
+ import { sharedConnection, SITE } from './connection.js';
3
+ import { isPreview, withPreviewDraft } from './preview.js';
4
+ /** The language of the pages at a type's `urlPattern`. */
5
+ const DEFAULT_LOCALE = 'en';
6
+ /** A type's address pattern as something an address can be tried against: the slug, when it fits. */
7
+ function slugIn(pattern, path) {
8
+ // Only a pattern whose one name is the slug says which entry a page is for. `[...slug]` is a
9
+ // slug that may have several parts, so pages can sit under other pages.
10
+ const name = pattern.includes('[...slug]') ? '[...slug]' : '[slug]';
11
+ if (pattern.replace(name, '').includes('[') || !pattern.includes(name))
12
+ return null;
13
+ const trim = (text) => text.replace(/\/+$/, '');
14
+ const [before = '', after = ''] = trim(pattern).split(name);
15
+ const address = trim(path);
16
+ // The top of a [...slug] pattern is the page called index, and that page has no other address.
17
+ if (name === '[...slug]' && address === trim(trim(before) + after))
18
+ return HOME_SLUG;
19
+ if (!address.startsWith(before) || !address.endsWith(after) || address.length <= before.length + after.length)
20
+ return null;
21
+ const slug = address.slice(before.length, address.length - after.length);
22
+ if (name === '[slug]')
23
+ return isSlug(slug) ? slug : null;
24
+ return isSlugPath(slug) && slug !== HOME_SLUG ? slug : null;
25
+ }
26
+ /** The entry whose own page is at `path`, if any: the published one, or in a preview the one being worked on. */
27
+ export async function pageAt(cms, types, path) {
28
+ for (const type of types) {
29
+ const patterns = [[DEFAULT_LOCALE, type.urlPattern], ...Object.entries(type.localizedUrlPatterns ?? {})];
30
+ for (const [locale, pattern] of patterns) {
31
+ const slug = pattern === undefined ? null : slugIn(pattern, path);
32
+ if (slug === null)
33
+ continue;
34
+ const version = isPreview() ? 'working' : 'published';
35
+ const entry = await cms.content.findBySlug(SITE, { typeId: type.id, locale, slug, version });
36
+ if (entry)
37
+ return { type, locale, entry: cms.content.toPublic(type, withPreviewDraft(entry), version) };
38
+ }
39
+ }
40
+ return {};
41
+ }
42
+ /**
43
+ * What the page being drawn should say about itself to search engines, after the site's plugins
44
+ * have had their say. A template calls it where it writes its `<title>` and description:
45
+ *
46
+ * ```astro
47
+ * const head = await pageHead(Astro, { title, description, noindex });
48
+ * ```
49
+ *
50
+ * It gives back what it was given when no plugin changes anything, and also when anything goes
51
+ * wrong: a page is never lost over its description.
52
+ */
53
+ export async function pageHead(page, head) {
54
+ try {
55
+ const { cms } = await sharedConnection({});
56
+ const path = page.url.pathname;
57
+ const found = await pageAt(cms, await cms.schema.list(), path);
58
+ const { locale = page.currentLocale ?? DEFAULT_LOCALE, ...entry } = found;
59
+ return await cms.pages.run({ path, locale, ...entry }, head);
60
+ }
61
+ catch (error) {
62
+ console.error('[velocity-cms] The page head could not be worked out, so the template\'s own is used.', error);
63
+ return head;
64
+ }
65
+ }
66
+ //# sourceMappingURL=page-head.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page-head.js","sourceRoot":"","sources":["../src/page-head.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAA+D,MAAM,0BAA0B,CAAC;AACtI,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAE3D,0DAA0D;AAC1D,MAAM,cAAc,GAAG,IAAI,CAAC;AAQ5B,qGAAqG;AACrG,SAAS,MAAM,CAAC,OAAe,EAAE,IAAY;IAC3C,6FAA6F;IAC7F,wEAAwE;IACxE,MAAM,IAAI,GAAG,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ,CAAC;IACpE,IAAI,OAAO,CAAC,OAAO,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CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;QAC/D,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC,aAAa,IAAI,cAAc,EAAE,GAAG,KAAK,EAAE,GAAG,KAAK,CAAC;QAC1E,OAAO,MAAM,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,EAAE,IAAI,CAAC,CAAC;IAC/D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,uFAAuF,EAAE,KAAK,CAAC,CAAC;QAC9G,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,50 @@
1
+ /** Attributes a template spreads on an element of a page. Nothing at all, except beside the editor's form. */
2
+ export type PageMark = Readonly<Record<string, string>>;
3
+ /**
4
+ * Says which section of a page an element is, for the page beside the editor's form: a click on
5
+ * it there finds the section in the form. A template spreads it on the element that wraps the
6
+ * section:
7
+ *
8
+ * ```astro
9
+ * <div {...sectionMark(section.id, section.data)}><Block {...section.data} /></div>
10
+ * ```
11
+ *
12
+ * Given what the section holds, the mark also says which words belong to which field
13
+ * (`data-cms-values`), and the page beside the form finds each field by the words it shows, so a
14
+ * click on a heading opens the heading's field with no mark on the heading. Where that is not
15
+ * enough (a value the block changes before it shows it), the block says so with `fieldMark`.
16
+ *
17
+ * A visitor's page gets no attribute, so nothing about the editor reaches the site.
18
+ */
19
+ export declare function sectionMark(id: string, data?: unknown): PageMark;
20
+ /**
21
+ * Says which place inside a section an element is: one that holds sections of its own, such as a
22
+ * column of a row. Given the section's id and where the place is in it, as `fieldMark` is:
23
+ *
24
+ * ```astro
25
+ * {columns.map((column, at) => (
26
+ * <div {...slotMark(section.id, 'columns', at, 'content')}>
27
+ * {column.content.map((inner) => <div {...sectionMark(inner.id, inner.data)}>…</div>)}
28
+ * </div>
29
+ * ))}
30
+ * ```
31
+ *
32
+ * Beside the editor's form, an empty place then has a button that adds its first section, and a
33
+ * section can be dragged from one place to another. A visitor's page gets no attribute.
34
+ */
35
+ export declare function slotMark(sectionId: string, ...path: readonly (string | number)[]): PageMark;
36
+ /**
37
+ * Says which field of its section an element shows, by the field's key, so a click on it beside
38
+ * the editor's form opens that field and not only the section:
39
+ *
40
+ * ```astro
41
+ * <h1 {...fieldMark('title')}>{title}</h1>
42
+ * {items.map((item, at) => <li {...fieldMark('items', at, 'text')}>{item.text}</li>)}
43
+ * ```
44
+ *
45
+ * The parts are joined with dots: a field inside a list is the list's key, the item's place and
46
+ * the field's own key. A mark written this way comes before what `sectionMark` finds by itself, and
47
+ * a field with no mark is still edited in the form.
48
+ */
49
+ export declare function fieldMark(...path: readonly (string | number)[]): PageMark;
50
+ //# sourceMappingURL=page-marks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page-marks.d.ts","sourceRoot":"","sources":["../src/page-marks.ts"],"names":[],"mappings":"AAEA,8GAA8G;AAC9G,MAAM,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AA8BxD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,QAAQ,CAKhE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,QAAQ,CAE3F;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,GAAG,IAAI,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,QAAQ,CAEzE"}
@@ -0,0 +1,90 @@
1
+ import { isEditorPreview } from './preview.js';
2
+ const NONE = Object.freeze({});
3
+ /** How deep a section's values are looked into for words, which is deeper than a list in a list goes. */
4
+ const DEPTH = 6;
5
+ /** What a section holds that a page can show, each with its place: `['items.0.text', 'Open daily']`. */
6
+ function shown(data, path, into) {
7
+ if (typeof data === 'string') {
8
+ if (data.trim() !== '' && path.length > 0)
9
+ into.push([path.join('.'), data]);
10
+ return;
11
+ }
12
+ if (typeof data !== 'object' || data === null || path.length >= DEPTH)
13
+ return;
14
+ if (Array.isArray(data)) {
15
+ data.forEach((item, at) => shown(item, [...path, at], into));
16
+ return;
17
+ }
18
+ // A section inside this one (a column's) says what it holds on its own mark.
19
+ const inner = data;
20
+ if (typeof inner.id === 'string' && typeof inner.block === 'string' && typeof inner.data === 'object' && inner.data !== null)
21
+ return;
22
+ const html = data.html;
23
+ // Text shaped in the editor is one field, found by its words and not its markup.
24
+ if (typeof html === 'string' && path.length > 0) {
25
+ if (html.trim() !== '')
26
+ into.push([path.join('.'), html, 'html']);
27
+ return;
28
+ }
29
+ for (const [key, value] of Object.entries(data))
30
+ shown(value, [...path, key], into);
31
+ }
32
+ /**
33
+ * Says which section of a page an element is, for the page beside the editor's form: a click on
34
+ * it there finds the section in the form. A template spreads it on the element that wraps the
35
+ * section:
36
+ *
37
+ * ```astro
38
+ * <div {...sectionMark(section.id, section.data)}><Block {...section.data} /></div>
39
+ * ```
40
+ *
41
+ * Given what the section holds, the mark also says which words belong to which field
42
+ * (`data-cms-values`), and the page beside the form finds each field by the words it shows, so a
43
+ * click on a heading opens the heading's field with no mark on the heading. Where that is not
44
+ * enough (a value the block changes before it shows it), the block says so with `fieldMark`.
45
+ *
46
+ * A visitor's page gets no attribute, so nothing about the editor reaches the site.
47
+ */
48
+ export function sectionMark(id, data) {
49
+ if (!isEditorPreview())
50
+ return NONE;
51
+ const values = [];
52
+ if (data !== undefined)
53
+ shown(data, [], values);
54
+ return values.length > 0 ? { 'data-cms-section': id, 'data-cms-values': JSON.stringify(values) } : { 'data-cms-section': id };
55
+ }
56
+ /**
57
+ * Says which place inside a section an element is: one that holds sections of its own, such as a
58
+ * column of a row. Given the section's id and where the place is in it, as `fieldMark` is:
59
+ *
60
+ * ```astro
61
+ * {columns.map((column, at) => (
62
+ * <div {...slotMark(section.id, 'columns', at, 'content')}>
63
+ * {column.content.map((inner) => <div {...sectionMark(inner.id, inner.data)}>…</div>)}
64
+ * </div>
65
+ * ))}
66
+ * ```
67
+ *
68
+ * Beside the editor's form, an empty place then has a button that adds its first section, and a
69
+ * section can be dragged from one place to another. A visitor's page gets no attribute.
70
+ */
71
+ export function slotMark(sectionId, ...path) {
72
+ return path.length > 0 && isEditorPreview() ? { 'data-cms-slot': `${sectionId}:${path.join('.')}` } : NONE;
73
+ }
74
+ /**
75
+ * Says which field of its section an element shows, by the field's key, so a click on it beside
76
+ * the editor's form opens that field and not only the section:
77
+ *
78
+ * ```astro
79
+ * <h1 {...fieldMark('title')}>{title}</h1>
80
+ * {items.map((item, at) => <li {...fieldMark('items', at, 'text')}>{item.text}</li>)}
81
+ * ```
82
+ *
83
+ * The parts are joined with dots: a field inside a list is the list's key, the item's place and
84
+ * the field's own key. A mark written this way comes before what `sectionMark` finds by itself, and
85
+ * a field with no mark is still edited in the form.
86
+ */
87
+ export function fieldMark(...path) {
88
+ return path.length > 0 && isEditorPreview() ? { 'data-cms-field': path.join('.') } : NONE;
89
+ }
90
+ //# sourceMappingURL=page-marks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page-marks.js","sourceRoot":"","sources":["../src/page-marks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAK/C,MAAM,IAAI,GAAa,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAEzC,yGAAyG;AACzG,MAAM,KAAK,GAAG,CAAC,CAAC;AAEhB,wGAAwG;AACxG,SAAS,KAAK,CAAC,IAAa,EAAE,IAAkC,EAAE,IAA2B;IAC3F,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,IAAI,IAAI,CAAC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