generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
192 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* intentionally empty */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { fetchAuthorInfo, fetchCategoryInfo, fetchPostsInfo } from '../../scripts/scripts.js'; | ||
import { getMetadata } from '../../scripts/aem.js'; | ||
import { addLdJsonScript } from '../../scripts/linking-data.js'; | ||
|
||
export default async function decorate(block) { | ||
block.parentElement.remove(); | ||
|
||
const promises = []; | ||
promises.push(fetchPostsInfo(window.location.pathname)); | ||
promises.push(fetchAuthorInfo(getMetadata('author'))); | ||
promises.push(fetchCategoryInfo(getMetadata('category'))); | ||
|
||
let article; | ||
let author; | ||
let category; | ||
|
||
await Promise.all(promises).then((results) => { | ||
[[article], author, category] = results; | ||
}); | ||
if (!article || !author || !category) { | ||
return; | ||
} | ||
|
||
const ldjson = { | ||
'@context': 'https://schema.org/', | ||
'@type': 'BlogPosting', | ||
mainEntityOfPage: { | ||
'@type': 'WebPage', | ||
'@id': window.location.href, | ||
}, | ||
url: window.location.href, | ||
headline: article.heading, | ||
datePublished: new Date(article.published * 1000).toISOString(), | ||
dateModified: new Date(article.lastModified * 1000).toISOString(), | ||
publisher: { | ||
'@type': 'Organization', | ||
'@id': 'https://www.famous-smoke.com/cigaradvisor#organization', | ||
name: 'Cigar Advisor', | ||
logo: { | ||
'@type': 'ImageObject', | ||
url: 'https://www.famous-smoke.com/cigaradvisor/styles/images/CALogo_512x512.png', | ||
}, | ||
}, | ||
image: { | ||
'@type': 'ImageObject', | ||
url: 'https://www.famous-smoke.com/cigaradvisor/wp-content/uploads/2019/10/cigar-advisor-asylum-essential-review-guide-cover.jpeg', | ||
}, | ||
articleSection: category.heading, | ||
description: article.description, | ||
author: { | ||
'@type': 'Person', | ||
name: author.name, | ||
url: `https://www.famous-smoke.com${author.path}`, | ||
description: author.intro, | ||
image: { | ||
'@type': 'ImageObject', | ||
url: author.image, | ||
}, | ||
}, | ||
}; | ||
addLdJsonScript(document.querySelector('head'), ldjson); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,25 @@ | ||
import { fetchCategoryInfo, fetchAuthorInfo, decorateSeoPicture } from '../../scripts/scripts.js'; | ||
import { | ||
a, div, h1, section, | ||
} from '../../scripts/dom-helpers.js'; | ||
import { fetchCategoryInfo, fetchAuthorInfo } from '../../scripts/scripts.js'; | ||
import { readBlockConfig } from '../../scripts/aem.js'; | ||
|
||
export default async function decorate(block) { | ||
const section = document.createElement('section'); | ||
const imageWrapper = document.createElement('div'); | ||
imageWrapper.classList.add('image-wrapper'); | ||
const config = readBlockConfig(block); | ||
|
||
const picture = block.querySelector('picture'); | ||
decorateSeoPicture(picture, window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1)); | ||
imageWrapper.append(picture); | ||
const articleInfo = document.createElement('div'); | ||
articleInfo.classList.add('article-info'); | ||
const categoryLink = block.querySelector('p.category').innerText; | ||
const category = await fetchCategoryInfo(categoryLink); | ||
if (category) { | ||
const categoryLinkEl = document.createElement('div'); | ||
categoryLinkEl.classList.add('article-category'); | ||
categoryLinkEl.innerHTML = `<a href="${categoryLink}">${category.heading}</a>`; | ||
articleInfo.append(categoryLinkEl); | ||
} | ||
articleInfo.append(block.querySelector('h1')); | ||
const authorLink = block.querySelector('p.author').innerText; | ||
const author = await fetchAuthorInfo(authorLink); | ||
const authorLinkEl = document.createElement('div'); | ||
authorLinkEl.classList.add('article-author'); | ||
if (author) { | ||
authorLinkEl.innerHTML = `<a href="${authorLink}">By ${author.name}</a>`; | ||
articleInfo.append(authorLinkEl); | ||
} | ||
articleInfo.append(authorLinkEl); | ||
section.append(imageWrapper); | ||
section.append(articleInfo); | ||
block.replaceChildren(section); | ||
|
||
const category = await fetchCategoryInfo(config.category); | ||
const author = await fetchAuthorInfo(config.author); | ||
const sect = section( | ||
{}, | ||
div({ class: 'image-wrapper' }, picture), | ||
div( | ||
{ class: 'article-info' }, | ||
div({ class: 'article-category' }, a({ href: config.category }, category.heading)), | ||
h1(config.heading), | ||
div({ class: 'article-author' }, a({ href: config.author }, author.name)), | ||
), | ||
); | ||
block.replaceChildren(sect); | ||
} |
10 changes: 10 additions & 0 deletions
10
cigaradvisor/blocks/dynamic-article-list/dynamic-article-list.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@import url('../article-list/article-list.css'); | ||
@import url('../article-teaser/article-teaser.css'); | ||
|
||
.dynamic-article-list h3 { | ||
font-family: var(--ff-opensans); | ||
font-weight: var(--font-weight-bold); | ||
font-size: var(--heading-font-size-xxs); | ||
line-height: var(--line-height-m); | ||
margin: 2rem 0 1rem; | ||
} |
41 changes: 41 additions & 0 deletions
41
cigaradvisor/blocks/dynamic-article-list/dynamic-article-list.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { loadPosts } from '../../scripts/scripts.js'; | ||
import { readBlockConfig } from '../../scripts/aem.js'; | ||
import { h3 } from '../../scripts/dom-helpers.js'; | ||
import { renderPage } from '../article-list/article-list.js'; | ||
|
||
async function loadMustReadPosts() { | ||
const resp = await fetch('/cigaradvisor/must-reads.plain.html'); | ||
if (resp.ok) { | ||
const dom = document.createElement('main'); | ||
dom.innerHTML = await resp.text(); | ||
const mustReadArticleList = dom.querySelectorAll('.article-list li a'); | ||
return Array.from(mustReadArticleList).map((a) => a.getAttribute('href')); | ||
} | ||
return []; | ||
} | ||
|
||
export default async function decorate(block) { | ||
const config = readBlockConfig(block); | ||
// Build Article's Custom List Article Block | ||
const { author, category } = config; | ||
|
||
const posts = await loadPosts(); | ||
const articles = []; | ||
|
||
// 3 posts from the same author and category “most recent by published date” | ||
articles.push(...posts.filter((post) => author.includes(post.author) && post.path !== window.location.pathname).slice(0, 2)); | ||
articles.push(...posts.filter((post) => category.includes(post.category) && !articles.includes(post) && post.path !== window.location.pathname).slice(0, 3 - articles.length)); | ||
|
||
// 1 random post from must-reads page | ||
const mustReadPosts = await loadMustReadPosts(); | ||
const mustReadCandidatePosts = posts.filter((post) => mustReadPosts.includes(post.path) && !articles.includes(post) && post.path !== window.location.pathname); | ||
const randomIndex = Math.floor(Math.random() * mustReadCandidatePosts.length); | ||
articles.push(...mustReadCandidatePosts.slice(randomIndex, randomIndex + 1)); | ||
|
||
const wrapper = document.createElement('div'); | ||
wrapper.classList.add('article-teaser-wrapper'); | ||
|
||
await renderPage(wrapper, articles); | ||
block.replaceChildren(h3('You Might Also Like...'), wrapper); | ||
block.classList.add('article-list'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.