diff --git a/blocks/footer/footer.js b/blocks/footer/footer.js index 66f89b3c..766b1280 100644 --- a/blocks/footer/footer.js +++ b/blocks/footer/footer.js @@ -3,11 +3,11 @@ import { decorateIcons, decorateLinkedPictures, createOptimizedPicture, - lookupPages, wrapImgsInLinks, - fetchPlaceholders, } from '../../scripts/scripts.js'; +import { setupSponsors } from '../sponsors/sponsors.js'; + function setupCookieChoices(section) { const cookieLink = section.querySelector('a[href*="onetrust-link"]'); if (cookieLink) { @@ -26,27 +26,23 @@ function setupSocialButtons(section) { } async function setupPartners(section) { - const pages = await lookupPages(); - const { sponsorOrder } = await fetchPlaceholders(); - const sponsors = pages.filter((e) => e.path.startsWith('/sponsors/')); - const orderedSponsors = []; - if (sponsorOrder) { - sponsorOrder.split(',').forEach((sp) => { - // eslint-disable-next-line no-param-reassign - sp = sp.trim(); - const match = sponsors.find((sponsor) => sponsor.title === sp); - if (match) { - // remove match from sponsors - sponsors.splice(sponsors.indexOf(match), 1); - // add match to ordered sponsors - orderedSponsors.push(match); - } - }); + let sponsorLinks = []; + try { + const resp = await fetch('/sponsors'); + // eslint-disable-next-line no-await-in-loop + const html = await resp.text(); + const dp = new DOMParser(); + const sponsorsDoc = dp.parseFromString(html, 'text/html'); + sponsorLinks = [...sponsorsDoc.querySelectorAll('.sponsors a')].map((a) => a.href); + } catch (err) { + // eslint-disable-next-line no-console + console.log(err); } + const sponsors = await setupSponsors(sponsorLinks); const wrapper = document.createElement('div'); // combine ordered sponsors with any remaining unordered sponsors - [...orderedSponsors, ...sponsors].forEach((sponsor) => { + sponsors.forEach((sponsor) => { const partner = document.createElement('div'); partner.className = 'footer-partner'; const link = document.createElement('a'); diff --git a/blocks/header/header.js b/blocks/header/header.js index cf2890d8..95d21c5b 100644 --- a/blocks/header/header.js +++ b/blocks/header/header.js @@ -2,12 +2,13 @@ import { readBlockConfig, fetchPlaceholders, decorateIcons, - lookupPages, createOptimizedPicture, wrapImgsInLinks, decorateLinkedPictures, } from '../../scripts/scripts.js'; +import { setupSponsors } from '../sponsors/sponsors.js'; + /** * collapses all open nav sections * @param {Element} sections The container element @@ -26,8 +27,19 @@ function displayNextPartner(proud) { } async function setupPartners(section) { - const pages = await lookupPages(); - const sponsors = pages.filter((e) => e.path.startsWith('/sponsors/')); + let sponsorLinks = []; + try { + const resp = await fetch('/sponsors'); + // eslint-disable-next-line no-await-in-loop + const html = await resp.text(); + const dp = new DOMParser(); + const sponsorsDoc = dp.parseFromString(html, 'text/html'); + sponsorLinks = [...sponsorsDoc.querySelectorAll('.sponsors a')].map((a) => a.href); + } catch (err) { + // eslint-disable-next-line no-console + console.log(err); + } + const sponsors = await setupSponsors(sponsorLinks); if (sponsors.length > 0) { const hasWhiteBg = [...section.classList].includes('white'); diff --git a/blocks/sponsors/sponsors.js b/blocks/sponsors/sponsors.js index b3af9c38..5e34413d 100644 --- a/blocks/sponsors/sponsors.js +++ b/blocks/sponsors/sponsors.js @@ -5,28 +5,58 @@ import { toClassName, } from '../../scripts/scripts.js'; +export async function setupSponsors(sponsorLinks = []) { + let sponsors = []; + let orderedSponsors = []; + + if (sponsorLinks.length) { + for (let i = 0; i < sponsorLinks.length; i += 1) { + const sponsorLink = sponsorLinks[i]; + // eslint-disable-next-line no-await-in-loop + const resp = await fetch(sponsorLink); + // eslint-disable-next-line no-await-in-loop + const html = await resp.text(); + const dp = new DOMParser(); + const sponsorDoc = dp.parseFromString(html, 'text/html'); + const title = sponsorDoc.querySelector('title').textContent; + const image = sponsorDoc.querySelector('meta[property="og:image"]').content; + const description = sponsorDoc.querySelector('meta[property="og:description"]').content; + const link = sponsorDoc.querySelector('meta[name="external-link"]').content; + const logoWhite = sponsorDoc.querySelector('meta[name="logo-white"]').content; + + sponsors.push({ + title, image, description, link, logoWhite, + }); + } + } else { + const pages = await lookupPages(); + const { sponsorOrder } = await fetchPlaceholders(); + sponsors = pages.filter((e) => e.path.startsWith('/sponsors/')); + orderedSponsors = []; + if (sponsorOrder) { + sponsorOrder.split(',').forEach((sp) => { + // eslint-disable-next-line no-param-reassign + sp = sp.trim(); + const match = sponsors.find((sponsor) => sponsor.title === sp); + if (match) { + // remove match from sponsors + sponsors.splice(sponsors.indexOf(match), 1); + // add match to ordered sponsors + orderedSponsors.push(match); + } + }); + } + } + return ([...orderedSponsors, ...sponsors]); +} + export default async function decorate(block) { + const sponsorLinks = [...block.querySelectorAll('a')].map((a) => a.href); + const sponsors = await setupSponsors(sponsorLinks); block.textContent = ''; - const pages = await lookupPages(); - const { sponsorOrder } = await fetchPlaceholders(); - const sponsors = pages.filter((e) => e.path.startsWith('/sponsors/')); - const orderedSponsors = []; - if (sponsorOrder) { - sponsorOrder.split(',').forEach((sp) => { - // eslint-disable-next-line no-param-reassign - sp = sp.trim(); - const match = sponsors.find((sponsor) => sponsor.title === sp); - if (match) { - // remove match from sponsors - sponsors.splice(sponsors.indexOf(match), 1); - // add match to ordered sponsors - orderedSponsors.push(match); - } - }); - } // combine ordered sponsors with any remaining unordered sponsors - [...orderedSponsors, ...sponsors].forEach((sponsor) => { + sponsors.forEach((sponsor) => { const card = document.createElement('div'); card.className = 'sponsors-sponsor'; const wrapper = document.createElement('div');