metascraper 5.48.0 → 5.49.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": "A library to easily scrape metadata from an article on the web using Open Graph, JSON+LD, regular HTML metadata, and series of fallbacks.",
4
4
  "homepage": "https://metascraper.js.org",
5
- "version": "5.48.0",
5
+ "version": "5.49.0",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "repository": {
@@ -86,5 +86,5 @@
86
86
  "tsd": {
87
87
  "directory": "test/types"
88
88
  },
89
- "gitHead": "13ea77daf72c3a2f2b3e47d2d386b39485a59491"
89
+ "gitHead": "e5ff13c9d3e6be235d0e952af4f205580977c067"
90
90
  }
package/src/index.d.ts CHANGED
@@ -39,6 +39,12 @@ declare namespace createMetascraper {
39
39
  * These properties will be filtered out before processing the rules.
40
40
  */
41
41
  omitPropNames?: Set<string>;
42
+ /**
43
+ * A Set of property names to pick for the metadata extraction process.
44
+ * When provided, only rules for these properties will be executed.
45
+ * Takes precedence over omitPropNames when both are specified.
46
+ */
47
+ pickPropNames?: Set<string>;
42
48
  }
43
49
 
44
50
  export interface Metadata {
package/src/index.js CHANGED
@@ -18,6 +18,7 @@ module.exports = rules => {
18
18
  rules: inlineRules,
19
19
  validateUrl = true,
20
20
  omitPropNames = new Set(),
21
+ pickPropNames,
21
22
  ...props
22
23
  } = {}) => {
23
24
  if (validateUrl && !isUrl(url)) {
@@ -30,7 +31,7 @@ module.exports = rules => {
30
31
  return getData({
31
32
  url,
32
33
  htmlDom: htmlDom ?? load(html, { baseURI: url }),
33
- rules: mergeRules(inlineRules, loadedRules, omitPropNames),
34
+ rules: mergeRules(inlineRules, loadedRules, omitPropNames, pickPropNames),
34
35
  ...props
35
36
  })
36
37
  }
package/src/rules.js CHANGED
@@ -25,12 +25,25 @@ const loadRules = rulesBundle => {
25
25
  return Object.entries(acc)
26
26
  }
27
27
 
28
- const mergeRules = (rules, baseRules, omitPropNames = new Set()) => {
28
+ const mergeRules = (
29
+ rules,
30
+ baseRules,
31
+ omitPropNames = new Set(),
32
+ pickPropNames
33
+ ) => {
29
34
  const result = {}
30
35
 
36
+ // Helper function to determine if a property should be included
37
+ const shouldIncludeProp = propName => {
38
+ if (pickPropNames && pickPropNames.size > 0) {
39
+ return pickPropNames.has(propName)
40
+ }
41
+ return !omitPropNames.has(propName)
42
+ }
43
+
31
44
  // Process base rules first (shallow clone arrays only)
32
45
  for (const [propName, ruleArray] of baseRules) {
33
- if (!omitPropNames.has(propName)) {
46
+ if (shouldIncludeProp(propName)) {
34
47
  result[propName] = [...ruleArray] // Shallow clone array
35
48
  }
36
49
  }
@@ -43,7 +56,7 @@ const mergeRules = (rules, baseRules, omitPropNames = new Set()) => {
43
56
  // Process inline rules
44
57
  for (const { test, ...ruleSet } of rules) {
45
58
  for (const [propName, innerRules] of Object.entries(ruleSet)) {
46
- if (omitPropNames.has(propName)) continue
59
+ if (!shouldIncludeProp(propName)) continue
47
60
 
48
61
  const processedRules = Array.isArray(innerRules)
49
62
  ? [...innerRules]