-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
476 lines (427 loc) · 13.2 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* Titleize a string
* @param {string} name Original string
* @return {string} Original string with the first letter uppercased
*/
const titleize = name => name.charAt(0).toUpperCase() + name.slice(1).replace(/-/g, ' ').replace(/([A-Z])/g, ' $1')
/** @typedef {'boolean'|'code'|'color'|'datetime'|'file'|'hidden'|'image'|'list'|'map'|'markdown'|'number'|'object'|'relation'|'select'|'string'|'text'} Widget */
/** @typedef {'bold'|'italic'|'code'|'link'|'heading-one'|'heading-two'|'heading-three'|'heading-four'|'heading-five'|'heading-six'|'quote'|'bulleted-list'|'numbered-list'} Button */
/** @typedef {'raw'|'rich_text'} Mode */
/** @typedef {'yml'|'yaml'|'toml'|'json'|'frontmatter'|'yaml-frontmatter'|'toml-frontmatter'|'json-frontmatter'} PostFormat */
/** @typedef {'yml'|'yaml'|'toml'|'json'|'md'|'markdown'|'html'} PageExtension */
/**
* Arguments that generate a field.
* @typedef {Object} FieldArgs
* @property {Widget} [widget] Widget type
* @property {string} [label] The field label that appears in CMS
* @property {boolean} [required]
* @property {boolean} [collapsed]
* @property {Field|Field[]} [fields]
* @property {string} [label_singular]
* @property {Field} [field]
* @property {any} [defaultValue]
* @property {boolean} [allow_add]
* @property {string} [hint]
* @property {string} [pattern]
* @property {any} [default]
* @property {string} [default_language] For code widget
* @property {boolean} [allow_language_selection] For code widget
* @property {Object} [keys] For code widget
* @property {boolean} [output_code_only] For code widget
* @property {boolean} [allowInput] For color widget
* @property {boolean} [enableAlpha] For color widget
* @property {string} [format] For datetime widget
* @property {string|boolean} [date_format] For datetime widget
* @property {string|boolean} [time_format] For datetime widget
* @property {boolean} [picker_utc] For datetime widget
* @property {string} [media_library] For image/file widget
* @property {boolean} [allow_multiple] For image/file widget
* @property {Object} [config] For image/file widget
* @property {string} [summary] For list widget
* @property {boolean} [minimize_collapsed] For list widget
* @property {number} [max] For list/number widget
* @property {number} [min] For list/number widget
* @property {boolean} [add_to_top] For list widget
* @property {number} [decimals] For map widget
* @property {any} [type] For map widget
* @property {boolean} [minimal] For markdown widget
* @property {Button[]} [buttons] For markdown widget
* @property {string[]} [editor_components] For markdown widget
* @property {Mode[]} [modes] For markdown widget
* @property {'int'|'float'} [value_type] For number widget
* @property {number} [step] For number widget
* @property {string} [collection] For relation widget
* @property {string} [value_field] For relation widget
* @property {string|string[]} [search_fields] For relation widget
* @property {string} [file] For relation widget
* @property {string|string[]} [display_fields] For relation widget
* @property {number} [options_length] For relation widget
* @property {[object|boolean]} i18n
* @property {FieldArgs} [...rest]
*/
/**
* @typedef Field
* @property {string} name
* @augments FieldArgs
*/
/**
* Generates a generic field. Defaults to a 'string' if no widget declared. Defaults title to {@link titleize}d name.
* @param {string} name The name of the field in code
* @param {any|FieldArgs} [args]
* @return Field
*/
export const field = (name,
{
widget = 'string',
label,
required,
collapsed = false,
fields = [],
label_singular,
field,
defaultValue,
allow_add,
i18n = true,
...rest
} = {}) => {
if (!label) {
label = titleize(name)
}
if (typeof required === 'undefined')
required = ['object', 'list'].includes(widget)
const _field = {
widget,
name,
label,
required,
collapsed,
default: defaultValue,
i18n,
...rest,
}
if (fields.length)
_field.fields = fields
if (typeof field !== 'undefined')
_field.field = field
if (typeof label_singular !== 'undefined')
_field.label_singular = label_singular
if (typeof allow_add !== 'undefined')
_field.allow_add = allow_add
return _field
}
export const string = field
/**
* Generates a required field
* @param {string} name
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const required = (name, args) => field(name, { ...args, required: true })
/**
* Generates a required string field. Defaults name to 'title'.
* @param {string=} name
* @param {any|FieldArgs=} args
* @return {Field}
*/
export const title = (name = 'title', args) => field(name, { ...args, required: true })
/**
* Generates an object field.
* @param {string} name
* @param {Field|Field[]} fields
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const object = (name, fields, args) => required(name, {
...args,
widget: 'object',
fields,
})
export const group = object
/**
* Generates a list field.
* Defaults singular label to the label with the last letter removed.
* Defaults to collapsed.
* @param {string} name
* @param {Field|Field[]=} fields
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const list = (name, fields, args) => {
const _args = {
...args,
widget: 'list',
collapsed: (typeof args === 'undefined' || typeof args.collapsed === 'undefined') ? true : args.collapsed,
label_singular: (typeof args === 'undefined' || typeof args.label_singular === 'undefined') ? name.slice(0, -1) : args.label_singular,
}
let _label_singular
if (typeof args !== 'undefined') {
if (typeof args.label_singular !== 'undefined') {
_label_singular = args.label_singular
} else if (typeof args.label !== 'undefined') {
_label_singular = args.label.slice(0, -1)
}
} else {
_label_singular = name.slice(0, -1)
}
_args.label_singular = _label_singular
if (Array.isArray(fields))
_args.fields = fields
else
_args.field = fields
_args.minimize_collapsed = typeof args !== 'undefined' && typeof args.minimize_collapsed !== 'undefined' ? args.minimize_collapsed : true
return required(name, _args)
}
/**
* Generates a tags field
* @param {string} name
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const tags = (name = 'tags', args) => list(name, undefined, { hint: 'Comma separated. Make sure there is no comma at the end.', ...args })
/**
* Generates an image field. Defaults name to 'image'.
* @param {string} name
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const image = (name = 'image', args) => field(name, { ...args, widget: 'image' })
/**
* Generates a text field. Defaults name to 'text'.
* @param {string=} name
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const text = (name = 'text', args) => field(name, { ...args, widget: 'text' })
/**
* Generates a minimal markdown field. Defaults name to 'text'.
* @param {string=} name
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const md = (name = 'text', args) => field(name, {
widget: 'markdown',
minimal: true,
buttons: ['bold', 'italic', 'link', 'heading-three', 'heading-four', 'heading-five', 'heading-six', 'bulleted-list', 'numbered-list'],
editor_components: [], ...args,
})
export const markdown = md
export const body = args => md('body', args)
/**
* Generates a date field. Defaults name to 'date'.
* @param {string=} name
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const date = (name = 'date', args) => field(name, {
widget: 'datetime',
date_format: 'DD/MM/YYYY',
time_format: false,
format: 'LLL',
...args,
})
/**
* Generates a boolean field.
* @param {string} name
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const boolean = (name, args) => field(name, { widget: 'boolean', defaultValue: false, ...args })
/**
* @typedef Option
* @property {string} label
* @property {string} value
*/
/**
* Generates an option, to be used inside a {@link select}.
* @param value
* @param label
* @return {Option}
*/
export const option = (value, label) => ({
value,
label: label || titleize(value),
})
/**
* Generates a select field.
* @param {string} name
* @param {Option[]} options Populated with {@link option}s.
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const select = (name, options, args) => field(name, {
widget: 'select',
options,
...args,
})
/**
* Generates a string field validated as a URL
* @param {string=} name
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const url = (name = 'url', args) => field(name, {
pattern: ['https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,24}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)', 'Must be a valid URL'],
...args,
})
/**
* Generates a string field validated as a slug
* @param {string=} name
* @param {any|FieldArgs} [args]
* @return {Field}
*/
export const slug = (name = 'slug', args) => field(name, {
pattern: ['^[a-z0-9]+(?:-*[a-z0-9]+)*$', 'Lower case English, no spaces or special characters except for `-`'],
required: true,
...args,
})
/**
* @typedef RelationAdditionalArgs
* @property {boolean} [multiple] For relation widget
* @property {[string]} [file] For relation widget
* @property {string[]} [display_fields] For relation widget
* @property {number} [options_length] For relation widget
*/
/**
* Generates a relation field
* @param {string} name
* @param {string} collection
* @param {string} value_field
* @param {string[]} search_fields
* @param {any|RelationAdditionalArgs=} args
* @return {Field}
*/
export const relation = (name, collection, value_field, search_fields, args) => field(name, {
widget: 'relation',
collection,
value_field,
search_fields,
...args,
})
/**
* Generates a number
* @param {string} name
* @param {any|FieldArgs=} args
* @return {Field}
*/
export const number = (name, args) => field(name, { widget: 'number', ...args })
/**
* Generates an integer
* @param {string} name
* @param {any|FieldArgs=} args
* @return {Field}
*/
export const int = (name, args) => number(name, { value_type: 'int', ...args })
/**
* Generates a floating point number
* @param {string} name
* @param {any|FieldArgs=} args
* @return {Field}
*/
export const float = (name, args) => number(name, { value_type: 'float', ...args })
/**
* Generates a range
* @param {string} name
* @param {number=} min
* @param {number=} max
* @param {number=} step
* @param {any|FieldArgs=} args
* @return {Field}
*/
export const range = (name, min = 0, max = 1, step = 0.1, args) => number(name, { min, max, step, ...args })
/**
* Generates a percentage range
* @param {string=} name
* @param {any|FieldArgs=} args
* @return {Field}
*/
export const percentage = (name = 'percentage', args) => range(name, 1, 100, 1, args)
/**
* Generates a field with a single file
* @param {string} name
* @param {any|FieldArgs=} args
* @return {Field}
*/
export const file = (name = 'file', args) => field(name, { widget: 'file', allow_multiple: false, ...args })
/**
* @typedef Page
* @todo PageArgs, PostArgs
*/
/**
* Generates a single page field. Defaults label to {@link titleize}d name. Defaults to a .yml file.
* @param {string} name
* @param {Field[]} fields
* @param {string=} label
* @param {string=} filename
* @param {string=} path
* @param {string=} folder
* @param {PageExtension} extension
* @param {object|boolean=} i18n
* @param {any|FieldArgs} [...rest]
* @return {Page}
*/
export const page = (name, fields, {
label,
filename,
path = 'src/content/',
folder = 'pages',
extension = 'yml',
i18n = true,
...rest
} = {}) => ({
name,
file: `${path}${folder}/${filename || name}.${extension}`,
label: label || titleize(name),
fields,
[i18n ? 'i18n' : '_i18n']: i18n,
...rest,
})
/**
* Generates a page in the data folder.
* @param {string} name
* @param {Field[]} fields
* @param {any|FieldArgs} [args]
* @return {Page}
*/
export const settingsPage = (name, fields, args) => page(name, fields, {
folder: '_data',
...args,
})
/**
* @typedef PostType
*/
/**
* Generates a post type. Defaults label to {@link titleize}d name. Defaults to a .md file.
* @param {string} name
* @param {Field[]} fields
* @param {string=} label
* @param {PostFormat=} format
* @param {string=} path
* @param {string=} subfolder
* @param {string=} slug
* @param {string=} label_singular
* @param {object|boolean=} i18n
* @param {any|FieldArgs} [...rest]
* @return {PostType}
*/
export const postType = (name, fields, {
label,
format = 'frontmatter',
path = 'src/content',
subfolder = '',
slug = fields.some(field => field.name === 'slug') ? '{{fields.slug}}' : '{{slug}}',
label_singular,
i18n = true,
...rest
} = {}) => ({
name,
folder: `${path}${subfolder || ''}/${name}`,
label: label || titleize(name),
format,
fields,
editor: { preview: false },
label_singular: label_singular || titleize(name.slice(0, -1)),
create: true,
slug,
i18n,
...rest,
})