Skip to content

Commit

Permalink
feat: update aem-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
buuhuu committed Apr 19, 2024
1 parent 9497608 commit 19fa8da
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/cleanup-on-create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ on:
create:
branches:
- main
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
permissions:
contents: write
actions: write
# only run if commit message is "Initial commit" on main branch
if: ${{ github.ref == 'refs/heads/main' && github.event.head_commit.message == 'Initial commit' }}
if: ${{ github.event_name == 'workflow_dispatch' || ( github.ref == 'refs/heads/main' && !(contains(github.event, 'head_commit') || github.event.head_commit.message == 'Initial commit' )) }}
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -42,7 +43,7 @@ jobs:
- name: Commit changes
run: |
git config --local user.email "[email protected]"
git config --local user.name "Helix Bot"
git config --local user.name "AEM Bot"
git add .
git commit -m "chore: cleanup repository template"
git push
75 changes: 54 additions & 21 deletions scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,58 @@ function decorateTemplateAndTheme() {
if (theme) addClasses(document.body, theme);
}

/**
* Wrap inline text content of block cells within a <p> tag.
* @param {Element} block the block element
*/
function wrapTextNodes(block) {
const validWrappers = [
'P',
'PRE',
'UL',
'OL',
'PICTURE',
'TABLE',
'H1',
'H2',
'H3',
'H4',
'H5',
'H6',
];

const wrap = (el) => {
const wrapper = document.createElement('p');
[...el.attributes]
// move the instrumentation from the cell to the new paragraph, also keep the class
// in case the content is a buttton and the cell the button-container
.filter(({ nodeName }) => nodeName === 'class'
|| nodeName.startsWith('data-aue')
|| nodeName.startsWith('data-richtext'))
.forEach(({ nodeName, nodeValue }) => {
wrapper.setAttribute(nodeName, nodeValue);
el.removeAttribute(nodeName);
});
wrapper.append(...el.childNodes);
el.append(wrapper);
};

block.querySelectorAll(':scope > div > div').forEach((blockColumn) => {
if (blockColumn.hasChildNodes()) {
const hasWrapper = !!blockColumn.firstElementChild
&& validWrappers.some((tagName) => blockColumn.firstElementChild.tagName === tagName);
if (!hasWrapper) {
wrap(blockColumn);
} else if (
blockColumn.firstElementChild.tagName === 'PICTURE'
&& (blockColumn.children.length > 1 || !!blockColumn.textContent.trim())
) {
wrap(blockColumn);
}
}
});
}

/**
* Decorates paragraphs containing a single link as buttons.
* @param {Element} element container element
Expand Down Expand Up @@ -640,31 +692,11 @@ function decorateBlock(block) {
block.classList.add('block');
block.dataset.blockName = shortBlockName;
block.dataset.blockStatus = 'initialized';
wrapTextNodes(block);
const blockWrapper = block.parentElement;
blockWrapper.classList.add(`${shortBlockName}-wrapper`);
const section = block.closest('.section');
if (section) section.classList.add(`${shortBlockName}-container`);
// wrap plain text and non-block elements in a <p> or <pre>
block.querySelectorAll(':scope > div > div').forEach((cell) => {
const firstChild = cell.firstElementChild;
const cellText = cell.textContent.trim();
if ((!firstChild && cellText)
|| (firstChild && !firstChild.tagName.match(/^(P(RE)?|H[1-6]|(U|O)L|TABLE)$/))) {
const paragraph = document.createElement('p');
[...cell.attributes]
// move the instrumentation from the cell to the new paragraph, also keep the class
// in case the content is a buttton and the cell the button-container
.filter(({ nodeName }) => nodeName === 'class'
|| nodeName.startsWith('data-aue')
|| nodeName.startsWith('data-richtext'))
.forEach(({ nodeName, nodeValue }) => {
paragraph.setAttribute(nodeName, nodeValue);
cell.removeAttribute(nodeName);
});
paragraph.append(...cell.childNodes);
cell.replaceChildren(paragraph);
}
});
// eslint-disable-next-line no-use-before-define
decorateButtons(block);
}
Expand Down Expand Up @@ -751,4 +783,5 @@ export {
toClassName,
updateSectionsStatus,
waitForLCP,
wrapTextNodes,
};

0 comments on commit 19fa8da

Please sign in to comment.