Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v8.0.11 #402

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"pretest": "npm run lint",
"test": "node --test",
"test": "node --test --experimental-test-coverage",
"eval": "node eval",
"reset": "node reset"
},
Expand Down
33 changes: 18 additions & 15 deletions src/utils/extractLdSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,30 @@ const parseJson = (text) => {
* @returns {Object} The entry object after being merged/populated with data.
*/
export default (document, entry) => {
const ldSchema = document.querySelector('script[type="application/ld+json"]')?.textContent
const ldSchemas = document.querySelectorAll('script[type="application/ld+json"]')

const ldJson = ldSchema ? parseJson(ldSchema) : null
ldSchemas.forEach(ldSchema => {
const ldJson = parseJson(ldSchema.textContent.replace(/[\n\r\t]/g, ''))
const isAllowedLdJsonType = typeSchemas.includes(ldJson['@type'].toLowerCase())

if (ldJson) {
Object.entries(attributeLists).forEach(([key, attr]) => {
if ((typeof entry[key] === 'undefined' || entry[key] === '') && ldJson[attr]) {
if (key === 'type' && typeof ldJson[attr] === 'string') {
return entry[key] = typeSchemas.includes(ldJson[attr].toLowerCase()) ? ldJson[attr].toLowerCase() : ''
}
if (ldJson && isAllowedLdJsonType) {
Object.entries(attributeLists).forEach(([key, attr]) => {
const isEntryAlreadyPopulated = typeof entry[key] !== 'undefined' && entry[key] !== ''

if (typeof ldJson[attr] === 'string') {
return entry[key] = ldJson[attr].toLowerCase()
if (isEntryAlreadyPopulated || !ldJson[attr]) {
return
}

if (Array.isArray(ldJson[attr]) && typeof ldJson[attr][0] === 'string') {
return entry[key] = ldJson[attr][0].toLowerCase()
const keyValue = ldJson[attr]
if (keyValue) {
entry[key] = Array.isArray(keyValue) ? keyValue[0] : keyValue
if (typeof entry[key] === 'string') {
entry[key] = entry[key].toLowerCase().trim()
}
}
}
})
}
})
}
})

return entry
}
2 changes: 2 additions & 0 deletions test-data/regular-article-json-ld.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"https://somewhere.com/path/to/image2.jpg",
"https://somewhere.com/path/to/image3.jpg"
],
"keywords": "keyword1, keyword2, keyword3",
"keywords_with_line_breaks": "keyword1, keyword2, keyword3",
"datePublished": "23\/01\/2014",
"dateCreated": "23\/01\/2014",
"description": "Navigation here Few can name a rational peach that isn't a conscientious goldfish! One cannot separate snakes from plucky pomegranates? Draped neatly on a hanger, the melons could be said to resemble knowledgeable pigs."
Expand Down
Loading