Skip to content

Commit 14d212d

Browse files
committed
Add JSON-LD section to book details (#1380)
This provides information, including published and modified dates, for search engines.
1 parent 31356ff commit 14d212d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/app/pages/details/details.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,41 @@ export default class Details extends BaseClass {
8989
});
9090
}
9191

92+
insertJsonLd() {
93+
const el = document.createElement('script');
94+
const data = this.pageData;
95+
const authorData = data.authors.map((obj) => ({
96+
'@type': 'Person',
97+
'name': obj.name,
98+
'affiliation': obj.university
99+
}));
100+
const polish = $.isPolish(data.title);
101+
const ldData = {
102+
'@content': 'https://schema.org',
103+
'@type': 'WebPage',
104+
'datePublished': data.created,
105+
'dateModified': data.updated,
106+
'mainEntity': {
107+
'type': 'Book',
108+
'name': data.title,
109+
'author': authorData,
110+
'publisher': {
111+
'type': 'Organization',
112+
'name': 'OpenStax'
113+
},
114+
'image': data.cover_url,
115+
'inLanguage': polish ? 'Polish' : 'English',
116+
'isbn': data.digital_isbn_13,
117+
'url': data.webview_link
118+
}
119+
};
120+
const descriptionEl = document.querySelector('head meta[name="description"]');
121+
122+
el.type = 'application/ld+json';
123+
el.textContent = JSON.stringify(ldData, null, 2);
124+
descriptionEl.parentNode.insertBefore(el, descriptionEl.nextSibling);
125+
}
126+
92127
onLoaded() {
93128
document.body.classList.remove('page-loaded');
94129
document.body.classList.add('page-loading');
@@ -261,6 +296,7 @@ export default class Details extends BaseClass {
261296
}));
262297
this.regions.tabController.attach(tabGroup);
263298
this.regions.tabContent.attach(contentGroup);
299+
this.insertJsonLd();
264300
}
265301

266302
onDataError() {

0 commit comments

Comments
 (0)