Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: switch hero autoblock for fragment autoblocking #327

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 20 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,32 @@ async function loadFonts() {
}
}

/**
* Builds fragment blocks in a container element.
* @param {Element} container The container element
*/

function buildFragmentBlocks(container) {
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);
Expand Down