diff --git a/cigaradvisor/blocks/article-navigation/article-navigation.css b/cigaradvisor/blocks/article-navigation/article-navigation.css new file mode 100644 index 00000000..8bfcdb03 --- /dev/null +++ b/cigaradvisor/blocks/article-navigation/article-navigation.css @@ -0,0 +1,76 @@ +.article-navigation.block { + display: flex; + flex-wrap: wrap; + justify-content: space-between; + max-width: 1080px; + margin: 30px auto 0; + align-items: stretch; +} + +.article-navigation.block>div { + padding: 10px; + width: 100%; +} + +.article-navigation.block a { + background-color: var(--tan); + padding: 20px 10px; + border: 1px solid var(--tan); + display: block; + width: 100%; + height: 100%; +} + +.article-navigation.block h3 { + line-height: 35px; + font-size: var(--body-font-size-l); + font-weight: var(--font-weight-bold); + font-family: montserrat, sans-serif; +} + +.article-navigation.block span { + line-height: 35px; + font-size: var(--body-font-size-l); + font-weight: var(--font-weight-bold); + font-family: montserrat, sans-serif; + display: inline-block; + color: var(--tan); +} + +.article-navigation.block .previous-article-nav a { + text-align: center; +} + +.article-navigation.block .next-article-nav a { + text-align: center; +} + +.article-navigation.block .previous-article-nav h3::before { + content: ''; + font-family: var(--ff-fontawesome); + margin-right: 10px; + font-size: var(--body-font-size-s); + ; +} + +.article-navigation.block .next-article-nav h3::after { + content: ''; + font-family: var(--ff-fontawesome); + margin-left: 10px; + font-size: var(--body-font-size-s); +} + + +@media screen and (min-width: 600px) { + .article-navigation.block>div { + width: 50%; + } + + .article-navigation.block .previous-article-nav a { + text-align: left; + } + + .article-navigation.block .next-article-nav a { + text-align: right; + } +} \ No newline at end of file diff --git a/cigaradvisor/blocks/article-navigation/article-navigation.js b/cigaradvisor/blocks/article-navigation/article-navigation.js new file mode 100644 index 00000000..2ea0b83c --- /dev/null +++ b/cigaradvisor/blocks/article-navigation/article-navigation.js @@ -0,0 +1,50 @@ +import { getAllPosts } from '../../scripts/scripts.js'; + +function getFinalOrCurrentUrl(url) { + return fetch(url, { method: 'HEAD', redirect: 'follow' }) + .then((response) => { + if (response.ok) { + // Not redirected, use the current page URL + return url; + } if (response.redirected) { + // Redirected, get the final URL + return response.url; + } + // Handle other cases if needed + throw new Error('Unexpected response status'); + }); +} + +export default async function decorate(block) { + const allArticles = await getAllPosts(); + let previousArticle; + let nextArticle; + getFinalOrCurrentUrl(window.location.href) + .then((currentArticle) => { + allArticles.forEach((article, index) => { + if (currentArticle.indexOf(article.path) > -1) { + previousArticle = allArticles[index - 1]; + nextArticle = allArticles[index + 1]; + } + }); + const previousArticleNav = document.createElement('div'); + previousArticleNav.classList.add('previous-article-nav'); + const nextArticleNav = document.createElement('div'); + nextArticleNav.classList.add('next-article-nav'); + + if (previousArticle) { + previousArticleNav.innerHTML = ``; + } + if (nextArticle) { + nextArticleNav.innerHTML = ``; + } + + block.replaceChildren(previousArticleNav); + block.append(nextArticleNav); + }) + .catch((error) => console.log('Error:', error)); +} diff --git a/cigaradvisor/blocks/author-teaser/author-teaser.js b/cigaradvisor/blocks/author-teaser/author-teaser.js index 21408b15..15d1bf4a 100644 --- a/cigaradvisor/blocks/author-teaser/author-teaser.js +++ b/cigaradvisor/blocks/author-teaser/author-teaser.js @@ -64,6 +64,7 @@ export default async function decorate(block) { const link = block.querySelector('a'); const path = link ? link.getAttribute('href') : block.textContent.trim(); const author = await fetchAuthorInfo(path); + block.innerHTML = ''; if (author) { buildAuthorTeaser(block, author); } diff --git a/cigaradvisor/scripts/scripts.js b/cigaradvisor/scripts/scripts.js index a691d7c4..0b313223 100644 --- a/cigaradvisor/scripts/scripts.js +++ b/cigaradvisor/scripts/scripts.js @@ -229,6 +229,23 @@ export async function getPostByIdx(idx) { return undefined; } +/** + * Retrieves all posts and sorts them by published date. + * @param {boolean} sort - Indicates whether to sort the posts by published date in ascending order. + * @returns {Promise} - A promise that resolves to an array of post data. + */ +export async function getAllPosts(sort = true) { + await loadPosts(); + if (sort) { + articleIndexData.sort((a, b) => { + const dateA = new Date(a.published * 1000); + const dateB = new Date(b.published * 1000); + return dateA - dateB; + }); + } + return articleIndexData; +} + let authorIndexData; /** * Retrieves all authors from the server.