Skip to content

Commit

Permalink
Merge pull request #182 from adobecom/MWPW-164941-localize-links
Browse files Browse the repository at this point in the history
Mwpw 164941 localize links
  • Loading branch information
Ben-Zahler authored Jan 14, 2025
2 parents c973b78 + 4bff374 commit 1f52410
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 52 deletions.
10 changes: 5 additions & 5 deletions edsdme/scripts/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ const domainMappings = {
/**
* Rewrite a link href domain based on production to stage domain mappings.
* @param {string} href - The link href to rewrite.
* @param domainMap map of domains to update
* @returns {string} - The rewritten link href, or the original if the environment is prod,
* there was a problem processing, or there is no domain mapping defined for it.
*/
export function rewriteLinkHref(href) {
export function rewriteHrefDomainOnStage(href, domainMap) {
const { env } = getConfig();

if (env.name === 'prod') return href;

let url;

try {
Expand All @@ -29,8 +29,8 @@ export function rewriteLinkHref(href) {
return href;
}

if (domainMappings[url.hostname]) {
url.hostname = domainMappings[url.hostname];
if (domainMap[url.hostname]) {
url.hostname = domainMap[url.hostname];
return url.toString();
}

Expand All @@ -45,7 +45,7 @@ export function rewriteLinkHref(href) {
export function applyGnavLinkRewriting(gnav) {
const links = gnav.querySelectorAll('a[href]');
links.forEach((link) => {
link.href = rewriteLinkHref(link.href);
link.href = rewriteHrefDomainOnStage(link.href, domainMappings);
});

return gnav;
Expand Down
171 changes: 137 additions & 34 deletions edsdme/scripts/rewriteLinks.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,145 @@
import { getConfig } from '../blocks/utils/utils.js';
import { partnerIsSignedIn } from './utils.js';
import { rewriteHrefDomainOnStage } from './links.js';

export function rewriteLinks() {
const environments = {
cbcProd: 'https://cbconnection.adobe.com',
cbcStage: 'https://cbconnection-stage.adobe.com',
partnersProd: 'https://partners.adobe.com',
partnersStage: 'https://partners.stage.adobe.com',
};
/**
* Domain map where the key is the production domain,
* and the value is the corresponding stage domain.
*/
const domainMap = {
'cbconnection.adobe.com': 'cbconnection-stage.adobe.com',
'partners.adobe.com': 'partners.stage.adobe.com',
};

const { env } = getConfig();
const isProd = env.name === 'prod';
/**
* Domain configs where the key is the production domain,
* and the value is config object for it.
*/
const domainConfigs = {
'cbconnection.adobe.com': {
localeMap: {
de: 'de',
cn: 'zh_cn',
fr: 'fr',
it: 'it',
jp: 'jp',
kr: 'ko',
es: 'es',
},
expectedLocale: 'en',
loginPath: '/bin/fusion/modalImsLogin',
},
'www.adobe.com': {
localeMap: {
emea: 'uk',
fr: 'fr',
de: 'de',
it: 'it',
es: 'es',
kr: 'kr',
cn: 'cn',
jp: 'jp',
},
},
'www.helpx.adobe.com': {
localeMap: {
emea: 'uk',
fr: 'fr',
de: 'de',
it: 'it',
es: 'es',
kr: 'kr',
cn: 'cn',
jp: 'jp',
},
},
'www.business.adobe.com': {
localeMap: {
emea: 'uk',
fr: 'fr',
de: 'de',
it: 'it',
es: 'es',
kr: 'kr',
cn: 'cn',
jp: 'jp',
},
},
};

const updateLinks = (currentDomain, newDomain, loginPath) => {
const isSignedIn = partnerIsSignedIn();
document.querySelectorAll(`[href^="${currentDomain}"]`).forEach((link) => {
let url;
try {
url = new URL(link.href);
} catch {
return;
}
url.hostname = new URL(newDomain).hostname;
if (isSignedIn && loginPath && !url.pathname.includes(loginPath)) {
const resource = url.pathname;
url.searchParams.append('resource', resource);
url.pathname = loginPath;
}
link.href = url.toString();
});
};
/**
* Modifies the given URL object by updating its search params
* (aimed to handle cbcconnection links and attach login path to them)
* @param {URL} url - The URL object to be modified.
*/
// eslint-disable-next-line consistent-return
function setLoginPathIfSignedIn(url) {
const loginPath = domainConfigs[url.hostname]?.loginPath;
if (loginPath) {
const isUserSignedIn = partnerIsSignedIn();
if (isUserSignedIn && !url.pathname.includes(loginPath)) {
const resource = url.pathname;
url.searchParams.append('resource', resource);
url.pathname = loginPath;
}
}
}

// Update cbc links
const cbcDomain = isProd ? environments.cbcProd : environments.cbcStage;
updateLinks(environments.cbcProd, cbcDomain, '/bin/fusion/modalImsLogin');
/**
* Modifies the given URL object by updating its path with correct locale for url domain,
* based on current page locale and locale maps that are defined for specific domains
*
* for cbcconnection it is expected that url already includes /en in path,
* so it will be updated to correct locale
* for other domains it is not expected to have locale in path,
* so correct locale will be appended to url.pathname
* @param {URL} url - The URL object to be modified.
*/
function setLocale(url) {
const localesToSkip = ['na', 'latam', 'apac'];
const currentPageLocale = window.location.pathname.split('/')?.[1];
if (localesToSkip.indexOf(currentPageLocale) !== -1) return;
const domainConfig = domainConfigs[url.hostname];
if (!domainConfig) return;
const localeFromMap = domainConfig.localeMap[currentPageLocale];
if (!localeFromMap) return;
const pathParts = url.pathname.split('/').filter(Boolean);
if (domainConfig.expectedLocale) {
const localeFromHref = pathParts[0];
if (localeFromHref !== domainConfig.expectedLocale) return;
pathParts[0] = localeFromMap;
} else {
pathParts.unshift(localeFromMap);
}
url.pathname = `/${pathParts.join('/')}`;
}

// Update partners links if not prod
if (!isProd) {
updateLinks(environments.partnersProd, environments.partnersStage);
/**
* Takes string that represent url href,
* updates locale, login path and domain
* @param href
* @returns {*|string} modified href
*/
function getUpdatedHref(href) {
let url;
try {
url = new URL(href);
} catch {
return href;
}
setLocale(url);
setLoginPathIfSignedIn(url);
// always as last step since we need original domains for mappings
return rewriteHrefDomainOnStage(url.href, domainMap);
}

/**
* Iterates throw all links on the page and updates their hrefs if conditions are fulfilled
* (conditions: appropriate domain, appropriate current page locale,
* environment and is user logged in)
*/
export function rewriteLinks() {
const links = document.querySelectorAll('a[href]');
links.forEach((link) => {
link.href = getUpdatedHref(link.href);
});
}
17 changes: 11 additions & 6 deletions test/scripts/links.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
* @jest-environment jsdom
*/

import { applyGnavLinkRewriting, rewriteLinkHref } from '../../edsdme/scripts/links.js';
import { applyGnavLinkRewriting, rewriteHrefDomainOnStage } from '../../edsdme/scripts/links.js';
import { getConfig } from '../../edsdme/blocks/utils/utils.js';

jest.mock('../../edsdme/blocks/utils/utils.js', () => ({ getConfig: jest.fn() }));

const domainMappings = {
'adobe.force.com': 'adobe--sfstage.sandbox.my.site.com',
'io-partners-dx.adobe.com': 'io-partners-dx.stage.adobe.com',
};

describe('Test links.js', () => {
beforeEach(() => {
getConfig.mockReturnValue({ env: { name: 'stage' } });
Expand All @@ -17,35 +22,35 @@ describe('Test links.js', () => {
getConfig.mockReturnValue({ env: { name: 'prod' } });

const href = 'https://adobe.force.com/path';
const result = rewriteLinkHref(href);
const result = rewriteHrefDomainOnStage(href, domainMappings);

expect(result).toBe(href);
});

test('should rewrite sales for link hrefs', () => {
const href = 'https://adobe.force.com/path';
const result = rewriteLinkHref(href);
const result = rewriteHrefDomainOnStage(href, domainMappings);

expect(result).toBe('https://adobe--sfstage.sandbox.my.site.com/path');
});

test('should rewrite runtime link hrefs', () => {
const href = 'https://io-partners-dx.adobe.com/path';
const result = rewriteLinkHref(href);
const result = rewriteHrefDomainOnStage(href, domainMappings);

expect(result).toBe('https://io-partners-dx.stage.adobe.com/path');
});

test('should return unchanged link hrefs if invalid', () => {
const href = 'invalid-url';
const result = rewriteLinkHref(href);
const result = rewriteHrefDomainOnStage(href, domainMappings);

expect(result).toBe(href);
});

test('should return unchanged link hrefs if domain is not mapped', () => {
const href = 'https://unmapped-domain.com/path';
const result = rewriteLinkHref(href);
const result = rewriteHrefDomainOnStage(href, domainMappings);

expect(result).toBe(href);
});
Expand Down
Loading

0 comments on commit 1f52410

Please sign in to comment.