@sinnstack/ui 0.2.0 → 0.3.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.
Files changed (138) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/README.md +6 -4
  3. package/docs/blocks.md +373 -364
  4. package/docs/data.md +128 -128
  5. package/docs/finishes.md +165 -0
  6. package/docs/primitives.md +4 -4
  7. package/docs/seo.md +191 -187
  8. package/package.json +15 -24
  9. package/src/blocks/BlockRenderer.astro +13 -7
  10. package/src/blocks/CTABanner.astro +8 -8
  11. package/src/blocks/CTAPair.astro +9 -9
  12. package/src/blocks/CardGrid.astro +7 -7
  13. package/src/blocks/ConsentEmbed.astro +1 -1
  14. package/src/blocks/ConsentManager.astro +1 -1
  15. package/src/blocks/ContactCard.astro +14 -14
  16. package/src/blocks/ContactForm.astro +4 -4
  17. package/src/blocks/ErrorPage.astro +1 -1
  18. package/src/blocks/FAQ.astro +9 -9
  19. package/src/blocks/FeatureGrid.astro +7 -7
  20. package/src/blocks/Hero.astro +11 -11
  21. package/src/blocks/HeroCarousel.astro +11 -11
  22. package/src/blocks/HighlightCards.astro +10 -10
  23. package/src/blocks/LocationHours.astro +14 -14
  24. package/src/blocks/LogoCloud.astro +7 -7
  25. package/src/blocks/MaintenancePage.astro +8 -3
  26. package/src/blocks/MediaText.astro +12 -12
  27. package/src/blocks/PageHeader.astro +9 -9
  28. package/src/blocks/ProseSection.astro +8 -8
  29. package/src/blocks/Reviews.astro +11 -11
  30. package/src/blocks/RichText.astro +58 -57
  31. package/src/blocks/StatsBand.astro +117 -117
  32. package/src/blocks/Steps.astro +9 -9
  33. package/src/blocks/Testimonials.astro +13 -13
  34. package/src/blocks/cms/card-grid.ts +48 -80
  35. package/src/blocks/cms/consent.ts +1 -30
  36. package/src/blocks/cms/contact-card.ts +4 -46
  37. package/src/blocks/cms/contact-form.ts +2 -20
  38. package/src/blocks/cms/cta-banner.ts +2 -23
  39. package/src/blocks/cms/cta-pair.ts +4 -37
  40. package/src/blocks/cms/data-base.ts +2 -4
  41. package/src/blocks/cms/data.ts +19 -19
  42. package/src/blocks/cms/faq.ts +4 -36
  43. package/src/blocks/cms/feature-grid.ts +47 -80
  44. package/src/blocks/cms/fixtures.ts +591 -599
  45. package/src/blocks/cms/hero-carousel.ts +4 -48
  46. package/src/blocks/cms/hero.ts +2 -46
  47. package/src/blocks/cms/highlight-cards.ts +4 -39
  48. package/src/blocks/cms/images.ts +1 -2
  49. package/src/blocks/cms/index.ts +59 -87
  50. package/src/blocks/cms/location-hours.ts +3 -85
  51. package/src/blocks/cms/logo-cloud.ts +36 -62
  52. package/src/blocks/cms/media-text.ts +2 -20
  53. package/src/blocks/cms/page-header.ts +27 -49
  54. package/src/blocks/cms/pages.ts +35 -81
  55. package/src/blocks/cms/prose-section.ts +2 -22
  56. package/src/blocks/cms/reviews.ts +5 -56
  57. package/src/blocks/cms/schema-dsl.ts +134 -0
  58. package/src/blocks/cms/schema.ts +794 -0
  59. package/src/blocks/cms/shared.ts +63 -115
  60. package/src/blocks/cms/stats-band.ts +35 -61
  61. package/src/blocks/cms/steps.ts +4 -31
  62. package/src/blocks/cms/testimonials.ts +4 -38
  63. package/src/blocks/cms/topbar.ts +4 -41
  64. package/src/blocks/cms/types.ts +1 -37
  65. package/src/blocks/consent.ts +2 -2
  66. package/src/blocks/index.ts +2 -2
  67. package/src/blocks/internal/Actions.astro +11 -5
  68. package/src/blocks/internal/BlockHeader.astro +7 -7
  69. package/src/blocks/internal/Placeholder.astro +3 -3
  70. package/src/blocks/internal/fields.ts +18 -0
  71. package/src/blocks/internal/registry-contract.ts +67 -0
  72. package/src/blocks/internal/util.ts +4 -4
  73. package/src/blocks/lib/contact.ts +10 -4
  74. package/src/blocks/lib/faq.ts +1 -1
  75. package/src/blocks/lib/markdown.ts +350 -0
  76. package/src/blocks/lib/rich-text.ts +132 -143
  77. package/src/blocks/scripts/consent-manager.ts +1 -1
  78. package/src/blocks/scripts/consent-store.ts +1 -1
  79. package/src/blocks/scripts/hours.ts +3 -2
  80. package/src/blocks/types.ts +33 -23
  81. package/src/data/ChartContainer.astro +2 -1
  82. package/src/data/cms.ts +130 -190
  83. package/src/data/lib/chart.ts +13 -4
  84. package/src/data/schema.ts +65 -0
  85. package/src/finishes/aurora.css +75 -0
  86. package/src/finishes/autumn.css +87 -0
  87. package/src/finishes/blueprint.css +69 -0
  88. package/src/finishes/brutalist.css +55 -0
  89. package/src/finishes/clay.css +48 -0
  90. package/src/finishes/day-cycle.css +46 -0
  91. package/src/finishes/fog.css +71 -0
  92. package/src/finishes/glass.css +149 -0
  93. package/src/finishes/holographic.css +91 -0
  94. package/src/finishes/night.css +77 -0
  95. package/src/finishes/paper.css +58 -0
  96. package/src/finishes/polished-glass.css +159 -0
  97. package/src/finishes/rain.css +84 -0
  98. package/src/finishes/scripts/day-cycle.ts +30 -0
  99. package/src/finishes/scripts/holographic.ts +37 -0
  100. package/src/finishes/scripts/spotlight.ts +30 -0
  101. package/src/finishes/spotlight.css +41 -0
  102. package/src/finishes/spring.css +80 -0
  103. package/src/finishes/terminal.css +49 -0
  104. package/src/primitives/Divider.astro +1 -1
  105. package/src/primitives/FocusRing.astro +1 -1
  106. package/src/primitives/FormField.astro +2 -2
  107. package/src/primitives/Image.astro +3 -3
  108. package/src/primitives/Input.astro +4 -2
  109. package/src/primitives/MetricCard.astro +3 -1
  110. package/src/primitives/Picture.astro +3 -3
  111. package/src/primitives/Prose.astro +1 -1
  112. package/src/primitives/Select.astro +4 -2
  113. package/src/primitives/Slider.astro +1 -1
  114. package/src/primitives/Table.astro +1 -1
  115. package/src/primitives/Textarea.astro +4 -2
  116. package/src/primitives/Thumbnail.astro +2 -2
  117. package/src/primitives/index.ts +4 -4
  118. package/src/primitives/internal/ChoiceField.astro +4 -2
  119. package/src/primitives/scripts/slider.ts +1 -1
  120. package/src/primitives/scripts/table-sort.ts +2 -2
  121. package/src/seo/cms.ts +59 -80
  122. package/src/seo/index.ts +1 -1
  123. package/src/seo/integration.ts +40 -3
  124. package/src/seo/lib/jsonld.ts +10 -2
  125. package/src/seo/lib/meta.ts +2 -2
  126. package/src/seo/lib/redirects.ts +14 -4
  127. package/src/seo/lib/rss.ts +1 -1
  128. package/src/seo/lib/sitemap.ts +1 -1
  129. package/src/seo/schema.ts +43 -0
  130. package/src/blocks/internal/tina.ts +0 -19
  131. /package/src/blocks/{lib → internal}/consent.ts +0 -0
  132. /package/src/blocks/{lib → internal}/reviews.ts +0 -0
  133. /package/src/blocks/{lib → internal}/slug.ts +0 -0
  134. /package/src/data/{lib → internal}/scale.ts +0 -0
  135. /package/src/primitives/{lib → internal}/image-plan.ts +0 -0
  136. /package/src/primitives/{lib → internal}/image.ts +0 -0
  137. /package/src/primitives/{lib → internal}/slider.ts +0 -0
  138. /package/src/seo/{lib → internal}/url.ts +0 -0
