Skip to content

Commit

Permalink
chore: switch hero autoblock for fragment autoblocking
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnuescheler committed Jan 24, 2024
1 parent 569c3e4 commit ee02fdb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
37 changes: 21 additions & 16 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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');

Check warning on line 36 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
container.querySelectorAll('a[href*="/fragments/"]:only-child').forEach((a) => {
const parent = a.parentNode;
const fragment =buildBlock('fragment', [[ a.cloneNode(true) ]])

Check failure on line 39 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

Operator '=' must be spaced

Check failure on line 39 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

There should be no space after '['

Check failure on line 39 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

There should be no space before ']'

Check failure on line 39 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed

Check failure on line 39 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
if (parent.tagName === 'P') {
parent.before(fragment);
parent.remove();

Check failure on line 42 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
} 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);
Expand Down

0 comments on commit ee02fdb

Please sign in to comment.