Skip to content

Commit

Permalink
Fetch current article from index. Add breadcrumb, datePublished, and …
Browse files Browse the repository at this point in the history
…dateModified to ldjson. Try to make eslint and prettier play nice.
  • Loading branch information
codecodeio committed Jun 11, 2024
1 parent ede56ea commit e0ea67b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
10 changes: 7 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"rules": {
"prettier/prettier": ["error"]
}
}
}
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 800
}
}
42 changes: 28 additions & 14 deletions best-cigars-guide/blocks/footer/footer.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -111,6 +123,8 @@ function buildLdJson(container) {
};
}

// console.log(ldJson);

addLdJsonScript(container, ldJson);
}

Expand Down
20 changes: 19 additions & 1 deletion best-cigars-guide/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -59,6 +59,24 @@ export async function fetchArticleList() {
return articleIndexData;
}

/**
* Fetches article information.
* @returns {Promise<Array<Object>>} - 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
Expand Down

0 comments on commit e0ea67b

Please sign in to comment.