basklee-react 0.9.17 → 0.9.19

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/README.md CHANGED
@@ -523,7 +523,7 @@ maintained API client. Page metadata uses those returned records and follows
523
523
  client navigation and language selection. Home-page event clicks open enrollment
524
524
  for the returned event, preserving its return path through sign-in.
525
525
  A product page takes the product's SEO tab: its heading is the page title (the product name without one), its meta description and keywords fill their tags, and its own Open Graph and Twitter Cards tags are added in the page's language only, replacing a default tag of the same name.
526
- Pages with a canonical address also link the same page in the other storefront languages (`hreflang`) and set `og:type` and `og:locale`; a journal article adds its publication date (`article:published_time`) and schema.org `BlogPosting` data. The store icon (`logos.ico`) is the page's `<link rel="icon">`. An unknown `?locale` opens the language closest to the browser's, else the first one. A visit without `?locale` opens in the language and currency the customer picked in the selectors on an earlier visit (the `basklee_locale` and `basklee_currency` cookies, kept for a year; the privacy page and the cookie notice name them). Without a pick it opens in the storefront language closest to the browser's languages (`Accept-Language`; `navigator.languages` in the app build): the same code first, then the same language (`ru-RU` finds `ru`, `en` finds `en-GB`); the first available language when none of them is offered. A link with another `?locale` shows that language without replacing the pick: `setLocale(code, false)` and `setCurrency(id, false)` change the selection without keeping it.
526
+ Pages with a canonical address also link the same page in the other storefront languages (`hreflang`) and set `og:type` and `og:locale`; a journal article adds its publication date (`article:published_time`) and schema.org `BlogPosting` data, with every video of the post (YouTube players and uploaded videos of the text, then the post's own videos) as a `VideoObject`. YouTube players and uploaded videos stay in the server-rendered article, so search engines find them without scripts. A product page's `Product` data lists the product's videos as `VideoObject`s under `subjectOf` (preview, else the first photo, as the picture; the product's last change as the upload date). The store icon (`logos.ico`) is the page's `<link rel="icon">`. An unknown `?locale` opens the language closest to the browser's, else the first one. A visit without `?locale` opens in the language and currency the customer picked in the selectors on an earlier visit (the `basklee_locale` and `basklee_currency` cookies, kept for a year; the privacy page and the cookie notice name them). Without a pick it opens in the storefront language closest to the browser's languages (`Accept-Language`; `navigator.languages` in the app build): the same code first, then the same language (`ru-RU` finds `ru`, `en` finds `en-GB`); the first available language when none of them is offered. A link with another `?locale` shows that language without replacing the pick: `setLocale(code, false)` and `setCurrency(id, false)` change the selection without keeping it.
527
527
 
528
528
  List pages (journal, categories, stores) link their previous and next pages as real links (`?page=2`). Each later page has its own canonical address and a title with its page number; a journal page past the last one answers 404.
529
529
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "basklee-react",
3
- "version": "0.9.17",
3
+ "version": "0.9.19",
4
4
  "description": "Next.js 16 storefront for your Basklee marketplace: setup CLI, server API gateway, typed store and ready-made catalog, cart, checkout and account components in English, Russian and Spanish, plus an Android app build. Powered by Basklee: https://basklee.com",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -29,6 +29,7 @@ import {
29
29
  } from '@/lib/basklee/blogArticle';
30
30
  import {
31
31
  blogArticleBlocks,
32
+ blogYoutubeAllow,
32
33
  removeLeadingDuplicateBlogHeading,
33
34
  safeBlogArticleHtml,
34
35
  type IBlogArticleInline,
@@ -599,6 +600,31 @@ function ArticleBlocks({ blocks }: { blocks: TBlogArticleBlock[] }) {
599
600
  {block.caption ? <figcaption>{block.caption}</figcaption> : null}
600
601
  </figure>
601
602
  );
603
+ case 'youtube':
604
+ return (
605
+ <iframe
606
+ key={index}
607
+ className="blog-post-editor-youtube"
608
+ src={block.src}
609
+ title={block.title}
610
+ allow={blogYoutubeAllow}
611
+ allowFullScreen
612
+ loading="lazy"
613
+ referrerPolicy="strict-origin-when-cross-origin"
614
+ />
615
+ );
616
+ case 'video':
617
+ return (
618
+ <video
619
+ key={index}
620
+ src={block.src}
621
+ poster={block.poster}
622
+ title={block.title || undefined}
623
+ controls
624
+ playsInline
625
+ preload="none"
626
+ />
627
+ );
602
628
  default:
603
629
  return <p key={index}><ArticleInline parts={block.inline} /></p>;
604
630
  }
