From e0ea67b04c644fa2388ec224ba4b7986f9312e07 Mon Sep 17 00:00:00 2001 From: Matthew Gold Date: Tue, 11 Jun 2024 10:52:29 -0400 Subject: [PATCH] Fetch current article from index. Add breadcrumb, datePublished, and dateModified to ldjson. Try to make eslint and prettier play nice. --- .eslintrc.js | 10 ++++-- .eslintrc.json | 2 +- .prettierrc.json | 2 +- best-cigars-guide/blocks/footer/footer.js | 42 +++++++++++++++-------- best-cigars-guide/scripts/scripts.js | 20 ++++++++++- 5 files changed, 56 insertions(+), 20 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 76f220d..067b749 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -14,8 +14,12 @@ module.exports = { // allow reassigning param 'no-param-reassign': [2, { props: false }], 'linebreak-style': ['error', 'unix'], - 'import/extensions': ['error', { - js: 'always', - }], + 'import/extensions': [ + 'error', + { + js: 'always', + }, + ], + 'object-curly-newline': 0, }, }; diff --git a/.eslintrc.json b/.eslintrc.json index 848c2d4..17ed29a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,4 +4,4 @@ "rules": { "prettier/prettier": ["error"] } -} \ No newline at end of file +} diff --git a/.prettierrc.json b/.prettierrc.json index f64cefc..f25d167 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -3,4 +3,4 @@ "singleQuote": true, "trailingComma": "es5", "printWidth": 800 -} \ No newline at end of file +} diff --git a/best-cigars-guide/blocks/footer/footer.js b/best-cigars-guide/blocks/footer/footer.js index a03acf3..6925731 100644 --- a/best-cigars-guide/blocks/footer/footer.js +++ b/best-cigars-guide/blocks/footer/footer.js @@ -1,23 +1,31 @@ import { getMetadata } from '../../scripts/aem.js'; import { loadFragment } from '../fragment/fragment.js'; -import { isInternal, isCategory, isArticlePage } from '../../scripts/scripts.js'; +import { isInternal, isCategory, isArticlePage, fetchArticleInfo } from '../../scripts/scripts.js'; import { addLdJsonScript } from '../../scripts/linking-data.js'; -function dateToISOString(dateStr) { - // Check if dateStr is a string - if (typeof dateStr !== 'string') { - return null; - } +function dateToISOString(input) { + let date; try { - // Parse the date string into a Date object - const date = new Date(dateStr); + // Check if the input is a number (Unix timestamp) + if (typeof input === 'number') { + date = new Date(input * 1000); + } else if (typeof input === 'string') { + // Check if the string is a Unix timestamp + if (/^\d+$/.test(input)) { + date = new Date(parseInt(input, 10) * 1000); + } else { + // Otherwise, assume it's a date string + date = new Date(input); + } + } else { + return null; // Return null if the input is neither a number nor a string + } // Check if the date is valid if (date.isNaN) { return null; } - // Convert the Date object to ISO string format return date.toISOString(); } catch (error) { @@ -64,7 +72,7 @@ function createBreadcrumbsSchema() { return breadcrumbsSchema; } -function buildLdJson(container) { +async function buildLdJson(container) { // Base page LD+JSON const ldJson = { '@context': 'https://schema.org', @@ -87,13 +95,17 @@ function buildLdJson(container) { // Add Article Page Data if (isArticlePage()) { // Add datePublished from metadata - const date = dateToISOString(getMetadata('publisheddate')); - if (date) { - ldJson.datePublished = date; + const datePublished = dateToISOString(getMetadata('publisheddate')); + if (datePublished) { + ldJson.datePublished = datePublished; } // Add dateModified - // Requires fetching the article from the index + const articleInfo = await fetchArticleInfo(); + if (articleInfo) { + const lastModified = dateToISOString(articleInfo.lastModified); + ldJson.dateModified = lastModified; + } } // Add breadcrumb when available @@ -111,6 +123,8 @@ function buildLdJson(container) { }; } + // console.log(ldJson); + addLdJsonScript(container, ldJson); } diff --git a/best-cigars-guide/scripts/scripts.js b/best-cigars-guide/scripts/scripts.js index cf4801d..eaab250 100644 --- a/best-cigars-guide/scripts/scripts.js +++ b/best-cigars-guide/scripts/scripts.js @@ -44,7 +44,7 @@ export async function fetchArticleList() { const resp = await fetch(ARTICLE_INDEX_PATH); if (resp.ok) { const jsonData = await resp.json(); - articleIndexData = jsonData.data.map((item) => ({ path: item.path, title: item.title })); + articleIndexData = jsonData.data.map((item) => ({ path: item.path, title: item.title, lastModified: item.lastModified })); } else { // eslint-disable-next-line no-console console.error('Failed to fetch category article list:', resp.status); @@ -59,6 +59,24 @@ export async function fetchArticleList() { return articleIndexData; } +/** + * Fetches article information. + * @returns {Promise>} - A promise that resolves to an array of article path objects. + */ +export async function fetchArticleInfo() { + // Fetch article list + if (!articleIndexData) { + articleIndexData = fetchArticleList(); + } + // Get the current URL path + const currentPath = window.location.pathname; + + // Find the article that matches the current URL path + const matchingArticle = articleIndexData.find((article) => article.path === currentPath); + + return matchingArticle || null; +} + /** * Builds hero block and prepends to main in a new section. * @param {Element} main The container element