@metascraper/helpers 5.53.0 → 5.54.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.
Files changed (2) hide show
  1. package/package.json +12 -4
  2. package/src/index.js +62 -4
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@metascraper/helpers",
3
- "description": "Collection of helper functions used by metascraper",
3
+ "description": "Shared helper utilities used across metascraper rules for parsing and normalizing metadata.",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-helpers",
5
- "version": "5.53.0",
5
+ "version": "5.54.1",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -19,7 +19,15 @@
19
19
  },
20
20
  "keywords": [
21
21
  "helpers",
22
- "metascraper"
22
+ "html",
23
+ "json-ld",
24
+ "metadata",
25
+ "metascraper",
26
+ "normalize",
27
+ "open-graph",
28
+ "parse",
29
+ "scraper",
30
+ "utils"
23
31
  ],
24
32
  "dependencies": {
25
33
  "audio-extensions": "0.0.0",
@@ -65,5 +73,5 @@
65
73
  "publishConfig": {
66
74
  "access": "public"
67
75
  },
68
- "gitHead": "440ebd15db49ef9cf793869bceba5121bca49ad1"
76
+ "gitHead": "9f72883982e1c5486f2b3dbc6fa4d7958885e518"
69
77
  }
package/src/index.js CHANGED
@@ -548,9 +548,22 @@ const validator = {
548
548
 
549
549
  const truthyTest = () => true
550
550
 
551
- const findRule = async (rules, args, propName) => {
551
+ const isValidValue = async (value, args) => {
552
+ try {
553
+ return await args.validate(value, args, debug)
554
+ } catch (error) {
555
+ debug('validate:error', {
556
+ errorName: error?.name,
557
+ errorMessage: error?.message
558
+ })
559
+ return false
560
+ }
561
+ }
562
+
563
+ const findRule = async (rules, args = {}, propName) => {
552
564
  let index = 0
553
565
  let value
566
+ let hasValue = false
554
567
 
555
568
  do {
556
569
  const rule = rules[index++]
@@ -558,9 +571,20 @@ const findRule = async (rules, args, propName) => {
558
571
  if (test(args)) {
559
572
  const duration = debug.duration()
560
573
  value = await rule(args)
561
- duration(`${rule.pkgName}:${propName}:${index - 1}:${has(value)}`)
574
+ hasValue = has(value)
575
+ const isRejected =
576
+ hasValue && args.validate && !(await isValidValue(value, args))
577
+ if (isRejected) {
578
+ value = undefined
579
+ hasValue = false
580
+ }
581
+ duration(
582
+ `${rule.pkgName}:${propName}:${index - 1}:${hasValue}${
583
+ isRejected ? ':rejected' : ''
584
+ }`
585
+ )
562
586
  }
563
- } while (!has(value) && index < rules.length)
587
+ } while (!hasValue && index < rules.length)
564
588
 
565
589
  return value
566
590
  }
@@ -624,6 +648,39 @@ const createGetIframeCached = getIframe => {
624
648
  }
625
649
  }
626
650
 
651
+ const withIframe = (rules, getIframe, propName) => {
652
+ const probe = async (src, args) => {
653
+ try {
654
+ return await findRule(
655
+ rules,
656
+ { ...args, htmlDom: await getIframe(args.url, args.htmlDom, src) },
657
+ propName
658
+ )
659
+ } catch (_) {}
660
+ }
661
+
662
+ return rules.concat(async args => {
663
+ const { htmlDom: $, url } = args
664
+ const srcs = $('iframe[src^="http"], iframe[src^="/"]')
665
+ .map((_, el) => $(el).attr('src'))
666
+ .get()
667
+
668
+ if (srcs.length > 0) {
669
+ const seen = new Set()
670
+ for (const src of srcs) {
671
+ const normalized = normalizeUrl(url, src)
672
+ if (!normalized || seen.has(normalized)) continue
673
+ seen.add(normalized)
674
+ const value = await probe(normalized, args)
675
+ if (has(value)) return value
676
+ }
677
+ }
678
+
679
+ const twitter = $('meta[name="twitter:player"]').attr('content')
680
+ if (twitter) return probe(twitter, args)
681
+ })
682
+ }
683
+
627
684
  module.exports = {
628
685
  $filter,
629
686
  $jsonld,
@@ -676,5 +733,6 @@ module.exports = {
676
733
  url,
677
734
  validator,
678
735
  video,
679
- videoExtensions
736
+ videoExtensions,
737
+ withIframe
680
738
  }