@prezly/theme-kit-nextjs 3.2.3-0 → 3.2.3

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.
Files changed (46) hide show
  1. package/build/components-nextjs/PageSeo/PageSeo.js +15 -4
  2. package/build/components-nextjs/PageSeo/PageSeo.js.map +1 -1
  3. package/build/components-nextjs/document.js.map +1 -1
  4. package/build/intl/index.d.ts +1 -2
  5. package/build/intl/index.js +1 -2
  6. package/build/intl/index.js.map +1 -1
  7. package/build/intl/locale.d.ts +0 -9
  8. package/build/intl/locale.js.map +1 -1
  9. package/build/intl/localeObject.d.ts +0 -5
  10. package/build/intl/localeObject.js +0 -7
  11. package/build/intl/localeObject.js.map +1 -1
  12. package/build/sentry/common.js +26 -0
  13. package/build/sentry/common.js.map +1 -1
  14. package/build/tsconfig.tsbuildinfo +1 -1
  15. package/package.json +8 -8
  16. package/src/components-nextjs/PageSeo/PageSeo.tsx +25 -14
  17. package/src/components-nextjs/document.tsx +1 -0
  18. package/src/intl/index.ts +1 -2
  19. package/src/intl/locale.ts +1 -5
  20. package/src/intl/localeObject.ts +0 -8
  21. package/src/sentry/common.ts +42 -0
  22. package/build/intl/getFallbackLocale.d.ts +0 -8
  23. package/build/intl/getFallbackLocale.js +0 -21
  24. package/build/intl/getFallbackLocale.js.map +0 -1
  25. package/build/seo/getAlternateLanguageLinks.d.ts +0 -4
  26. package/build/seo/getAlternateLanguageLinks.js +0 -49
  27. package/build/seo/getAlternateLanguageLinks.js.map +0 -1
  28. package/build/seo/index.d.ts +0 -1
  29. package/build/seo/index.js +0 -18
  30. package/build/seo/index.js.map +0 -1
  31. package/build/seo/utils/createAlternateLanguageLink.d.ts +0 -5
  32. package/build/seo/utils/createAlternateLanguageLink.js +0 -15
  33. package/build/seo/utils/createAlternateLanguageLink.js.map +0 -1
  34. package/build/seo/utils/getDefaultHrefLang.d.ts +0 -11
  35. package/build/seo/utils/getDefaultHrefLang.js +0 -28
  36. package/build/seo/utils/getDefaultHrefLang.js.map +0 -1
  37. package/build/seo/utils/mapLanguagesToLocales.d.ts +0 -4
  38. package/build/seo/utils/mapLanguagesToLocales.js +0 -24
  39. package/build/seo/utils/mapLanguagesToLocales.js.map +0 -1
  40. package/src/intl/getFallbackLocale.ts +0 -26
  41. package/src/seo/getAlternateLanguageLinks.test.ts +0 -150
  42. package/src/seo/getAlternateLanguageLinks.ts +0 -72
  43. package/src/seo/index.ts +0 -1
  44. package/src/seo/utils/createAlternateLanguageLink.ts +0 -18
  45. package/src/seo/utils/getDefaultHrefLang.ts +0 -57
  46. package/src/seo/utils/mapLanguagesToLocales.ts +0 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prezly/theme-kit-nextjs",
3
- "version": "3.2.3-0",
3
+ "version": "3.2.3",
4
4
  "description": "Data layer and utility library for developing Prezly themes with NextJS",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -55,17 +55,17 @@
55
55
  "parse-data-url": "^4.0.1"
56
56
  },
