@metascraper/helpers 5.49.25 → 5.50.1
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 +3 -3
- package/src/index.js +116 -4
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.
|
|
5
|
+
"version": "5.50.1",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"isostring": "0.0.1",
|
|
38
38
|
"jsdom": "~27.4.0",
|
|
39
39
|
"jsonrepair": "~3.13.2",
|
|
40
|
-
"lodash": "~4.
|
|
40
|
+
"lodash": "~4.18.0",
|
|
41
41
|
"memoize-one": "~6.0.0",
|
|
42
42
|
"microsoft-capitalize": "~1.0.6",
|
|
43
43
|
"mime": "3",
|
|
@@ -65,5 +65,5 @@
|
|
|
65
65
|
"publishConfig": {
|
|
66
66
|
"access": "public"
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "688ff08cd5618a0fd9afa55e31552e95136814f2"
|
|
69
69
|
}
|
package/src/index.js
CHANGED
|
@@ -389,13 +389,85 @@ const jsonld = memoizeOne(
|
|
|
389
389
|
(newArgs, oldArgs) => isSameHtmlDom(newArgs[0], oldArgs[0])
|
|
390
390
|
)
|
|
391
391
|
|
|
392
|
+
/**
|
|
393
|
+
* Recursive schema.org search with array traversal and object-to-name resolution.
|
|
394
|
+
* Returns array of primitive values (strings, numbers, booleans) for the given path.
|
|
395
|
+
*/
|
|
396
|
+
function searchSchemaResults (data, props, isExact) {
|
|
397
|
+
if (data === null || data === undefined) return []
|
|
398
|
+
if (
|
|
399
|
+
typeof data === 'string' ||
|
|
400
|
+
typeof data === 'number' ||
|
|
401
|
+
typeof data === 'boolean'
|
|
402
|
+
) {
|
|
403
|
+
return props.length === 0 ? [data] : []
|
|
404
|
+
}
|
|
405
|
+
if (Array.isArray(data)) {
|
|
406
|
+
const current = props[0]
|
|
407
|
+
if (current && /^\[\d+\]$/.test(current)) {
|
|
408
|
+
const index = parseInt(current.slice(1, -1), 10)
|
|
409
|
+
if (data[index] !== undefined) {
|
|
410
|
+
return searchSchemaResults(data[index], props.slice(1), isExact)
|
|
411
|
+
}
|
|
412
|
+
return []
|
|
413
|
+
}
|
|
414
|
+
if (
|
|
415
|
+
props.length === 0 &&
|
|
416
|
+
data.every(item => typeof item === 'string' || typeof item === 'number')
|
|
417
|
+
) {
|
|
418
|
+
return data.map(String)
|
|
419
|
+
}
|
|
420
|
+
return data.flatMap(item => searchSchemaResults(item, props, isExact))
|
|
421
|
+
}
|
|
422
|
+
if (typeof data === 'object') {
|
|
423
|
+
const [currentProp, ...rest] = props
|
|
424
|
+
if (!currentProp) {
|
|
425
|
+
if (data.name != null && typeof data.name === 'string') return [data.name]
|
|
426
|
+
return []
|
|
427
|
+
}
|
|
428
|
+
if (Object.prototype.hasOwnProperty.call(data, currentProp)) {
|
|
429
|
+
const result = searchSchemaResults(data[currentProp], rest, isExact)
|
|
430
|
+
if (result.length > 0) return result
|
|
431
|
+
}
|
|
432
|
+
if (!isExact) {
|
|
433
|
+
const nested = []
|
|
434
|
+
for (const key of Object.keys(data)) {
|
|
435
|
+
if (key.startsWith('@')) continue
|
|
436
|
+
nested.push(...searchSchemaResults(data[key], props, false))
|
|
437
|
+
}
|
|
438
|
+
return nested
|
|
439
|
+
}
|
|
440
|
+
return []
|
|
441
|
+
}
|
|
442
|
+
return []
|
|
443
|
+
}
|
|
444
|
+
|
|
392
445
|
const $jsonld = propName => $ => {
|
|
393
446
|
const collection = jsonld($)
|
|
447
|
+
const props = propName.split('.')
|
|
394
448
|
let value
|
|
395
|
-
|
|
449
|
+
let fallback
|
|
450
|
+
|
|
451
|
+
for (const item of collection) {
|
|
396
452
|
value = get(item, propName)
|
|
397
|
-
|
|
398
|
-
|
|
453
|
+
if (!isEmpty(value) || isNumber(value) || isBoolean(value)) break
|
|
454
|
+
if (value != null) continue
|
|
455
|
+
if (fallback !== undefined) continue
|
|
456
|
+
const exactResults = searchSchemaResults(item, props, true)
|
|
457
|
+
if (exactResults.length > 0) {
|
|
458
|
+
const filtered = exactResults.filter(v => v != null)
|
|
459
|
+
fallback = filtered.length > 1 ? filtered.join(' and ') : filtered[0]
|
|
460
|
+
} else {
|
|
461
|
+
const fuzzyResults = searchSchemaResults(item, props, false)
|
|
462
|
+
if (fuzzyResults.length > 0) {
|
|
463
|
+
fallback = fuzzyResults.find(v => v != null)
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (value == null && !isNumber(value) && !isBoolean(value)) {
|
|
469
|
+
value = fallback
|
|
470
|
+
}
|
|
399
471
|
|
|
400
472
|
return isString(value) ? decodeHTML(value) : value
|
|
401
473
|
}
|
|
@@ -483,6 +555,43 @@ const has = value =>
|
|
|
483
555
|
|
|
484
556
|
const getUrls = input => String(input).match(urlRegexForMatch) ?? []
|
|
485
557
|
|
|
558
|
+
const htmlCache = new WeakMap()
|
|
559
|
+
|
|
560
|
+
const getHtml = htmlDom => {
|
|
561
|
+
if (!htmlCache.has(htmlDom)) {
|
|
562
|
+
htmlCache.set(htmlDom, htmlDom.html())
|
|
563
|
+
}
|
|
564
|
+
return htmlCache.get(htmlDom)
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const loadIframe = require('./load-iframe')
|
|
568
|
+
|
|
569
|
+
const defaultGetIframe = (url, $, { src }) =>
|
|
570
|
+
loadIframe(url, $.load(`<iframe src="${src}"></iframe>`))
|
|
571
|
+
|
|
572
|
+
const createGetIframeCached = getIframe => {
|
|
573
|
+
const cacheByHtmlDom = new WeakMap()
|
|
574
|
+
|
|
575
|
+
return async (url, $, src) => {
|
|
576
|
+
let cacheBySrc = cacheByHtmlDom.get($)
|
|
577
|
+
if (!cacheBySrc) {
|
|
578
|
+
cacheBySrc = new Map()
|
|
579
|
+
cacheByHtmlDom.set($, cacheBySrc)
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
const cachedHtmlDom = cacheBySrc.get(src)
|
|
583
|
+
if (cachedHtmlDom) return cachedHtmlDom
|
|
584
|
+
|
|
585
|
+
const pendingHtmlDom = getIframe(url, $, { src }).catch(error => {
|
|
586
|
+
cacheBySrc.delete(src)
|
|
587
|
+
throw error
|
|
588
|
+
})
|
|
589
|
+
|
|
590
|
+
cacheBySrc.set(src, pendingHtmlDom)
|
|
591
|
+
return pendingHtmlDom
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
486
595
|
module.exports = {
|
|
487
596
|
$filter,
|
|
488
597
|
$jsonld,
|
|
@@ -491,11 +600,14 @@ module.exports = {
|
|
|
491
600
|
audioExtensions,
|
|
492
601
|
author,
|
|
493
602
|
composeRule,
|
|
603
|
+
createGetIframeCached,
|
|
494
604
|
date,
|
|
605
|
+
defaultGetIframe,
|
|
495
606
|
description,
|
|
496
607
|
extension,
|
|
497
608
|
fileExtension,
|
|
498
609
|
findRule,
|
|
610
|
+
getHtml,
|
|
499
611
|
getUrls,
|
|
500
612
|
has,
|
|
501
613
|
image,
|
|
@@ -517,7 +629,7 @@ module.exports = {
|
|
|
517
629
|
iso6393,
|
|
518
630
|
jsonld,
|
|
519
631
|
lang,
|
|
520
|
-
loadIframe
|
|
632
|
+
loadIframe,
|
|
521
633
|
logo,
|
|
522
634
|
memoizeOne,
|
|
523
635
|
mimeExtension,
|