@@ -467,7 +467,12 @@ export type TBlogArticleBlock =
467
467
  | { kind: 'paragraph'; inline: IBlogArticleInline[] }
468
468
  | { kind: 'list'; ordered: boolean; items: IBlogArticleInline[][] }
469
469
  | { kind: 'quote'; blocks: TBlogArticleBlock[] }
470
- | { kind: 'image'; src: string; alt: string; caption: string; width?: number; height?: number };
470
+ | { kind: 'image'; src: string; alt: string; caption: string; width?: number; height?: number }
471
+ // A YouTube player (the privacy-enhanced address) and an uploaded video:
472
+ // the server render keeps them, so search engines find the video without
473
+ // scripts (Google: a video must be on the page without a click).
474
+ | { kind: 'youtube'; src: string; title: string }
475
+ | { kind: 'video'; src: string; poster?: string; title: string };
471
476
 
472
477
  const MAX_ARTICLE_BLOCKS = 600;
473
478
 
@@ -537,13 +542,146 @@ function blockImageSizeOf(figure: { width?: number; height?: number }) {
537
542
  return figure.width && figure.height ? { width: figure.width, height: figure.height } : {};
538
543
  }
539
544
 
545
+ /** A video of an article text: a YouTube player or an uploaded file. */
546
+ export interface IBlogArticleVideo {
547
+ kind: 'youtube' | 'file';
548
+ /** youtube: the privacy-enhanced player address; file: the file address. */
549
+ src: string;
550
+ /** youtube: the video ID. */
551
+ id?: string;
552
+ title: string;
553
+ poster?: string;
554
+ }
555
+
556
+ const articleMedia = /<(iframe|video)\b([^>]*)>([\s\S]*?)<\/\1\s*>/gi;
557
+
558
+ function articleMediaOf(
559
+ name: string,
560
+ attributes: string,
561
+ inner: string,
562
+ appOrigin: string,
563
+ ): IBlogArticleVideo | undefined {
564
+ const title = tagAttribute(attributes, 'title') || tagAttribute(attributes, 'aria-label');
565
+
566
+ if (name.toLowerCase() === 'iframe') {
567
+ const src = blogYoutubeEmbedSource(tagAttribute(attributes, 'src'));
568
+ const id = /\/embed\/([\w-]{11})/.exec(src)?.[1];
569
+
570
+ return src && id ? { kind: 'youtube', src, id, title } : undefined;
571
+ }
572
+
573
+ const own = tagAttribute(attributes, 'src') ||
574
+ tagAttribute(/<source\b([^>]*)>/i.exec(inner)?.[1] ?? '', 'src');
575
+ const src = blockImage(own, appOrigin);
576
+ const poster = blockImage(tagAttribute(attributes, 'poster'), appOrigin);
577
+
578
+ return src ? { kind: 'file', src, title, ...(poster ? { poster } : {}) } : undefined;
579
+ }
580
+
581
+ /** The YouTube players and uploaded videos of an article text, in order. */
582
+ export function blogArticleVideos(
583
+ value: string | undefined,
584
+ appOrigin = BLOCK_MEDIA_ORIGIN,
585
+ ): IBlogArticleVideo[] {
586
+ const found: IBlogArticleVideo[] = [];
587
+
588
+ for (const match of (value ?? '').replace(/<!--[\s\S]*?-->/g, ' ').matchAll(articleMedia)) {
589
+ const video = articleMediaOf(match[1]!, match[2] ?? '', match[3] ?? '', appOrigin);
590
+
591
+ if (video) {
592
+ found.push(video);
593
+ }
594
+ }
595
+
596
+ return found;
597
+ }
598
+
599
+ /** One video of an article for search engines (pageSeo IStorefrontVideo). */
600
+ export interface IBlogArticleVideoData {
601
+ name: string;
602
+ description?: string;
603
+ thumbnailUrl?: string;
604
+ uploadDate?: string;
605
+ embedUrl?: string;
606
+ contentUrl?: string;
607
+ }
608
+
609
+ /**
610
+ * An article's videos for search engines (pageSeo storefrontVideoObject):
611
+ * the YouTube players and uploaded videos of its text, then the post's own
612
+ * videos. A YouTube video gets YouTube's own picture; an uploaded one its
613
+ * poster, else the article's cover. The article's date stands for the upload
614
+ * date. Each video once.
615
+ */
616
+ export function blogArticleVideoData(input: {
617
+ html: string | undefined;
618
+ /** The post's own videos (post.videos): file and poster addresses. */
619
+ uploaded?: { source?: string; poster?: string }[];
620
+ title: string;
621
+ description?: string;
622
+ cover?: string;
623
+ published?: string;
624
+ /** Makes an address complete (storefrontAbsoluteUrl). */
625
+ absoluteUrl: (value: string | undefined) => string | undefined;
626
+ }): IBlogArticleVideoData[] {
627
+ const seen = new Set<string>();
628
+ const named = (title: string) =>
629
+ title && !/^youtube video$/i.test(title) ? title : input.title;
630
+ const own = (input.uploaded ?? []).map((video): IBlogArticleVideo => ({
631
+ kind: 'file',
632
+ src: video.source ?? '',
633
+ title: '',
634
+ ...(video.poster ? { poster: video.poster } : {}),
635
+ }));
636
+
637
+ return [...blogArticleVideos(input.html, ''), ...own].flatMap((video): IBlogArticleVideoData[] => {
638
+ const key = video.id ?? video.src;
639
+
640
+ if (!key || seen.has(key)) {
641
+ return [];
642
+ }
643
+ seen.add(key);
644
+ if (video.kind === 'youtube') {
645
+ return [{
646
+ name: named(video.title),
647
+ description: input.description,
648
+ thumbnailUrl: `https://i.ytimg.com/vi/${video.id}/hqdefault.jpg`,
649
+ uploadDate: input.published,
650
+ embedUrl: `https://www.youtube.com/embed/${video.id}`,
651
+ }];
652
+ }
653
+ const contentUrl = input.absoluteUrl(video.src);
654
+
655
+ return contentUrl
656
+ ? [{
657
+ name: named(video.title),
658
+ description: input.description,
659
+ thumbnailUrl: input.absoluteUrl(video.poster) || input.cover,
660
+ uploadDate: input.published,
661
+ contentUrl,
662
+ }]
663
+ : [];
664
+ });
665
+ }
666
+
540
667
  /** The article as plain blocks for the server render and the hydration. */
