Skip to content

Commit

Permalink
Merge branch 'main' into 47-add-ldjson-data
Browse files Browse the repository at this point in the history
  • Loading branch information
codecodeio authored Jun 12, 2024
2 parents f65d4f1 + c440b8a commit fd38bb5
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 31 deletions.
1 change: 0 additions & 1 deletion best-cigars-guide/blocks/article-list/article-list.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
height: auto;
transition: transform 0.3s ease;
line-height: 0;
aspect-ratio: 4 / 3;
object-fit: cover;
}

Expand Down
6 changes: 1 addition & 5 deletions best-cigars-guide/blocks/nav/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ async function createCategoriesDropdown() {
categoriesList.forEach((category) => {
const option = document.createElement('option');
option.value = category.path;
option.textContent = category.path
.split('/')
.pop()
.replace(/-/g, ' ')
.replace(/\b\w/g, (char) => char.toUpperCase());
option.textContent = category.title.split('|')[0].trim();
const categoryPath = category.path.split('/').slice(0, 3).join('/');
if (categoryPath === currentCategoryPath) {
option.selected = true;
Expand Down
7 changes: 7 additions & 0 deletions best-cigars-guide/blocks/related/related.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.sidebar-related-articles a::before {
content: url('/best-cigars-guide/icons/external-link.svg');
display: inline-block;
width: 12px;
height: 12px;
padding-right: 0.5em;
}
33 changes: 33 additions & 0 deletions best-cigars-guide/blocks/related/related.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default async function decorate(block) {
const links = block.querySelectorAll(':scope > div');
if (!links || links.length === 0) {
return;
}

const wrap = document.createElement('div');
wrap.className = 'sidebar-related-articles';

const heading = document.createElement('h3');
heading.textContent = 'Related Articles';
wrap.append(heading);

const list = document.createElement('ul');
// eslint-disable-next-line no-restricted-syntax
for (const link of links) {
const el = document.createElement('li');

const linkEl = document.createElement('a');
linkEl.textContent = link.firstElementChild.textContent;
linkEl.href = link.lastElementChild.textContent;
linkEl.target = '_blank';
el.appendChild(linkEl);

list.appendChild(el);
}

wrap.append(list);

const sidebar = document.querySelector('.sidebar-wrapper > div.sidebar');
sidebar.append(wrap);
block.remove();
}
24 changes: 1 addition & 23 deletions best-cigars-guide/blocks/sidebar/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,11 @@ import { fetchCategoryList, fetchArticleList } from '../../scripts/scripts.js';
// get the current category name
const currentCategoryPath = window.location.pathname.split('/').slice(0, 3).join('/');

async function getRelatedArticles() {
const wrap = document.createElement('div');
wrap.className = 'sidebar-related-articles';

const heading = document.createElement('h3');
heading.textContent = 'Related Articles';
wrap.append(heading);

const list = document.createElement('ul');

// todo: related article magic 🪄
list.innerHTML = '<li><a href="/best-cigars-guide">Dummy list item</a></li><li><a href="/best-cigars-guide">Dummy list item 2</a></li>';

wrap.append(list);

return wrap;
}

async function getCategoryArticles() {
const wrap = document.createElement('div');
wrap.className = 'sidebar-categories';

const heading = document.createElement('h3');
heading.textContent = 'CATEGORY';
wrap.append(heading);

const categoriesList = await fetchCategoryList();
Expand Down Expand Up @@ -69,10 +50,7 @@ async function getCategoryArticles() {

export default async function decorate(block) {
const categories = await getCategoryArticles();
block.append(categories);

const relatedArticles = await getRelatedArticles();
block.append(relatedArticles);
block.prepend(categories);

return block;
}
1 change: 1 addition & 0 deletions best-cigars-guide/icons/external-link.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion best-cigars-guide/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function fetchCategoryList() {
const resp = await fetch(CATEGORY_INDEX_PATH);
if (resp.ok) {
const jsonData = await resp.json();
categoryIndexData = jsonData.data.map((item) => ({ path: item.path }));
categoryIndexData = jsonData.data.map((item) => ({ path: item.path, title: item.title }));
} else {
// eslint-disable-next-line no-console
console.error('Failed to fetch category list:', resp.status);
Expand Down
22 changes: 21 additions & 1 deletion tools/importer/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const createItemBlocks = (post, document) => {
const items = document.querySelectorAll('.cigars-listing .cigar');
if (!items || items.length === 0) {
// no blocks to add
return [];
return;
}

for (const item of items) {
Expand Down Expand Up @@ -93,11 +93,31 @@ const createItemBlocks = (post, document) => {
}
};

// Add the related post links
const createRelatedLinkBlock = (post, document) => {
const relatedLinks = document.querySelectorAll('.related li');

if (!relatedLinks || relatedLinks.length === 0) {
// no related links to add
return;
}

const cell = [];
cell.push(['Related']);
for (const link of relatedLinks) {
cell.push([link.textContent, link.querySelector('a').href.replace('http://localhost:3001', 'https://www.famous-smoke.com')]);
}

const block = WebImporter.DOMUtils.createTable(cell, document);
post.append(block);
}

export default {
transformDOM: ({ document }) => {
const post = document.querySelector('article.post');
createNavBlock(post, document);
createItemBlocks(post, document);
createRelatedLinkBlock(post, document);
createMetadataBlock(post, document);
return post;
},
Expand Down

0 comments on commit fd38bb5

Please sign in to comment.