@metascraper/helpers 5.34.9 → 5.35.0
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/index.js +33 -24
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -218,6 +218,15 @@ const isAudioExtension = url => isMediaTypeExtension(url, AUDIO)
|
|
|
218
218
|
|
|
219
219
|
const isImageExtension = url => isMediaTypeExtension(url, IMAGE)
|
|
220
220
|
|
|
221
|
+
const isContentType =
|
|
222
|
+
extensions =>
|
|
223
|
+
({ type = '' } = {}) =>
|
|
224
|
+
extensions.some(extension => type.endsWith(extension))
|
|
225
|
+
|
|
226
|
+
const isVideoContentType = isContentType(Object.keys(videoExtensions))
|
|
227
|
+
|
|
228
|
+
const isAudioContentType = isContentType(Object.keys(audioExtensions))
|
|
229
|
+
|
|
221
230
|
const extension = (str = '') => {
|
|
222
231
|
const url = urlObject(
|
|
223
232
|
str,
|
|
@@ -323,11 +332,7 @@ const jsonld = memoizeOne(
|
|
|
323
332
|
.map(function () {
|
|
324
333
|
try {
|
|
325
334
|
const el = $(this)
|
|
326
|
-
const json = JSON.parse(
|
|
327
|
-
$(el)
|
|
328
|
-
.contents()
|
|
329
|
-
.text()
|
|
330
|
-
)
|
|
335
|
+
const json = JSON.parse($(el).contents().text())
|
|
331
336
|
|
|
332
337
|
const { '@graph': graph, ...props } = json
|
|
333
338
|
if (!graph) return json
|
|
@@ -364,15 +369,16 @@ const image = (value, opts) => {
|
|
|
364
369
|
|
|
365
370
|
const logo = image
|
|
366
371
|
|
|
367
|
-
const
|
|
372
|
+
const media = (urlValidator, contentTypeValidator) => (value, opts) => {
|
|
368
373
|
const urlValue = url(value, opts)
|
|
369
|
-
return
|
|
374
|
+
return urlValidator(urlValue, opts) || contentTypeValidator(opts)
|
|
375
|
+
? urlValue
|
|
376
|
+
: undefined
|
|
370
377
|
}
|
|
371
378
|
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
379
|
+
const video = media(isVideoUrl, isVideoContentType)
|
|
380
|
+
|
|
381
|
+
const audio = media(isAudioUrl, isAudioContentType)
|
|
376
382
|
|
|
377
383
|
const validator = {
|
|
378
384
|
audio,
|
|
@@ -403,19 +409,22 @@ const findRule = async (rules, args) => {
|
|
|
403
409
|
return value
|
|
404
410
|
}
|
|
405
411
|
|
|
406
|
-
const toRule =
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
412
|
+
const toRule =
|
|
413
|
+
(mapper, opts) =>
|
|
414
|
+
rule =>
|
|
415
|
+
async ({ htmlDom, url }) => {
|
|
416
|
+
const value = await rule(htmlDom, url)
|
|
417
|
+
return mapper(value, { url, ...opts })
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const composeRule =
|
|
421
|
+
rule =>
|
|
422
|
+
({ from, to = from, ...opts }) =>
|
|
423
|
+
async ({ htmlDom, url }) => {
|
|
424
|
+
const data = await rule(htmlDom, url)
|
|
425
|
+
const value = get(data, from)
|
|
426
|
+
return invoke(validator, to, value, { url, ...opts })
|
|
427
|
+
}
|
|
419
428
|
|
|
420
429
|
const has = value =>
|
|
421
430
|
value !== undefined && !Number.isNaN(value) && hasValues(value)
|
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://nicedoc.io/microlinkhq/metascraper/packages/metascraper-helpers",
|
|
5
|
-
"version": "5.
|
|
5
|
+
"version": "5.35.0",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
"microsoft-capitalize": "~1.0.5",
|
|
40
40
|
"mime": "~3.0.0",
|
|
41
41
|
"normalize-url": "~6.1.0",
|
|
42
|
-
"re2": "~1.
|
|
42
|
+
"re2": "~1.20.1",
|
|
43
43
|
"smartquotes": "~2.3.2",
|
|
44
|
-
"tldts": "~6.0.
|
|
44
|
+
"tldts": "~6.0.12",
|
|
45
45
|
"url-regex-safe": "~3.0.0",
|
|
46
46
|
"video-extensions": "~1.2.0"
|
|
47
47
|
},
|
|
@@ -62,5 +62,5 @@
|
|
|
62
62
|
"publishConfig": {
|
|
63
63
|
"access": "public"
|
|
64
64
|
},
|
|
65
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "4050798d28b1118e3bb8058bf17b182bb84b7c5b"
|
|
66
66
|
}
|