Skip to content

Commit 588f6ff

Browse files
authoredOct 14, 2024··
Merge pull request #402 from extractus/8.0.11
v8.0.11
2 parents 34c58f1 + 7b5eb42 commit 588f6ff

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"lint": "eslint .",
2626
"lint:fix": "eslint --fix .",
2727
"pretest": "npm run lint",
28-
"test": "node --test",
28+
"test": "node --test --experimental-test-coverage",
2929
"eval": "node eval",
3030
"reset": "node reset"
3131
},

‎src/utils/extractLdSchema.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,30 @@ const parseJson = (text) => {
5252
* @returns {Object} The entry object after being merged/populated with data.
5353
*/
5454
export default (document, entry) => {
55-
const ldSchema = document.querySelector('script[type="application/ld+json"]')?.textContent
55+
const ldSchemas = document.querySelectorAll('script[type="application/ld+json"]')
5656

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

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

66-
if (typeof ldJson[attr] === 'string') {
67-
return entry[key] = ldJson[attr].toLowerCase()
65+
if (isEntryAlreadyPopulated || !ldJson[attr]) {
66+
return
6867
}
6968

70-
if (Array.isArray(ldJson[attr]) && typeof ldJson[attr][0] === 'string') {
71-
return entry[key] = ldJson[attr][0].toLowerCase()
69+
const keyValue = ldJson[attr]
70+
if (keyValue) {
71+
entry[key] = Array.isArray(keyValue) ? keyValue[0] : keyValue
72+
if (typeof entry[key] === 'string') {
73+
entry[key] = entry[key].toLowerCase().trim()
74+
}
7275
}
73-
}
74-
})
75-
}
76+
})
77+
}
78+
})
7679

7780
return entry
7881
}

‎test-data/regular-article-json-ld.html

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"https://somewhere.com/path/to/image2.jpg",
2323
"https://somewhere.com/path/to/image3.jpg"
2424
],
25+
"keywords": "keyword1, keyword2, keyword3",
26+
"keywords_with_line_breaks": "keyword1, keyword2, keyword3",
2527
"datePublished": "23\/01\/2014",
2628
"dateCreated": "23\/01\/2014",
2729
"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."

0 commit comments

Comments
 (0)
Please sign in to comment.