metascraper 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.
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "metascraper",
3
- "description": "A library to easily scrape metadata from an article on the web using Open Graph, JSON+LD, regular HTML metadata, and series of fallbacks.",
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.53.0",
5
+ "version": "5.54.1",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "repository": {
@@ -13,52 +13,21 @@
13
13
  "url": "https://github.com/microlinkhq/metascraper/issues"
14
14
  },
15
15
  "keywords": [
16
- "RDF",
17
16
  "article",
18
- "browser",
19
17
  "cheerio",
20
- "content",
21
- "expand",
22
18
  "extract",
23
- "fallback",
24
- "fetch",
25
- "get",
26
- "graph",
27
19
  "html",
28
20
  "json-ld",
29
- "jsonld",
30
- "linked data",
31
- "meta",
32
21
  "metadata",
33
22
  "metascraper",
34
- "micro format",
35
- "microformat",
36
23
  "og",
37
- "open",
38
- "open graph",
39
- "opengraph",
40
- "page",
41
- "parse",
42
- "parser",
24
+ "open-graph",
43
25
  "preview",
44
- "scrape",
45
26
  "scraper",
46
- "semantic",
47
- "semantic web",
48
- "server",
49
- "site",
50
- "summarize",
51
- "summary",
52
- "tag",
53
- "tags",
54
- "unfluff",
55
- "unfurl",
56
- "url",
57
- "web",
58
- "website"
27
+ "unfurl"
59
28
  ],
60
29
  "dependencies": {
61
- "@metascraper/helpers": "5.53.0",
30
+ "@metascraper/helpers": "5.54.1",
62
31
  "cheerio": "~1.2.0",
63
32
  "debug-logfmt": "~1.4.12",
64
33
  "whoops": "~5.1.2"
@@ -86,5 +55,5 @@
86
55
  "tsd": {
87
56
  "directory": "test/types"
88
57
  },
89
- "gitHead": "440ebd15db49ef9cf793869bceba5121bca49ad1"
58
+ "gitHead": "9f72883982e1c5486f2b3dbc6fa4d7958885e518"
90
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.
@@ -89,7 +94,7 @@ declare namespace createMetascraper {
89
94
  */
90
95
  publisher?: string;
91
96
  /**
92
- * Get title property from HTML markup
97
+ * Get title property from HTML markup.
93
98
  * The package [metascraper-title](https://example.com/metascraper-title) needs to be loaded.
94
99
  */
95
100
  title?: string;
@@ -110,11 +115,38 @@ declare namespace createMetascraper {
110
115
  [C in keyof Metadata as string extends C ? never : C]?: Array<RulesOptions> | RulesOptions;
111
116
  };
112
117
 
118
+ /**
119
+ * Pre-run gate for a rule. A falsy result skips the rule entirely.
120
+ */
121
+ export type Test = (options: RulesTestOptions) => boolean;
122
+
123
+ /**
124
+ * Scrape-level check inside `findRule`. Falsy or throw rejects the candidate
125
+ * and continues with the next rule.
126
+ */
127
+ export type Validate = (
128
+ value: string,
129
+ options: RulesTestOptions,
130
+ debug: Debug
131
+ ) => boolean | Promise<boolean>;
132
+
133
+ /**
134
+ * The `metascraper:find-rule` logger, so a validate can explain a rejection.
135
+ * Only writes when `DEBUG=metascraper:find-rule` is enabled.
136
+ */
137
+ export interface Debug {
138
+ (...args: Array<string | object>): void;
139
+ readonly enabled: boolean | undefined;
140
+ info(...args: Array<string | object>): void;
141
+ warn(...args: Array<string | object>): void;
142
+ error(...args: Array<string | object>): void;
143
+ }
144
+
113
145
  export interface Rules extends NamedRules {
114
146
  /**
115
147
  * The test function to be executed for skipping rules that doesn't return `true`.
116
148
  */
117
- test?: (options: RulesTestOptions) => boolean;
149
+ test?: Test;
118
150
  /**
119
151
  * The package name associated with the rule, used for debugging purposes.
120
152
  */
@@ -122,19 +154,25 @@ declare namespace createMetascraper {
122
154
  /**
123
155
  * allow any other string key to be
124
156
  * a rule-function (for ad-hoc metadata),
125
- * or the two special keys above.
157
+ * or the special keys above.
126
158
  **/
127
159
  [key: string]:
128
160
  | Array<RulesOptions>
129
161
  | RulesOptions
130
- | ((options: RulesTestOptions) => boolean)
162
+ | Test
131
163
  | string
132
164
  | undefined;
133
165
  }
134
166
 
135
- export type RulesOptions = (
136
- options: RulesTestOptions
137
- ) => string | null | undefined;
167
+ export interface RulesOptions {
168
+ (options: RulesTestOptions):
169
+ | string
170
+ | null
171
+ | undefined
172
+ | Promise<string | null | undefined>;
173
+ test?: Test;
174
+ pkgName?: string;
175
+ }
138
176
 
139
177
  export interface RulesTestOptions {
140
178
  htmlDom: import("cheerio").CheerioAPI;
package/src/rules.js CHANGED
@@ -2,18 +2,20 @@
2
2
 
3
3
  const castArray = value => (Array.isArray(value) ? value : [value])
4
4
 
5
+ const attachRuleMeta = (rules, test, pkgName) => {
6
+ for (const rule of rules) {
7
+ if (test) rule.test = test
8
+ if (pkgName) rule.pkgName = pkgName
9
+ }
10
+ }
11
+
5
12
  const loadRules = rulesBundle => {
6
13
  const acc = {}
7
14
 
8
- for (const { test, pkgName, ...rules } of rulesBundle) {
15
+ for (const { test, pkgName, validate: _validate, ...rules } of rulesBundle) {
9
16
  for (const [propName, innerRules] of Object.entries(rules)) {
10
17
  const processedRules = castArray(innerRules)
11
- if (test || pkgName) {
12
- for (const rule of processedRules) {
13
- if (test) rule.test = test
14
- if (pkgName) rule.pkgName = pkgName ?? 'unknown'
15
- }
16
- }
18
+ attachRuleMeta(processedRules, test, pkgName)
17
19
 
18
20
  if (acc[propName]) {
19
21
  acc[propName].push(...processedRules)
@@ -58,16 +60,12 @@ const mergeRules = (
58
60
  return Object.entries(result)
59
61
  }
60
62
 
61
- for (const { test, ...ruleSet } of rules) {
63
+ for (const { test, pkgName, validate: _validate, ...ruleSet } of rules) {
62
64
  for (const [propName, innerRules] of Object.entries(ruleSet)) {
63
65
  if (!shouldIncludeProp(propName)) continue
64
66
 
65
67
  const processedRules = castArray(innerRules)
66
- if (test) {
67
- for (const rule of processedRules) {
68
- rule.test = test
69
- }
70
- }
68
+ attachRuleMeta(processedRules, test, pkgName)
71
69
 
72
70
  if (result[propName]) {
73
71
  result[propName] = processedRules.concat(result[propName])