Skip to content

Commit

Permalink
Add datePublished to article pages ldjson. Add breadcrumb ldjson.
Browse files Browse the repository at this point in the history
  • Loading branch information
codecodeio committed Jun 10, 2024
1 parent 1b37a1a commit ede56ea
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
81 changes: 80 additions & 1 deletion best-cigars-guide/blocks/footer/footer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,69 @@
import { getMetadata } from '../../scripts/aem.js';
import { loadFragment } from '../fragment/fragment.js';
import { isInternal, isCategory } from '../../scripts/scripts.js';
import { isInternal, isCategory, isArticlePage } 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;
}

try {
// Parse the date string into a Date object
const date = new Date(dateStr);

// Check if the date is valid
if (date.isNaN) {
return null;
}

// Convert the Date object to ISO string format
return date.toISOString();
} catch (error) {
// Return null if there is an error
return null;
}
}

function createBreadcrumbsSchema() {
const breadcrumbDiv = document.querySelector('.breadcrumb #breadcrumbs');

if (!breadcrumbDiv) {
return null;
}

const spans = breadcrumbDiv.querySelectorAll('span');
const itemListElement = [];

spans.forEach((span, index) => {
const anchor = span.querySelector('a');
const position = index + 1;
const item = {
'@type': 'ListItem',
position,
item: {},
};

if (anchor) {
item.item['@id'] = anchor.href;
item.item.name = anchor.textContent;
} else {
item.item['@id'] = window.location.href;
item.item.name = span.textContent;
}

itemListElement.push(item);
});

const breadcrumbsSchema = {
'@type': 'BreadcrumbList',
itemListElement,
};

return breadcrumbsSchema;
}

function buildLdJson(container) {
// Base page LD+JSON
const ldJson = {
Expand All @@ -23,6 +84,24 @@ function buildLdJson(container) {
ldJson['@type'] = 'CollectionPage';
}

// Add Article Page Data
if (isArticlePage()) {
// Add datePublished from metadata
const date = dateToISOString(getMetadata('publisheddate'));
if (date) {
ldJson.datePublished = date;
}

// Add dateModified
// Requires fetching the article from the index
}

// Add breadcrumb when available
const breadcrumbsSchema = createBreadcrumbsSchema();
if (breadcrumbsSchema) {
ldJson.breadcrumb = breadcrumbsSchema;
}

// Add image from metadata
const primaryImage = getMetadata('og:image');
if (primaryImage) {
Expand Down
17 changes: 17 additions & 0 deletions best-cigars-guide/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ function buildHeroBlock(main) {
}
}

export function isArticlePage() {
// Select the div with class 'breadcrumb'
const breadcrumbDiv = document.querySelector('.breadcrumb');

// Check if the breadcrumb div exists
if (breadcrumbDiv) {
// Select all span tags within the breadcrumb div
const spans = breadcrumbDiv.querySelectorAll('span');

// Check if there are exactly 3 span tags
if (spans.length === 3) {
return true;
}
}
return false;
}

/**
* check if this is a category listing page
*/
Expand Down

0 comments on commit ede56ea

Please sign in to comment.