Skip to content

Commit

Permalink
Merge pull request #402 from extractus/8.0.11
Browse files Browse the repository at this point in the history
v8.0.11
  • Loading branch information
ndaidong authored Oct 14, 2024
2 parents 34c58f1 + 7b5eb42 commit 588f6ff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
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

0 comments on commit 588f6ff

Please sign in to comment.