Skip to content

Commit

Permalink
check for /best-cigars-guide in path. (#62)
Browse files Browse the repository at this point in the history
* check for /best-cigars-guide in path.

* Format hero picture path.

* Spelling mistake.
  • Loading branch information
codecodeio authored Jun 18, 2024
1 parent 16887cd commit f9b748b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
15 changes: 13 additions & 2 deletions best-cigars-guide/blocks/article-list/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,19 @@ export default function decorate(block) {
}
});

// optimize images
ul.querySelectorAll('img').forEach((img) => img.closest('picture').replaceWith(createOptimizedPicture(img.src, img.alt, false, [{ width: '750' }])));
// optimize and format images
ul.querySelectorAll('img').forEach((img) => {
// Create a new URL object
const url = new URL(img.src, window.location.origin);

// Check if the pathname begins with "/best-cigars-guide"
if (!url.pathname.startsWith('/best-cigars-guide')) {
// Prepend "/best-cigars-guide" to the pathname
url.pathname = `/best-cigars-guide${url.pathname}`;
}

img.closest('picture').replaceWith(createOptimizedPicture(url.href, img.alt, false, [{ width: '750' }]));
});

// Remove any empty div tags
[...ul.querySelectorAll('div')].forEach((div) => {
Expand Down
23 changes: 23 additions & 0 deletions best-cigars-guide/blocks/hero/hero.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ function addSecondParagraphToHero() {
}
}

// Format picture path
function formatPicturePath() {
const picture = document.querySelector('.hero picture');
if (picture) {
picture.querySelectorAll('img, source').forEach((element) => {
const url = new URL(element.src || element.srcset, window.location.origin);

// Check if the pathname begins with "/best-cigars-guide"
if (!url.pathname.startsWith('/best-cigars-guide')) {
// Prepend "/best-cigars-guide" to the pathname
url.pathname = `/best-cigars-guide${url.pathname}`;
}

if (element.tagName === 'IMG') {
element.src = url.href;
} else if (element.tagName === 'SOURCE') {
element.srcset = url.href;
}
});
}
}

export default function decorate() {
addSecondParagraphToHero();
formatPicturePath();
}

0 comments on commit f9b748b

Please sign in to comment.