Skip to content

Commit 0c8ee94

Browse files
slavaGanzinKikobeats
authored andcommitted
feat: fixes #207: parse multiple json-ld blocks (#208)
* feat: fixes #207: parse multiple json-ld blocks * fix: parse array json-ld blocks
1 parent 87576a1 commit 0c8ee94

File tree

4 files changed

+654
-10
lines changed

4 files changed

+654
-10
lines changed

packages/metascraper-helpers/index.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
'use strict'
22

33
const {
4-
castArray,
54
chain,
65
eq,
7-
first,
86
flow,
97
get,
108
includes,
@@ -18,7 +16,8 @@ const {
1816
toLower,
1917
trim,
2018
invoke,
21-
isNil
19+
isNil,
20+
castArray
2221
} = require('lodash')
2322

2423
const langs = require('iso-639-3').map(({ iso6391 }) => iso6391)
@@ -224,16 +223,23 @@ const isMime = (contentType, type) => {
224223

225224
const jsonld = mem(
226225
(url, $) => {
227-
let data = {}
226+
const data = {}
228227
try {
229-
data = JSON.parse(
230-
$('script[type="application/ld+json"]')
231-
.first()
232-
.contents()
233-
.text()
228+
$('script[type="application/ld+json"]').map((i, e) =>
229+
Object.assign(
230+
data,
231+
...castArray(
232+
JSON.parse(
233+
$(e)
234+
.contents()
235+
.text()
236+
)
237+
)
238+
)
234239
)
235240
} catch (err) {}
236-
return first(castArray(data))
241+
242+
return data
237243
},
238244
{ cacheKey: url => url }
239245
)

packages/metascraper-helpers/test/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ describe('metascraper-helpers', () => {
207207
should(Array.isArray(json)).be.false()
208208
should(Object.keys(json).length > 0).be.true()
209209
})
210+
211+
it('reads multiple JSON-LD blocks', () => {
212+
const $ = cheerio.load(`
213+
<script type="application/ld+json"> { "@context": "http://schema.org", "@type": "Organization", "url": "https://bykvu.com/ru", "logo": "https://bykvu.com/wp-content/themes/bykvu/img/logo.svg" } </script>
214+
<script type="application/ld+json"> { "@context": "http://schema.org", "@type": "NewsArticle", "mainEntityOfPage": { "@type": "WebPage", "@id": "https://bykvu.com/ru/bukvy/uchenye-nazvali-depressiju-prichinoj-22-opasnyh-zabolevanij/" }, "headline": "Ученые назвали депрессию причиной 22 опасных заболеваний", "image": [ "https://bykvu.com/wp-content/themes/bykvu/includes/images/noimage_large.jpg" ], "datePublished": "2019-09-09T00:29:09+02:00", "dateModified": "2019-09-09T00:29:09+02:00", "author": { "@type": "Person", "name": "Буквы" }, "publisher": { "@type": "Organization", "name": "Буквы", "logo": { "@type": "ImageObject", "url": "https://bykvu.com/wp-content/themes/bykvu/img/apple-icon-180x180.png" } }, "description": "Ученые австралийского центра точного здравоохранения при Университете Южной Австралии выяснили, что депрессия является причиной 22 различных заболеваний." } </script>
215+
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [ { "@type": "ListItem", "position": 1, "item": { "@id": "https://bykvu.com/ru", "name": "Буквы" } }, { "@type": "ListItem", "position": 2, "item": { "@id": "https://bykvu.com/ru/category/bukvy/", "name": "Новости" } }, { "@type": "ListItem", "position": 3, "item": { "@id": "https://bykvu.com/ru/bukvy/uchenye-nazvali-depressiju-prichinoj-22-opasnyh-zabolevanij/", "name": "Ученые назвали депрессию причиной 22 опасных заболеваний" } } ] } </script>`)
216+
217+
const json = jsonld(url, $)
218+
should(typeof json).be.equal('object')
219+
should(Array.isArray(json)).be.false()
220+
should(Object.keys(json).length).equal(13)
221+
})
210222
})
211223

212224
describe('.titleize', function () {

0 commit comments

Comments
 (0)