@@ -1,49 +1,27 @@
1
- import type { PageHeaderData } from './data';
2
- import type { MapContext, TinaTemplate } from './types';
3
- import { actionsField, clean, compact, longTextField, mapActions, mapSettings, settingsField, textField } from './shared';
4
-
5
- export const pageHeaderDefaults: PageHeaderData = {
6
- _template: 'pageHeader',
7
- heading: 'Page title',
8
- breadcrumbs: [{ label: 'Home', href: '/' }],
9
- };
10
-
11
- export const pageHeaderTemplate: TinaTemplate = {
12
- name: 'pageHeader',
13
- label: 'Page header',
14
- ui: { defaultItem: pageHeaderDefaults, itemProps: (item: PageHeaderData) => ({ label: `Page header: ${item?.heading ?? ''}` }) },
15
- fields: [
16
- textField('eyebrow', 'Eyebrow'),
17
- textField('heading', 'Page title', { required: true }),
18
- longTextField('description', 'Intro text'),
19
- {
20
- type: 'object',
21
- name: 'breadcrumbs',
22
- label: 'Breadcrumbs',
23
- list: true,
24
- description: 'From Home down to the parent of this page. This page is added at the end automatically.',
25
- ui: { max: 6, itemProps: (item: { label?: string }) => ({ label: item?.label || 'Crumb' }), defaultItem: { label: 'Section', href: '/' } },
26
- fields: [textField('label', 'Label', { required: true }), textField('href', 'Link', { required: true })],
27
- },
28
- { type: 'string', name: 'breadcrumbPosition', label: 'Breadcrumbs position', options: [{ value: 'above', label: 'Above the title' }, { value: 'below', label: 'Below the title' }] },
29
- actionsField(2),
30
- settingsField(),
31
- ],
32
- };
33
-
34
- export function pageHeaderToProps(d: PageHeaderData, ctx: MapContext) {
35
- const breadcrumbs = (d.breadcrumbs ?? [])
36
- .filter((c) => clean(c?.label))
37
- .map((c) => compact({ label: clean(c.label)!, href: clean(c.href) }) as { label: string; href?: string });
38
- return compact({
39
- ...mapSettings(d.settings),
40
- eyebrow: clean(d.eyebrow),
41
- heading: clean(d.heading),
42
- description: clean(d.description),
43
- breadcrumbs,
44
- breadcrumbPosition: d.breadcrumbPosition,
45
- actions: mapActions(d.actions),
46
- headingLevel: ctx.headingLevel,
47
- placeholder: ctx.placeholder,
48
- });
49
- }
1
+ import type { PageHeaderData } from './data';
2
+ import type { MapContext } from './types';
3
+ import { safeHref } from '../lib/contact';
4
+ import { clean, compact, mapActions, mapSettings } from './shared';
5
+
6
+ export const pageHeaderDefaults: PageHeaderData = {
7
+ _template: 'pageHeader',
8
+ heading: 'Page title',
9
+ breadcrumbs: [{ label: 'Home', href: '/' }],
10
+ };
11
+
12
+ export function pageHeaderToProps(d: PageHeaderData, ctx: MapContext) {
13
+ const breadcrumbs = (d.breadcrumbs ?? [])
14
+ .filter((c) => clean(c?.label))
15
+ .map((c) => compact({ label: clean(c.label)!, href: safeHref(c.href) }) as { label: string; href?: string });
16
+ return compact({
17
+ ...mapSettings(d.settings),
18
+ eyebrow: clean(d.eyebrow),
19
+ heading: clean(d.heading),
20
+ description: clean(d.description),
21
+ breadcrumbs,
22
+ breadcrumbPosition: d.breadcrumbPosition,
23
+ actions: mapActions(d.actions),
24
+ headingLevel: ctx.headingLevel,
25
+ placeholder: ctx.placeholder,
26
+ });
27
+ }
@@ -1,81 +1,35 @@
1
- /**
2
- * Tina fields for site-wide page settings (not page-builder blocks): error-page copy and links,
3
- * and maintenance mode. Add them to a "Site settings" singleton collection:
4
- *
5
- * { name: 'settings', label: 'Site settings', path: 'content/settings', format: 'json',
6
- * ui: { allowedActions: { create: false, delete: false } },
7
- * fields: [errorPagesField, maintenanceField] }
8
- *
9
- * Note: a static site needs a rebuild for maintenance changes to show; on SSR, read the setting in
10
- * `createMaintenanceMiddleware({ enabled: async () => (await getSettings()).maintenance.enabled })`.
11
- */
12
- import { blockIconNames } from '../icons';
13
- import type { TinaField } from './types';
14
- import { longTextField, textField } from './shared';
15
-
16
- export const errorPagesField: TinaField = {
17
- type: 'object',
18
- name: 'errorPages',
19
- label: 'Error pages',
20
- fields: [
21
- textField('notFoundHeading', 'Page not found: heading', { description: 'Leave empty for the default.' }),
22
- longTextField('notFoundDescription', 'Page not found: text'),
23
- textField('serverErrorHeading', 'Server error: heading'),
24
- longTextField('serverErrorDescription', 'Server error: text'),
25
- {
26
- type: 'object',
27
- name: 'links',
28
- label: 'Popular pages',
29
- list: true,
30
- ui: { max: 6, itemProps: (item: { label?: string }) => ({ label: item?.label || 'Link' }) },
31
- fields: [
32
- textField('label', 'Label', { required: true }),
33
- textField('href', 'Link', { required: true }),
34
- textField('description', 'Short description'),
35
- { type: 'string', name: 'icon', label: 'Icon', options: blockIconNames },
36
- ],
37
- },
38
- textField('contactText', 'Help line intro', { description: 'e.g. “Need help now?”' }),
39
- textField('contactLabel', 'Help line link text', { description: 'e.g. “Call (817) 555-0100”' }),
40
- textField('contactHref', 'Help line link', { description: 'tel:, mailto: or a page.' }),
41
- ],
42
- };
43
-
44
- export const maintenanceField: TinaField = {
45
- type: 'object',
46
- name: 'maintenance',
47
- label: 'Maintenance mode',
48
- fields: [
49
- { type: 'boolean', name: 'enabled', label: 'Site is down for maintenance' },
50
- { type: 'datetime', name: 'until', label: 'Expected back', ui: { component: 'date' } as never },
51
- textField('heading', 'Heading'),
52
- longTextField('description', 'Message'),
53
- textField('notice', 'Important notice', { description: 'e.g. “Client dashboards are unaffected.”' }),
54
- ],
55
- };
56
-
57
- export interface ErrorPagesSettings {
58
- notFoundHeading?: string;
59
- notFoundDescription?: string;
60
- serverErrorHeading?: string;
61
- serverErrorDescription?: string;
62
- links?: Array<{ label?: string; href?: string; description?: string; icon?: string }>;
63
- contactText?: string;
64
- contactLabel?: string;
65
- contactHref?: string;
66
- }
67
-
68
- /** Props for <ErrorPage> from the settings singleton (empty fields fall back to defaults). */
69
- export function errorPageProps(settings: ErrorPagesSettings | null | undefined, status: number) {
70
- const s = settings ?? {};
71
- const t = (v?: string) => (v && v.trim() ? v.trim() : undefined);
72
- const server = status >= 500;
73
- const links = (s.links ?? []).filter((l) => t(l.label) && t(l.href)).map((l) => ({ label: t(l.label)!, href: t(l.href)!, description: t(l.description), icon: l.icon && (blockIconNames as string[]).includes(l.icon) ? l.icon : undefined }));
74
- return {
75
- status,
76
- heading: t(server ? s.serverErrorHeading : s.notFoundHeading),
77
- description: t(server ? s.serverErrorDescription : s.notFoundDescription),
78
- links,
79
- contact: t(s.contactLabel) && t(s.contactHref) ? { text: t(s.contactText), label: t(s.contactLabel)!, href: t(s.contactHref)! } : undefined,
80
- };
81
- }
1
+ /**
2
+ * Site-wide page settings (not page-builder blocks): error-page copy and links, and maintenance
3
+ * mode. Store them in a "Site settings" singleton and map them with `errorPageProps`.
4
+ *
5
+ * Note: a static site needs a rebuild for maintenance changes to show; on SSR, read the setting in
6
+ * `createMaintenanceMiddleware({ enabled: async () => (await getSettings()).maintenance.enabled })`.
7
+ */
8
+ import { blockIconNames } from '../icons';
9
+ import { safeHref } from '../lib/contact';
10
+
11
+ export interface ErrorPagesSettings {
12
+ notFoundHeading?: string;
13
+ notFoundDescription?: string;
14
+ serverErrorHeading?: string;
15
+ serverErrorDescription?: string;
16
+ links?: Array<{ label?: string; href?: string; description?: string; icon?: string }>;
17
+ contactText?: string;
18
+ contactLabel?: string;
19
+ contactHref?: string;
20
+ }
21
+
22
+ /** Props for <ErrorPage> from the settings singleton (empty fields fall back to defaults). */
23
+ export function errorPageProps(settings: ErrorPagesSettings | null | undefined, status: number) {
24
+ const s = settings ?? {};
25
+ const t = (v?: string) => (v && v.trim() ? v.trim() : undefined);
26
+ const server = status >= 500;
27
+ const links = (s.links ?? []).filter((l) => t(l.label) && safeHref(l.href)).map((l) => ({ label: t(l.label)!, href: safeHref(l.href)!, description: t(l.description), icon: l.icon && (blockIconNames as string[]).includes(l.icon) ? l.icon : undefined }));
28
+ return {
29
+ status,
30
+ heading: t(server ? s.serverErrorHeading : s.notFoundHeading),
31
+ description: t(server ? s.serverErrorDescription : s.notFoundDescription),
32
+ links,
33
+ contact: t(s.contactLabel) && safeHref(s.contactHref) ? { text: t(s.contactText), label: t(s.contactLabel)!, href: safeHref(s.contactHref)! } : undefined,
34
+ };
35
+ }
@@ -1,6 +1,6 @@
1
1
  import type { ProseSectionData } from './data';