541
668
  export function blogArticleBlocks(
542
669
  value: string | undefined,
543
670
  appOrigin = BLOCK_MEDIA_ORIGIN,
544
671
  ): TBlogArticleBlock[] {
672
+ const media: IBlogArticleVideo[] = [];
545
673
  const html = (value ?? '')
546
674
  .replace(/<!--[\s\S]*?-->/g, ' ')
675
+ // Players and videos become markers before the other media are dropped.
676
+ .replace(articleMedia, (_, name: string, attributes: string, inner: string) => {
677
+ const video = articleMediaOf(name, attributes, inner, appOrigin);
678
+
679
+ if (!video) {
680
+ return ' ';
681
+ }
682
+ media.push(video);
683
+ return `<bkmedia i="${media.length - 1}"></bkmedia>`;
684
+ })
547
685
  .replace(skippedElements, ' ');
548
686
  const blocks: TBlogArticleBlock[] = [];
549
687
  const quotes: TBlogArticleBlock[][] = [];
@@ -686,6 +824,15 @@ export function blogArticleBlocks(
686
824
  flush();
687
825
  push({ kind: 'image', src, alt, caption: '', ...blockImageSize(attributes) });
688
826
  }
827
+ } else if (name === 'bkmedia') {
828
+ const video = media[Number(tagAttribute(attributes, 'i'))];
829
+
830
+ if (video) {
831
+ flush();
832
+ push(video.kind === 'youtube'
833
+ ? { kind: 'youtube', src: video.src, title: video.title || 'YouTube video' }
834
+ : { kind: 'video', src: video.src, title: video.title, ...(video.poster ? { poster: video.poster } : {}) });
835
+ }
689
836
  } else if (name === 'a') {
690
837
  link = blockLink(tagAttribute(attributes, 'href'));
691
838
  } else if (name === 'br' || name === 'td' || name === 'th') {
@@ -39,7 +39,9 @@ import {
39
39
  import {
40
40
  getAvailableNestedValueFromObject,
41
41
  plainText,
42
+ safePublicUrl,
42
43
  } from '@/lib/basklee/presentation';
44
+ import { blogArticleVideoData } from '@/lib/basklee/blogArticleHtml';
43
45
 
44
46
  const privatePages = new Set([
45
47
  'activity',
@@ -333,6 +335,29 @@ export function storefrontMetadata(
333
335
  modified,
334
336
  locale,
335
337
  storeName: init?.contact?.company_name,
338
+ // The players and videos of the text, and the post's own videos.
339
+ videos: blogArticleVideoData({
340
+ html:
341
+ getAvailableNestedValueFromObject(
342
+ records.post.locale,
343
+ locale,
344
+ 'post_view_text',
345
+ ) ||
346
+ getAvailableNestedValueFromObject(
347
+ records.post.locale,
348
+ locale,
349
+ 'post_preview_text',
350
+ ),
351
+ uploaded: (records.post.videos ?? []).map((video) => ({
352
+ source: safePublicUrl(video.video_url),
353
+ poster: safePublicUrl(video.preview_url),
354
+ })),
355
+ title: content.title,
356
+ description: content.description,
357
+ cover: image,
358
+ published,
359
+ absoluteUrl: (value) => storefrontAbsoluteUrl(value, origin),
360
+ }),
336
361
  }),
337
362
  )
338
363
  : '';
@@ -626,9 +626,47 @@ export function storefrontIsoDate(
626
626
  return match ? `${match[3]}-${match[2]}-${match[1]}` : undefined;
627
627
  }
628
628
 
629
+ /** A video of a page, for search engines (blogArticleVideoData gives an article's). */
630
+ export interface IStorefrontVideo {
631
+ name: string;
632
+ description?: string;
633
+ /** Complete address of the video's picture. */
634
+ thumbnailUrl?: string;
635
+ /** ISO 8601 date. */
636
+ uploadDate?: string;
637
+ /** The player address (YouTube), or the file address. */
638
+ embedUrl?: string;
639
+ contentUrl?: string;
640
+ }
641
+
642
+ /**
643
+ * schema.org VideoObject (Google "Video" structured data). Google needs a
644
+ * name, a thumbnail and an upload date, and a player or file address; a video
645
+ * without them gives `undefined`.
646
+ */
647
+ export function storefrontVideoObject(
648
+ video: IStorefrontVideo,
649
+ ): Record<string, unknown> | undefined {
650
+ const name = video.name.trim();
651
+ if (!name || !video.thumbnailUrl || !video.uploadDate || !(video.embedUrl || video.contentUrl)) {
652
+ return undefined;
653
+ }
654
+ return {
655
+ '@type': 'VideoObject',
656
+ name: name.slice(0, 200),
657
+ description: video.description?.trim() || name,
658
+ thumbnailUrl: [video.thumbnailUrl],
659
+ uploadDate: video.uploadDate,
660
+ ...(video.embedUrl ? { embedUrl: video.embedUrl } : {}),
661
+ ...(video.contentUrl ? { contentUrl: video.contentUrl } : {}),
662
+ };
663
+ }
664
+
629
665
  /**
630
666
  * schema.org BlogPosting of a journal article (Google "Article" structured
631
- * data): headline, cover, dates, and the store as author and publisher.
667
+ * data): headline, cover, dates, and the store as author and publisher. The
668
+ * videos of the text (YouTube players and uploaded files) go in as
669
+ * VideoObject, so search engines know what the article shows.
632
670
  */
633
671
  export function storefrontArticleData(input: {
634
672
  canonical: string;
@@ -640,6 +678,7 @@ export function storefrontArticleData(input: {
640
678
  modified?: string;
641
679
  locale: string;
642
680
  storeName?: string;
681
+ videos?: IStorefrontVideo[];
643
682
  }): Record<string, unknown> {
644
683
  const store = {
645
684
  '@type': 'Organization',
@@ -647,6 +686,9 @@ export function storefrontArticleData(input: {
647
686
  url: `${input.origin}/`,
648
687
  };
649
688
  const modified = input.modified || input.published;
689
+ const videos = (input.videos ?? [])
690
+ .map((video) => storefrontVideoObject(video))
691
+ .filter((video): video is Record<string, unknown> => Boolean(video));
650
692
  return {
651
693
  '@context': 'https://schema.org',
652
694
  '@type': 'BlogPosting',
@@ -661,6 +703,7 @@ export function storefrontArticleData(input: {
661
703
  inLanguage: input.locale,
662
704
  author: store,
663
705
  publisher: store,
706
+ ...(videos.length ? { video: videos } : {}),
664
707
  };
665
708
  }
666
709
 
@@ -35,6 +35,9 @@ export interface StorefrontStructuredProduct {
35
35
  c_feedbacks?: number | null;
36
36
  avg_raiting?: number | string | null;
37
37
  } | null;
38
+ /** The product's videos (Video tab of the panel) and its last change. */
39
+ videos?: { product_video?: string | null; preview?: string | null }[] | null;
40
+ date_updated?: string | null;
38
41
  }
39
42
 
40
43
  export interface StorefrontProductDataOptions {
@@ -130,6 +133,52 @@ function priceValue(
130
133
  return prices?.[currencyId] && Number.isFinite(value) ? value : undefined;
131
134
  }
132
135
 
136
+ // A backend date (`d.m.Y`, or ISO 8601 with a time) as an ISO 8601 date.
137
+ function structuredDate(value: string | null | undefined): string | undefined {
138
+ const text = (value ?? '').trim();
139
+ const dmy = /^(\d{2})\.(\d{2})\.(\d{4})/.exec(text);
140
+ if (dmy) {
141
+ return `${dmy[3]}-${dmy[2]}-${dmy[1]}`;
142
+ }
143
+ return /^\d{4}-\d{2}-\d{2}/.exec(text)?.[0];
144
+ }
145
+
146
+ /**
147
+ * The product's videos as schema.org VideoObject (Google "Video" structured
148
+ * data): the file, its preview (or the first photo) as the thumbnail, the
149
+ * product's name and description. Google needs a name, a thumbnail and an
150
+ * upload date: the product's last change stands for the upload date, and a
151
+ * video without a thumbnail or a date is left out.
152
+ */
153
+ export function storefrontProductVideoObjects(
154
+ product: StorefrontStructuredProduct | undefined,
155
+ options: Pick<StorefrontProductDataOptions, 'absoluteUrl' | 'description'> & {
156
+ name: string;
157
+ fallbackImage?: string;
158
+ },
159
+ ): Record<string, unknown>[] {
160
+ const uploadDate = structuredDate(product?.date_updated);
161
+ if (!product || !uploadDate) {
162
+ return [];
163
+ }
164
+ return (product.videos ?? []).flatMap((video, index, list): Record<string, unknown>[] => {
165
+ const contentUrl = options.absoluteUrl(video.product_video ?? undefined);
166
+ const thumbnailUrl = options.absoluteUrl(video.preview ?? undefined) || options.fallbackImage;
167
+ if (!contentUrl || !thumbnailUrl) {
168
+ return [];
169
+ }
170
+ const name = list.length > 1 ? `${options.name} (${index + 1})` : options.name;
171
+ return [{
172
+ '@type': 'VideoObject',
173
+ name,
174
+ 'description': options.description?.trim() || name,
175
+ 'thumbnailUrl': [thumbnailUrl],
176
+ uploadDate,
177
+ contentUrl,
178
+ }];
179
+ });
180
+ }
181
+
133
182
  /** The schema.org Product of the page, or undefined when it cannot be built. */
134
183
  export function storefrontProductStructuredData(
135
184
  product: StorefrontStructuredProduct | undefined,
@@ -160,6 +209,12 @@ export function storefrontProductStructuredData(
160
209
  Boolean(image) && list.indexOf(image) === index,
161
210
  )
162
211
  .slice(0, MAX_IMAGES);
212
+ const videos = storefrontProductVideoObjects(product, {
213
+ name,
214
+ description: options.description,
215
+ absoluteUrl: options.absoluteUrl,
216
+ fallbackImage: images[0],
217
+ });
163
218
  const inStock =
164
219
  ON_SALE_STATUSES.includes(product.status?.status) &&
165
220
  product.c_real_left > 0;
@@ -213,6 +268,8 @@ export function storefrontProductStructuredData(
213
268
  ? { description: options.description.trim() }
214
269
  : {}),
215
270
  ...(images.length ? { image: images } : {}),
271
+ // The product's videos: the page plays them in its gallery.
272
+ ...(videos.length ? { subjectOf: videos } : {}),
216
273
  ...(brand ? { brand: { '@type': 'Brand', 'name': brand } } : {}),
217
274
  ...(offer ? { offers: offer } : {}),
218
275
  ...(reviews > 0 && Number.isFinite(rating) && rating > 0