Skip to content

Commit

Permalink
[MWPW-161098] Links conversion localization support (#3120)
Browse files Browse the repository at this point in the history
* links conversion localization support

* added unit tests
  • Loading branch information
robert-bogos authored Nov 7, 2024
1 parent fe3acdb commit 948a940
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion libs/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import locales from '../utils/locales.js';

// Production Domain
const prodDomains = ['milo.adobe.com'];
const prodDomains = ['milo.adobe.com', 'business.adobe.com', 'www.adobe.com'];

const stageDomainsMap = {
'www.stage.adobe.com': {
Expand Down
14 changes: 10 additions & 4 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,21 +646,27 @@ const decorateCopyLink = (a, evt) => {
};

export function convertStageLinks({ anchors, config, hostname, href }) {
if (config.env?.name === 'prod' || !config.stageDomainsMap) return;
const matchedRules = Object.entries(config.stageDomainsMap)
const { env, stageDomainsMap, locale } = config;
if (env?.name === 'prod' || !stageDomainsMap) return;
const matchedRules = Object.entries(stageDomainsMap)
.find(([domain]) => (new RegExp(domain)).test(href));
if (!matchedRules) return;
const [, domainsMap] = matchedRules;
[...anchors].forEach((a) => {
const hasLocalePrefix = a.pathname.startsWith(locale.prefix);
const noLocaleLink = hasLocalePrefix ? a.href.replace(locale.prefix, '') : a.href;
const matchedDomain = Object.keys(domainsMap)
.find((domain) => (new RegExp(domain)).test(a.href));
.find((domain) => (new RegExp(domain)).test(noLocaleLink));
if (!matchedDomain) return;
a.href = a.href.replace(
const convertedLink = noLocaleLink.replace(
new RegExp(matchedDomain),
domainsMap[matchedDomain] === 'origin'
? `${matchedDomain.includes('https') ? 'https://' : ''}${hostname}`
: domainsMap[matchedDomain],
);
const convertedUrl = new URL(convertedLink);
convertedUrl.pathname = `${hasLocalePrefix ? locale.prefix : ''}${convertedUrl.pathname}`;
a.href = convertedUrl.toString();
if (/(\.page|\.live).*\.html(?=[?#]|$)/.test(a.href)) a.href = a.href.replace(/\.html(?=[?#]|$)/, '');
});
}
Expand Down
12 changes: 10 additions & 2 deletions test/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,16 +537,18 @@ describe('Utils', () => {
it('should convert links when stageDomainsMap provided without regex', async () => {
const stageConfig = {
...config,
locale: { prefix: '/ae_ar' },
env: { name: 'stage' },
stageDomainsMap,
};

Object.entries(stageDomainsMap).forEach(([hostname, domainsMap]) => {
const anchors = Object.keys(domainsMap).map((d) => utils.createTag('a', { href: `https://${d}` }));
const localizedAnchors = Object.keys(domainsMap).map((d) => utils.createTag('a', { href: `https://${d}/ae_ar` }));
const externalAnchors = externalDomains.map((url) => utils.createTag('a', { href: url }));

utils.convertStageLinks({
anchors: [...anchors, ...externalAnchors],
anchors: [...anchors, ...localizedAnchors, ...externalAnchors],
config: stageConfig,
hostname,
href: `https://${hostname}`,
Expand All @@ -565,16 +567,22 @@ describe('Utils', () => {
const { hostname, map } = stageDomainsMapWRegex;
const stageConfigWRegex = {
...config,
locale: { prefix: '/de' },
env: { name: 'stage' },
stageDomainsMap: map,
};

Object.entries(map).forEach(([, domainsMap]) => {
const anchors = Object.keys(domainsMap).map((d) => utils.createTag('a', { href: d.replace('^', '') }));
const localizedAnchors = Object.keys(domainsMap).map((d) => {
const convertedUrl = new URL(d.replace('^', ''));
convertedUrl.pathname = `de/${convertedUrl.pathname}`;
return utils.createTag('a', { href: convertedUrl.toString() });
});
const externalAnchors = externalDomains.map((url) => utils.createTag('a', { href: url }));

utils.convertStageLinks({
anchors: [...anchors, ...externalAnchors],
anchors: [...anchors, ...localizedAnchors, ...externalAnchors],
config: stageConfigWRegex,
hostname,
href: `https://${hostname}`,
Expand Down

0 comments on commit 948a940

Please sign in to comment.