2
- import type { MapContext, TinaTemplate } from './types';
3
- import { actionsField, clean, compact, mapActions, mapSettings, settingsField, textField } from './shared';
2
+ import type { MapContext } from './types';
3
+ import { clean, compact, mapActions, mapSettings } from './shared';
4
4
  import { isRichTextEmpty } from '../lib/rich-text';
5
5
 
6
6
  export const proseSectionDefaults: ProseSectionData = {
@@ -9,26 +9,6 @@ export const proseSectionDefaults: ProseSectionData = {
9
9
  body: 'Sinnstack builds fast, accessible websites for small businesses.\n\nEvery project starts with a short discovery call, and every site ships with a CMS your team can edit.',
10
10
  };
11
11
 
12
- export const proseSectionTemplate: TinaTemplate = {
13
- name: 'proseSection',
14
- label: 'Text',
15
- ui: { defaultItem: proseSectionDefaults, itemProps: (item: ProseSectionData) => ({ label: `Text: ${item?.heading ?? ''}` }) },
16
- fields: [
17
- textField('eyebrow', 'Eyebrow'),
18
- textField('heading', 'Heading'),
19
- {
20
- type: 'rich-text',
21
- name: 'body',
22
- label: 'Text',
23
- description: 'Headings inside the text are placed under the section heading automatically.',
24
- },
25
- actionsField(2),
26
- { type: 'boolean', name: 'measure', label: 'Readable line length', description: 'On by default. Turn off to use the full width.' },
27
- { type: 'string', name: 'size', label: 'Text size', options: [{ value: 's', label: 'Small' }, { value: 'm', label: 'Medium' }, { value: 'l', label: 'Large' }] },
28
- settingsField(),
29
- ],
30
- };
31
-
32
12
  export function proseSectionToProps(d: ProseSectionData, ctx: MapContext) {
33
13
  return compact({
34
14
  ...mapSettings(d.settings),
@@ -1,16 +1,9 @@
1
1
  import type { ReviewItem, ReviewSummary } from '../types';
2
2
  import type { ReviewsData } from './data';
3
- import type { MapContext, TinaTemplate } from './types';
4
- import { clean, compact, longTextField, mapSettings, settingsField, textField } from './shared';
3
+ import type { MapContext } from './types';
4
+ import { clean, compact, mapSettings, withFields } from './shared';
5
5
  import { safeHref } from '../lib/contact';
6
- import { validReviews } from '../lib/reviews';
7
-
8
- const ratingField = (name: string, label: string) => ({
9
- type: 'number' as const,
10
- name,
11
- label,
12
- ui: { validate: (v: unknown) => (v != null && v !== '' && !(Number(v) >= 1 && Number(v) <= 5) ? 'Use a number from 1 to 5.' : undefined) },
13
- });
6
+ import { validReviews } from '../internal/reviews';
14
7
 
15
8
  export const reviewsDefaults: ReviewsData = {
16
9
  _template: 'reviews',
@@ -22,53 +15,9 @@ export const reviewsDefaults: ReviewsData = {
22
15
  ],
23
16
  };
24
17
 
25
- export const reviewsTemplate: TinaTemplate = {
26
- name: 'reviews',
27
- label: 'Reviews',
28
- ui: { defaultItem: reviewsDefaults, itemProps: (item: ReviewsData) => ({ label: `Reviews: ${item?.heading ?? ''}` }) },
29
- fields: [
30
- textField('eyebrow', 'Eyebrow'),
31
- textField('heading', 'Heading'),
32
- longTextField('description', 'Intro text'),
33
- {
34
- type: 'object',
35
- name: 'summary',
36
- label: 'Overall rating (optional)',
37
- description: 'Copy these from the platform and keep them current.',
38
- fields: [
39
- { type: 'number', name: 'rating', label: 'Average rating (e.g. 4.8)' },
40
- { type: 'number', name: 'count', label: 'Number of reviews' },
41
- textField('source', 'Platform', { description: 'e.g. Google.' }),
42
- textField('href', 'Link to all reviews'),
43
- textField('writeHref', 'Link to leave a review'),
44
- ],
45
- },
46
- {
47
- type: 'object',
48
- name: 'items',
49
- label: 'Reviews',
50
- list: true,
51
- description: 'Copy reviews word for word, with the platform and date. Don’t leave out the critical ones.',
52
- ui: { itemProps: (item: { author?: string; rating?: number }) => ({ label: [item?.rating ? `${item.rating}★` : '', item?.author].filter(Boolean).join(' ') || 'Review' }), defaultItem: { rating: 5 } },
53
- fields: [
54
- longTextField('text', 'Review', { required: true }),
55
- textField('author', 'Author', { required: true }),
56
- { ...ratingField('rating', 'Stars (1–5)'), required: true },
57
- { type: 'datetime', name: 'date', label: 'Date' },
58
- textField('sourceLabel', 'Platform', { description: 'e.g. Google, Yelp.' }),
59
- textField('sourceHref', 'Link to the review (optional)'),
60
- longTextField('response', 'Your public reply (optional)'),
61
- ],
62
- },
63
- { type: 'string', name: 'variant', label: 'Style', options: [{ value: 'card', label: 'Boxes' }, { value: 'plain', label: 'Plain' }] },
64
- { type: 'string', name: 'columns', label: 'Columns (at most)', options: ['1', '2', '3'] },
65
- settingsField(),
66
- ],
67
- };
68
-
69
18
  export function reviewsToProps(d: ReviewsData, ctx: MapContext) {
70
19
  const items = validReviews(
71
- (d.items ?? []).map((i) => {
20
+ withFields(d.items, 'items').map((i) => {
72
21
  const label = clean(i?.sourceLabel);
73
22
  return compact({
74
23
  text: clean(i?.text) ?? '',
@@ -77,7 +26,7 @@ export function reviewsToProps(d: ReviewsData, ctx: MapContext) {
77
26
  date: clean(i?.date)?.slice(0, 10),
78
27
  source: label ? compact({ label, href: safeHref(clean(i.sourceHref)) }) : undefined,
79
28
  response: clean(i?.response),
80
- tina: i as never,
29
+ field: i._field,
81
30
  }) as ReviewItem;
82
31
  }),
83
32
  );
@@ -0,0 +1,134 @@
1
+ /**
2
+ * The field-definition format SinnStackCMS reads (`fields.text()`, `fields.list()`…), written out
3
+ * as plain data so @sinnstack/ui can ship schemas without depending on the CMS. Every value here
4
+ * is JSON: the CMS sends schemas to its admin as-is.
5
+ *
6
+ * The types are tied to the stored data shapes: `FieldsFor<HeroData>` only accepts keys HeroData
7
+ * has, a field kind that fits the value (a select for a set of strings, a list for an array…) and
8
+ * select options the mapper accepts. So `astro check` fails when a schema and its data shape drift.
9
+ */
10
+ import type { RichTextRoot } from '../lib/rich-text';
11
+
12
+ interface FieldBase {
13
+ /** Form label. Defaults to the field key, humanized. */
14
+ label?: string;
15
+ /** Short text shown under the label. */
16
+ description?: string;
17
+ /** Plain-language hint for non-technical editors. */
18
+ help?: string;
19
+ required?: boolean;
20
+ /** Starting value for a newly added item. */
21
+ default?: unknown;
22
+ readOnly?: boolean;
23
+ }
24
+ export interface TextField extends FieldBase {
25
+ type: 'text';
26
+ min?: number;
27
+ max?: number;
28
+ /** A regular expression the whole value must match. */
29
+ pattern?: string;
30
+ patternMessage?: string;
31
+ placeholder?: string;
32
+ }
33
+ export interface TextareaField extends FieldBase {
34
+ type: 'textarea';
35
+ min?: number;
36
+ max?: number;
37
+ rows?: number;
38
+ }
39
+ export interface MarkdownField extends FieldBase {
40
+ type: 'markdown';
41
+ rows?: number;
42
+ }
43
+ export interface NumberField extends FieldBase {
44
+ type: 'number';
45
+ min?: number;
46
+ max?: number;
47
+ step?: number;
48
+ integer?: boolean;
49
+ }
50
+ export interface BooleanField extends FieldBase {
51
+ type: 'boolean';
52
+ }
53
+ export type SelectOption<V extends string = string> = V | { value: V; label: string };
54
+ export interface SelectField<V extends string = string> extends FieldBase {
55
+ type: 'select';
56
+ options: Array<SelectOption<V>>;
57
+ }
58
+ export interface ImageField extends FieldBase {
59
+ type: 'image';
60
+ }
61
+ export interface LinkField extends FieldBase {
62
+ type: 'link';
63
+ placeholder?: string;
64
+ }
65
+ export interface ListField<F = SchemaField> extends FieldBase {
66
+ type: 'list';
67
+ of: F;
68
+ min?: number;
69
+ max?: number;
70
+ /** For lists of objects: the key whose value names each item. */
71
+ itemLabel?: string;
72
+ }
73
+ export interface ObjectField<F = Record<string, SchemaField>> extends FieldBase {
74
+ type: 'object';
75
+ fields: F;
76
+ }
77
+ export type SchemaField =
78
+ | TextField
79
+ | TextareaField
80
+ | MarkdownField
81
+ | NumberField
82
+ | BooleanField
83
+ | SelectField
84
+ | ImageField
85
+ | LinkField
86
+ | ListField
87
+ | ObjectField;
88
+
89
+ /** A block type for `fields.blocks({ types })`. Its key in `types` is the stored `_template`. */
90
+ export interface BlockSchema {
91
+ label: string;
92
+ /** Key whose value names the block in the admin's list. */
93
+ itemLabel?: string;
94
+ fields: Record<string, SchemaField>;
95
+ }
96
+
97
+ // --- Data shape → allowed field ---------------------------------------------------------------
98
+
99
+ type Defined<V> = Exclude<V, null | undefined>;
100
+ /** Free text: any string field, or a select with options of your own. */
101
+ type StringField = TextField | TextareaField | LinkField | ImageField | SelectField;
102
+
103
+ /** The field kinds that can store a value of type V. */
104
+ export type FieldFor<V> = [Defined<V>] extends [never]
105
+ ? never
106
+ : // Rich text is stored as Markdown (a textarea also works: plain text is valid Markdown).
107
+ [string, RichTextRoot] extends [Defined<V>, Defined<V>]
108
+ ? TextareaField | MarkdownField
109
+ : [Defined<V>] extends [boolean]
110
+ ? BooleanField
111
+ : [Defined<V>] extends [number]
112
+ ? NumberField
113
+ : [Defined<V>] extends [string]
114
+ ? string extends Defined<V>
115
+ ? StringField
116
+ : SelectField<Defined<V> & string>
117
+ : [Defined<V>] extends [ReadonlyArray<infer E>]
118
+ ? ListField<FieldFor<E>>
119
+ : [Defined<V>] extends [object]
120
+ ? ObjectField<FieldsFor<Defined<V>>>
121
+ : never;
122
+
123
+ /** Fields for a data shape: any subset of its keys, each with a field kind that fits. */
124
+ export type FieldsFor<T> = { [K in Exclude<keyof T, '_template'>]?: FieldFor<T[K]> };
125
+
126
+ /** A block schema checked against its data shape. */
127
+ export function blockSchema<T>(schema: { label: string; itemLabel?: Extract<keyof T, string>; fields: FieldsFor<T> }): BlockSchema {
128
+ return schema as BlockSchema;
129
+ }
130
+
131
+ /** An object field checked against a data shape (site settings, nested groups). */
132
+ export function objectSchema<T>(field: Omit<ObjectField, 'type' | 'fields'> & { fields: FieldsFor<T> }): ObjectField {
133
+ return { type: 'object', ...field } as ObjectField;
134
+ }