@metascraper/helpers 5.49.19 → 5.49.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/src/index.js +21 -10
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@metascraper/helpers",
3
3
  "description": "Collection of helper functions used by metascraper",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-helpers",
5
- "version": "5.49.19",
5
+ "version": "5.49.23",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -36,6 +36,7 @@
36
36
  "iso-639-3": "~2.2.0",
37
37
  "isostring": "0.0.1",
38
38
  "jsdom": "~27.4.0",
39
+ "jsonrepair": "~3.13.2",
39
40
  "lodash": "~4.17.23",
40
41
  "memoize-one": "~6.0.0",
41
42
  "microsoft-capitalize": "~1.0.6",
@@ -64,5 +65,5 @@
64
65
  "publishConfig": {
65
66
  "access": "public"
66
67
  },
67
- "gitHead": "b4b7f9133abd1bcfa9724fd05a5a4b0075969475"
68
+ "gitHead": "74f58f4db8e6cb26e441f1e731012e3bf8be6bd2"
68
69
  }
package/src/index.js CHANGED
@@ -8,6 +8,7 @@ const capitalize = require('microsoft-capitalize')
8
8
  const isRelativeUrl = require('is-relative-url')
9
9
  const fileExtension = require('file-extension')
10
10
  const _normalizeUrl = require('normalize-url')
11
+ const { jsonrepair } = require('jsonrepair')
11
12
  const smartquotes = require('smartquotes')
12
13
  const { decodeHTML } = require('entities')
13
14
  const iso6393 = require('iso-639-3/to-1')
@@ -354,19 +355,30 @@ memoizeOne.EqualityUrlAndHtmlDom = (newArgs, oldArgs) =>
354
355
  memoizeOne.EqualityFirstArgument = (newArgs, oldArgs) =>
355
356
  newArgs[0] === oldArgs[0]
356
357
 
358
+ const parseJSON = text => {
359
+ try {
360
+ return JSON.parse(text)
361
+ } catch {
362
+ try {
363
+ return JSON.parse(jsonrepair(text))
364
+ } catch {
365
+ return undefined
366
+ }
367
+ }
368
+ }
369
+
357
370
  const jsonld = memoizeOne(
358
371
  $ =>
359
372
  $('script[type="application/ld+json"]')
360
373
  .map((_, element) => {
361
- try {
362
- const el = $(element)
363
- const json = JSON.parse($(el).contents().text())
364
- const { '@graph': graph, ...props } = json
365
- if (!graph) return json
366
- return graph.map(item => ({ ...props, ...item }))
367
- } catch (_) {
368
- return undefined
369
- }
374
+ const el = $(element)
375
+ const text = $(el).contents().text()
376
+ const json = parseJSON(text)
377
+ if (!json) return false
378
+ const { '@graph': graph, ...props } = json
379
+ return Array.isArray(graph)
380
+ ? graph.map(item => ({ ...props, ...item }))
381
+ : props
370
382
  })
371
383
  .get()
372
384
  .filter(Boolean),
@@ -376,7 +388,6 @@ const jsonld = memoizeOne(
376
388
  const $jsonld = propName => $ => {
377
389
  const collection = jsonld($)
378
390
  let value
379
-
380
391
  collection.find(item => {
381
392
  value = get(item, propName)
382
393
  return !isEmpty(value) || isNumber(value) || isBoolean(value)