57
57
  "devDependencies": {
58
- "@prezly/eslint-config": "4.3.2",
59
- "@sentry/nextjs": "7.35.0",
58
+ "@prezly/eslint-config": "4.4.1",
59
+ "@sentry/nextjs": "7.38.0",
60
60
  "@types/jest": "29.4.0",
61
61
  "@types/node": "17.0.14",
62
62
  "@types/parse-data-url": "3.0.0",
63
- "@types/react": "18.0.27",
64
- "@types/react-dom": "18.0.10",
65
- "eslint": "8.33.0",
66
- "jest": "29.4.1",
63
+ "@types/react": "18.0.28",
64
+ "@types/react-dom": "18.0.11",
65
+ "eslint": "8.35.0",
66
+ "jest": "29.4.3",
67
67
  "np": "7.6.3",
68
- "prettier": "2.8.3",
68
+ "prettier": "2.8.4",
69
69
  "rimraf": "3.0.2",
70
70
  "ts-jest": "29.0.5",
71
71
  "typescript": "4.9.5"
@@ -3,7 +3,7 @@ import type { NextSeoProps } from 'next-seo';
3
3
  import { useRouter } from 'next/router';
4
4
  import { useMemo } from 'react';
5
5
 
6
- import { getUsedLanguages } from '../../intl';
6
+ import { getUsedLanguages, LocaleObject } from '../../intl';
7
7
  import {
8
8
  useCompanyInformation,
9
9
  useCurrentLocale,
@@ -13,7 +13,6 @@ import {
13
13
  useLanguages,
14
14
  useNewsroom,
15
15
  } from '../../newsroom-context';
16
- import { getAlternateLanguageLinks } from '../../seo';
17
16
  import { getNewsroomLogoUrl } from '../../utils';
18
17
 
19
18
  import { getAbsoluteUrl } from './lib/getAbsoluteUrl';
@@ -66,19 +65,31 @@ export function PageSeo({
66
65
  return [];
67
66
  }
68
67
 
69
- const usedLanguages = getUsedLanguages(languages);
70
-
71
- return getAlternateLanguageLinks(
72
- usedLanguages,
73
- getTranslationUrl,
74
- (locale, translationUrl) =>
75
- getAbsoluteUrl(
76
- translationUrl,
77
- site.url,
78
- currentStory && translationUrl !== '/' ? false : getLinkLocaleSlug(locale),
79
- ),
68
+ const alternateLanguages = getUsedLanguages(languages).filter(
69
+ (language) => language.code !== currentLocale.toUnderscoreCode(),
80
70
  );
81
- }, [currentStory, getLinkLocaleSlug, getTranslationUrl, languages, site.url]);
71
+
72
+ return alternateLanguages
73
+ .map((language) => {
74
+ const locale = LocaleObject.fromAnyCode(language.code);
75
+
76
+ const translationLink = getTranslationUrl(locale, true);
77
+
78
+ if (!translationLink) {
79
+ return undefined;
80
+ }
81
+
82
+ return {
83
+ hrefLang: locale.toHyphenCode(),
84
+ href: getAbsoluteUrl(
85
+ translationLink,
86
+ site.url,
87
+ currentStory && translationLink !== '/' ? false : getLinkLocaleSlug(locale),
88
+ ),
89
+ };
90
+ })
91
+ .filter<AlternateLanguageLink>(Boolean as any);
92
+ }, [currentLocale, getLinkLocaleSlug, getTranslationUrl, languages, site.url, currentStory]);
82
93
 
