@nickpaustian/tela 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/README.md +1 -1
- package/bin/tela.ts +18 -2
- package/lib/index.js +172 -7
- package/package.json +5 -2
- package/scripts/check-components.ts +20 -0
- package/scripts/media-sync.ts +41 -0
- package/scripts/republish.ts +24 -0
- package/src/api.ts +3 -3
- package/src/components/EntryEditor.lsp +174 -11
- package/src/components/EntryList.lsp +8 -0
- package/src/components/Field.lsp +92 -2
- package/src/components/LinkField.lsp +477 -0
- package/src/components/MarkdownField.lsp +19 -24
- package/src/components/PersonField.lsp +255 -0
- package/src/components/ReferenceField.lsp +22 -1
- package/src/components/RepeaterField.lsp +141 -13
- package/src/components/ToggleField.lsp +63 -0
- package/src/config/fields.ts +134 -1
- package/src/config/seo.ts +4 -2
- package/src/config/types.ts +158 -7
- package/src/content/derive-values.ts +14 -3
- package/src/content/entries.ts +1 -1
- package/src/content/entry.ts +15 -1
- package/src/content/export.ts +173 -14
- package/src/content/git.ts +1 -1
- package/src/content/hooks.ts +75 -0
- package/src/content/link-address.ts +168 -0
- package/src/content/links.ts +13 -0
- package/src/content/person.ts +11 -0
- package/src/content/placement.ts +48 -0
- package/src/content/references.ts +48 -4
- package/src/content/rules.ts +24 -4
- package/src/content/shape.ts +192 -0
- package/src/content/store.ts +17 -0
- package/src/content/url.ts +15 -1
- package/src/content/users.ts +32 -0
- package/src/content/validate.ts +12 -2
- package/src/index.ts +4 -1
- package/src/keys.ts +47 -0
- package/src/media/index.ts +5 -0
- package/src/server/index.ts +168 -34
- package/src/server/library-routes.ts +11 -3
package/src/config/fields.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Field } from "./types.ts"
|
|
1
|
+
import type { Block, Collection, Field } from "./types.ts"
|
|
2
|
+
import { acceptedKinds, linkMessage, linkPattern, type LinkKind } from "../content/link-address.ts"
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Field shapes several collections want, written once.
|
|
@@ -101,3 +102,135 @@ export function imageUse(
|
|
|
101
102
|
],
|
|
102
103
|
}
|
|
103
104
|
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Somebody named on an entry: one of the CMS's people, or a name typed in.
|
|
108
|
+
*
|
|
109
|
+
* A byline is usually a colleague, who has an account, and sometimes not - a
|
|
110
|
+
* guest writer, a team, "Margaret Chen and Dev Rao". One input offers both:
|
|
111
|
+
* typing suggests people by name, and anything not picked is kept as typed.
|
|
112
|
+
*
|
|
113
|
+
* Stored as `{ user }` or `{ name }`, never both. `user` is a reference to an
|
|
114
|
+
* account and publishes as `{ name }` only, so a site reads
|
|
115
|
+
* `value.user?.name ?? value.name`.
|
|
116
|
+
*/
|
|
117
|
+
export function person(
|
|
118
|
+
name: string,
|
|
119
|
+
label: string,
|
|
120
|
+
options: { hint?: string; required?: boolean } = {},
|
|
121
|
+
): Field {
|
|
122
|
+
return {
|
|
123
|
+
name,
|
|
124
|
+
label,
|
|
125
|
+
type: "block",
|
|
126
|
+
component: "person",
|
|
127
|
+
...(options.required ? { required: true } : {}),
|
|
128
|
+
...(options.hint ? { options: { hint: options.hint } } : {}),
|
|
129
|
+
fields: [
|
|
130
|
+
{ name: "user", type: "reference", to: "users", label: "Person" },
|
|
131
|
+
{ name: "name", type: "text", label: "Name", maxLength: 200 },
|
|
132
|
+
],
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Somewhere to go, chosen the way an editor thinks of it.
|
|
138
|
+
*
|
|
139
|
+
* One box. Typing searches this site's pages, and the library's files where
|
|
140
|
+
* they are accepted; pasting an address keeps it, tidied: an email gets
|
|
141
|
+
* `mailto:`, `example.org` its `https://`, and an address on this site becomes
|
|
142
|
+
* the page it names, or its path.
|
|
143
|
+
*
|
|
144
|
+
* `accepts` narrows what it may go to (see `LINK_KINDS`); by default anything
|
|
145
|
+
* but a file. `collections` narrows which entries count as pages.
|
|
146
|
+
*
|
|
147
|
+
* Stored as `tela:<collection>/<id>` for an entry or a file and as the address
|
|
148
|
+
* otherwise. Published as `tela:<collection>/<slug>` for an entry, which a
|
|
149
|
+
* site resolves as it does a rich-text link; as the file's own address for a
|
|
150
|
+
* file; as written for anything else. A link to something not on the site is
|
|
151
|
+
* left out, as a reference is.
|
|
152
|
+
*/
|
|
153
|
+
export function link(
|
|
154
|
+
name: string,
|
|
155
|
+
label: string,
|
|
156
|
+
options: {
|
|
157
|
+
hint?: string
|
|
158
|
+
required?: boolean
|
|
159
|
+
accepts?: LinkKind[]
|
|
160
|
+
collections?: string[]
|
|
161
|
+
placeholder?: string
|
|
162
|
+
} = {},
|
|
163
|
+
): Field {
|
|
164
|
+
const { required, accepts, collections, ...rest } = options
|
|
165
|
+
const kinds = acceptedKinds(accepts)
|
|
166
|
+
return {
|
|
167
|
+
name,
|
|
168
|
+
label,
|
|
169
|
+
type: "link",
|
|
170
|
+
...(required ? { required: true } : {}),
|
|
171
|
+
patterns: [{ pattern: linkPattern(kinds, collections), message: linkMessage(kinds) }],
|
|
172
|
+
options: { ...rest, accepts: kinds, ...(collections?.length ? { collections } : {}) },
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* A section written for one page or a few: its content and its look are code,
|
|
178
|
+
* and only the parts named in `editable` are the editor's to change.
|
|
179
|
+
*
|
|
180
|
+
* Each editable field shows the code's own value until somebody changes it,
|
|
181
|
+
* and goes back to it when cleared. Facts shared with other pages or apps
|
|
182
|
+
* belong in a data source the block `uses` rather than in its content.
|
|
183
|
+
* The block is offered only on the pages it
|
|
184
|
+
* is `for`, at most `once`, and is `kept` - moved, never removed - unless it
|
|
185
|
+
* says otherwise.
|
|
186
|
+
*/
|
|
187
|
+
export function customBlock(
|
|
188
|
+
name: string,
|
|
189
|
+
{
|
|
190
|
+
label,
|
|
191
|
+
description,
|
|
192
|
+
note,
|
|
193
|
+
for: pages,
|
|
194
|
+
uses,
|
|
195
|
+
once = true,
|
|
196
|
+
kept = true,
|
|
197
|
+
content = {},
|
|
198
|
+
editable = [],
|
|
199
|
+
}: {
|
|
200
|
+
label: string
|
|
201
|
+
description?: string
|
|
202
|
+
note?: string
|
|
203
|
+
for: string[]
|
|
204
|
+
uses?: string[]
|
|
205
|
+
once?: boolean
|
|
206
|
+
kept?: boolean
|
|
207
|
+
content?: Record<string, unknown>
|
|
208
|
+
editable?: Field[]
|
|
209
|
+
},
|
|
210
|
+
): Block {
|
|
211
|
+
return {
|
|
212
|
+
name,
|
|
213
|
+
label,
|
|
214
|
+
description,
|
|
215
|
+
note,
|
|
216
|
+
for: pages,
|
|
217
|
+
...(uses?.length ? { uses } : {}),
|
|
218
|
+
once,
|
|
219
|
+
kept,
|
|
220
|
+
content,
|
|
221
|
+
fields: editable.map((field) =>
|
|
222
|
+
field.fallback === undefined && content[field.name] !== undefined ? { ...field, fallback: content[field.name] } : field,
|
|
223
|
+
),
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* One record of facts with no page of its own, kept where editors can change
|
|
229
|
+
* it and published once for every page and app that shows it.
|
|
230
|
+
*/
|
|
231
|
+
export function dataSource(
|
|
232
|
+
name: string,
|
|
233
|
+
options: Omit<Collection, "name" | "data" | "url" | "publishable" | "fields"> & { fields: Field[] },
|
|
234
|
+
): Collection {
|
|
235
|
+
return { ...options, name, data: true }
|
|
236
|
+
}
|
package/src/config/seo.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Collection, Field } from "./types.ts"
|
|
2
2
|
import { imageUse } from "./fields.ts"
|
|
3
|
+
import { dataFields } from "./components.ts"
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Search and sharing, as a collection option.
|
|
@@ -63,8 +64,9 @@ export function seoGroup(collection: Collection, siteName?: string): Field | nul
|
|
|
63
64
|
const options: SeoOptions = collection.seo === true ? {} : collection.seo
|
|
64
65
|
const at = `Collection "${collection.name}" seo`
|
|
65
66
|
|
|
67
|
+
const stored = dataFields(collection.fields)
|
|
66
68
|
const named = (name: string) => {
|
|
67
|
-
const field =
|
|
69
|
+
const field = stored.find((one) => one.name === name)
|
|
68
70
|
if (!field) throw new Error(`${at}: falls back to "${name}", which is not one of its fields.`)
|
|
69
71
|
return field
|
|
70
72
|
}
|
|
@@ -88,7 +90,7 @@ export function seoGroup(collection: Collection, siteName?: string): Field | nul
|
|
|
88
90
|
maxLength: 70,
|
|
89
91
|
counter: { target: options.title?.target ?? 60, ...(suffix ? { adds: suffix } : {}) },
|
|
90
92
|
options: {
|
|
91
|
-
hint: options.title?.hint ?? `Leave empty to use the ${
|
|
93
|
+
hint: options.title?.hint ?? `Leave empty to use the ${stored.some((one) => one.name === titleField) ? labelOf(titleField) : "title"}.`,
|
|
92
94
|
},
|
|
93
95
|
},
|
|
94
96
|
{
|
package/src/config/types.ts
CHANGED
|
@@ -34,6 +34,15 @@ export const STORAGE_TYPES: Record<string, (value: unknown) => boolean> = {
|
|
|
34
34
|
number: (v) => typeof v === "number" && Number.isFinite(v),
|
|
35
35
|
boolean: (v) => typeof v === "boolean",
|
|
36
36
|
repeater: (v) => Array.isArray(v),
|
|
37
|
+
/**
|
|
38
|
+
* Somewhere to go: another entry, a file, or an address.
|
|
39
|
+
*
|
|
40
|
+
* Stored as `tela:<collection>/<id>` for an entry or a file, so it follows a
|
|
41
|
+
* rename the way a reference does, and as the address itself for anything
|
|
42
|
+
* else. Published with the entry's slug, as rich-text links are, or as the
|
|
43
|
+
* file's address.
|
|
44
|
+
*/
|
|
45
|
+
link: (v) => typeof v === "string",
|
|
37
46
|
/**
|
|
38
47
|
* A pointer to another entry.
|
|
39
48
|
*
|
|
@@ -91,6 +100,29 @@ export interface Block {
|
|
|
91
100
|
* it, and so nothing has to move when it does.
|
|
92
101
|
*/
|
|
93
102
|
preview?: string
|
|
103
|
+
/** A line in the row, above its fields: what is set elsewhere and who to ask. */
|
|
104
|
+
note?: string
|
|
105
|
+
/**
|
|
106
|
+
* The pages this block is made for, by address: `/membership`, or
|
|
107
|
+
* `/fellowships/*` for everything under one. Offered only there, in a group
|
|
108
|
+
* of its own, and never in the picker anywhere else.
|
|
109
|
+
*/
|
|
110
|
+
for?: string[]
|
|
111
|
+
/**
|
|
112
|
+
* Data sources this block shows, by collection name. The site reads them
|
|
113
|
+
* when it builds, so a change to one reaches every block that uses it.
|
|
114
|
+
*/
|
|
115
|
+
uses?: string[]
|
|
116
|
+
/** At most one in a list. Once added it leaves the picker. */
|
|
117
|
+
once?: boolean
|
|
118
|
+
/** Moved like any other, never removed in the editor. */
|
|
119
|
+
kept?: boolean
|
|
120
|
+
/**
|
|
121
|
+
* What the block holds that is written in code rather than typed. Published
|
|
122
|
+
* under the row's own values, so a field of the same name is a part of it an
|
|
123
|
+
* editor may change.
|
|
124
|
+
*/
|
|
125
|
+
content?: Record<string, unknown>
|
|
94
126
|
fields: Field[]
|
|
95
127
|
}
|
|
96
128
|
|
|
@@ -141,8 +173,18 @@ export interface Field {
|
|
|
141
173
|
* than guess.
|
|
142
174
|
*/
|
|
143
175
|
required?: boolean
|
|
144
|
-
/**
|
|
176
|
+
/**
|
|
177
|
+
* What a field holds before anyone sets it. Seeded into the editor, never
|
|
178
|
+
* written for the field by the API. A date field takes `"today"`: the
|
|
179
|
+
* editor's own date when the form opens.
|
|
180
|
+
*/
|
|
145
181
|
default?: unknown
|
|
182
|
+
/**
|
|
183
|
+
* What the field means while nobody has set it. Unlike `default` it is never
|
|
184
|
+
* copied into the entry: the editor shows it greyed until somebody changes
|
|
185
|
+
* it, and publishing fills it in, so a field left alone follows the code.
|
|
186
|
+
*/
|
|
187
|
+
fallback?: unknown
|
|
146
188
|
/** Shown, and judged, only when a sibling field agrees. */
|
|
147
189
|
when?: Condition
|
|
148
190
|
/** Unique within the collection. Checked server-side, excluding this entry. */
|
|
@@ -356,7 +398,8 @@ export interface Field {
|
|
|
356
398
|
*/
|
|
357
399
|
of?: Field[]
|
|
358
400
|
/**
|
|
359
|
-
* How many rows a repeater must and may hold
|
|
401
|
+
* How many rows a repeater must and may hold, or how many entries a `many`
|
|
402
|
+
* reference may pick.
|
|
360
403
|
*
|
|
361
404
|
* Not `min`/`max`: those already mean numeric bounds, and a key that means
|
|
362
405
|
* two things depending on the type is the mistake `type`/`component` was
|
|
@@ -435,6 +478,22 @@ export interface Collection {
|
|
|
435
478
|
* once, and the editor offers nothing to publish.
|
|
436
479
|
*/
|
|
437
480
|
publishable?: boolean
|
|
481
|
+
/**
|
|
482
|
+
* A data source: one record of facts with no page of its own - memberships,
|
|
483
|
+
* opening hours, site switches - kept in one place so every page and every
|
|
484
|
+
* app that shows them reads the same thing. Lists inside it are repeaters,
|
|
485
|
+
* so editors set their order.
|
|
486
|
+
*
|
|
487
|
+
* The admin opens straight onto the record. Publishing writes it to
|
|
488
|
+
* `content/data/<name>.json`, with no index and no slug in the file.
|
|
489
|
+
*/
|
|
490
|
+
data?: boolean
|
|
491
|
+
/**
|
|
492
|
+
* A data source's shape version, from 1. Raised by a developer when a change
|
|
493
|
+
* breaks the apps reading it: a field removed, retyped or no longer
|
|
494
|
+
* required. Publishing refuses such a change until it is raised.
|
|
495
|
+
*/
|
|
496
|
+
version?: number
|
|
438
497
|
/**
|
|
439
498
|
* What this collection is drawn with, by id from the built-in set in
|
|
440
499
|
* `config/icons.ts`. Left out, the collection's name suggests one, and a
|
|
@@ -595,6 +654,25 @@ export interface TelaConfig {
|
|
|
595
654
|
* since a development copy and the real site are not at the same address.
|
|
596
655
|
*/
|
|
597
656
|
site?: string
|
|
657
|
+
/**
|
|
658
|
+
* Other addresses the site is known by, such as the live site while a
|
|
659
|
+
* development copy is `site`. A link pasted from any of them is read as a
|
|
660
|
+
* page on this site.
|
|
661
|
+
*/
|
|
662
|
+
siteAliases?: string[]
|
|
663
|
+
/**
|
|
664
|
+
* Where data sources can be read once published, if anywhere: a path on the
|
|
665
|
+
* site (`/data/{name}.json`) or a whole address, `{name}` standing for the
|
|
666
|
+
* source. Left out, the editor names the file in the content repo alone,
|
|
667
|
+
* since what serves it is the project's choice.
|
|
668
|
+
*/
|
|
669
|
+
dataAddress?: string
|
|
670
|
+
/**
|
|
671
|
+
* Addresses told when published content changes, once it is committed and
|
|
672
|
+
* pushed: a data API to pull, an indexer, a build. A hook that fails is
|
|
673
|
+
* logged and never fails the publish.
|
|
674
|
+
*/
|
|
675
|
+
afterPublish?: PublishHook[]
|
|
598
676
|
/**
|
|
599
677
|
* The site's name, as search results and link cards give it. Collections
|
|
600
678
|
* with `seo` add it to titles in search results unless they say otherwise.
|
|
@@ -660,6 +738,15 @@ export interface TelaConfig {
|
|
|
660
738
|
components?: string[]
|
|
661
739
|
}
|
|
662
740
|
|
|
741
|
+
export interface PublishHook {
|
|
742
|
+
/** POSTed `{ event, commit, files, cells, collections }`. */
|
|
743
|
+
url: string
|
|
744
|
+
/** Sent as `Authorization: Bearer <token>`. Read it from the environment. */
|
|
745
|
+
token?: string
|
|
746
|
+
/** Only when one of these changed, by collection or data source name. */
|
|
747
|
+
collections?: string[]
|
|
748
|
+
}
|
|
749
|
+
|
|
663
750
|
export interface NavSection {
|
|
664
751
|
heading: string
|
|
665
752
|
/** Collection names, in the order they should appear under it. */
|
|
@@ -805,7 +892,7 @@ export const LABEL_KEY = "internalLabel"
|
|
|
805
892
|
* as wrong as one at the top, and finding it at boot beats finding it in
|
|
806
893
|
* JSONB.
|
|
807
894
|
*/
|
|
808
|
-
function checkFields(fields: Field[], path: string, known: Set<string> = new Set()) {
|
|
895
|
+
function checkFields(fields: Field[], path: string, known: Set<string> = new Set(), dataNames: Set<string> = new Set()) {
|
|
809
896
|
for (const field of fields) {
|
|
810
897
|
const where = `${path}.${field.name}`
|
|
811
898
|
|
|
@@ -818,7 +905,7 @@ function checkFields(fields: Field[], path: string, known: Set<string> = new Set
|
|
|
818
905
|
}
|
|
819
906
|
|
|
820
907
|
// A group's children are ordinary fields and are checked as such.
|
|
821
|
-
if (field.fields) checkFields(field.fields, where, known)
|
|
908
|
+
if (field.fields) checkFields(field.fields, where, known, dataNames)
|
|
822
909
|
|
|
823
910
|
for (const tool of field.tools ?? []) {
|
|
824
911
|
if (!TOOL_NAMES.includes(tool as any)) {
|
|
@@ -873,15 +960,26 @@ function checkFields(fields: Field[], path: string, known: Set<string> = new Set
|
|
|
873
960
|
}
|
|
874
961
|
}
|
|
875
962
|
|
|
876
|
-
if (field.of) checkFields(field.of, where, known)
|
|
963
|
+
if (field.of) checkFields(field.of, where, known, dataNames)
|
|
877
964
|
|
|
878
965
|
const seen = new Set<string>()
|
|
879
966
|
for (const block of field.blocks ?? []) {
|
|
880
967
|
if (seen.has(block.name)) {
|
|
881
968
|
throw new Error(`${where}: two blocks are both named "${block.name}".`)
|
|
882
969
|
}
|
|
970
|
+
if (block.content && BLOCK_KEY in block.content) {
|
|
971
|
+
throw new Error(`${where}: block "${block.name}" has "${BLOCK_KEY}" in its content, which is how a row records its shape.`)
|
|
972
|
+
}
|
|
973
|
+
for (const source of block.uses ?? []) {
|
|
974
|
+
if (!dataNames.has(source)) {
|
|
975
|
+
throw new Error(`${where}: block "${block.name}" uses "${source}", which is not a data source.`)
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
if (block.for?.some((page) => !page.startsWith("/"))) {
|
|
979
|
+
throw new Error(`${where}: block "${block.name}" is for pages by address, each starting with "/".`)
|
|
980
|
+
}
|
|
883
981
|
seen.add(block.name)
|
|
884
|
-
checkFields(block.fields, `${where}[${block.name}]`, known)
|
|
982
|
+
checkFields(block.fields, `${where}[${block.name}]`, known, dataNames)
|
|
885
983
|
}
|
|
886
984
|
}
|
|
887
985
|
}
|
|
@@ -895,6 +993,23 @@ function checkFields(fields: Field[], path: string, known: Set<string> = new Set
|
|
|
895
993
|
*/
|
|
896
994
|
export const BUILT_IN = ["users", "groups", "media"]
|
|
897
995
|
|
|
996
|
+
export const COLLECTION_NAME = /^[a-z][a-zA-Z0-9-]*$/
|
|
997
|
+
|
|
998
|
+
function checkCollectionName(name: string) {
|
|
999
|
+
if (!COLLECTION_NAME.test(name)) {
|
|
1000
|
+
throw new Error(
|
|
1001
|
+
`"${name}": a collection's name is a folder in the content repo and part of its rebuild keys, ` +
|
|
1002
|
+
`so start with a lowercase letter and use only letters, digits and hyphens.`,
|
|
1003
|
+
)
|
|
1004
|
+
}
|
|
1005
|
+
if (name === "data") {
|
|
1006
|
+
throw new Error(
|
|
1007
|
+
`"data" is reserved: data sources are published to content/data/ and rebuilt as cms:data:<name>, ` +
|
|
1008
|
+
`so a collection called data would overwrite them. Choose another name.`,
|
|
1009
|
+
)
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
|
|
898
1013
|
/**
|
|
899
1014
|
* The half of a workflow that can be checked without a database.
|
|
900
1015
|
*
|
|
@@ -953,6 +1068,32 @@ function checkWorkflow(collection: Collection) {
|
|
|
953
1068
|
}
|
|
954
1069
|
}
|
|
955
1070
|
|
|
1071
|
+
/** A data source is one record with no page: nothing to address it by but its name. */
|
|
1072
|
+
function checkDataSource(collection: Collection) {
|
|
1073
|
+
const where = collection.name
|
|
1074
|
+
if (collection.url) throw new Error(`${where}: a data source has no page, so no \`url\`.`)
|
|
1075
|
+
if (collection.publishable === false) throw new Error(`${where}: a data source is published, so it cannot set \`publishable: false\`.`)
|
|
1076
|
+
if (collection.version !== undefined && !(Number.isInteger(collection.version) && collection.version >= 1)) {
|
|
1077
|
+
throw new Error(`${where}: \`version\` counts up from 1.`)
|
|
1078
|
+
}
|
|
1079
|
+
const flat = (fields: Field[]): Field[] => fields.flatMap((field) => (field.type === "group" ? flat(field.fields ?? []) : [field]))
|
|
1080
|
+
if (flat(collection.fields).some((field) => field.name === "slug")) {
|
|
1081
|
+
throw new Error(`${where}: a data source is named by its collection, so it has no "slug" field.`)
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
function checkHook(hook: PublishHook, known: Set<string>) {
|
|
1086
|
+
let url: URL
|
|
1087
|
+
try {
|
|
1088
|
+
url = new URL(hook.url)
|
|
1089
|
+
} catch {
|
|
1090
|
+
throw new Error(`afterPublish: "${hook.url}" is not an address.`)
|
|
1091
|
+
}
|
|
1092
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") throw new Error(`afterPublish: ${hook.url} must be http or https.`)
|
|
1093
|
+
const unknown = (hook.collections ?? []).filter((name) => !known.has(name))
|
|
1094
|
+
if (unknown.length) throw new Error(`afterPublish: ${hook.url} names ${unknown.join(", ")}, which no collection is called.`)
|
|
1095
|
+
}
|
|
1096
|
+
|
|
956
1097
|
export function defineConfig(config: TelaConfig): TelaConfig {
|
|
957
1098
|
// A type nothing knows how to store would otherwise pass every check
|
|
958
1099
|
// silently - the field simply never gets shape-checked, and nobody finds out
|
|
@@ -963,11 +1104,18 @@ export function defineConfig(config: TelaConfig): TelaConfig {
|
|
|
963
1104
|
|
|
964
1105
|
const declaredCollections = config.collections.map((one) => withSeo(one, config.siteName))
|
|
965
1106
|
|
|
1107
|
+
const dataNames = new Set(config.collections.filter((one) => one.data).map((one) => one.name))
|
|
1108
|
+
|
|
966
1109
|
for (const collection of declaredCollections) {
|
|
967
|
-
|
|
1110
|
+
checkCollectionName(collection.name)
|
|
1111
|
+
checkFields(collection.fields, collection.name, known, dataNames)
|
|
968
1112
|
checkWorkflow(collection)
|
|
1113
|
+
if (collection.data) checkDataSource(collection)
|
|
1114
|
+
else if (collection.version !== undefined) throw new Error(`${collection.name}: only a data source has a \`version\`.`)
|
|
969
1115
|
}
|
|
970
1116
|
|
|
1117
|
+
for (const hook of config.afterPublish ?? []) checkHook(hook, known)
|
|
1118
|
+
|
|
971
1119
|
const declared = declaredCollections.find((c) => c.name === "users")
|
|
972
1120
|
|
|
973
1121
|
// Names sit straight after the address: who somebody is comes before what
|
|
@@ -1051,6 +1199,9 @@ export function defineConfig(config: TelaConfig): TelaConfig {
|
|
|
1051
1199
|
...(config.overrides ? { overrides: config.overrides } : {}),
|
|
1052
1200
|
...(config.components ? { components: config.components } : {}),
|
|
1053
1201
|
...(config.site ? { site: config.site } : {}),
|
|
1202
|
+
...(config.siteAliases?.length ? { siteAliases: config.siteAliases } : {}),
|
|
1203
|
+
...(config.dataAddress ? { dataAddress: config.dataAddress } : {}),
|
|
1204
|
+
...(config.afterPublish?.length ? { afterPublish: config.afterPublish } : {}),
|
|
1054
1205
|
...(config.siteName ? { siteName: config.siteName } : {}),
|
|
1055
1206
|
...(config.userNames ? { userNames: true } : {}),
|
|
1056
1207
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Collection, Field } from "../config/types.ts"
|
|
1
|
+
import type { Block, Collection, Field } from "../config/types.ts"
|
|
2
2
|
import { blockOf, blocksOf, dataFields, isSlot } from "../config/components.ts"
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -15,6 +15,9 @@ import { blockOf, blocksOf, dataFields, isSlot } from "../config/components.ts"
|
|
|
15
15
|
* in somebody else's build - and a rule reduced to something safe to ship
|
|
16
16
|
* cannot call `Intl`.
|
|
17
17
|
*
|
|
18
|
+
* Fields left unset take their `fallback`, and a block written partly in code
|
|
19
|
+
* gets that `content` under what the editor set, so the file is whole.
|
|
20
|
+
*
|
|
18
21
|
* Pure, and given the whole entry, so it can be applied to a draft on its way
|
|
19
22
|
* to a preview as easily as to a release on its way to a file.
|
|
20
23
|
*/
|
|
@@ -82,10 +85,18 @@ export function withDerived(
|
|
|
82
85
|
return walk(dataFields(collection.fields), data)
|
|
83
86
|
}
|
|
84
87
|
|
|
88
|
+
const unset = (value: unknown) => value === undefined || value === null || value === ""
|
|
89
|
+
|
|
90
|
+
/** A block's code content under the row's own values: what an editor changed wins. */
|
|
91
|
+
function withContent(block: Block, row: Record<string, unknown>): Record<string, unknown> {
|
|
92
|
+
return block.content ? { ...structuredClone(block.content), ...row } : row
|
|
93
|
+
}
|
|
94
|
+
|
|
85
95
|
function walk(fields: Field[], data: Record<string, unknown>): Record<string, unknown> {
|
|
86
96
|
const out = { ...data }
|
|
87
97
|
|
|
88
98
|
for (const field of dataFields(fields)) {
|
|
99
|
+
if (field.fallback !== undefined && unset(out[field.name])) out[field.name] = structuredClone(field.fallback)
|
|
89
100
|
const value = out[field.name]
|
|
90
101
|
|
|
91
102
|
if (isSlot(field)) {
|
|
@@ -93,7 +104,7 @@ function walk(fields: Field[], data: Record<string, unknown>): Record<string, un
|
|
|
93
104
|
|
|
94
105
|
const inner = value as Record<string, unknown>
|
|
95
106
|
const block = blockOf(field, inner)
|
|
96
|
-
const walked = block ? walk(block.fields, inner) : inner
|
|
107
|
+
const walked = block ? walk(block.fields, withContent(block, inner)) : inner
|
|
97
108
|
|
|
98
109
|
// A composite derives from its own parts; a slot holding a chosen block
|
|
99
110
|
// derives from whatever that block put there.
|
|
@@ -107,7 +118,7 @@ function walk(fields: Field[], data: Record<string, unknown>): Record<string, un
|
|
|
107
118
|
out[field.name] = value.map((row) => {
|
|
108
119
|
if (!row || typeof row !== "object") return row
|
|
109
120
|
const block = blockOf(field, row)
|
|
110
|
-
return block ? walk(block.fields, row as Record<string, unknown>) : row
|
|
121
|
+
return block ? walk(block.fields, withContent(block, row as Record<string, unknown>)) : row
|
|
111
122
|
})
|
|
112
123
|
continue
|
|
113
124
|
}
|
package/src/content/entries.ts
CHANGED
|
@@ -30,7 +30,7 @@ export interface Entry {
|
|
|
30
30
|
const MAX_LIMIT = 100
|
|
31
31
|
|
|
32
32
|
const ENTRY_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
33
|
-
const isEntryId = (id: string) => ENTRY_ID.test(id)
|
|
33
|
+
export const isEntryId = (id: string) => ENTRY_ID.test(id)
|
|
34
34
|
|
|
35
35
|
export async function listEntries(
|
|
36
36
|
collection: Collection,
|
package/src/content/entry.ts
CHANGED
|
@@ -90,10 +90,24 @@ function walkFields(fields: Field[], data: Record<string, unknown>, prefix: stri
|
|
|
90
90
|
continue
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
set(VALUE + path, value ?? field
|
|
93
|
+
set(VALUE + path, value ?? defaultOf(field))
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
/**
|
|
98
|
+
* What a field starts as. A date's `"today"` is the editor's own today, worked
|
|
99
|
+
* out as the form opens rather than when the config was written, which is the
|
|
100
|
+
* only way a default can move.
|
|
101
|
+
*/
|
|
102
|
+
export function defaultOf(field: Field, now: Date = new Date()): unknown {
|
|
103
|
+
if (field.type === "date" && field.default === "today") {
|
|
104
|
+
const month = String(now.getMonth() + 1).padStart(2, "0")
|
|
105
|
+
const day = String(now.getDate()).padStart(2, "0")
|
|
106
|
+
return `${now.getFullYear()}-${month}-${day}`
|
|
107
|
+
}
|
|
108
|
+
return field.default
|
|
109
|
+
}
|
|
110
|
+
|
|
97
111
|
/** Gather the entry back out of the cells. */
|
|
98
112
|
export function assemble(collection: Collection, get: Get): Record<string, unknown> {
|
|
99
113
|
return gather(dataFields(collection.fields), "", get)
|