Skip to content

Commit

Permalink
Article-list block based on Cards block. PNG image based on favicon f…
Browse files Browse the repository at this point in the history
…or meta image. Prelim import-category.js file
  • Loading branch information
codecodeio committed May 9, 2024
1 parent 84c1ab3 commit 4f18a8e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
31 changes: 31 additions & 0 deletions best-cigars-guide/blocks/article-list/article-list.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.cards > ul {
list-style: none;
margin: 0;
padding: 0;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(278px, 1fr));
grid-gap: 16px;
}

.cards > ul > li {
border: 1px solid var(--dark-color);
background-color: var(--background-color)
}

.cards .cards-card-body {
margin: 16px;
}

.cards .cards-card-image {
line-height: 0;
}

.cards .cards-card-body > *:first-child {
margin-top: 0;
}

.cards > ul > li img {
width: 100%;
aspect-ratio: 4 / 3;
object-fit: cover;
}
18 changes: 18 additions & 0 deletions best-cigars-guide/blocks/article-list/article-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createOptimizedPicture } from '../../scripts/aem.js';

export default function decorate(block) {
/* change to ul, li */
const ul = document.createElement('ul');
[...block.children].forEach((row) => {
const li = document.createElement('li');
while (row.firstElementChild) li.append(row.firstElementChild);
[...li.children].forEach((div) => {
if (div.children.length === 1 && div.querySelector('picture')) div.className = 'cards-card-image';
else div.className = 'cards-card-body';
});
ul.append(li);
});
ul.querySelectorAll('img').forEach((img) => img.closest('picture').replaceWith(createOptimizedPicture(img.src, img.alt, false, [{ width: '750' }])));
block.textContent = '';
block.append(ul);
}
Binary file added best-cigars-guide/icons/best-cigars-guide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions tools/importer/import-category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const createMetadataBlock = (main, document) => {
const meta = {};

// find the <title> element
const title = document.querySelector("title");

Check failure on line 5 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
if (title) {
meta.Title = title.innerHTML.replace(/[\n\t]/gm, "");

Check failure on line 7 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
}

// find the <meta property="og:description"> element
const desc = document.querySelector('[property="og:description"]');
if (desc) {
meta.Description = desc.content;
}

// set the <meta property="og:image"> element
meta.image =

Check failure on line 17 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

There should be no line break before or after '='
"/best-cigars-guide/icons/best-cigars-guide.png";

Check failure on line 18 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote

// helper to create the metadata block
const block = WebImporter.Blocks.getMetadataBlock(document, meta);

Check failure on line 21 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

'WebImporter' is not defined

// append the block to the main element
main.append(block);

// returning the meta object might be usefull to other rules
return meta;
};

const createArticleListBlock = (main, document) => {
var categories = [["Article-list"]];

Check failure on line 31 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

Unexpected var, use let or const instead

Check failure on line 31 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
var categoriesList = document.querySelector(".categories-list");

Check failure on line 32 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

Unexpected var, use let or const instead

Check failure on line 32 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
var divElements = categoriesList.querySelectorAll("div");

Check failure on line 33 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

Unexpected var, use let or const instead

divElements.forEach(function (element) {

Check warning on line 35 in tools/importer/import-category.js

View workflow job for this annotation

GitHub Actions / build

Unexpected unnamed function
var title = element.querySelector("h3").textContent.trim();
var description = element.querySelector("p").textContent.trim();
var image = element.querySelector("img");
var imageSrc = image ? image.src : "";
var linkElement = element.querySelector("a.button");
var link = linkElement ? linkElement.href : "";

var card = [
[image, link],
[title, description, linkElement],
];

categories.push(card);
});

const articleList = WebImporter.DOMUtils.createTable(categories, document);

main.append(articleList);

// remove .categories-list from main because we just added it manually
WebImporter.DOMUtils.remove(main, [".categories-list"]);

return articleList;
};

const removeSectionsNotForImport = (main, document) => {
// remove any section from main not needed for import
WebImporter.DOMUtils.remove(main, [".category-dropdown"]);
};

export default {
transformDOM: ({ document }) => {
const main = document.querySelector("main");

createMetadataBlock(main, document);
createArticleListBlock(main, document);
removeSectionsNotForImport(main, document);

return main;
},
};

0 comments on commit 4f18a8e

Please sign in to comment.