@anywayseo/gatsby-plugin 2.7.0 → 2.8.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@anywayseo/gatsby-plugin",
3
3
  "description": "Shared config for Anywayseo sites",
4
- "version": "2.7.0",
4
+ "version": "2.8.0",
5
5
  "author": "zerg41",
6
6
  "license": "MIT",
7
7
  "publishConfig": {
@@ -11,7 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.createMdxPages = createMdxPages;
13
13
  const path_1 = require("path");
14
- const slug_1 = require("../slug");
14
+ const slug_1 = require("../utils/slug");
15
15
  function createMdxPages(_a, root_1) {
16
16
  return __awaiter(this, arguments, void 0, function* ({ actions, reporter, graphql }, root) {
17
17
  var _b, _c, _d, _e, _f, _g;
@@ -11,7 +11,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.createStrapiPages = createStrapiPages;
13
13
  const path_1 = require("path");
14
- const slug_1 = require("../slug");
14
+ const slug_1 = require("../utils/slug");
15
+ const hreflang_1 = require("../utils/hreflang");
15
16
  function createStrapiPages(_a, root_1) {
16
17
  return __awaiter(this, arguments, void 0, function* ({ actions, reporter, graphql }, root) {
17
18
  var _b, _c, _d, _e;
@@ -42,8 +43,11 @@ function createStrapiPages(_a, root_1) {
42
43
  return;
43
44
  }
44
45
  reporter.info(`Found ${strapiPages.length} Strapi pages`);
46
+ const hrefLangMap = (0, hreflang_1.buildHrefLangMap)(strapiPages, defaultLocale);
47
+ reporter.info(`Generated hrefLangs for Strapi pages`);
45
48
  let pageCount = 0;
46
49
  strapiPages.forEach(({ id, slug, title, locale }) => {
50
+ var _a;
47
51
  if ((0, slug_1.isDummyPage)(slug)) {
48
52
  reporter.warn('Skipping page creation: dummy page detected');
49
53
  return;
@@ -53,10 +57,12 @@ function createStrapiPages(_a, root_1) {
53
57
  return;
54
58
  }
55
59
  const strapiPageTemplate = (0, path_1.resolve)(root, `./src/templates/strapi-page.tsx`);
60
+ const strapiPagePath = (0, slug_1.buildPath)(slug, locale, defaultLocale);
61
+ const strapiPageHrefLangs = (_a = hrefLangMap[strapiPagePath]) !== null && _a !== void 0 ? _a : [];
56
62
  actions.createPage({
57
- path: (0, slug_1.buildPath)(slug, locale, defaultLocale),
63
+ path: strapiPagePath,
58
64
  component: strapiPageTemplate,
59
- context: { id },
65
+ context: { id, hrefLangs: strapiPageHrefLangs },
60
66
  });
61
67
  pageCount++;
62
68
  });
@@ -0,0 +1,2 @@
1
+ import type { HrefLangMap, IPage } from './types';
2
+ export declare function buildHrefLangMap(pages: IPage[], defaultLocale?: string): HrefLangMap;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildHrefLangMap = buildHrefLangMap;
4
+ const slug_1 = require("./slug");
5
+ /** Choose x-default: prefer defaultLocale, fallback to first locale */
6
+ function getDefaultHrefLang(pages, defaultLocale) {
7
+ var _a;
8
+ const defaultPage = (_a = pages.find((page) => page.locale === defaultLocale)) !== null && _a !== void 0 ? _a : pages[0];
9
+ return { href: (0, slug_1.buildPath)(defaultPage.slug, defaultPage.locale, defaultLocale), hrefLang: 'x-default' };
10
+ }
11
+ function buildHrefLangMap(pages, defaultLocale = 'en') {
12
+ const hrefLangMap = {};
13
+ for (const pageGroup of Object.values((0, slug_1.groupPagesBySlug)(pages, defaultLocale))) {
14
+ const defaultHreflang = getDefaultHrefLang(pageGroup, defaultLocale);
15
+ for (const page of pageGroup) {
16
+ const hrefLangs = pageGroup.map(({ slug, locale }) => ({
17
+ hrefLang: locale,
18
+ href: (0, slug_1.buildPath)(slug, locale, defaultLocale),
19
+ }));
20
+ hrefLangs.push(defaultHreflang);
21
+ const path = (0, slug_1.buildPath)(page.slug, page.locale, defaultLocale);
22
+ hrefLangMap[path] = hrefLangs;
23
+ }
24
+ }
25
+ return hrefLangMap;
26
+ }
@@ -0,0 +1,6 @@
1
+ import type { IPage, PageGroup } from './types';
2
+ export declare function buildPath(slug?: IPage['slug'], locale?: IPage['locale'], defaultLocale?: string): string;
3
+ export declare function isEqualSlug(a?: IPage['slug'], b?: IPage['slug']): boolean;
4
+ export declare function isEmptyPage(title?: IPage['title'], slug?: IPage['slug']): boolean;
5
+ export declare function isDummyPage(slug?: IPage['slug']): boolean;
6
+ export declare function groupPagesBySlug(pages: IPage[], defaultLocale: string): PageGroup;
@@ -4,6 +4,7 @@ exports.buildPath = buildPath;
4
4
  exports.isEqualSlug = isEqualSlug;
5
5
  exports.isEmptyPage = isEmptyPage;
6
6
  exports.isDummyPage = isDummyPage;
7
+ exports.groupPagesBySlug = groupPagesBySlug;
7
8
  const INDEX_PAGE_SLUG = 'index';
8
9
  const DUMMY_PAGE_SLUG = 'dummy';
9
10
  function normalizeSlug(slug) {
@@ -30,3 +31,16 @@ function isEmptyPage(title, slug) {
30
31
  function isDummyPage(slug) {
31
32
  return normalizeSlug(slug) === DUMMY_PAGE_SLUG;
32
33
  }
34
+ function groupPagesBySlug(pages, defaultLocale) {
35
+ var _a;
36
+ const groups = {};
37
+ for (const page of pages) {
38
+ if (!page.slug)
39
+ continue;
40
+ if (!groups[page.slug]) {
41
+ groups[page.slug] = [];
42
+ }
43
+ groups[page.slug].push({ slug: page.slug, locale: (_a = page.locale) !== null && _a !== void 0 ? _a : defaultLocale });
44
+ }
45
+ return groups;
46
+ }
@@ -0,0 +1,17 @@
1
+ type Path = string;
2
+ export interface IPage {
3
+ title?: string | null;
4
+ slug?: string | null;
5
+ locale?: string | null;
6
+ }
7
+ interface IExistedPage {
8
+ slug: NonNullable<IPage['slug']>;
9
+ locale: NonNullable<IPage['locale']>;
10
+ }
11
+ export type PageGroup = Record<IExistedPage['slug'], IExistedPage[]>;
12
+ export interface IHrefLang {
13
+ href: string;
14
+ hrefLang: string;
15
+ }
16
+ export type HrefLangMap = Record<Path, IHrefLang[]>;
17
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +0,0 @@
1
- export declare function buildPath(slug?: string | null, locale?: string | null, defaultLocale?: string | null): string;
2
- export declare function isEqualSlug(a?: string | null, b?: string | null): boolean;
3
- export declare function isEmptyPage(title?: string | null, slug?: string | null): boolean;
4
- export declare function isDummyPage(slug?: string | null): boolean;