Skip to content

Commit

Permalink
Add FAQ ld json to article pages
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeffleyfamous committed Jun 13, 2024
1 parent bd2ecf7 commit 775d5d2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cigaradvisor/scripts/linking-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,49 @@ function addBlogPosts() {
addLdJsonScript(document.querySelector('head'), ldjson);
}

/**
* Determines if the page contains a FAQ section and writes the LD JSON to the head
*/
function addFAQLdJson() {
const contentDivs = document.querySelectorAll('.default-content-wrapper');
//find the content div that contains a FAQ

Check failure on line 91 in cigaradvisor/scripts/linking-data.js

View workflow job for this annotation

GitHub Actions / build

Expected exception block, space or tab after '//' in comment
for (let i = 0; i < contentDivs.length; i++) {

Check failure on line 92 in cigaradvisor/scripts/linking-data.js

View workflow job for this annotation

GitHub Actions / build

Unary operator '++' used
let isFAQPage = false;
const h2Elements = contentDivs[i].getElementsByTagName('h2');
// Does the div contain a faq section?
for (let j = 0; j < h2Elements.length; j++) {

Check failure on line 96 in cigaradvisor/scripts/linking-data.js

View workflow job for this annotation

GitHub Actions / build

Unary operator '++' used
if (h2Elements[j].id.includes('frequently-asked-questions')) {
isFAQPage = true;
}
}
if (isFAQPage) {
const ldjson = {
'@context': 'http://schema.org/',
'@type': 'FAQPage',
mainEntity: [],
};
//questions should be in the h3 elements, with the answers in the following element

Check failure on line 107 in cigaradvisor/scripts/linking-data.js

View workflow job for this annotation

GitHub Actions / build

Expected exception block, space or tab after '//' in comment
const h3Elements = contentDivs[i].getElementsByTagName('h3');
for (let k = 0; k < h3Elements.length; k++) {

Check failure on line 109 in cigaradvisor/scripts/linking-data.js

View workflow job for this annotation

GitHub Actions / build

Unary operator '++' used
let QAEntity = {

Check failure on line 110 in cigaradvisor/scripts/linking-data.js

View workflow job for this annotation

GitHub Actions / build

'QAEntity' is never reassigned. Use 'const' instead
"@type": "Question",

Check failure on line 111 in cigaradvisor/scripts/linking-data.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote

Check failure on line 111 in cigaradvisor/scripts/linking-data.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
name: h3Elements[k].textContent,
acceptedAnswer : {}

Check failure on line 113 in cigaradvisor/scripts/linking-data.js

View workflow job for this annotation

GitHub Actions / build

Extra space after key 'acceptedAnswer'

Check failure on line 113 in cigaradvisor/scripts/linking-data.js

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
};
if (h3Elements[k].nextElementSibling) {
QAEntity.acceptedAnswer = {
"@type": "Answer",
text: h3Elements[k].nextElementSibling.textContent
}
//add question only if we found the answer
ldjson.mainEntity.push(QAEntity);
}
}
addLdJsonScript(document.querySelector('head'), ldjson);
}
}
}

export default function addLinkingData() {
addOrg(document.querySelector('head'));
if (window.location.pathname === '/cigaradvisor') {
Expand All @@ -91,4 +134,5 @@ export default function addLinkingData() {
addOrUpdateCollection();
window.addEventListener('hashchange', addOrUpdateCollection);
}
addFAQLdJson();
}

0 comments on commit 775d5d2

Please sign in to comment.