Skip to content

Commit

Permalink
Support multiple sections
Browse files Browse the repository at this point in the history
  • Loading branch information
herzog31 committed Mar 1, 2024
1 parent 94e6337 commit cc71160
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions blocks/enrichment/enrichment.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ export default async function decorate(block) {
(await Promise.all(matchingFragments.map((path) => loadFragment(path))))
.filter((fragment) => fragment)
.forEach((fragment) => {
const section = fragment.querySelector(':scope .section');
if (section) {
block.closest('.section').classList.add(...section.classList);
const sections = fragment.querySelectorAll(':scope .section');

// If only single section, replace enrichment block with content of section
if (sections.length === 1) {
block.closest('.section').classList.add(...sections[0].classList);
const wrapper = block.closest('.enrichment-wrapper');
Array.from(section.children)
Array.from(sections[0].children)
.forEach((child) => wrapper.parentNode.insertBefore(child, wrapper));
} else if (sections.length > 1) {
// If multiple section, append them to current section
const blockSection = block.closest('.section');
Array.from(sections)
.reverse()
.forEach((section) => blockSection
.parentNode.insertBefore(section, blockSection.nextSibling));
}
});

Expand Down

0 comments on commit cc71160

Please sign in to comment.