From ee02fdb4c1164e7670cd8c3edf2335edea4ed6be Mon Sep 17 00:00:00 2001 From: David Nuescheler Date: Wed, 24 Jan 2024 15:28:06 -0700 Subject: [PATCH] chore: switch hero autoblock for fragment autoblocking --- scripts/aem.js | 2 +- scripts/scripts.js | 37 +++++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/scripts/aem.js b/scripts/aem.js index 99f56f5442..96434ecc3c 100644 --- a/scripts/aem.js +++ b/scripts/aem.js @@ -630,7 +630,7 @@ function decorateBlock(block) { * @param {Element} main The container element */ function decorateBlocks(main) { - main.querySelectorAll('div.section > div > div').forEach(decorateBlock); + main.querySelectorAll('div.section div[class]:not(.section)').forEach(decorateBlock); } /** diff --git a/scripts/scripts.js b/scripts/scripts.js index 0211c1dd34..69df240d07 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -15,21 +15,6 @@ import { const LCP_BLOCKS = []; // add your LCP blocks to the list -/** - * Builds hero block and prepends to main in a new section. - * @param {Element} main The container element - */ -function buildHeroBlock(main) { - const h1 = main.querySelector('h1'); - const picture = main.querySelector('picture'); - // eslint-disable-next-line no-bitwise - if (h1 && picture && (h1.compareDocumentPosition(picture) & Node.DOCUMENT_POSITION_PRECEDING)) { - const section = document.createElement('div'); - section.append(buildBlock('hero', { elems: [picture, h1] })); - main.prepend(section); - } -} - /** * load fonts.css and set a session storage flag */ @@ -42,13 +27,33 @@ async function loadFonts() { } } +/** + * Builds fragment blocks in a container element. + * @param {Element} container The container element + */ + +function buildFragmentBlocks(container) { + console.log('building blocks'); + container.querySelectorAll('a[href*="/fragments/"]:only-child').forEach((a) => { + const parent = a.parentNode; + const fragment =buildBlock('fragment', [[ a.cloneNode(true) ]]) + if (parent.tagName === 'P') { + parent.before(fragment); + parent.remove(); + } else { + a.before(fragment); + a.remove(); + } + }); +} + /** * Builds all synthetic blocks in a container element. * @param {Element} main The container element */ function buildAutoBlocks(main) { try { - buildHeroBlock(main); + buildFragmentBlocks(main); } catch (error) { // eslint-disable-next-line no-console console.error('Auto Blocking failed', error);