83
94
  return (
84
95
  <NextSeo
@@ -26,6 +26,7 @@ export class PrezlyThemeDocument extends Document {
26
26
  <Html lang={locale.toHyphenCode()} dir={direction}>
27
27
  <Head>
28
28
  <meta name="og:locale" content={locale.toHyphenCode()} />
29
+ {/* TODO: Add alternate locales */}
29
30
  </Head>
30
31
  <body>
31
32
  <Main />
package/src/intl/index.ts CHANGED
@@ -1,5 +1,4 @@
1
- export * from './getFallbackLocale';
2
1
  export * from './languages';
3
- export { default as localeConfig } from './localeConfig';
4
2
  export * from './locale';
3
+ export { default as localeConfig } from './localeConfig';
5
4
  export * from './localeObject';
@@ -8,15 +8,11 @@ export const DEFAULT_LOCALE = 'en';
8
8
  // We use pseudo locale used for localization testing, to reliably determine if we need to fallback to the default newsroom language
9
9
  export const DUMMY_DEFAULT_LOCALE = 'qps-ploc';
10
10
 
11
- export type LangCode = string & { __BRAND__?: 'langCode' };
12
- export type RegionCode = string & { __BRAND__?: 'regionCode' };
13
- export type LocaleCode = string & { __BRAND__?: 'localeCode' };
14
-
15
11
  /**
16
12
  * This a list of locales that Prezly has translations for. Each code represents a language file in the `@prezly/themes-intl-messages` package.
17
13
  * See https://github.com/prezly/themes-intl-messages for more info.
18
14
  */
19
- const SUPPORTED_LOCALES: LocaleCode[] = [
15
+ const SUPPORTED_LOCALES = [
20
16
  'af',
21
17
  'ar',
22
18
  'az',
@@ -12,12 +12,6 @@ export class LocaleObject {
12
12
  */
13
13
  private localeCode: string;
14
14
 
15
- /**
16
- * Whether the locale is global (without region/culture part).
17
- * I.E. `en` is global, while `en-US` is not.
18
- */
19
- public isGlobal: boolean;
20
-
21
15
  private constructor(localeCode: string) {
22
16
  if (!/^([a-z]{2,3})(-[a-z\d]{1,4})?$/i.test(localeCode)) {
23
17
  throw new Error('Invalid locale code provided!');
@@ -26,13 +20,11 @@ export class LocaleObject {
26
20
  const [language, region] = localeCode.split('-');
27
21
 
28
22
  if (region) {
29
- this.isGlobal = false;
30
23
  // Workaround for `zh-Hant` locale, which is the only locale with non-uppercase region part
31
24
  const regionSuffix = region === 'hant' ? 'Hant' : region.toUpperCase();
32
25
 
33
26
  this.localeCode = [language, regionSuffix].join('-');
34
27
  } else {
35
- this.isGlobal = true;
36
28
  this.localeCode = language;
37
29
  }
38
30
  }
@@ -1,5 +1,13 @@
1
1
  import type { init } from '@sentry/nextjs';
2
2
 
3
+ const IGNORED_EVENT_CULPRITS = [
4
+ // This usually means that something happened outside of application code.
5
+ // All instances of errors with this origin that I found usually were caused by referring to variables that couldn't ever exist in our code.
6
+ // Likely misbehaving browser extensions or people playing around in DevTools trying to break the app.
7
+ '?(<anonymous>)',
8
+ 'global code',
9
+ ];
10
+
3
11
  export function getCommonClientOptions(dsn: string, themeName: string): Parameters<typeof init>[0] {
4
12
  return {
5
13
  dsn,
@@ -18,6 +26,40 @@ export function getCommonClientOptions(dsn: string, themeName: string): Paramete
18
26
  // This error is caused by `beacon.min.js` polyfill in Cloudflare Insights. From what we see, it doesn't affect the user experience.
19
27
  'Illegal invocation',
20
28
  ],
29
+
30
+ // Ignore global code errors that are not related to the application code.
31
+ // See comments in `IGNORED_EVENT_CULPRITS` const for context on each entry
32
+ beforeSend: (event) => {
33
+ // `culprit` is not a documented property, but it is present in all Sentry events and is even displayed in the interface.
34
+ // Just in case it's not defined, we can fallback to the stack trace.
35
+ if ('culprit' in event && typeof event.culprit === 'string') {
36
+ const { culprit } = event;
37
+ if (
38
+ IGNORED_EVENT_CULPRITS.some((ignoredCulprit) =>
39
+ culprit.includes(ignoredCulprit),
40
+ )
41
+ ) {
42
+ return null;
43
+ }
44
+ } else if (event.exception) {
45
+ if (
46
+ event.exception.values?.some((value) =>
47
+ value.stacktrace?.frames?.some(
48
+ (frame) =>
49
+ frame.abs_path &&
50
+ IGNORED_EVENT_CULPRITS.some((ignoredCulprit) =>
51
+ frame.abs_path?.includes(ignoredCulprit),
52
+ ),
53
+ ),
54
+ )
55
+ ) {
56
+ return null;
57
+ }
58
+ }
59
+
60
+ return event;
61
+ },
62
+
21
63
  // Attach theme tag to each event
22
64
  initialScope: (scope) => {
23
65
  scope.setTags({ prezly_theme: themeName });
@@ -1,8 +0,0 @@
1
- import type { LangCode } from './locale';
2
- import { LocaleObject } from './localeObject';
3
- /**
4
- * Fallback locales for each language.
5
- * For example, a story has no en(global) locale, then we can take some fallback locale from here.
6
- */
7
- export declare const localeFallback: Record<LangCode, LocaleObject[]>;
8
- export declare function getFallbackLocale(langCode: LangCode, availableLocales: LocaleObject[]): LocaleObject | undefined;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFallbackLocale = exports.localeFallback = void 0;
4
- const localeObject_1 = require("./localeObject");
5
- /**
6
- * Fallback locales for each language.
7
- * For example, a story has no en(global) locale, then we can take some fallback locale from here.
8
- */
9
- exports.localeFallback = {
10
- en: [localeObject_1.LocaleObject.fromAnyCode('en-US'), localeObject_1.LocaleObject.fromAnyCode('en-GB')],
11
- fr: [localeObject_1.LocaleObject.fromAnyCode('fr-FR'), localeObject_1.LocaleObject.fromAnyCode('fr-CA')],
12
- };
13
- function getFallbackLocale(langCode, availableLocales) {
14
- const fallbacks = exports.localeFallback[langCode];
15
- if (!fallbacks) {
16
- return undefined;
17
- }
18
- return fallbacks.find((fallback) => Boolean(availableLocales.find((available) => available.isEqual(fallback))));
19
- }
20
- exports.getFallbackLocale = getFallbackLocale;
21
- //# sourceMappingURL=getFallbackLocale.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getFallbackLocale.js","sourceRoot":"","sources":["../../src/intl/getFallbackLocale.ts"],"names":[],"mappings":";;;AACA,iDAA8C;AAE9C;;;GAGG;AACU,QAAA,cAAc,GAAqC;IAC5D,EAAE,EAAE,CAAC,2BAAY,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,2BAAY,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAC1E,EAAE,EAAE,CAAC,2BAAY,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,2BAAY,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;CAC7E,CAAC;AAEF,SAAgB,iBAAiB,CAC7B,QAAkB,EAClB,gBAAgC;IAEhC,MAAM,SAAS,GAAG,sBAAc,CAAC,QAAQ,CAAC,CAAC;IAE3C,IAAI,CAAC,SAAS,EAAE;QACZ,OAAO,SAAS,CAAC;KACpB;IAED,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAC/B,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAC7E,CAAC;AACN,CAAC;AAbD,8CAaC"}
@@ -1,4 +0,0 @@
1
- import type { NewsroomLanguageSettings } from '@prezly/sdk';
2
- import type { AlternateLanguageLink } from '../components-nextjs/PageSeo';
3
- import type { LocaleObject } from '../intl';
4
- export declare function getAlternateLanguageLinks(languages: NewsroomLanguageSettings[], getTranslationUrl: (locale: LocaleObject) => string | undefined, createHref: (locale: LocaleObject, translationUrl: string) => string): AlternateLanguageLink[];
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAlternateLanguageLinks = void 0;
4
- const intl_1 = require("../intl");
5
- const createAlternateLanguageLink_1 = require("./utils/createAlternateLanguageLink");
6
- const getDefaultHrefLang_1 = require("./utils/getDefaultHrefLang");
7
- const mapLanguagesToLocales_1 = require("./utils/mapLanguagesToLocales");
8
- function getAlternateLanguageLinks(languages, getTranslationUrl, createHref) {
9
- const alternateLanguageFallbackGlobalLinks = [];
10
- const alternateLanguageLocales = [];
11
- const [localesByLangCode, defaultLangLocale] = (0, mapLanguagesToLocales_1.mapLanguagesToLocales)(languages);
12
- localesByLangCode.forEach((locales, globalLocaleCode) => {
13
- let hasGlobalLocale = false;
14
- locales.forEach((locale) => {
15
- alternateLanguageLocales.push(locale);
16
- if (!hasGlobalLocale && locale.isGlobal) {
17
- hasGlobalLocale = true;
18
- }
19
- });
20
- if (!hasGlobalLocale) {
21
- const localesArray = Array.from(locales.values());
22
- const fallback = (0, intl_1.getFallbackLocale)(globalLocaleCode, localesArray) ?? localesArray.at(0);
23
- if (fallback) {
24
- const fallbackLink = (0, createAlternateLanguageLink_1.createAlternateLanguageLink)(fallback, getTranslationUrl, createHref);
25
- if (fallbackLink) {
26
- alternateLanguageFallbackGlobalLinks.push({
27
- ...fallbackLink,
28
- hrefLang: globalLocaleCode,
29
- });
30
- }
31
- }
32
- }
33
- });
34
- let languageAlternates = alternateLanguageLocales.map((locale) => (0, createAlternateLanguageLink_1.createAlternateLanguageLink)(locale, getTranslationUrl, createHref));
35
- languageAlternates = languageAlternates.concat(alternateLanguageFallbackGlobalLinks);
36
- const xDefault = (0, getDefaultHrefLang_1.getDefaultHrefLang)({
37
- alternateLanguageLocales,
38
- alternateLanguageFallbackGlobalLinks,
39
- defaultLangLocale,
40
- getTranslationUrl,
41
- createHref,
42
- });
43
- languageAlternates.push(xDefault);
44
- return languageAlternates
45
- .filter((link) => Boolean(link))
46
- .sort((a, b) => a.hrefLang.localeCompare(b.hrefLang));
47
- }
48
- exports.getAlternateLanguageLinks = getAlternateLanguageLinks;
49
- //# sourceMappingURL=getAlternateLanguageLinks.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getAlternateLanguageLinks.js","sourceRoot":"","sources":["../../src/seo/getAlternateLanguageLinks.ts"],"names":[],"mappings":";;;AAIA,kCAA4C;AAE5C,qFAAkF;AAClF,mEAAgE;AAChE,yEAAsE;AAEtE,SAAgB,yBAAyB,CACrC,SAAqC,EACrC,iBAA+D,EAC/D,UAAoE;IAEpE,MAAM,oCAAoC,GAA4B,EAAE,CAAC;IACzE,MAAM,wBAAwB,GAAmB,EAAE,CAAC;IACpD,MAAM,CAAC,iBAAiB,EAAE,iBAAiB,CAAC,GAAG,IAAA,6CAAqB,EAAC,SAAS,CAAC,CAAC;IAEhF,iBAAiB,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,gBAAgB,EAAE,EAAE;QACpD,IAAI,eAAe,GAAG,KAAK,CAAC;QAE5B,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;YACvB,wBAAwB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAEtC,IAAI,CAAC,eAAe,IAAI,MAAM,CAAC,QAAQ,EAAE;gBACrC,eAAe,GAAG,IAAI,CAAC;aAC1B;QACL,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,eAAe,EAAE;YAClB,MAAM,YAAY,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;YAClD,MAAM,QAAQ,GACV,IAAA,wBAAiB,EAAC,gBAAgB,EAAE,YAAY,CAAC,IAAI,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YAE5E,IAAI,QAAQ,EAAE;gBACV,MAAM,YAAY,GAAG,IAAA,yDAA2B,EAC5C,QAAQ,EACR,iBAAiB,EACjB,UAAU,CACb,CAAC;gBAEF,IAAI,YAAY,EAAE;oBACd,oCAAoC,CAAC,IAAI,CAAC;wBACtC,GAAG,YAAY;wBACf,QAAQ,EAAE,gBAAgB;qBAC7B,CAAC,CAAC;iBACN;aACJ;SACJ;IACL,CAAC,CAAC,CAAC;IAEH,IAAI,kBAAkB,GAA6C,wBAAwB,CAAC,GAAG,CAC3F,CAAC,MAAM,EAAE,EAAE,CAAC,IAAA,yDAA2B,EAAC,MAAM,EAAE,iBAAiB,EAAE,UAAU,CAAC,CACjF,CAAC;IAEF,kBAAkB,GAAG,kBAAkB,CAAC,MAAM,CAAC,oCAAoC,CAAC,CAAC;IAErF,MAAM,QAAQ,GAAG,IAAA,uCAAkB,EAAC;QAChC,wBAAwB;QACxB,oCAAoC;QACpC,iBAAiB;QACjB,iBAAiB;QACjB,UAAU;KACb,CAAC,CAAC;IAEH,kBAAkB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAElC,OAAO,kBAAkB;SACpB,MAAM,CAAC,CAAC,IAAI,EAA2C,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;SACxE,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC9D,CAAC;AA7DD,8DA6DC"}
@@ -1 +0,0 @@
1
- export * from './getAlternateLanguageLinks';
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./getAlternateLanguageLinks"), exports);
18
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/seo/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8DAA4C"}
@@ -1,5 +0,0 @@
1
- import type { LocaleObject } from '../../intl';
2
- export declare function createAlternateLanguageLink(locale: LocaleObject, getTranslationUrl: (locale: LocaleObject) => string | undefined, createHref: (locale: LocaleObject, translationUrl: string) => string): {
3
- hrefLang: string;
4
- href: string;
5
- } | undefined;
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createAlternateLanguageLink = void 0;
4
- function createAlternateLanguageLink(locale, getTranslationUrl, createHref) {
5
- const translationUrl = getTranslationUrl(locale);
6
- if (!translationUrl) {
7
- return undefined;
8
- }
9
- return {
10
- hrefLang: locale.toHyphenCode(),
11
- href: createHref(locale, translationUrl),
12
- };
13
- }
14
- exports.createAlternateLanguageLink = createAlternateLanguageLink;
15
- //# sourceMappingURL=createAlternateLanguageLink.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createAlternateLanguageLink.js","sourceRoot":"","sources":["../../../src/seo/utils/createAlternateLanguageLink.ts"],"names":[],"mappings":";;;AAEA,SAAgB,2BAA2B,CACvC,MAAoB,EACpB,iBAA+D,EAC/D,UAAoE;IAEpE,MAAM,cAAc,GAAG,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAEjD,IAAI,CAAC,cAAc,EAAE;QACjB,OAAO,SAAS,CAAC;KACpB;IAED,OAAO;QACH,QAAQ,EAAE,MAAM,CAAC,YAAY,EAAE;QAC/B,IAAI,EAAE,UAAU,CAAC,MAAM,EAAE,cAAc,CAAC;KAC3C,CAAC;AACN,CAAC;AAfD,kEAeC"}
@@ -1,11 +0,0 @@
1
- import type { AlternateLanguageLink } from '../../components-nextjs/PageSeo';
2
- import type { LocaleObject } from '../../intl';
3
- interface Options {
4
- alternateLanguageLocales: LocaleObject[];
5
- alternateLanguageFallbackGlobalLinks: AlternateLanguageLink[];
6
- defaultLangLocale: LocaleObject | undefined;
7
- getTranslationUrl: (locale: LocaleObject) => string | undefined;
8
- createHref: (locale: LocaleObject, translationUrl: string) => string;
9
- }
10
- export declare function getDefaultHrefLang({ createHref, defaultLangLocale, getTranslationUrl, alternateLanguageFallbackGlobalLinks, alternateLanguageLocales, }: Options): AlternateLanguageLink | undefined;
11
- export {};
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDefaultHrefLang = void 0;
4
- const createAlternateLanguageLink_1 = require("./createAlternateLanguageLink");
5
- function getDefaultHrefLang({ createHref, defaultLangLocale, getTranslationUrl, alternateLanguageFallbackGlobalLinks, alternateLanguageLocales, }) {
6
- const engFallback = 'en';
7
- const hrefLang = 'x-default';
8
- const fallbackFromLocales = alternateLanguageLocales.find((alternate) => alternate.isGlobal && alternate.toNeutralLanguageCode() === engFallback);
9
- if (fallbackFromLocales) {
10
- const link = (0, createAlternateLanguageLink_1.createAlternateLanguageLink)(fallbackFromLocales, getTranslationUrl, createHref);
11
- if (link) {
12
- return { ...link, hrefLang };
13
- }
14
- }
15
- const fallbackFromGlobalLocales = alternateLanguageFallbackGlobalLinks.find((alternate) => alternate.hrefLang === engFallback);
16
- if (fallbackFromGlobalLocales) {
17
- return { ...fallbackFromGlobalLocales, hrefLang };
18
- }
19
- if (defaultLangLocale) {
20
- const link = (0, createAlternateLanguageLink_1.createAlternateLanguageLink)(defaultLangLocale, getTranslationUrl, createHref);
21
- if (link) {
22
- return { ...link, hrefLang };
23
- }
24
- }
25
- return undefined;
26
- }
27
- exports.getDefaultHrefLang = getDefaultHrefLang;
28
- //# sourceMappingURL=getDefaultHrefLang.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getDefaultHrefLang.js","sourceRoot":"","sources":["../../../src/seo/utils/getDefaultHrefLang.ts"],"names":[],"mappings":";;;AAGA,+EAA4E;AAU5E,SAAgB,kBAAkB,CAAC,EAC/B,UAAU,EACV,iBAAiB,EACjB,iBAAiB,EACjB,oCAAoC,EACpC,wBAAwB,GAClB;IACN,MAAM,WAAW,GAAG,IAAI,CAAC;IACzB,MAAM,QAAQ,GAAG,WAAW,CAAC;IAE7B,MAAM,mBAAmB,GAAG,wBAAwB,CAAC,IAAI,CACrD,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,QAAQ,IAAI,SAAS,CAAC,qBAAqB,EAAE,KAAK,WAAW,CACzF,CAAC;IAEF,IAAI,mBAAmB,EAAE;QACrB,MAAM,IAAI,GAAG,IAAA,yDAA2B,EACpC,mBAAmB,EACnB,iBAAiB,EACjB,UAAU,CACb,CAAC;QAEF,IAAI,IAAI,EAAE;YACN,OAAO,EAAE,GAAG,IAAI,EAAE,QAAQ,EAAE,CAAC;SAChC;KACJ;IAED,MAAM,yBAAyB,GAAG,oCAAoC,CAAC,IAAI,CACvE,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,QAAQ,KAAK,WAAW,CACpD,CAAC;IAEF,IAAI,yBAAyB,EAAE;QAC3B,OAAO,EAAE,GAAG,yBAAyB,EAAE,QAAQ,EAAE,CAAC;KACrD;IAED,IAAI,iBAAiB,EAAE;QACnB,MAAM,IAAI,GAAG,IAAA,yDAA2B,EAAC,iBAAiB,EAAE,iBAAiB,EAAE,UAAU,CAAC,CAAC;QAE3F,IAAI,IAAI,EAAE;YACN,OAAO,EAAE,GAAG,IAAI,EAAE,QAAQ,EAAE,CAAC;SAChC;KACJ;IAED,OAAO,SAAS,CAAC;AACrB,CAAC;AA3CD,gDA2CC"}
@@ -1,4 +0,0 @@
1
- import type { NewsroomLanguageSettings } from '@prezly/sdk';
2
- import type { LangCode, LocaleCode } from '../../intl';
3
- import { LocaleObject } from '../../intl';
4
- export declare function mapLanguagesToLocales(languages: NewsroomLanguageSettings[]): readonly [Map<LangCode, Map<LocaleCode, LocaleObject>>, LocaleObject | undefined];
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapLanguagesToLocales = void 0;
4
- const intl_1 = require("../../intl");
5
- function mapLanguagesToLocales(languages) {
6
- let defaultLangLocale;
7
- const localesByLangCode = new Map();
8
- languages.forEach((language) => {
9
- const languageLocale = intl_1.LocaleObject.fromAnyCode(language.code);
10
- const globalLanguageCode = languageLocale.toNeutralLanguageCode();
11
- let currentLocale = localesByLangCode.get(globalLanguageCode);
12
- if (!currentLocale) {
13
- currentLocale = new Map([[languageLocale.toHyphenCode(), languageLocale]]);
14
- localesByLangCode.set(globalLanguageCode, currentLocale);
15
- }
16
- currentLocale.set(languageLocale.toHyphenCode(), languageLocale);
17
- if (language.is_default) {
18
- defaultLangLocale = languageLocale;
19
- }
20
- });
21
- return [localesByLangCode, defaultLangLocale];
22
- }
23
- exports.mapLanguagesToLocales = mapLanguagesToLocales;
24
- //# sourceMappingURL=mapLanguagesToLocales.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mapLanguagesToLocales.js","sourceRoot":"","sources":["../../../src/seo/utils/mapLanguagesToLocales.ts"],"names":[],"mappings":";;;AAGA,qCAA0C;AAE1C,SAAgB,qBAAqB,CAAC,SAAqC;IACvE,IAAI,iBAA2C,CAAC;IAChD,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAA2C,CAAC;IAE7E,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC3B,MAAM,cAAc,GAAG,mBAAY,CAAC,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAC/D,MAAM,kBAAkB,GAAG,cAAc,CAAC,qBAAqB,EAAE,CAAC;QAClE,IAAI,aAAa,GAAG,iBAAiB,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;QAE9D,IAAI,CAAC,aAAa,EAAE;YAChB,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,cAAc,CAAC,YAAY,EAAE,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC;YAC3E,iBAAiB,CAAC,GAAG,CAAC,kBAAkB,EAAE,aAAa,CAAC,CAAC;SAC5D;QAED,aAAa,CAAC,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,EAAE,cAAc,CAAC,CAAC;QAEjE,IAAI,QAAQ,CAAC,UAAU,EAAE;YACrB,iBAAiB,GAAG,cAAc,CAAC;SACtC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,iBAAiB,EAAE,iBAAiB,CAAU,CAAC;AAC3D,CAAC;AAtBD,sDAsBC"}
@@ -1,26 +0,0 @@
1
- import type { LangCode } from './locale';
2
- import { LocaleObject } from './localeObject';
3
-
4
- /**
5
- * Fallback locales for each language.
6
- * For example, a story has no en(global) locale, then we can take some fallback locale from here.
7
- */
8
- export const localeFallback: Record<LangCode, LocaleObject[]> = {
9
- en: [LocaleObject.fromAnyCode('en-US'), LocaleObject.fromAnyCode('en-GB')],
10
- fr: [LocaleObject.fromAnyCode('fr-FR'), LocaleObject.fromAnyCode('fr-CA')],
11
- };
12
-
13
- export function getFallbackLocale(
14
- langCode: LangCode,
15
- availableLocales: LocaleObject[],
16
- ): LocaleObject | undefined {
17
- const fallbacks = localeFallback[langCode];
18
-
19
- if (!fallbacks) {
20
- return undefined;
21
- }
22
-
23
- return fallbacks.find((fallback) =>
24
- Boolean(availableLocales.find((available) => available.isEqual(fallback))),
25
- );
26
- }
@@ -1,150 +0,0 @@
1
- import { NewsroomLanguageSettings } from '@prezly/sdk';
2
- import { LocaleCode, LocaleObject } from '../intl';
3
- import { getAlternateLanguageLinks } from './getAlternateLanguageLinks';
4
-
5
- function lang(code: LocaleCode, isDefault = false) {
6
- return { code, is_default: isDefault } as NewsroomLanguageSettings;
7
- }
8
-
9
- function getTranslationUrl(locale: LocaleObject) {
10
- return `translationUrl/${locale.toHyphenCode()}`;
11
- }
12
-
13
- function createHref(locale: LocaleObject, translationUrl: string) {
14
- return `http://localhost:3000/${locale.toNeutralLanguageCode()}/${translationUrl}`;
15
- }
16
-
17
- describe('getAlternateLanguageLinks', () => {
18
- it('should handle one en-GB non-default language', () => {
19
- const links = getAlternateLanguageLinks([lang('en-GB')], getTranslationUrl, createHref);
20
-
21
- expect(links).toMatchObject([
22
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en' },
23
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
24
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'x-default' },
25
- ]);
26
- });
27
-
28
- it('should handle one en-GB default language', () => {
29
- const links = getAlternateLanguageLinks(
30
- [lang('en-GB', true)],
31
- getTranslationUrl,
32
- createHref,
33
- );
34
-
35
- expect(links).toMatchObject([
36
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en' },
37
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
38
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'x-default' },
39
- ]);
40
- });
41
-
42
- it('should handle two full locales', () => {
43
- const links = getAlternateLanguageLinks(
44
- [lang('en-GB', true), lang('fr-FR')],
45
- getTranslationUrl,
46
- createHref,
47
- );
48
-
49
- expect(links).toMatchObject([
50
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en' },
51
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
52
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr' },
53
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
54
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'x-default' },
55
- ]);
56
- });
57
-
58
- it('should use en-GB locale as x-default even if other locale is set as default', () => {
59
- const links = getAlternateLanguageLinks(
60
- [lang('en-GB'), lang('fr-FR', true)],
61
- getTranslationUrl,
62
- createHref,
63
- );
64
-
65
- expect(links).toMatchObject([
66
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en' },
67
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
68
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr' },
69
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
70
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'x-default' },
71
- ]);
72
- });
73
-
74
- it('should use default locale as x-default when there is eng locale', () => {
75
- const links = getAlternateLanguageLinks(
76
- [lang('ru-RU'), lang('fr-FR', true)],
77
- getTranslationUrl,
78
- createHref,
79
- );
80
-
81
- expect(links).toMatchObject([
82
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr' },
83
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
84
- { href: 'http://localhost:3000/ru/translationUrl/ru-RU', hrefLang: 'ru' },
85
- { href: 'http://localhost:3000/ru/translationUrl/ru-RU', hrefLang: 'ru-RU' },
86
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'x-default' },
87
- ]);
88
- });
89
-
90
- it('should include provided global locale', () => {
91
- const links = getAlternateLanguageLinks(
92
- [lang('en-US'), lang('fr'), lang('fr-FR', true)],
93
- getTranslationUrl,
94
- createHref,
95
- );
96
-
97
- expect(links).toMatchObject([
98
- { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en' },
99
- { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en-US' },
100
- { href: 'http://localhost:3000/fr/translationUrl/fr', hrefLang: 'fr' },
101
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
102
- { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'x-default' },
103
- ]);
104
- });
105
-
106
- it('should use provided en global locale as x-default', () => {
107
- const links = getAlternateLanguageLinks(
108
- [lang('en'), lang('en-US'), lang('fr'), lang('fr-FR')],
109
- getTranslationUrl,
110
- createHref,
111
- );
112
-
113
- expect(links).toMatchObject([
114
- { href: 'http://localhost:3000/en/translationUrl/en', hrefLang: 'en' },
115
- { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en-US' },
116
- { href: 'http://localhost:3000/fr/translationUrl/fr', hrefLang: 'fr' },
117
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
118
- { href: 'http://localhost:3000/en/translationUrl/en', hrefLang: 'x-default' },
119
- ]);
120
- });
121
-
122
- it('should fallback en link to en-US locale when there is en-GB locale', () => {
123
- const links = getAlternateLanguageLinks(
124
- [lang('en-GB'), lang('en-US')],
125
- getTranslationUrl,
126
- createHref,
127
- );
128
-
129
- expect(links).toMatchObject([
130
- { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en' },
131
- { href: 'http://localhost:3000/en/translationUrl/en-GB', hrefLang: 'en-GB' },
132
- { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'en-US' },
133
- { href: 'http://localhost:3000/en/translationUrl/en-US', hrefLang: 'x-default' },
134
- ]);
135
- });
136
-
137
- it('should should omit locales if getTranslationUrl is undefined', () => {
138
- const links = getAlternateLanguageLinks(
139
- [lang('en-US'), lang('fr'), lang('fr-FR', true)],
140
- (locale) => (locale.toHyphenCode() === 'en-US' ? undefined : getTranslationUrl(locale)),
141
- createHref,
142
- );
143
-
144
- expect(links).toMatchObject([
145
- { href: 'http://localhost:3000/fr/translationUrl/fr', hrefLang: 'fr' },
146
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'fr-FR' },
147
- { href: 'http://localhost:3000/fr/translationUrl/fr-FR', hrefLang: 'x-default' },
148
- ]);
149
- });
150
- });