@times-components/ssr 2.60.7-fb3064628e9000ef2312d19f22af52ae1bf50c5c.22 → 2.60.8-461f0aedb6c4ea29448bba0700b68a6858a42145.10

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.
@@ -0,0 +1,77 @@
1
+ const { wrapSkimlinks } = require("./skimlinks-wrapping");
2
+ const { wrapTrackonomics } = require("../lib/trackonomics-wrapping");
3
+
4
+ const wrapAffiliateLink = (affiliateLink, contentPageUrl) => {
5
+ const skimlinksUrl = wrapSkimlinks(affiliateLink, contentPageUrl);
6
+
7
+ return wrapTrackonomics(skimlinksUrl, contentPageUrl);
8
+ };
9
+
10
+ module.exports.affiliateLinksValidation = (children, articleDataFromRender) => {
11
+ const clonedChildren = [...children];
12
+ const { canonicalUrl, hostName } = articleDataFromRender;
13
+ const contentPageUrl = `${hostName}${canonicalUrl}`;
14
+
15
+ const checkAndSetLinkTarget = elements =>
16
+ elements.map(el => {
17
+ let newElement = { ...el };
18
+
19
+ // Check if element is a link or an interactive element with a URL.
20
+ if (
21
+ newElement.name === "link" ||
22
+ (newElement.name === "interactive" &&
23
+ newElement.attributes &&
24
+ newElement.attributes.element &&
25
+ newElement.attributes.element.value === "times-travel-cta")
26
+ ) {
27
+ const { attributes } = newElement;
28
+ const { element } = attributes || {};
29
+ const { attributes: elementAttributes } = element || {};
30
+ const href =
31
+ newElement.name === "interactive"
32
+ ? elementAttributes.url || ""
33
+ : attributes.href || "";
34
+
35
+ // If the link is external, set target to _blank. Wrap affiliate link if necessary.
36
+ if (
37
+ href &&
38
+ !href.startsWith("https://www.thetimes.co.uk") &&
39
+ !href.startsWith("https://www.thetimes.com")
40
+ ) {
41
+ if (newElement.name === "interactive") {
42
+ newElement = {
43
+ ...newElement,
44
+ attributes: {
45
+ ...attributes,
46
+ element: {
47
+ ...element,
48
+ attributes: {
49
+ ...elementAttributes,
50
+ target: "_blank",
51
+ url: wrapAffiliateLink(href, contentPageUrl)
52
+ }
53
+ }
54
+ }
55
+ };
56
+ } else {
57
+ newElement = {
58
+ ...newElement,
59
+ attributes: {
60
+ ...attributes,
61
+ target: "_blank",
62
+ href: wrapAffiliateLink(href, contentPageUrl)
63
+ }
64
+ };
65
+ }
66
+ }
67
+ }
68
+
69
+ if (newElement.children && newElement.children.length) {
70
+ newElement.children = checkAndSetLinkTarget(newElement.children);
71
+ }
72
+
73
+ return newElement;
74
+ });
75
+
76
+ return checkAndSetLinkTarget(clonedChildren);
77
+ };
@@ -0,0 +1,41 @@
1
+ const { publisherId } = require("../constants/affiliate-links-validation");
2
+ const { skimlinksDomainSet } = require("../constants/skimlinks-domains");
3
+
4
+ const extractDomain = url => {
5
+ try {
6
+ const { hostname } = new URL(url);
7
+ return hostname.replace(/^www\./, "");
8
+ } catch (error) {
9
+ return null;
10
+ }
11
+ };
12
+
13
+ const urlContainsDomain = url => {
14
+ const domain = extractDomain(url);
15
+
16
+ // Ensure skimlinksDomainSet is a Set and domain is valid
17
+ if (domain && skimlinksDomainSet instanceof Set) {
18
+ return skimlinksDomainSet.has(domain);
19
+ }
20
+
21
+ return false;
22
+ };
23
+
24
+ /**
25
+ *
26
+ * @param merchantUrl The merchants url (monetizable link)
27
+ * @param contentPageUrl The referring page
28
+ * @returns Skimlinks url
29
+ */
30
+ const constructSkimlinksUrl = (merchantUrl, contentPageUrl) => {
31
+ const skimlinksWrapper = `https://go.skimresources.com/?id=${publisherId}&url=${encodeURIComponent(
32
+ merchantUrl
33
+ )}&sref=${encodeURIComponent(contentPageUrl)}`;
34
+
35
+ return skimlinksWrapper;
36
+ };
37
+
38
+ const wrapSkimlinks = (url, contentPageUrl) =>
39
+ urlContainsDomain(url) ? constructSkimlinksUrl(url, contentPageUrl) : url;
40
+
41
+ module.exports = { wrapSkimlinks };
@@ -0,0 +1,32 @@
1
+ const {
2
+ travelSiteCode,
3
+ theTimesSiteCode,
4
+ trackonomicsRegex
5
+ } = require("../constants/affiliate-links-validation");
6
+
7
+ const isTrackonomicsUrl = url => trackonomicsRegex.test(url);
8
+
9
+ const getSiteCode = contentPageUrl => {
10
+ const isTravelUrl =
11
+ contentPageUrl.includes("https://www.thetimes.com/travel") ||
12
+ contentPageUrl.includes("https://www.thetimes.co.uk/travel");
13
+
14
+ return isTravelUrl ? travelSiteCode : theTimesSiteCode;
15
+ };
16
+
17
+ const constructTrackonomicsUrl = (trackonomicsUrl, contentPageUrl) => {
18
+ const siteCode = getSiteCode(contentPageUrl);
19
+
20
+ const trackonomicsWrapper = `https://clicks.trx-hub.com/xid/${siteCode}?q=${encodeURIComponent(
21
+ trackonomicsUrl
22
+ )}&p=${encodeURIComponent(contentPageUrl)}`;
23
+
24
+ return trackonomicsWrapper;
25
+ };
26
+
27
+ const wrapTrackonomics = (skimlinksUrl, contentPageUrl) =>
28
+ isTrackonomicsUrl(skimlinksUrl)
29
+ ? constructTrackonomicsUrl(skimlinksUrl, contentPageUrl)
30
+ : skimlinksUrl;
31
+
32
+ module.exports = { wrapTrackonomics };