@metascraper/helpers 5.49.17 → 5.49.21
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.
- package/package.json +4 -2
- package/src/index.js +20 -9
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.
|
|
5
|
+
"version": "5.49.21",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -36,10 +36,12 @@
|
|
|
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",
|
|
42
43
|
"mime": "3",
|
|
44
|
+
"normalize-url": "6",
|
|
43
45
|
"re2": "~1.23.0",
|
|
44
46
|
"smartquotes": "~2.3.2",
|
|
45
47
|
"tldts": "~7.0.19",
|
|
@@ -63,5 +65,5 @@
|
|
|
63
65
|
"publishConfig": {
|
|
64
66
|
"access": "public"
|
|
65
67
|
},
|
|
66
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "b49f1cd9aa131c2ba5abe8aa55f5a33b0afdabca"
|
|
67
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,29 @@ 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
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
return undefined
|
|
369
|
-
}
|
|
374
|
+
const el = $(element)
|
|
375
|
+
const text = $(el).contents().text()
|
|
376
|
+
const json = parseJSON(text)
|
|
377
|
+
if (!json) return undefined
|
|
378
|
+
const { '@graph': graph, ...props } = json
|
|
379
|
+
if (!graph) return json
|
|
380
|
+
return graph.map(item => ({ ...props, ...item }))
|
|
370
381
|
})
|
|
371
382
|
.get()
|
|
372
383
|
.filter(Boolean),
|