@@ -40,6 +40,13 @@ export class HTMLTemplate {
40
40
*/
41
41
static priceRange = ( min , max ) => ( min !== max ? ` (${ min } - ${ max } )` : '' ) ;
42
42
43
+ /**
44
+ * @param {string } str
45
+ * @param {number } spaces
46
+ * @returns {string }
47
+ */
48
+ static indent = ( str , spaces ) => str . split ( '\n' ) . map ( ( line ) => `${ ' ' . repeat ( spaces ) } ${ line } ` ) . join ( '\n' ) ;
49
+
43
50
/** @type {Context } */
44
51
ctx = undefined ;
45
52
@@ -156,12 +163,12 @@ ${HTMLTemplate.metaProperty('product:price.currency', product.prices.final.curre
156
163
renderHead ( ) {
157
164
return /* html */ `\
158
165
<head>
159
- ${ this . renderDocumentMetaTags ( ) }
160
- ${ this . renderOpenGraphMetaTags ( ) }
161
- ${ this . renderTwitterMetaTags ( ) }
162
- ${ this . renderCommerceMetaTags ( ) }
163
- ${ this . renderHelixDependencies ( ) }
164
- ${ this . renderJSONLD ( ) }
166
+ ${ HTMLTemplate . indent ( this . renderDocumentMetaTags ( ) , 2 ) }
167
+ ${ HTMLTemplate . indent ( this . renderOpenGraphMetaTags ( ) , 2 ) }
168
+ ${ HTMLTemplate . indent ( this . renderTwitterMetaTags ( ) , 2 ) }
169
+ ${ HTMLTemplate . indent ( this . renderCommerceMetaTags ( ) , 2 ) }
170
+ ${ HTMLTemplate . indent ( this . renderHelixDependencies ( ) , 2 ) }
171
+ ${ HTMLTemplate . indent ( this . renderJSONLD ( ) , 2 ) }
165
172
</head>` ;
166
173
}
167
174
@@ -172,19 +179,19 @@ ${HTMLTemplate.metaProperty('product:price.currency', product.prices.final.curre
172
179
*/
173
180
renderProductImages ( images ) {
174
181
return /* html */ `\
175
- <div class="product-images">
176
- <div>
177
- ${ images . map ( ( img ) => /* html */ `\
178
- <div>
179
- <picture>
180
- <source type="image/webp" srcset="${ img . url } " alt="" media="(min-width: 600px)">
181
- <source type="image/webp" srcset="${ img . url } ">
182
- <source type="image/png" srcset="${ img . url } " media="(min-width: 600px)">
183
- <img loading="lazy" alt="${ img . label } " src="${ img . url } ">
184
- </picture>
185
- </div>` ) . join ( '\n' ) }
186
- </div>
187
- </div>`;
182
+ <div class="product-images">
183
+ <div>
184
+ ${ images . map ( ( img ) => /* html */ `\
185
+ <div>
186
+ <picture>
187
+ <source type="image/webp" srcset="${ img . url } " alt="" media="(min-width: 600px)">
188
+ <source type="image/webp" srcset="${ img . url } ">
189
+ <source type="image/png" srcset="${ img . url } " media="(min-width: 600px)">
190
+ <img loading="lazy" alt="${ img . label } " src="${ img . url } ">
191
+ </picture>
192
+ </div>` ) . join ( '\n' ) }
193
+ </div>
194
+ </div>` ;
188
195
}
189
196
190
197
/**
@@ -195,12 +202,12 @@ ${HTMLTemplate.metaProperty('product:price.currency', product.prices.final.curre
195
202
renderProductAttributes ( attributes ) {
196
203
return /* html */ `\
197
204
<div class="product-attributes">
198
- ${ attributes . map ( ( attr ) => /* html */ `\
199
- <div>
200
- <div>${ attr . name } </div>
201
- <div>${ attr . label } </div>
202
- <div>${ attr . value } </div>
203
- </div>` ) . join ( '\n' ) }
205
+ ${ attributes . map ( ( attr ) => /* html */ `\
206
+ <div>
207
+ <div>${ attr . name } </div>
208
+ <div>${ attr . label } </div>
209
+ <div>${ attr . value } </div>
210
+ </div>` ) . join ( '\n' ) }
204
211
</div>` ;
205
212
}
206
213
@@ -211,14 +218,14 @@ ${HTMLTemplate.metaProperty('product:price.currency', product.prices.final.curre
211
218
*/
212
219
renderProductItems ( items ) {
213
220
return items . map ( ( item ) => /* html */ `\
214
- <div>
215
- <div>option</div>
216
- <div>${ item . id } </div>
217
- <div>${ item . label } </div>
218
- <div>${ item . value ?? '' } </div>
219
- <div>${ item . selected ? 'selected' : '' } </div>
220
- <div>${ item . inStock ? 'inStock' : '' } </div>
221
- </div>`) . join ( '\n' ) ;
221
+ <div>
222
+ <div>option</div>
223
+ <div>${ item . id } </div>
224
+ <div>${ item . label } </div>
225
+ <div>${ item . value ?? '' } </div>
226
+ <div>${ item . selected ? 'selected' : '' } </div>
227
+ <div>${ item . inStock ? 'inStock' : '' } </div>
228
+ </div>` ) . join ( '\n' ) ;
222
229
}
223
230
224
231
/**
@@ -228,18 +235,18 @@ ${HTMLTemplate.metaProperty('product:price.currency', product.prices.final.curre
228
235
*/
229
236
renderProductOptions ( options ) {
230
237
return /* html */ `\
231
- <div class="product-options">
232
- ${ options . map ( ( opt ) => /* html */ `\
233
- <div>
234
- <div>${ opt . id } </div>
235
- <div>${ opt . label } </div>
236
- <div>${ opt . typename } </div>
237
- <div>${ opt . type ?? '' } </div>
238
- <div>${ opt . multiple ? 'multiple' : '' } </div>
239
- <div>${ opt . required === true ? 'required' : '' } </div>
240
- </div>
241
- ${ this . renderProductItems ( opt . items ) } `) . join ( '\n' ) }
242
- </div>`;
238
+ <div class="product-options">
239
+ ${ options . map ( ( opt ) => /* html */ `\
240
+ <div>
241
+ <div>${ opt . id } </div>
242
+ <div>${ opt . label } </div>
243
+ <div>${ opt . typename } </div>
244
+ <div>${ opt . type ?? '' } </div>
245
+ <div>${ opt . multiple ? 'multiple' : '' } </div>
246
+ <div>${ opt . required === true ? 'required' : '' } </div>
247
+ </div>
248
+ ${ HTMLTemplate . indent ( this . renderProductItems ( opt . items ) , 2 ) } `) . join ( '\n' ) }
249
+ </div>` ;
243
250
}
244
251
245
252
/**
@@ -249,12 +256,12 @@ ${HTMLTemplate.metaProperty('product:price.currency', product.prices.final.curre
249
256
*/
250
257
renderVariantImages ( images ) {
251
258
return images . map ( ( img ) => /* html */ `\
252
- <picture>
253
- <source type="image/webp" srcset="${ img . url } " alt="" media="(min-width: 600px)">
254
- <source type="image/webp" srcset="${ img . url } ">
255
- <source type="image/png" srcset="${ img . url } " media="(min-width: 600px)">
256
- <img loading="lazy" alt="${ img . label } " src="${ img . url } ">
257
- </picture>`) . join ( '\n' ) ;
259
+ <picture>
260
+ <source type="image/webp" srcset="${ img . url } " alt="" media="(min-width: 600px)">
261
+ <source type="image/webp" srcset="${ img . url } ">
262
+ <source type="image/png" srcset="${ img . url } " media="(min-width: 600px)">
263
+ <img loading="lazy" alt="${ img . label } " src="${ img . url } ">
264
+ </picture>` ) . join ( '\n' ) ;
258
265
}
259
266
260
267
/**
@@ -264,8 +271,8 @@ ${HTMLTemplate.metaProperty('product:price.currency', product.prices.final.curre
264
271
*/
265
272
renderVariantPrices ( prices ) {
266
273
return /* html */ `\
267
- <div>Regular: ${ prices . regular . amount } ${ prices . regular . currency } ${ HTMLTemplate . priceRange ( prices . regular . minimumAmount , prices . regular . maximumAmount ) } </div>
268
- <div>Final: ${ prices . final . amount } ${ prices . final . currency } ${ HTMLTemplate . priceRange ( prices . final . minimumAmount , prices . final . maximumAmount ) } </div>`;
274
+ <div>Regular: ${ prices . regular . amount } ${ prices . regular . currency } ${ HTMLTemplate . priceRange ( prices . regular . minimumAmount , prices . regular . maximumAmount ) } </div>
275
+ <div>Final: ${ prices . final . amount } ${ prices . final . currency } ${ HTMLTemplate . priceRange ( prices . final . minimumAmount , prices . final . maximumAmount ) } </div>` ;
269
276
}
270
277
271
278
/**
@@ -278,18 +285,20 @@ ${HTMLTemplate.metaProperty('product:price.currency', product.prices.final.curre
278
285
}
279
286
280
287
return /* html */ `\
281
- <div class="product-variants">
282
- ${ this . variants . map ( ( v ) => /* html */ `\
283
- <div>
284
- <div>${ v . sku } </div>
285
- <div>${ v . name } </div>
286
- <div>${ v . description } </div>
287
- <div>${ v . inStock ? 'inStock' : '' } </div>
288
- ${ this . renderVariantPrices ( v . prices ) }
289
- <div>${ this . renderVariantImages ( v . images ) } </div>
290
- <div>${ ( v . selections ?? [ ] ) . join ( ', ' ) } </div>
291
- </div>` ) . join ( '\n' ) }
292
- </div>` ;
288
+ <div class="product-variants">
289
+ ${ this . variants . map ( ( v ) => /* html */ `\
290
+ <div>
291
+ <div>${ v . sku } </div>
292
+ <div>${ v . name } </div>
293
+ <div>${ v . description } </div>
294
+ <div>${ v . inStock ? 'inStock' : '' } </div>
295
+ ${ HTMLTemplate . indent ( this . renderVariantPrices ( v . prices ) , 4 ) }
296
+ <div>
297
+ ${ HTMLTemplate . indent ( this . renderVariantImages ( v . images ) , 6 ) }
298
+ </div>
299
+ <div>${ ( v . selections ?? [ ] ) . join ( ', ' ) } </div>
300
+ </div>` ) . join ( '\n' ) }
301
+ </div>` ;
293
302
}
294
303
295
304
/**
@@ -302,23 +311,23 @@ ${HTMLTemplate.metaProperty('product:price.currency', product.prices.final.curre
302
311
}
303
312
304
313
return /* html */ `\
305
- <div class="variant-attributes">
306
- ${ this . variants ?. map ( ( v ) => /* html */ `\
314
+ <div class="variant-attributes">
315
+ ${ this . variants ?. map ( ( v ) => /* html */ `\
316
+ <div>
317
+ <div>sku</div>
318
+ <div>${ v . sku } </div>
319
+ <div></div>
320
+ <div></div>
321
+ </div>
322
+ ${ v . attributes ?. map ( ( attribute ) => /* html */ `\
307
323
<div>
308
- <div>sku</div>
309
- <div>${ v . sku } </div>
310
- <div></div>
311
- <div></div>
312
- </div>
313
- ${ v . attributes ?. map ( ( attribute ) => /* html */ `\
314
- <div>
315
- <div>attribute</div>
316
- <div>${ attribute . name } </div>
317
- <div>${ attribute . label } </div>
318
- <div>${ attribute . value } </div>
319
- </div>` ) . join ( '\n' ) } \
320
- ` ) . join ( '\n' ) }
321
- </div>` ;
324
+ <div>attribute</div>
325
+ <div>${ attribute . name } </div>
326
+ <div>${ attribute . label } </div>
327
+ <div>${ attribute . value } </div>
328
+ </div>` ) . join ( '\n' ) } \
329
+ ` ) . join ( '\n' ) }
330
+ </div>` ;
322
331
}
323
332
324
333
/**
@@ -336,18 +345,18 @@ ${HTMLTemplate.metaProperty('product:price.currency', product.prices.final.curre
336
345
return /* html */ `\
337
346
<!DOCTYPE html>
338
347
<html>
339
- ${ this . renderHead ( ) }
348
+ ${ HTMLTemplate . indent ( this . renderHead ( ) , 2 ) }
340
349
<body>
341
350
<header></header>
342
351
<main>
343
352
<div>
344
353
<h1>${ name } </h1>
345
354
${ description ? `<p>${ description } </p>` : '' }
346
- ${ this . renderProductImages ( images ) }
347
- ${ this . renderProductAttributes ( attributes ) }
348
- ${ this . renderProductOptions ( options ) }
349
- ${ this . renderProductVariants ( ) }
350
- ${ this . renderProductVariantsAttributes ( ) }
355
+ ${ HTMLTemplate . indent ( this . renderProductImages ( images ) , 8 ) }
356
+ ${ HTMLTemplate . indent ( this . renderProductAttributes ( attributes ) , 8 ) }
357
+ ${ HTMLTemplate . indent ( this . renderProductOptions ( options ) , 8 ) }
358
+ ${ HTMLTemplate . indent ( this . renderProductVariants ( ) , 8 ) }
359
+ ${ HTMLTemplate . indent ( this . renderProductVariantsAttributes ( ) , 8 ) }
351
360
</div>
352
361
</main>
353
362
<footer></footer>
0 commit comments