@eldrajs/theme-cli 0.2.0 → 0.2.1
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/package.json +3 -3
- package/template/.eldra/block-types.d.ts +12 -3
- package/template/.eldra/manifest.json +315 -25
- package/template/.eldra/previews.json +35 -34
- package/template/.github/workflows/eldra-deploy.yml +2 -0
- package/template/.gitlab-ci.yml +2 -0
- package/template/.storybook/main.ts +3 -0
- package/template/README.md +22 -10
- package/template/app/components/FormHoneypot.vue +21 -0
- package/template/app/composables/useFormSpam.ts +55 -0
- package/template/app/composables/usePageStructuredData.ts +112 -0
- package/template/app/composables/useTurnstile.ts +111 -0
- package/template/app/formSeeds.ts +93 -0
- package/template/app/pages/[...slug].vue +48 -12
- package/template/app/plugins/eldra-storefront.ts +19 -2
- package/template/app/seededPages.ts +7 -0
- package/template/app/storefront/analytics.ts +202 -0
- package/template/app/storefront/cart.ts +7 -0
- package/template/app/storefront/demo.ts +48 -2
- package/template/app/storefront/forms.ts +133 -0
- package/template/app/storefront/gateway.ts +113 -7
- package/template/app/storefront/structuredData.ts +161 -0
- package/template/app/storefront/types.ts +101 -2
- package/template/app/utils/seoInput.ts +172 -0
- package/template/bitbucket-pipelines.yml +3 -1
- package/template/blocks/breadcrumbs/Block.vue +1 -0
- package/template/blocks/cart/Block.vue +1 -0
- package/template/blocks/cart/parts/Summary.vue +1 -0
- package/template/blocks/contact/Block.vue +50 -21
- package/template/blocks/contact/__tests__/Block.spec.ts +89 -8
- package/template/blocks/contact/block.json +2 -9
- package/template/blocks/contact/mock.json +1 -2
- package/template/blocks/footer/Block.vue +56 -6
- package/template/blocks/footer/__tests__/Block.spec.ts +70 -16
- package/template/blocks/footer/preview.png +0 -0
- package/template/blocks/form/Block.vue +614 -0
- package/template/blocks/form/__tests__/Block.spec.ts +536 -0
- package/template/blocks/form/block.json +69 -0
- package/template/blocks/form/mock.json +25 -0
- package/template/blocks/form/preview.png +0 -0
- package/template/blocks/form/validate.ts +68 -0
- package/template/blocks/newsletter/Block.vue +57 -3
- package/template/blocks/newsletter/__tests__/Block.spec.ts +89 -31
- package/template/blocks/newsletter/block.json +4 -5
- package/template/blocks/newsletter/mock.json +1 -1
- package/template/blocks/newsletter/preview.png +0 -0
- package/template/i18n/en-US.json +33 -2
- package/template/i18n/is-IS.json +33 -2
- package/template/nuxt.config.ts +6 -0
- package/template/stories/generated/form.stories.ts +68 -0
- package/template/test/formValidate.spec.ts +46 -0
- package/template/test/prerenderFailure.spec.ts +1 -1
- package/template/test/prerenderRefresh.browser.spec.ts +5 -1
- package/template/test/seededPages.spec.ts +13 -0
- package/template/test/seoInput.spec.ts +119 -0
- package/template/test/slugPage.spec.ts +62 -26
- package/template/test/slugPageTemplate.spec.ts +5 -1
- package/template/test/starter.spec.ts +41 -3
- package/template/test/storefront/analytics.spec.ts +283 -0
- package/template/test/storefront/forms.spec.ts +385 -0
- package/template/test/storefront/structuredData.spec.ts +138 -0
- package/template/test/structuredData.generate.spec.ts +159 -0
- package/template/test/support/mockGateway.ts +29 -1
- package/template/test/support/turnstileForms.ts +53 -0
- package/template/app/utils/pageTitle.ts +0 -37
- package/template/test/pageTitle.spec.ts +0 -49
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's schema.org JSON-LD, from data the page already renders: the **same** product, the same
|
|
3
|
+
* variant prices, the same store currency and the same stock states (`variantStock` in
|
|
4
|
+
* `gateway.ts`) the buy box and `<Price>` read, so the markup cannot disagree with the page.
|
|
5
|
+
*
|
|
6
|
+
* Pure — no Vue, no Nuxt, no storefront — so `test/storefront/structuredData.spec.ts` pins every
|
|
7
|
+
* page type without mounting anything. The builders themselves are `@eldrajs/theme-core`'s
|
|
8
|
+
* (framework-free, theme-agnostic); this file is what is specific to this theme: which nodes a
|
|
9
|
+
* page type writes and how a `StorefrontProduct` maps onto them.
|
|
10
|
+
*
|
|
11
|
+
* | Page | Nodes |
|
|
12
|
+
* | --------------- | ---------------------------------------------- |
|
|
13
|
+
* | home | `Organization`, `WebSite` (+ `SearchAction`) |
|
|
14
|
+
* | product | `BreadcrumbList`, `Product` |
|
|
15
|
+
* | category/collection | `BreadcrumbList`, `ItemList` of the products shown |
|
|
16
|
+
* | any other page | `BreadcrumbList` |
|
|
17
|
+
* | not found | nothing |
|
|
18
|
+
*
|
|
19
|
+
* `Organization` and `WebSite` need an absolute URL, so they are written only when the build knows
|
|
20
|
+
* the site's origin; the others fall back to relative URLs without one (see `docs/theme-seo.md`).
|
|
21
|
+
*/
|
|
22
|
+
import {
|
|
23
|
+
buildBreadcrumbLd,
|
|
24
|
+
buildItemListLd,
|
|
25
|
+
buildOrganizationLd,
|
|
26
|
+
buildProductLd,
|
|
27
|
+
buildWebSiteLd,
|
|
28
|
+
ldPlainText,
|
|
29
|
+
type LdAvailability,
|
|
30
|
+
type ProductLdVariant,
|
|
31
|
+
type StructuredDataEntry,
|
|
32
|
+
} from '@eldrajs/theme-core';
|
|
33
|
+
import type { StorefrontProduct } from './types';
|
|
34
|
+
|
|
35
|
+
export type PageLdKind = 'home' | 'product' | 'category' | 'collection' | 'page' | 'notFound';
|
|
36
|
+
|
|
37
|
+
export interface PageLdParts {
|
|
38
|
+
kind: PageLdKind;
|
|
39
|
+
/** The current page's name: the last breadcrumb level. */
|
|
40
|
+
title: string;
|
|
41
|
+
/** The current page's own URL (canonical when the build knows one). */
|
|
42
|
+
currentUrl: string;
|
|
43
|
+
/** A site path (`/products/x`) as the URL to write: locale-prefixed, absolute when possible. */
|
|
44
|
+
url: (path: string) => string;
|
|
45
|
+
/** Whether the build knows the site's origin, i.e. `url()` is absolute. */
|
|
46
|
+
absolute: boolean;
|
|
47
|
+
homeLabel: string;
|
|
48
|
+
/** The levels between Home and the current page, root first. */
|
|
49
|
+
trail: ReadonlyArray<{ label: string; path: string }>;
|
|
50
|
+
/** The product, on a product page. */
|
|
51
|
+
product: StorefrontProduct | null;
|
|
52
|
+
/** The store currency (`useMoney().currency`), or `undefined` when the platform published none. */
|
|
53
|
+
currency: string | undefined;
|
|
54
|
+
/** The products a listing page shows, in order. */
|
|
55
|
+
items: ReadonlyArray<{ title: string; url: string }>;
|
|
56
|
+
profile: {
|
|
57
|
+
name: string;
|
|
58
|
+
logoUrl: string | null;
|
|
59
|
+
sameAs: readonly string[];
|
|
60
|
+
contact: { email: string | null; telephone: string | null };
|
|
61
|
+
};
|
|
62
|
+
/** The site's search page path (`/search`, unprefixed: it follows the home URL), or `null` when
|
|
63
|
+
* the site has none. */
|
|
64
|
+
searchPath: string | null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const AVAILABILITY: Record<StorefrontProduct['stock'], LdAvailability> = {
|
|
68
|
+
in: 'InStock',
|
|
69
|
+
low: 'LimitedAvailability',
|
|
70
|
+
out: 'OutOfStock',
|
|
71
|
+
preorder: 'PreOrder',
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** One offer per variant; a source without variants gets one from the product's own price. */
|
|
75
|
+
function variantsOf(product: StorefrontProduct, url: string): ProductLdVariant[] {
|
|
76
|
+
if (product.variants !== undefined && product.variants.length > 0) {
|
|
77
|
+
return product.variants.map((variant) => ({
|
|
78
|
+
sku: variant.sku,
|
|
79
|
+
gtin: variant.barcode,
|
|
80
|
+
price: variant.price,
|
|
81
|
+
compareAt: variant.compareAt,
|
|
82
|
+
availability: AVAILABILITY[variant.stock],
|
|
83
|
+
url,
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
return [
|
|
87
|
+
{
|
|
88
|
+
price: product.price.amount,
|
|
89
|
+
compareAt: product.price.compareAt,
|
|
90
|
+
availability: AVAILABILITY[product.stock],
|
|
91
|
+
url,
|
|
92
|
+
},
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function pageStructuredData(parts: PageLdParts): StructuredDataEntry[] {
|
|
97
|
+
if (parts.kind === 'notFound') return [];
|
|
98
|
+
|
|
99
|
+
if (parts.kind === 'home') {
|
|
100
|
+
// Both need an absolute URL and say nothing useful without one.
|
|
101
|
+
if (!parts.absolute) return [];
|
|
102
|
+
const origin = parts.url('/');
|
|
103
|
+
return [
|
|
104
|
+
{
|
|
105
|
+
type: 'organization',
|
|
106
|
+
node: buildOrganizationLd({
|
|
107
|
+
name: parts.profile.name,
|
|
108
|
+
url: origin,
|
|
109
|
+
logo: parts.profile.logoUrl,
|
|
110
|
+
sameAs: parts.profile.sameAs,
|
|
111
|
+
contact: parts.profile.contact,
|
|
112
|
+
}),
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
type: 'website',
|
|
116
|
+
node: buildWebSiteLd({
|
|
117
|
+
name: parts.profile.name,
|
|
118
|
+
url: origin,
|
|
119
|
+
searchPath: parts.searchPath,
|
|
120
|
+
}),
|
|
121
|
+
},
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const entries: StructuredDataEntry[] = [
|
|
126
|
+
{
|
|
127
|
+
type: 'breadcrumb',
|
|
128
|
+
node: buildBreadcrumbLd([
|
|
129
|
+
{ name: parts.homeLabel, url: parts.url('/') },
|
|
130
|
+
...parts.trail.map((level) => ({ name: level.label, url: parts.url(level.path) })),
|
|
131
|
+
{ name: parts.title, url: parts.currentUrl },
|
|
132
|
+
]),
|
|
133
|
+
},
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
if (parts.kind === 'product' && parts.product !== null) {
|
|
137
|
+
const product = parts.product;
|
|
138
|
+
entries.push({
|
|
139
|
+
type: 'product',
|
|
140
|
+
node: buildProductLd({
|
|
141
|
+
url: parts.currentUrl,
|
|
142
|
+
name: product.title,
|
|
143
|
+
description: ldPlainText(product.description),
|
|
144
|
+
images: product.images.map((image) => image.src),
|
|
145
|
+
// A product with no brand of its own is sold under the shop's name (the site profile's).
|
|
146
|
+
brand: product.brand ?? parts.profile.name,
|
|
147
|
+
variants: variantsOf(product, parts.currentUrl),
|
|
148
|
+
currency: parts.currency,
|
|
149
|
+
ratings: product.rating,
|
|
150
|
+
}),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if ((parts.kind === 'category' || parts.kind === 'collection') && parts.items.length > 0) {
|
|
155
|
+
entries.push({
|
|
156
|
+
type: 'itemList',
|
|
157
|
+
node: buildItemListLd(parts.items.map((item) => ({ name: item.title, url: item.url }))),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return entries;
|
|
161
|
+
}
|
|
@@ -179,6 +179,28 @@ export interface StorefrontProduct extends StorefrontProductListItem {
|
|
|
179
179
|
*/
|
|
180
180
|
inventory: number | null;
|
|
181
181
|
shipsBy?: string | null;
|
|
182
|
+
/**
|
|
183
|
+
* The product's brand, when the platform carries one (`brand` on the public product read). Read
|
|
184
|
+
* by the page's structured data only; the page itself does not show it.
|
|
185
|
+
*/
|
|
186
|
+
brand?: string | null;
|
|
187
|
+
/**
|
|
188
|
+
* **Every variant, with the price and stock the page's own variant resolves from** — what the
|
|
189
|
+
* page's structured data writes one offer per. Absent from a source that has no variants to
|
|
190
|
+
* list (the demo), where the structured data falls back to one offer from `price` and `stock`.
|
|
191
|
+
* `barcode` is the catalog's free text; the structured-data builder validates it as a GTIN.
|
|
192
|
+
*/
|
|
193
|
+
variants?: StorefrontProductVariant[];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface StorefrontProductVariant {
|
|
197
|
+
sku: string;
|
|
198
|
+
barcode: string | null;
|
|
199
|
+
/** Major units, like every amount in this file. */
|
|
200
|
+
price: number;
|
|
201
|
+
compareAt: number | null;
|
|
202
|
+
/** `variantStock`'s four states for this variant: `out` for an unpublished one. */
|
|
203
|
+
stock: 'in' | 'low' | 'out' | 'preorder';
|
|
182
204
|
}
|
|
183
205
|
|
|
184
206
|
export interface StorefrontCollectionInfo {
|
|
@@ -717,9 +739,86 @@ export interface StorefrontCatalog {
|
|
|
717
739
|
notifyBackInStock(input: { email: string; variantId: string }): Promise<StorefrontAck>;
|
|
718
740
|
}
|
|
719
741
|
|
|
742
|
+
/** The field types a form definition carries (the platform's `site/v1/forms` contract). */
|
|
743
|
+
export type StorefrontFormFieldType =
|
|
744
|
+
| 'text'
|
|
745
|
+
| 'email'
|
|
746
|
+
| 'phone'
|
|
747
|
+
| 'textarea'
|
|
748
|
+
| 'select'
|
|
749
|
+
| 'checkbox'
|
|
750
|
+
| 'consent'
|
|
751
|
+
| 'number'
|
|
752
|
+
| 'url';
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* A form field with its text already resolved to the active content locale (`pickLocalized` in
|
|
756
|
+
* `forms.ts`): a block renders strings, never a per-locale map.
|
|
757
|
+
*/
|
|
758
|
+
export interface StorefrontFormField {
|
|
759
|
+
/** The key of this field's value in a submission and of its messages in a `422`. */
|
|
760
|
+
id: string;
|
|
761
|
+
type: StorefrontFormFieldType;
|
|
762
|
+
required: boolean;
|
|
763
|
+
label: string;
|
|
764
|
+
placeholder?: string;
|
|
765
|
+
options?: Array<{ value: string; label: string }>;
|
|
766
|
+
validation?: { minLength?: number; maxLength?: number; pattern?: string };
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/** A form as the platform serves it (`GET /site/v1/forms/{key}`), localized for the page. */
|
|
770
|
+
export interface StorefrontFormDefinition {
|
|
771
|
+
key: string;
|
|
772
|
+
name: string;
|
|
773
|
+
fields: StorefrontFormField[];
|
|
774
|
+
/** A field name a person never fills: render it off-screen and send its value as `hp`. */
|
|
775
|
+
honeypotField: string;
|
|
776
|
+
/** Cloudflare Turnstile site key; `null` when the merchant has not turned it on. */
|
|
777
|
+
turnstileSiteKey: string | null;
|
|
778
|
+
/** The merchant's confirmation text. */
|
|
779
|
+
successMessage: string;
|
|
780
|
+
/** Where to send the visitor after a success, or `null` to stay and show the message. */
|
|
781
|
+
successRedirect: string | null;
|
|
782
|
+
/**
|
|
783
|
+
* The consent sentence of a subscribing form, in the page's locale, `{shop}` already replaced.
|
|
784
|
+
* `''` when the form does not subscribe. Rendered as plain text beside the submit button.
|
|
785
|
+
*/
|
|
786
|
+
consentText: string;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
export interface StorefrontFormSubmitInput {
|
|
790
|
+
/** `fieldId` to the entered value; a checkbox is a boolean, every other type a string. */
|
|
791
|
+
values: Record<string, string | boolean>;
|
|
792
|
+
turnstileToken?: string;
|
|
793
|
+
/** The honeypot field's value. */
|
|
794
|
+
hp?: string;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* What a submission came to. `invalid` carries the platform's own field-keyed messages (the
|
|
799
|
+
* `422` body), which a form shows inline under each field; every other failure is a reason the
|
|
800
|
+
* visitor can act on. `pending` on a success means a confirmation email (double opt-in) is on its
|
|
801
|
+
* way, so a block says "check your inbox" rather than "you're subscribed".
|
|
802
|
+
*/
|
|
803
|
+
export type StorefrontFormSubmitResult =
|
|
804
|
+
| { ok: true; pending: boolean }
|
|
805
|
+
| { ok: false; reason: 'invalid'; errors: Record<string, string[]>; form: string[] }
|
|
806
|
+
| { ok: false; reason: 'rate-limited' | 'too-large' | 'unavailable' | 'unsupported' };
|
|
807
|
+
|
|
720
808
|
export interface StorefrontForms {
|
|
721
|
-
|
|
722
|
-
|
|
809
|
+
/**
|
|
810
|
+
* The definition of the form `key` names, in the page's content locale. Baked into the site at
|
|
811
|
+
* build time when the build could read it (`@eldrajs/theme-nuxt`'s `forms` read); fetched when a
|
|
812
|
+
* visitor opens the page otherwise. `data` stays `null` for a key the platform does not know
|
|
813
|
+
* (`error` is then set), and a block renders nothing for it rather than an empty form.
|
|
814
|
+
*/
|
|
815
|
+
definition(key: Ref<string>): StorefrontResult<StorefrontFormDefinition>;
|
|
816
|
+
/**
|
|
817
|
+
* Sends one submission. Never throws: a refusal is a `{ ok: false, reason }`. With the theme's
|
|
818
|
+
* `formsEndpoint` configured the submission goes there instead of the platform
|
|
819
|
+
* (`{ kind: 'form', key, values, locale, pagePath }` as JSON, any 2xx is a success).
|
|
820
|
+
*/
|
|
821
|
+
submit(key: string, input: StorefrontFormSubmitInput): Promise<StorefrontFormSubmitResult>;
|
|
723
822
|
}
|
|
724
823
|
|
|
725
824
|
export interface StorefrontSearch {
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { imageUrl } from '@eldrajs/sdk';
|
|
2
|
+
import type { SeoContext, SeoData, SeoEntryValue } from '@eldrajs/theme-core';
|
|
3
|
+
|
|
4
|
+
/** The shapes this reads are CMS data and catalog projections, so every access is guarded. */
|
|
5
|
+
type Doc = { data?: Record<string, unknown> } | null | undefined;
|
|
6
|
+
|
|
7
|
+
export interface SeoInputParts {
|
|
8
|
+
/** The route is showing the not-found shell. */
|
|
9
|
+
isNotFound: boolean;
|
|
10
|
+
/** The translated not-found title. */
|
|
11
|
+
notFoundTitle: string;
|
|
12
|
+
/** Which catalog object the route shows, or `null` for an ordinary page. */
|
|
13
|
+
catalog: { kind: 'product' | 'collection' | 'category'; slug: string } | null;
|
|
14
|
+
/** The resolved catalog object (a product, collection or category), or the page's own. */
|
|
15
|
+
entry: Doc;
|
|
16
|
+
page: Doc;
|
|
17
|
+
template: Doc;
|
|
18
|
+
/** The request path as served, locale prefix included. */
|
|
19
|
+
path: string;
|
|
20
|
+
/** Every locale the site serves; used to tell `/` from `/is-IS`. */
|
|
21
|
+
locales: readonly string[];
|
|
22
|
+
/**
|
|
23
|
+
* The name of the product's own (deepest) category, for `product.category`. The product
|
|
24
|
+
* projection serves only `categoryIds`; the name comes from the category list the storefront
|
|
25
|
+
* already loads (`categoryTrailFor`), so the caller resolves it.
|
|
26
|
+
*/
|
|
27
|
+
productCategory?: string | null;
|
|
28
|
+
/** Formats a major-unit amount in the store's currency and locale. */
|
|
29
|
+
formatPrice: (amount: number) => string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface SeoInput {
|
|
33
|
+
context: SeoContext;
|
|
34
|
+
data: SeoData;
|
|
35
|
+
options: {
|
|
36
|
+
entry: SeoEntryValue | null;
|
|
37
|
+
template: SeoEntryValue | null;
|
|
38
|
+
contentImage: string | null;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The meta description a product's own text may give the `product.shortDescription` variable. */
|
|
43
|
+
const SHORT_DESCRIPTION_MAX = 160;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Everything `useEldraSeo` needs for one route: the context, the variables that context offers
|
|
47
|
+
* (`page.title`, `product.name`, ... — the platform's registry names them), the route's own `seo`
|
|
48
|
+
* field values and the content's own image.
|
|
49
|
+
*
|
|
50
|
+
* The page's title follows the rule the document title always had: a catalog route is named by
|
|
51
|
+
* the object it shows (the shared route template's own title only names the template), anything
|
|
52
|
+
* else by its own document. The not-found shell names itself and is never indexed.
|
|
53
|
+
*/
|
|
54
|
+
export function seoInput(parts: SeoInputParts): SeoInput {
|
|
55
|
+
const entryData = parts.entry?.data ?? {};
|
|
56
|
+
const own = parts.template?.data ?? parts.page?.data ?? {};
|
|
57
|
+
const catalogKind = parts.catalog?.kind ?? null;
|
|
58
|
+
|
|
59
|
+
const title = text(catalogKind === null ? own.title : entryData.title) ?? text(own.title) ?? '';
|
|
60
|
+
const data: SeoData = {
|
|
61
|
+
'page.title': title,
|
|
62
|
+
'page.slug': text(own.slug) ?? text(entryData.slug) ?? lastSegment(parts.path),
|
|
63
|
+
};
|
|
64
|
+
let contentImage: string | null = null;
|
|
65
|
+
|
|
66
|
+
if (catalogKind === 'product') {
|
|
67
|
+
const variants = list(entryData.variants);
|
|
68
|
+
const prices = variants
|
|
69
|
+
.map((variant) => variant.price)
|
|
70
|
+
.filter((price): price is number => typeof price === 'number' && Number.isFinite(price));
|
|
71
|
+
data['product.name'] = title;
|
|
72
|
+
data['product.sku'] = text(variants[0]?.sku) ?? '';
|
|
73
|
+
data['product.price'] = prices.length === 0 ? '' : parts.formatPrice(Math.min(...prices));
|
|
74
|
+
data['product.shortDescription'] = shortDescription(entryData.description);
|
|
75
|
+
data['product.brand'] = text(entryData.brand) ?? '';
|
|
76
|
+
data['product.category'] = text(parts.productCategory) ?? '';
|
|
77
|
+
contentImage = shareImage(list(entryData.images)[0]);
|
|
78
|
+
} else if (catalogKind !== null) {
|
|
79
|
+
data[`${catalogKind}.name`] = title;
|
|
80
|
+
data[`${catalogKind}.description`] = shortDescription(entryData.description);
|
|
81
|
+
data[`${catalogKind}.productCount`] =
|
|
82
|
+
typeof entryData.productCount === 'number' ? entryData.productCount : '';
|
|
83
|
+
if (catalogKind === 'collection') contentImage = shareImage(entryData.image);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// A catalog object is not an authored document: its own fields are not an `seo` value, only the
|
|
87
|
+
// shared route template's are.
|
|
88
|
+
const entry = catalogKind === null ? seoValue(parts.page?.data?.seo) : null;
|
|
89
|
+
const template = seoValue(parts.template?.data?.seo);
|
|
90
|
+
|
|
91
|
+
if (parts.isNotFound) {
|
|
92
|
+
return {
|
|
93
|
+
context: 'page',
|
|
94
|
+
data: { 'page.title': parts.notFoundTitle },
|
|
95
|
+
options: {
|
|
96
|
+
entry: { title: parts.notFoundTitle, noindex: true },
|
|
97
|
+
template: null,
|
|
98
|
+
contentImage: null,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
context:
|
|
105
|
+
catalogKind !== null
|
|
106
|
+
? `catalog:${catalogKind}`
|
|
107
|
+
: isHome(parts.path, parts.locales)
|
|
108
|
+
? 'home'
|
|
109
|
+
: 'page',
|
|
110
|
+
data,
|
|
111
|
+
options: { entry, template, contentImage },
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function isHome(path: string, locales: readonly string[]): boolean {
|
|
116
|
+
const clean = path.split(/[?#]/)[0]!.replace(/\/+$/, '');
|
|
117
|
+
return clean === '' || locales.some((locale) => clean === `/${locale}`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function lastSegment(path: string): string {
|
|
121
|
+
const segments = path
|
|
122
|
+
.split(/[?#]/)[0]!
|
|
123
|
+
.split('/')
|
|
124
|
+
.filter((part) => part !== '');
|
|
125
|
+
return segments[segments.length - 1] ?? '';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function text(value: unknown): string | null {
|
|
129
|
+
return typeof value === 'string' && value.trim() !== '' ? value : null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function list(value: unknown): Record<string, unknown>[] {
|
|
133
|
+
return Array.isArray(value)
|
|
134
|
+
? value.filter(
|
|
135
|
+
(item): item is Record<string, unknown> =>
|
|
136
|
+
typeof item === 'object' && item !== null && !Array.isArray(item)
|
|
137
|
+
)
|
|
138
|
+
: [];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A media item's 1200 px rendering, the width the share card wants. */
|
|
142
|
+
function shareImage(value: unknown): string | null {
|
|
143
|
+
if (typeof value !== 'object' || value === null) return null;
|
|
144
|
+
const url = text((value as { url?: unknown }).url);
|
|
145
|
+
return url === null ? null : imageUrl(url, 'lg');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function seoValue(value: unknown): SeoEntryValue | null {
|
|
149
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
150
|
+
? (value as SeoEntryValue)
|
|
151
|
+
: null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** A description as one line of plain text: a string as it is, a rich-text document flattened. */
|
|
155
|
+
function shortDescription(value: unknown): string {
|
|
156
|
+
const plain = (typeof value === 'string' ? value : flatten(value)).replace(/\s+/g, ' ').trim();
|
|
157
|
+
if (plain.length <= SHORT_DESCRIPTION_MAX) return plain;
|
|
158
|
+
const cut = plain.slice(0, SHORT_DESCRIPTION_MAX - 1);
|
|
159
|
+
return `${cut.slice(0, cut.lastIndexOf(' ') > 40 ? cut.lastIndexOf(' ') : cut.length)}…`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function flatten(node: unknown): string {
|
|
163
|
+
if (typeof node !== 'object' || node === null) return '';
|
|
164
|
+
const {
|
|
165
|
+
type,
|
|
166
|
+
text: own,
|
|
167
|
+
content,
|
|
168
|
+
} = node as { type?: unknown; text?: unknown; content?: unknown };
|
|
169
|
+
if (type === 'text') return typeof own === 'string' ? own : '';
|
|
170
|
+
if (!Array.isArray(content)) return '';
|
|
171
|
+
return content.map(flatten).join(' ');
|
|
172
|
+
}
|
|
@@ -7,7 +7,9 @@ definitions:
|
|
|
7
7
|
script:
|
|
8
8
|
- corepack enable && corepack prepare pnpm@9.15.0 --activate
|
|
9
9
|
- pnpm install --frozen-lockfile
|
|
10
|
-
|
|
10
|
+
# A build is production unless ELDRA_BUILD_TARGET=preview (noindex, `Disallow: /`).
|
|
11
|
+
# Set ELDRA_SITE_URL as a repository variable.
|
|
12
|
+
- ELDRA_SITE_URL="$ELDRA_SITE_URL" pnpm exec nuxi generate
|
|
11
13
|
- pnpm exec eldra-theme deploy
|
|
12
14
|
|
|
13
15
|
pipelines:
|
|
@@ -40,7 +40,13 @@
|
|
|
40
40
|
* (the three `watch`es), which is what lets a corrected field's `aria-invalid` go false in time for
|
|
41
41
|
* that next submit to get past `FormLayout`'s own scan.
|
|
42
42
|
*
|
|
43
|
-
* **
|
|
43
|
+
* **Submitting** is the platform's own form, `forms.submit('contact', { values })`: the seeded
|
|
44
|
+
* `contact` form (`app/formSeeds.ts`) names the fields this block sends (`name`, `email`, `topic`,
|
|
45
|
+
* `orderNumber`, `message`), and who is notified is the merchant's setting on the Forms page, not
|
|
46
|
+
* block data. A `422` marks the field it names (the same three error refs a local check sets);
|
|
47
|
+
* every other failure shows the server summary.
|
|
48
|
+
*
|
|
49
|
+
* **Success.** `forms.submit` resolving `{ ok: true }` swaps the card's content for a
|
|
44
50
|
* `role="status" tabindex="-1"` panel that receives focus (spec: "the card content is replaced").
|
|
45
51
|
* "Send another message" restores an empty form and focuses the Name field (spec: "moves focus to
|
|
46
52
|
* its first field").
|
|
@@ -64,10 +70,12 @@ import {
|
|
|
64
70
|
} from '@eldrajs/ui';
|
|
65
71
|
import { useBlockData } from '../../app/composables/useBlockData';
|
|
66
72
|
import { useEditing } from '../../app/composables/useEditing';
|
|
73
|
+
import { useFormSpam } from '../../app/composables/useFormSpam';
|
|
67
74
|
import { useStorefront } from '../../app/composables/useStorefront';
|
|
68
75
|
import { useI18n } from 'vue-i18n';
|
|
69
76
|
import { useUiId } from '../../app/composables/useUiId';
|
|
70
77
|
import EldraIcon from '../../app/components/EldraIcon.vue';
|
|
78
|
+
import FormHoneypot from '../../app/components/FormHoneypot.vue';
|
|
71
79
|
import EldraRouterLink from '../../app/components/EldraRouterLink.vue';
|
|
72
80
|
import UiImage from '../../app/components/ui/UiImage.vue';
|
|
73
81
|
import { isInternalHref, safeHref } from '../../app/utils/links';
|
|
@@ -272,11 +280,13 @@ watch(message, () => {
|
|
|
272
280
|
if (messageError.value) messageError.value = null;
|
|
273
281
|
});
|
|
274
282
|
|
|
283
|
+
const spam = useFormSpam(computed(() => (hasForm.value ? 'contact' : '')));
|
|
284
|
+
const { honeypot, honeypotField, siteKey, turnstileRoot } = spam;
|
|
275
285
|
const sending = ref(false);
|
|
276
286
|
const serverError = ref(false);
|
|
287
|
+
/** The spam check (Turnstile) is on and the visitor has not passed it. */
|
|
288
|
+
const challengeError = ref<string | null>(null);
|
|
277
289
|
const sent = ref(false);
|
|
278
|
-
const sentFirstName = ref('');
|
|
279
|
-
const sentEmail = ref('');
|
|
280
290
|
|
|
281
291
|
const summaryRef = ref<HTMLElement | null>(null);
|
|
282
292
|
const statusRef = ref<HTMLElement | null>(null);
|
|
@@ -284,7 +294,9 @@ const statusRef = ref<HTMLElement | null>(null);
|
|
|
284
294
|
const invalidCount = computed(
|
|
285
295
|
() => [nameError.value, emailError.value, messageError.value].filter(Boolean).length
|
|
286
296
|
);
|
|
287
|
-
const hasFormError = computed(
|
|
297
|
+
const hasFormError = computed(
|
|
298
|
+
() => invalidCount.value > 0 || serverError.value || challengeError.value !== null
|
|
299
|
+
);
|
|
288
300
|
const summaryTitle = computed(() =>
|
|
289
301
|
invalidCount.value === 1
|
|
290
302
|
? t('contact.summaryTitleOne')
|
|
@@ -311,10 +323,14 @@ function focusField(id: string): void {
|
|
|
311
323
|
|
|
312
324
|
async function sendForm(): Promise<void> {
|
|
313
325
|
serverError.value = false;
|
|
326
|
+
challengeError.value = spam.challenge();
|
|
327
|
+
if (challengeError.value !== null) {
|
|
328
|
+
await focusSummary();
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
314
331
|
sending.value = true;
|
|
315
332
|
|
|
316
333
|
const payload: Record<string, string> = {
|
|
317
|
-
recipient: data.value.recipient,
|
|
318
334
|
name: name.value.trim(),
|
|
319
335
|
email: emailValue.value.trim(),
|
|
320
336
|
message: message.value.trim(),
|
|
@@ -324,17 +340,25 @@ async function sendForm(): Promise<void> {
|
|
|
324
340
|
}
|
|
325
341
|
if (hasTopics.value && topic.value !== '') payload.topic = topic.value;
|
|
326
342
|
|
|
327
|
-
const result = await storefront.forms.
|
|
343
|
+
const result = await storefront.forms.submit('contact', { values: payload, ...spam.signals() });
|
|
328
344
|
sending.value = false;
|
|
345
|
+
// A Turnstile token is single-use, whatever the outcome.
|
|
346
|
+
spam.reset();
|
|
329
347
|
|
|
330
348
|
if (!result.ok) {
|
|
331
|
-
|
|
349
|
+
if (result.reason === 'invalid') {
|
|
350
|
+
// The platform's own message for each field it refused, shown where a local check's would be.
|
|
351
|
+
nameError.value = result.errors.name?.[0] ?? null;
|
|
352
|
+
emailError.value = result.errors.email?.[0] ?? null;
|
|
353
|
+
messageError.value = result.errors.message?.[0] ?? null;
|
|
354
|
+
}
|
|
355
|
+
// A refusal that named none of the three fields (another field, the form as a whole, or no
|
|
356
|
+
// reason at all) would otherwise leave nothing on screen.
|
|
357
|
+
serverError.value = !nameError.value && !emailError.value && !messageError.value;
|
|
332
358
|
await focusSummary();
|
|
333
359
|
return;
|
|
334
360
|
}
|
|
335
361
|
|
|
336
|
-
sentFirstName.value = name.value.trim().split(/\s+/)[0] ?? '';
|
|
337
|
-
sentEmail.value = emailValue.value.trim();
|
|
338
362
|
sent.value = true;
|
|
339
363
|
await nextTick();
|
|
340
364
|
statusRef.value?.focus();
|
|
@@ -365,20 +389,15 @@ async function onSendAnother(): Promise<void> {
|
|
|
365
389
|
emailError.value = null;
|
|
366
390
|
messageError.value = null;
|
|
367
391
|
serverError.value = false;
|
|
392
|
+
challengeError.value = null;
|
|
393
|
+
honeypot.value = '';
|
|
368
394
|
sent.value = false;
|
|
369
395
|
await nextTick();
|
|
370
396
|
focusField(nameFieldId);
|
|
371
397
|
}
|
|
372
398
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
Object.hasOwn(params, key) ? params[key]! : match
|
|
376
|
-
);
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
const successText = computed(() =>
|
|
380
|
-
interpolate(data.value.successText || t('contact.successTextDefault'), { email: sentEmail.value })
|
|
381
|
-
);
|
|
399
|
+
/** The receipt never echoes what the visitor typed (no name, no address): it is shown to whoever is at the screen. */
|
|
400
|
+
const successText = computed(() => data.value.successText || t('contact.successTextDefault'));
|
|
382
401
|
</script>
|
|
383
402
|
|
|
384
403
|
<template>
|
|
@@ -468,8 +487,15 @@ const successText = computed(() =>
|
|
|
468
487
|
>
|
|
469
488
|
<template v-if="hasFormError" #errorSummary>
|
|
470
489
|
<div ref="summaryRef" tabindex="-1">
|
|
471
|
-
<
|
|
472
|
-
|
|
490
|
+
<p v-if="challengeError">{{ challengeError }}</p>
|
|
491
|
+
<template v-else-if="serverError">
|
|
492
|
+
<p>
|
|
493
|
+
{{
|
|
494
|
+
email
|
|
495
|
+
? t('contact.serverError', { email })
|
|
496
|
+
: t('contact.serverErrorGeneric')
|
|
497
|
+
}}
|
|
498
|
+
</p>
|
|
473
499
|
</template>
|
|
474
500
|
<template v-else>
|
|
475
501
|
<p class="font-semibold">{{ summaryTitle }}</p>
|
|
@@ -553,6 +579,9 @@ const successText = computed(() =>
|
|
|
553
579
|
/>
|
|
554
580
|
</FieldWrapper>
|
|
555
581
|
|
|
582
|
+
<FormHoneypot v-model="honeypot" :name="honeypotField" />
|
|
583
|
+
<div v-if="siteKey" ref="turnstileRoot" class="@two-col:col-span-2" />
|
|
584
|
+
|
|
556
585
|
<template #actions>
|
|
557
586
|
<Button variant="primary" type="submit" size="lg" :label="t('contact.sending')">{{
|
|
558
587
|
t('contact.sendMessage')
|
|
@@ -564,7 +593,7 @@ const successText = computed(() =>
|
|
|
564
593
|
<div v-else ref="statusRef" role="status" tabindex="-1" class="grid gap-4 text-center">
|
|
565
594
|
<EldraIcon name="circle-check" size="xl" class="text-success mx-auto" />
|
|
566
595
|
<p class="text-h4 font-heading font-semibold">
|
|
567
|
-
{{ t('contact.successTitle'
|
|
596
|
+
{{ t('contact.successTitle') }}
|
|
568
597
|
</p>
|
|
569
598
|
<p class="text-muted text-base">{{ successText }}</p>
|
|
570
599
|
<Button variant="outline" type="button" class="mx-auto" @click="onSendAnother">{{
|