Skip to content

Commit

Permalink
fix: update logic to detect inline els
Browse files Browse the repository at this point in the history
  • Loading branch information
buuhuu committed Feb 21, 2024
1 parent 79e40c0 commit 0d6e559
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,14 +622,16 @@ function decorateBlock(block) {
blockWrapper.classList.add(`${shortBlockName}-wrapper`);
const section = block.closest('.section');
if (section) section.classList.add(`${shortBlockName}-container`);
// wrap plain text and inline elements in a <p>
// wrap plain text and non-block elements in a <p> or <pre>
block.querySelectorAll(':scope > div > div').forEach((cell) => {
const firstChild = cell.firstElementChild;
if ((!firstChild && cell.textContent.trim())
|| (firstChild && ['SPAN', 'STRONG', 'EM', 'A', 'PICTURE'].includes(firstChild.tagName))) {
const p = document.createElement('p');
p.append(...cell.childNodes);
cell.replaceChildren(p);
const cellText = cell.textContent.trim();
if ((!firstChild && cellText)
|| (firstChild && !firstChild.tagName.match(/^(P(RE)?|H[1-6]|(U|O)L|TABLE)$/))) {
const tag = firstChild.tagName === 'CODE' && cellText === firstChild.textContent.trim() ? 'pre' : 'p';
const paragraph = document.createElement(tag);
paragraph.append(...cell.childNodes);
cell.replaceChildren(paragraph);
}
});
}
Expand Down

0 comments on commit 0d6e559

Please sign in to comment.