@40rty/ams-cli 0.5.6 → 0.5.8
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/bin/commands/dev.mjs +4 -1
- package/bin/commands/publish.mjs +12 -9
- package/bin/commands/theme.mjs +126 -0
- package/bin/forty.mjs +3 -0
- package/bin/lib/__tests__/theme-feeds.test.mjs +42 -0
- package/bin/lib/__tests__/theme-normalize.test.mjs +77 -0
- package/bin/lib/build.mjs +4 -3
- package/bin/lib/host-imports.mjs +136 -0
- package/bin/lib/theme/feeds.mjs +178 -0
- package/bin/lib/theme/normalize.mjs +224 -0
- package/dist/wire.js +398 -344
- package/package.json +2 -1
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// The store's PUBLIC feeds a product page draws on beyond its theme: the
|
|
2
|
+
// catalog, the rendered pages (a theme's videos resolve only there), and the
|
|
3
|
+
// apps a store places through the theme — Vimotia reels, Yotpo photo walls,
|
|
4
|
+
// StarApps sibling groups, Labeler photo badges. Each is read only when the
|
|
5
|
+
// store uses that app; every one is what the store's own widget reads.
|
|
6
|
+
|
|
7
|
+
/** One product page's HTML, fetched once per run — waiting out a 429 (Retry-After, else a doubling backoff). */
|
|
8
|
+
export function pageReader(storeUrl) {
|
|
9
|
+
const cache = new Map();
|
|
10
|
+
return async function storePage(handle) {
|
|
11
|
+
const cached = cache.get(handle);
|
|
12
|
+
if (cached !== undefined) return cached;
|
|
13
|
+
for (let attempt = 0, wait = 2000; attempt < 8; attempt++, wait *= 2) {
|
|
14
|
+
const res = await fetch(`${storeUrl}/products/${handle}`);
|
|
15
|
+
if (res.ok) {
|
|
16
|
+
const html = await res.text();
|
|
17
|
+
cache.set(handle, html);
|
|
18
|
+
return html;
|
|
19
|
+
}
|
|
20
|
+
if (res.status !== 429) throw new Error(`product page ${handle}: ${res.status}`);
|
|
21
|
+
const after = Number(res.headers.get('retry-after'));
|
|
22
|
+
await new Promise((r) => setTimeout(r, Number.isFinite(after) && after > 0 ? after * 1000 : wait));
|
|
23
|
+
}
|
|
24
|
+
throw new Error(`product page ${handle}: still rate-limited after retries`);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The store's primary address (`https://www.<brand>.com`) — what its CDN paths and product pages live under. */
|
|
29
|
+
export async function storeUrlOf(domain) {
|
|
30
|
+
const res = await fetch(`https://${domain}/meta.json`);
|
|
31
|
+
if (!res.ok) throw new Error(`${domain}/meta.json: ${res.status}`);
|
|
32
|
+
const meta = await res.json();
|
|
33
|
+
return String(meta.url).replace(/\/+$/, '');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The public catalog (every listed product, with variants and images). */
|
|
37
|
+
export async function catalogOf(storeUrl) {
|
|
38
|
+
const out = [];
|
|
39
|
+
for (let page = 1; ; page++) {
|
|
40
|
+
const res = await fetch(`${storeUrl}/products.json?limit=250&page=${page}`);
|
|
41
|
+
if (!res.ok) throw new Error(`products.json: ${res.status}`);
|
|
42
|
+
const { products } = await res.json();
|
|
43
|
+
out.push(...products);
|
|
44
|
+
if (products.length < 250) return out;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The mp4s each section of one rendered product page plays, by section key. */
|
|
49
|
+
export function renderedVideos(html) {
|
|
50
|
+
const videos = new Map();
|
|
51
|
+
for (const part of html.split(/id="shopify-section-template--\d+__/).slice(1)) {
|
|
52
|
+
const key = part.slice(0, part.indexOf('"'));
|
|
53
|
+
const srcs = [...part.matchAll(/(?:src|data-src)="((?:https:)?\/\/[^"]+?\.mp4[^"]*)"/g)].map((m) => (m[1].startsWith('//') ? `https:${m[1]}` : m[1]));
|
|
54
|
+
if (srcs.length > 0) videos.set(key, [...new Set(srcs)]);
|
|
55
|
+
}
|
|
56
|
+
return videos;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The JSON object that opens at `start`, by brace depth (strings respected). */
|
|
60
|
+
export function balancedObject(text, start) {
|
|
61
|
+
let depth = 0;
|
|
62
|
+
let inString = false;
|
|
63
|
+
for (let i = start; i < text.length; i++) {
|
|
64
|
+
const c = text[i];
|
|
65
|
+
if (inString) {
|
|
66
|
+
if (c === '\\') i++;
|
|
67
|
+
else if (c === '"') inString = false;
|
|
68
|
+
} else if (c === '"') inString = true;
|
|
69
|
+
else if (c === '{') depth++;
|
|
70
|
+
else if (c === '}' && --depth === 0) return text.slice(start, i + 1);
|
|
71
|
+
}
|
|
72
|
+
throw new Error('unterminated JSON object in the product page');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Does this page carry StarApps' sibling data (the app is installed)? */
|
|
76
|
+
export function hasStarApps(html) {
|
|
77
|
+
return html.includes('"collection_group_data":');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The product's StarApps sibling group — the "Variation" tiles above its
|
|
82
|
+
* buttons linking products sold as one line. Tile images and titles come from
|
|
83
|
+
* the public catalog; a tile's own label when the store names one.
|
|
84
|
+
*/
|
|
85
|
+
export function starappsGroup(html, productId, catalog) {
|
|
86
|
+
const at = html.indexOf('"collection_group_data":');
|
|
87
|
+
if (at === -1) throw new Error(`StarApps data missing from product ${productId}'s page`);
|
|
88
|
+
const data = JSON.parse(balancedObject(html, html.indexOf('{', at)));
|
|
89
|
+
// Keyed by handle, which can differ from the one asked for (a renamed product redirects): match the id.
|
|
90
|
+
const entry = Object.values(data).find((e) => String(e.id) === String(productId));
|
|
91
|
+
if (entry === undefined || entry.groups.length === 0) return undefined;
|
|
92
|
+
const group = entry.groups[0];
|
|
93
|
+
const category = JSON.parse(group.default_category_details)[0];
|
|
94
|
+
const tiles = [...group.products]
|
|
95
|
+
.sort((a, b) => a.product_position - b.product_position)
|
|
96
|
+
.flatMap((member) => {
|
|
97
|
+
const product = catalog.get(member.id);
|
|
98
|
+
if (product === undefined || member.published !== true) return [];
|
|
99
|
+
const named = member.product_display_name !== null && typeof member.product_display_name.en === 'string' ? member.product_display_name.en.trim() : '';
|
|
100
|
+
return [{ id: `gid://shopify/Product/${member.id}`, title: named.length > 0 ? named : product.title, image: product.images.length > 0 ? product.images[0].src : undefined }];
|
|
101
|
+
});
|
|
102
|
+
return tiles.length > 1 ? { label: category === undefined ? '' : category.category_name, tiles } : undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Does this page load Vimotia (its gallery app is installed)? */
|
|
106
|
+
export function hasVimotia(html) {
|
|
107
|
+
return html.includes('vimotia');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The product's Vimotia galleries — clips, columns, and where Vimotia places
|
|
112
|
+
* each: its own app block, or beside the n-th section of the page's main element.
|
|
113
|
+
*/
|
|
114
|
+
export async function vimotiaReels(domain, productId, sectionKeys) {
|
|
115
|
+
const key = Buffer.from(`${domain}+product+${productId}+true`).toString('base64');
|
|
116
|
+
const res = await fetch(`https://vimotia.hextom.com/g/${key}`);
|
|
117
|
+
if (!res.ok) throw new Error(`vimotia ${productId}: ${res.status}`);
|
|
118
|
+
return (await res.json()).current_payload.gallery_list.flatMap((gallery) => {
|
|
119
|
+
const clips = gallery.player_list.flatMap((entry) => {
|
|
120
|
+
const host = entry.linkedhosts[0];
|
|
121
|
+
if (host === undefined) return [];
|
|
122
|
+
return [{ cover: `https://cdn5.hextom.com/${entry.video.cover_image_url}`, src: host.cdn_url, label: 'Parent video' }];
|
|
123
|
+
});
|
|
124
|
+
if (clips.length === 0) return [];
|
|
125
|
+
const columns = { desktop: gallery.column_number, phone: gallery.column_number_m };
|
|
126
|
+
if (gallery.position_xpath === '#vimotia_gallery_app_block') return [{ placement: { kind: 'block' }, columns, clips }];
|
|
127
|
+
const m = /contains\(@class,'anchor'\)\]&&(\d+)&&(\d)$/.exec(gallery.position_xpath);
|
|
128
|
+
if (m === null) throw new Error(`vimotia gallery ${gallery.id}: unrecognised placement ${gallery.position_xpath}`);
|
|
129
|
+
const anchor = sectionKeys[Number(m[1]) - 1 + (m[2] === '1' ? 0 : -1)];
|
|
130
|
+
if (anchor === undefined) throw new Error(`vimotia gallery ${gallery.id}: no section ${m[1]} to sit beside`);
|
|
131
|
+
return [{ placement: { kind: 'after', key: anchor }, columns, clips }];
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The store's public Yotpo key, as every page embeds it for Yotpo's widgets. */
|
|
136
|
+
export function yotpoAppKey(html) {
|
|
137
|
+
const m = /yotpo_app_key\s*=\s*"([A-Za-z0-9]+)"|cdn-widgetsrepository\.yotpo\.com\/v1\/loader\/([A-Za-z0-9]{30,})/.exec(html);
|
|
138
|
+
return m === null ? undefined : m[1] !== undefined ? m[1] : m[2];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A Yotpo photo wall's first page — the call the store's `yotpo-pictures-widget` makes. */
|
|
142
|
+
export async function yotpoGallery(appKey, galleryId, pageSize = 9) {
|
|
143
|
+
const methods = [{
|
|
144
|
+
method: 'GenericGallery',
|
|
145
|
+
params: {
|
|
146
|
+
gid: galleryId, page: 1, max_images_in_view: pageSize, element_width: 1360, product_id: null, demo: false,
|
|
147
|
+
div_settings: { rounded_corners: '0', load_lightbox_sync: false, is_preview: false, is_css_editor: false },
|
|
148
|
+
layout_size: 'large', index: 1, element_id: '1',
|
|
149
|
+
},
|
|
150
|
+
}];
|
|
151
|
+
const res = await fetch(`https://staticw2.yotpo.com/batch/app_key/${appKey}/domain_key/yotpononproductrelatedwidget/widget/GenericGallery`, {
|
|
152
|
+
method: 'POST',
|
|
153
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
154
|
+
body: new URLSearchParams({ methods: JSON.stringify(methods), app_key: appKey, is_mobile: 'false' }),
|
|
155
|
+
});
|
|
156
|
+
if (!res.ok) throw new Error(`yotpo gallery ${galleryId}: ${res.status}`);
|
|
157
|
+
const body = await res.json();
|
|
158
|
+
if (body.length === 0) throw new Error(`yotpo gallery ${galleryId}: empty response`);
|
|
159
|
+
// `data-src` is the grid's own size (`medium`); the original is the lightbox's.
|
|
160
|
+
return [...new Set([...body[0].result.matchAll(/<img class="yotpo-image[^"]*"[^>]*?data-src="([^"]+)"/g)].map((m) => `https:${m[1]}`))];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The Labeler app's photo badges per product (percent positions); empty when the store has no Labeler feed. */
|
|
164
|
+
export async function labelerBadges(domain) {
|
|
165
|
+
const res = await fetch(`https://cdn.labeler.apps.acowebs.com/V3/${domain}/${domain}.json`);
|
|
166
|
+
if (res.status === 403 || res.status === 404) return {};
|
|
167
|
+
if (!res.ok) throw new Error(`labeler feed: ${res.status}`);
|
|
168
|
+
const byProduct = {};
|
|
169
|
+
const offset = (v) => (typeof v === 'number' ? v : undefined);
|
|
170
|
+
for (const badge of (await res.json()).badges) {
|
|
171
|
+
if (badge.isActivated !== true || !badge.selectedPages.includes('products')) continue;
|
|
172
|
+
if (!badge.rules.selected.includes('specificProducts')) continue;
|
|
173
|
+
const m = badge.style.margin;
|
|
174
|
+
const sticker = { image: `https://cdn.labeler.apps.acowebs.com/${badge.imageUrl}`, width: badge.style.width, top: offset(m.top), left: offset(m.left), right: offset(m.right), bottom: offset(m.bottom) };
|
|
175
|
+
for (const id of badge.rules.specificProducts) (byProduct[id] = byProduct[id] || []).push(sticker);
|
|
176
|
+
}
|
|
177
|
+
return byProduct;
|
|
178
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// A Shopify theme's product template → the spacefront PDP's sections.
|
|
2
|
+
//
|
|
3
|
+
// The section TYPES here are the theme's own (Impact: main-product,
|
|
4
|
+
// image-with-text, multi-column, …) plus the app blocks a store places through
|
|
5
|
+
// it (Yotpo reviews and photo walls, Vimotia reels, Shogun pages). What only
|
|
6
|
+
// one store has — its Shogun designs, its AI-generated theme blocks, its custom
|
|
7
|
+
// liquid in the buy box — comes from the brand's own designs module, named in
|
|
8
|
+
// forty.config.json. Anything neither knows FAILS the sync: a new section is
|
|
9
|
+
// named once, never dropped silently or injected as raw HTML.
|
|
10
|
+
|
|
11
|
+
/** Theme JSON may open with a comment Shopify writes; JSON.parse cannot read it. */
|
|
12
|
+
export function parseThemeJson(text) {
|
|
13
|
+
return JSON.parse(text.replace(/^\s*\/\*[\s\S]*?\*\//, ''));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The value helpers every builder — ours and a brand's designs — shares, bound to the store's own domain. */
|
|
17
|
+
export function themeHelpers(storeUrl) {
|
|
18
|
+
/** `shopify://shop_images/<file>` → the store CDN; '' stays absent. */
|
|
19
|
+
const image = (ref) => {
|
|
20
|
+
if (typeof ref !== 'string' || ref.length === 0) return undefined;
|
|
21
|
+
const m = /^shopify:\/\/shop_images\/(.+)$/.exec(ref);
|
|
22
|
+
if (m === null) throw new Error(`unrecognised image reference: ${ref}`);
|
|
23
|
+
return `${storeUrl}/cdn/shop/files/${encodeURIComponent(m[1])}`;
|
|
24
|
+
};
|
|
25
|
+
/** A theme colour of `rgba(0,0,0,0)` means "inherit the scheme" — absent. */
|
|
26
|
+
const color = (value) => (typeof value === 'string' && value.length > 0 && !/^rgba\(0,\s*0,\s*0,\s*0\)$/.test(value) ? value : undefined);
|
|
27
|
+
const text = (value) => (typeof value === 'string' && value.trim().length > 0 ? value : undefined);
|
|
28
|
+
/** Numbered settings (`icon_1`, `title_1`, …) as one row per index that has any value. */
|
|
29
|
+
const numbered = (settings, fields) => {
|
|
30
|
+
const rows = [];
|
|
31
|
+
for (let i = 1; i <= 12; i++) {
|
|
32
|
+
const row = {};
|
|
33
|
+
for (const [key, kind] of Object.entries(fields)) {
|
|
34
|
+
const v = kind === 'image' ? image(settings[`${key}_${i}`]) : text(settings[`${key}_${i}`]);
|
|
35
|
+
if (v !== undefined) row[key] = v;
|
|
36
|
+
}
|
|
37
|
+
if (Object.keys(row).length > 0) rows.push(row);
|
|
38
|
+
}
|
|
39
|
+
return rows;
|
|
40
|
+
};
|
|
41
|
+
return { image, color, text, numbered };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Theme JSON omits `settings` / `block_order` when they are empty. */
|
|
45
|
+
export function settingsOf(node) {
|
|
46
|
+
return node.settings === undefined ? {} : node.settings;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function visibleBlocks(section) {
|
|
50
|
+
if (section.block_order === undefined) return [];
|
|
51
|
+
return section.block_order.map((id) => section.blocks[id]).filter((b) => b !== undefined && b.disabled !== true);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A Shogun section's content in reading order. Shogun writes static markup
|
|
56
|
+
* with a desktop and a mobile copy of the same images and words, so each is
|
|
57
|
+
* kept once, at its first appearance.
|
|
58
|
+
*/
|
|
59
|
+
export function shogunContent(liquid) {
|
|
60
|
+
const markup = liquid.split('{% schema %}')[0].replace(/<(script|style)[\s\S]*?<\/\1>/g, '');
|
|
61
|
+
// One image is one Shogun asset id, whatever transform each copy asks the CDN for.
|
|
62
|
+
const byAsset = new Map();
|
|
63
|
+
for (const m of markup.matchAll(/<img[^>]*?src="https:\/\/i\.shgcdn\.com\/([0-9a-f-]{36})\//g)) {
|
|
64
|
+
if (!byAsset.has(m[1])) byAsset.set(m[1], `https://i.shgcdn.com/${m[1]}/-/format/auto/-/preview/3000x3000/-/quality/best/`);
|
|
65
|
+
}
|
|
66
|
+
const decode = (t) => t.replace(/&/g, '&').replace(/'|’/g, '’').replace(/"/g, '"').replace(/ /g, ' ');
|
|
67
|
+
const texts = [...new Set([...markup.matchAll(/>([^<>{}]+)</g)].map((m) => decode(m[1]).replace(/\s+/g, ' ').trim()).filter((t) => t.length > 1))];
|
|
68
|
+
return { images: [...byAsset.values()], texts };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A main-product `liquid` block: nothing when it is all style and script, else the brand's named design. */
|
|
72
|
+
function liquidBlock(liquid, designs, h) {
|
|
73
|
+
const visible = liquid.replace(/<(style|script)[\s\S]*?<\/\1>/g, '').replace(/<!--[\s\S]*?-->/g, '').trim();
|
|
74
|
+
if (visible.length === 0) return [];
|
|
75
|
+
const design = designs.liquid.find((d) => d.match(visible));
|
|
76
|
+
if (design === undefined) throw new Error(`main-product liquid block has no design — name it in the theme designs: ${visible.slice(0, 120)}`);
|
|
77
|
+
return design.build(visible, h);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function mainProduct(blocks, ctx) {
|
|
81
|
+
return {
|
|
82
|
+
kind: 'main',
|
|
83
|
+
blocks: blocks.flatMap((b) => {
|
|
84
|
+
const bs = settingsOf(b);
|
|
85
|
+
if (b.type === 'image') return [{ kind: 'image', src: ctx.h.image(bs.image), width: bs.image_width }];
|
|
86
|
+
if (b.type === 'text') return ctx.h.text(bs.text) ? [{ kind: 'text', html: bs.text }] : [];
|
|
87
|
+
if (b.type === 'buy_buttons') return [{ kind: 'buy' }];
|
|
88
|
+
if (b.type === 'variant_picker') return [{ kind: 'variants' }];
|
|
89
|
+
if (b.type === 'liquid') return liquidBlock(typeof bs.liquid === 'string' ? bs.liquid : '', ctx.designs, ctx.h);
|
|
90
|
+
throw new Error(`main-product block ${b.type} has no renderer`);
|
|
91
|
+
}),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function shopTheLook(set, blocks, ctx) {
|
|
96
|
+
const { image, text } = ctx.h;
|
|
97
|
+
return {
|
|
98
|
+
kind: 'shop-the-look',
|
|
99
|
+
title: text(set.label),
|
|
100
|
+
looks: blocks.map((b) => {
|
|
101
|
+
const bs = settingsOf(b);
|
|
102
|
+
const spots = [];
|
|
103
|
+
for (let i = 1; i <= 6; i++) {
|
|
104
|
+
const handle = bs[`product_${i}`];
|
|
105
|
+
if (typeof handle !== 'string' || handle.length === 0) continue;
|
|
106
|
+
const product = ctx.catalogByHandle.get(handle);
|
|
107
|
+
if (product === undefined) throw new Error(`shop-the-look: no product ${handle} in the catalog`);
|
|
108
|
+
spots.push({
|
|
109
|
+
id: `gid://shopify/Product/${product.id}`,
|
|
110
|
+
title: product.title,
|
|
111
|
+
image: product.images.length > 0 ? product.images[0].src : undefined,
|
|
112
|
+
// The card prices a multi-variant product "From" its lowest price and offers Quick view;
|
|
113
|
+
// a single-variant one adds straight to the cart.
|
|
114
|
+
price: product.variants.map((v) => v.price).sort((a, c) => Number(a) - Number(c))[0],
|
|
115
|
+
variants: product.variants.length,
|
|
116
|
+
variantId: `gid://shopify/ProductVariant/${product.variants[0].id}`,
|
|
117
|
+
x: bs[`product_${i}_horizontal_position`],
|
|
118
|
+
y: bs[`product_${i}_vertical_position`],
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
return { image: image(bs.image), spots };
|
|
122
|
+
}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Shogun's runtime scaffolding — no visible content of its own. */
|
|
127
|
+
const SHOGUN_SCAFFOLDING = new Set(['shogun-helper', 'shogun-above', 'shogun-below', 'shogun-optimizer']);
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* One theme section → the PDP's section, an array of them (a theme-blocks
|
|
131
|
+
* section stacks one band per block), or null when a visitor sees nothing.
|
|
132
|
+
* `ctx`: { h, designs, videos (this section's mp4s), shogun (Map of theme files), catalogByHandle }.
|
|
133
|
+
*/
|
|
134
|
+
export function section(s, ctx) {
|
|
135
|
+
const { image, color, text } = ctx.h;
|
|
136
|
+
const set = settingsOf(s);
|
|
137
|
+
const blocks = visibleBlocks(s);
|
|
138
|
+
switch (s.type) {
|
|
139
|
+
case 'main-product':
|
|
140
|
+
return mainProduct(blocks, ctx);
|
|
141
|
+
case 'product-content':
|
|
142
|
+
// `description` is the product's own description, under the theme's label;
|
|
143
|
+
// `complementary_products` is a carousel the spacefront's agent answers instead.
|
|
144
|
+
return {
|
|
145
|
+
kind: 'tabs',
|
|
146
|
+
tabs: blocks.flatMap((b) => {
|
|
147
|
+
if (b.type === 'description') return [{ kind: 'description', title: 'Description' }];
|
|
148
|
+
if (b.type === 'content') return [{ kind: 'content', title: b.settings.title, html: typeof b.settings.content === 'string' ? b.settings.content : '' }];
|
|
149
|
+
return [];
|
|
150
|
+
}),
|
|
151
|
+
};
|
|
152
|
+
case 'image-with-text':
|
|
153
|
+
return {
|
|
154
|
+
kind: 'image-with-text',
|
|
155
|
+
imageSide: set.image_position === 'right' ? 'right' : 'left',
|
|
156
|
+
accent: color(set.accent_background),
|
|
157
|
+
items: blocks.map((b) => ({ image: image(b.settings.image), subheading: text(b.settings.subheading), title: text(b.settings.title), html: text(b.settings.content) })),
|
|
158
|
+
};
|
|
159
|
+
case 'multi-column':
|
|
160
|
+
return {
|
|
161
|
+
kind: 'multi-column',
|
|
162
|
+
// The theme's columns per desktop row for each item size.
|
|
163
|
+
perRow: { pico: 8, small: 7, medium: 5, large: 3 }[set.desktop_item_size],
|
|
164
|
+
title: text(set.title),
|
|
165
|
+
html: text(set.content),
|
|
166
|
+
items: blocks.map((b) => ({ image: image(b.settings.image), width: b.settings.image_width, title: text(b.settings.title), html: text(b.settings.content) })),
|
|
167
|
+
};
|
|
168
|
+
case 'rich-text':
|
|
169
|
+
return { kind: 'rich-text', subheading: text(set.subheading), title: text(set.title), html: text(set.content), background: color(set.background) };
|
|
170
|
+
case 'timeline':
|
|
171
|
+
return { kind: 'timeline', title: text(set.title), items: blocks.map((b) => ({ image: image(b.settings.image), label: text(b.settings.label), title: text(b.settings.title), html: text(b.settings.content) })) };
|
|
172
|
+
case 'faq':
|
|
173
|
+
return { kind: 'faq', title: text(set.title), background: color(set.background), items: blocks.map((b) => ({ question: b.settings.title, html: b.settings.answer })) };
|
|
174
|
+
case 'press':
|
|
175
|
+
return { kind: 'press', items: blocks.map((b) => ({ logo: image(b.settings.logo), quote: b.settings.content })) };
|
|
176
|
+
case 'gallery':
|
|
177
|
+
return { kind: 'gallery', title: text(set.title), items: blocks.map((b) => ({ image: image(b.settings.image), html: text(b.settings.caption) })) };
|
|
178
|
+
case 'slideshow':
|
|
179
|
+
return { kind: 'slideshow', slides: blocks.map((b) => ({ image: image(b.settings.image), mobileImage: image(b.settings.mobile_image) })) };
|
|
180
|
+
case 'video':
|
|
181
|
+
return ctx.videos[0] === undefined ? null : { kind: 'video', src: ctx.videos[0], poster: image(set.image) };
|
|
182
|
+
case 'apps':
|
|
183
|
+
// Yotpo's reviews block is the store's review surface; Vimotia's gallery
|
|
184
|
+
// block is its video reel, whose clips are per product (`reels`).
|
|
185
|
+
if (blocks.some((b) => b.type.includes('/yotpo-product-reviews/'))) return { kind: 'reviews' };
|
|
186
|
+
if (blocks.some((b) => b.type.includes('/vimotia-shoppable-videos-ugc/'))) return { kind: 'video-stories' };
|
|
187
|
+
return null;
|
|
188
|
+
case 'custom-liquid': {
|
|
189
|
+
const m = /yotpo-pictures-widget"[\s\S]*?data-gallery-id="([^"]+)"/.exec(typeof set.liquid === 'string' ? set.liquid : '');
|
|
190
|
+
return m === null ? null : { kind: 'ugc-gallery', galleryId: m[1] };
|
|
191
|
+
}
|
|
192
|
+
case '_blocks':
|
|
193
|
+
return blocks.map((b) => {
|
|
194
|
+
const build = ctx.designs.blocks[b.type];
|
|
195
|
+
if (build === undefined) throw new Error(`theme block ${b.type} has no design — name it in the theme designs`);
|
|
196
|
+
return build(settingsOf(b), ctx.h, ctx.videos);
|
|
197
|
+
});
|
|
198
|
+
case 'shop-the-look':
|
|
199
|
+
return shopTheLook(set, blocks, ctx);
|
|
200
|
+
default:
|
|
201
|
+
if (s.type.startsWith('shogun-custom-')) {
|
|
202
|
+
const id = s.type.slice('shogun-custom-'.length);
|
|
203
|
+
const design = ctx.designs.shogun[id];
|
|
204
|
+
const liquid = ctx.shogun.get(`sections/${s.type}.liquid`);
|
|
205
|
+
if (design === undefined) throw new Error(`shogun design ${id} has no design — name it in the theme designs`);
|
|
206
|
+
if (liquid === undefined) throw new Error(`shogun section ${id} is not in the theme`);
|
|
207
|
+
return design(shogunContent(liquid), ctx.h);
|
|
208
|
+
}
|
|
209
|
+
if (SHOGUN_SCAFFOLDING.has(s.type)) return null;
|
|
210
|
+
throw new Error(`section type ${s.type} has no renderer`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** A template's enabled sections, keyed; a multi-band section keys each band by position (the first keeps the section's key). */
|
|
215
|
+
export function templateSections(template, ctxFor) {
|
|
216
|
+
const enabled = template.order.filter((key) => template.sections[key].disabled !== true);
|
|
217
|
+
const sections = enabled.flatMap((key) => {
|
|
218
|
+
const built = section(template.sections[key], ctxFor(key));
|
|
219
|
+
if (built === null) return [];
|
|
220
|
+
if (Array.isArray(built)) return built.map((b, i) => ({ key: i === 0 ? key : `${key}#${i}`, ...b }));
|
|
221
|
+
return [{ key, ...built }];
|
|
222
|
+
});
|
|
223
|
+
return { enabled, sections };
|
|
224
|
+
}
|