metascraper 5.54.0 → 5.55.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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper",
3
3
  "description": "Scrape metadata from any URL using Open Graph, JSON-LD, HTML meta tags, and smart fallbacks.",
4
4
  "homepage": "https://metascraper.js.org",
5
- "version": "5.54.0",
5
+ "version": "5.55.0",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "repository": {
@@ -27,7 +27,7 @@
27
27
  "unfurl"
28
28
  ],
29
29
  "dependencies": {
30
- "@metascraper/helpers": "5.54.0",
30
+ "@metascraper/helpers": "5.55.0",
31
31
  "cheerio": "~1.2.0",
32
32
  "debug-logfmt": "~1.4.12",
33
33
  "whoops": "~5.1.2"
@@ -55,5 +55,5 @@
55
55
  "tsd": {
56
56
  "directory": "test/types"
57
57
  },
58
- "gitHead": "23db3208c1dcc6367a705bdb9bf723fbaae259e0"
58
+ "gitHead": "c6fdffea348dcee4ff5ce22613518c4c68f50f2a"
59
59
  }
package/src/index.d.ts CHANGED
@@ -34,6 +34,11 @@ declare namespace createMetascraper {
34
34
  * Ensure the URL provided is validated as a WHATWG URL API compliant.
35
35
  */
36
36
  validateUrl?: boolean;
37
+ /**
38
+ * Runs inside `findRule` after each rule produces a value. Falsy or throw
39
+ * rejects that candidate and continues with the next rule.
40
+ */
41
+ validate?: Validate;
37
42
  /**
38
43
  * A Set of property names to omit from the metadata extraction process.
39
44
  * These properties will be filtered out before processing the rules.
@@ -116,12 +121,16 @@ declare namespace createMetascraper {
116
121
  export type Test = (options: RulesTestOptions) => boolean;
117
122
 
118
123
  /**
119
- * Post-accept check for a rule candidate. Falsy or throw rejects the
120
- * candidate and findRule continues with the next rule.
124
+ * Scrape-level check inside `findRule`. Falsy or throw rejects the candidate
125
+ * and continues with the next rule.
126
+ *
127
+ * `options.propName` is the property being resolved, so a validate can scope
128
+ * itself: the same value is a hostile asset under `logo` and the expected
129
+ * result under `url`.
121
130
  */
122
131
  export type Validate = (
123
132
  value: string,
124
- options: RulesTestOptions,
133
+ options: RulesTestOptions & { propName: string },
125
134
  debug: Debug
126
135
  ) => boolean | Promise<boolean>;
127
136
 
@@ -142,11 +151,6 @@ declare namespace createMetascraper {
142
151
  * The test function to be executed for skipping rules that doesn't return `true`.
143
152
  */
144
153
  test?: Test;
145
- /**
146
- * Copied onto each rule in the bundle. Runs after a rule produces a value;
147
- * falsy or throw rejects that candidate and continues.
148
- */
149
- validate?: Validate;
150
154
  /**
151
155
  * The package name associated with the rule, used for debugging purposes.
152
156
  */
@@ -160,7 +164,6 @@ declare namespace createMetascraper {
160
164
  | Array<RulesOptions>
161
165
  | RulesOptions
162
166
  | Test
163
- | Validate
164
167
  | string
165
168
  | undefined;
166
169
  }
@@ -172,7 +175,6 @@ declare namespace createMetascraper {
172
175
  | undefined
173
176
  | Promise<string | null | undefined>;
174
177
  test?: Test;
175
- validate?: Validate;
176
178
  pkgName?: string;
177
179
  }
178
180
 
package/src/rules.js CHANGED
@@ -2,10 +2,9 @@
2
2
 
3
3
  const castArray = value => (Array.isArray(value) ? value : [value])
4
4
 
5
- const attachRuleMeta = (rules, test, validate, pkgName) => {
5
+ const attachRuleMeta = (rules, test, pkgName) => {
6
6
  for (const rule of rules) {
7
7
  if (test) rule.test = test
8
- if (validate) rule.validate = validate
9
8
  if (pkgName) rule.pkgName = pkgName
10
9
  }
11
10
  }
@@ -13,10 +12,10 @@ const attachRuleMeta = (rules, test, validate, pkgName) => {
13
12
  const loadRules = rulesBundle => {
14
13
  const acc = {}
15
14
 
16
- for (const { test, validate, pkgName, ...rules } of rulesBundle) {
15
+ for (const { test, pkgName, validate: _validate, ...rules } of rulesBundle) {
17
16
  for (const [propName, innerRules] of Object.entries(rules)) {
18
17
  const processedRules = castArray(innerRules)
19
- attachRuleMeta(processedRules, test, validate, pkgName)
18
+ attachRuleMeta(processedRules, test, pkgName)
20
19
 
21
20
  if (acc[propName]) {
22
21
  acc[propName].push(...processedRules)
@@ -61,12 +60,12 @@ const mergeRules = (
61
60
  return Object.entries(result)
62
61
  }
63
62
 
64
- for (const { test, validate, pkgName, ...ruleSet } of rules) {
63
+ for (const { test, pkgName, validate: _validate, ...ruleSet } of rules) {
65
64
  for (const [propName, innerRules] of Object.entries(ruleSet)) {
66
65
  if (!shouldIncludeProp(propName)) continue
67
66
 
68
67
  const processedRules = castArray(innerRules)
69
- attachRuleMeta(processedRules, test, validate, pkgName)
68
+ attachRuleMeta(processedRules, test, pkgName)
70
69
 
71
70
  if (result[propName]) {
72
71
  result[propName] = processedRules.concat(result[propName])