metascraper 5.52.1 → 5.54.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
@@ -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.52.1",
5
+ "version": "5.54.0",
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.52.0",
30
+ "@metascraper/helpers": "5.54.0",
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": "e66c71fad2e6544dc276e12ca9175738cc6e0a75"
58
+ "gitHead": "23db3208c1dcc6367a705bdb9bf723fbaae259e0"
90
59
  }
package/src/index.d.ts CHANGED
@@ -89,7 +89,7 @@ declare namespace createMetascraper {
89
89
  */
90
90
  publisher?: string;
91
91
  /**
92
- * Get title property from HTML markup
92
+ * Get title property from HTML markup.
93
93
  * The package [metascraper-title](https://example.com/metascraper-title) needs to be loaded.
94
94
  */
95
95
  title?: string;
@@ -110,11 +110,43 @@ declare namespace createMetascraper {
110
110
  [C in keyof Metadata as string extends C ? never : C]?: Array<RulesOptions> | RulesOptions;
111
111
  };
112
112
 
113
+ /**
114
+ * Pre-run gate for a rule. A falsy result skips the rule entirely.
115
+ */
116
+ export type Test = (options: RulesTestOptions) => boolean;
117
+
118
+ /**
119
+ * Post-accept check for a rule candidate. Falsy or throw rejects the
120
+ * candidate and findRule continues with the next rule.
121
+ */
122
+ export type Validate = (
123
+ value: string,
124
+ options: RulesTestOptions,
125
+ debug: Debug
126
+ ) => boolean | Promise<boolean>;
127
+
128
+ /**
129
+ * The `metascraper:find-rule` logger, so a validate can explain a rejection.
130
+ * Only writes when `DEBUG=metascraper:find-rule` is enabled.
131
+ */
132
+ export interface Debug {
133
+ (...args: Array<string | object>): void;
134
+ readonly enabled: boolean | undefined;
135
+ info(...args: Array<string | object>): void;
136
+ warn(...args: Array<string | object>): void;
137
+ error(...args: Array<string | object>): void;
138
+ }
139
+
113
140
  export interface Rules extends NamedRules {
114
141
  /**
115
142
  * The test function to be executed for skipping rules that doesn't return `true`.
116
143
  */
117
- test?: (options: RulesTestOptions) => boolean;
144
+ 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;
118
150
  /**
119
151
  * The package name associated with the rule, used for debugging purposes.
120
152
  */
@@ -122,19 +154,27 @@ 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
163
+ | Validate
131
164
  | string
132
165
  | undefined;
133
166
  }
134
167
 
135
- export type RulesOptions = (
136
- options: RulesTestOptions
137
- ) => string | null | undefined;
168
+ export interface RulesOptions {
169
+ (options: RulesTestOptions):
170
+ | string
171
+ | null
172
+ | undefined
173
+ | Promise<string | null | undefined>;
174
+ test?: Test;
175
+ validate?: Validate;
176
+ pkgName?: string;
177
+ }
138
178
 
139
179
  export interface RulesTestOptions {
140
180
  htmlDom: import("cheerio").CheerioAPI;
package/src/rules.js CHANGED
@@ -2,18 +2,21 @@
2
2
 
3
3
  const castArray = value => (Array.isArray(value) ? value : [value])
4
4
 
5
+ const attachRuleMeta = (rules, test, validate, pkgName) => {
6
+ for (const rule of rules) {
7
+ if (test) rule.test = test
8
+ if (validate) rule.validate = validate
9
+ if (pkgName) rule.pkgName = pkgName
10
+ }
11
+ }
12
+
5
13
  const loadRules = rulesBundle => {
6
14
  const acc = {}
7
15
 
8
- for (const { test, pkgName, ...rules } of rulesBundle) {
16
+ for (const { test, validate, pkgName, ...rules } of rulesBundle) {
9
17
  for (const [propName, innerRules] of Object.entries(rules)) {
10
18
  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
- }
19
+ attachRuleMeta(processedRules, test, validate, pkgName)
17
20
 
18
21
  if (acc[propName]) {
19
22
  acc[propName].push(...processedRules)
@@ -58,16 +61,12 @@ const mergeRules = (
58
61
  return Object.entries(result)
59
62
  }
60
63
 
61
- for (const { test, ...ruleSet } of rules) {
64
+ for (const { test, validate, pkgName, ...ruleSet } of rules) {
62
65
  for (const [propName, innerRules] of Object.entries(ruleSet)) {
63
66
  if (!shouldIncludeProp(propName)) continue
64
67
 
65
68
  const processedRules = castArray(innerRules)
66
- if (test) {
67
- for (const rule of processedRules) {
68
- rule.test = test
69
- }
70
- }
69
+ attachRuleMeta(processedRules, test, validate, pkgName)
71
70
 
72
71
  if (result[propName]) {
73
72
  result[propName] = processedRules.concat(result[propName])