diff --git a/package.json b/package.json index 2f80f276..b5003510 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/utils/extractLdSchema.js b/src/utils/extractLdSchema.js index 4f837a31..b74ebd80 100644 --- a/src/utils/extractLdSchema.js +++ b/src/utils/extractLdSchema.js @@ -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 } diff --git a/test-data/regular-article-json-ld.html b/test-data/regular-article-json-ld.html index 24ac042a..5c9cda73 100644 --- a/test-data/regular-article-json-ld.html +++ b/test-data/regular-article-json-ld.html @@ -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."