Skip to content

Commit

Permalink
Merge branch 'main' into skms-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
amauch-adobe authored Aug 2, 2023
2 parents 01735ca + ae78906 commit b8b87ae
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
30 changes: 17 additions & 13 deletions libs/blocks/global-navigation/features/breadcrumbs/breadcrumbs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMetadata } from '../../../../utils/utils.js';
import { getMetadata, localizeLink } from '../../../../utils/utils.js';
import { toFragment, lanaLog } from '../../utilities/utilities.js';

const metadata = {
Expand Down Expand Up @@ -35,17 +35,10 @@ const setBreadcrumbSEO = (breadcrumbs) => {
document.head.append(script);
};

const getHiddenEntries = (str) => str
?.toLowerCase()
.split(',')
.map((item) => item.trim()) || [];

const removeHiddenEntries = ({ ul }) => {
const hidden = getHiddenEntries(getMetadata(metadata.hiddenEntries));
if (!hidden.length) return;
ul.querySelectorAll('li').forEach(
(li) => hidden.includes(li.innerText?.toLowerCase().trim()) && li.remove(),
);
const localizeEntries = (anchor) => {
if (!anchor) return;
const href = anchor.getAttribute('href');
anchor.setAttribute('href', localizeLink(href));
};

const createBreadcrumbs = (element) => {
Expand All @@ -59,7 +52,18 @@ const createBreadcrumbs = (element) => {
</li>
`);
}
removeHiddenEntries({ ul });

const hiddenEntries = getMetadata(metadata.hiddenEntries)
?.toLowerCase()
.split(',')
.map((item) => item.trim()) || [];

ul.querySelectorAll('li').forEach(
(li) => {
if (hiddenEntries.includes(li.innerText?.toLowerCase().trim())) return li.remove();
return localizeEntries(li.querySelector('a'));
},
);
const breadcrumbs = toFragment`
<div class="feds-breadcrumbs-wrapper">
<nav class="feds-breadcrumbs" aria-label="Breadcrumb">${ul}</nav>
Expand Down
48 changes: 46 additions & 2 deletions test/blocks/global-navigation/features/breadcrumbs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { stub } from 'sinon';
import breadcrumbs from '../../../../libs/blocks/global-navigation/features/breadcrumbs/breadcrumbs.js';
import { toFragment } from '../../../../libs/blocks/global-navigation/utilities/utilities.js';
import { mockRes } from '../test-utilities.js';
import { setConfig } from '../../../../libs/utils/utils.js';

export const breadcrumbMock = () => toFragment`
<div class="breadcrumbs">
<div>
<div>
<ul>
<li><a href="http://www.google.com/">1-from-page</a></li>
<li><a href="http://www.future-releases.com/">Future Releases</a></li>
<li><a href="http://localhost:2000/">Future Releases</a></li>
<li><a href="http://www.adobe.com/">Actors</a></li>
<li>Players</li>
</ul>
Expand Down Expand Up @@ -85,7 +86,7 @@ describe('breadcrumbs', () => {
'@type': 'ListItem',
position: 2,
name: 'Future Releases',
item: 'http://www.future-releases.com/',
item: 'http://localhost:2000/',
},
{
'@type': 'ListItem',
Expand Down Expand Up @@ -128,4 +129,47 @@ describe('breadcrumbs', () => {
.getAttribute('aria-current'),
).to.equal('page');
});

it('should localize breadcrumb links', async () => {
setConfig({
locales: {
'': { ietf: 'en-US' },
fi: { ietf: 'fi-FI' },
},
pathname: '/fi/',
prodDomains: 'http://localhost:2000',
});

await breadcrumbs(breadcrumbMock());
const script = document.querySelector('script');
expect(script).to.exist;
expect(script.type).to.equal('application/ld+json');
expect(JSON.parse(script.innerHTML)).to.deep.equal(
{
'@context': 'https://schema.org',
'@type': 'BreadcrumbList',
itemListElement: [
{
'@type': 'ListItem',
position: 1,
name: '1-from-page',
item: 'http://www.google.com/',
},
{
'@type': 'ListItem',
position: 2,
name: 'Future Releases',
item: 'http://localhost:2000/fi/', // <-- localised prod domain
},
{
'@type': 'ListItem',
position: 3,
name: 'Actors',
item: 'http://www.adobe.com/',
},
{ '@type': 'ListItem', position: 4, name: 'Players' },
],
},
);
});
});

0 comments on commit b8b87ae

Please sign in to comment.