Skip to content

Commit 936749a

Browse files
authored
feat: add inline rules at the end (#163)
1 parent bd21875 commit 936749a

File tree

3 files changed

+22
-68
lines changed

3 files changed

+22
-68
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ The HTML markup for extracting the content.
277277

278278
Type: `Array`
279279

280-
You can pass additional rules on execution time. These rules will be merged with your loaded rules.
280+
You can pass additional rules to add on execution time.
281+
282+
These rules will be merged with your loaded [`rules`](#rules) at the beginning.
281283

282284
## Benchmark
283285

packages/metascraper/src/merge-rules.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
const { cloneDeep, concat, first, findIndex, forEach, chain } = require('lodash')
44

5-
module.exports = (rules, baseRules) => chain(rules)
6-
.reduce((acc, rules) => {
7-
forEach(rules, (rule, propName) => {
8-
const index = findIndex(acc, item => first(item) === propName)
9-
if (index !== -1) acc[index][1] = concat(acc[index][1], rule)
10-
else acc.push([propName, rule])
11-
})
12-
return acc
13-
}, cloneDeep(baseRules))
14-
.value()
5+
module.exports = (rules, baseRules) =>
6+
chain(rules)
7+
.reduce((acc, rules) => {
8+
forEach(rules, (rule, propName) => {
9+
// find the rules associated with `propName`
10+
const index = findIndex(acc, item => first(item) === propName)
11+
// if `propName` has more rule, add the new rule from the end
12+
if (index !== -1) acc[index][1] = concat(rule, ...acc[index][1])
13+
// otherwise, create an array of rules
14+
else acc.push([propName, rule])
15+
})
16+
return acc
17+
}, cloneDeep(baseRules))
18+
.value()

packages/metascraper/test/unit/merge-rules.js

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -49,76 +49,24 @@ it('add a new rule for a prop that exists', async () => {
4949
<meta charset="UTF-8">
5050
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5151
<meta http-equiv="X-UA-Compatible" content="ie=edge">
52+
<meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
5253
<title>Document</title>
5354
</head>
5455
<body>
55-
<div class="logos">
56-
<img class="logo" href="https://microlink.io/logo.png">
57-
<img class="logo" href="https://microlink.io/logo.png">
58-
<img class="logo" href="https://microlink.io/logo.png">
59-
<img class="logo" href="https://microlink.io/logo.png">
60-
</div>
61-
62-
<img class="main-logo" href="https://microlink.io/logo.png">
56+
<img id="logo" src="https://microlink.io/logo.png">
6357
<p>Hello World </p>
6458
</body>
6559
</html>
6660
`
6761

6862
const rules = [
6963
{
70-
foo: [() => 'bar']
64+
image: [({ htmlDom: $ }) => $('#logo').attr('src')]
7165
}
7266
]
7367

74-
const metascraper = require('../..')([
75-
{
76-
foo: [() => false, () => false, () => false]
77-
}
78-
])
79-
80-
const meta = await metascraper({ url, html, rules })
81-
should(meta.foo).be.equal('bar')
82-
})
83-
84-
it('rules are added from the end', async () => {
85-
const url = 'https://microlink.io'
86-
87-
const html = `
88-
<!DOCTYPE html>
89-
<html lang="en">
90-
<head>
91-
<meta charset="UTF-8">
92-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
93-
<meta http-equiv="X-UA-Compatible" content="ie=edge">
94-
<title>Document</title>
95-
</head>
96-
<body>
97-
<div class="logos">
98-
<img class="logo" href="https://microlink.io/logo.png">
99-
<img class="logo" href="https://microlink.io/logo.png">
100-
<img class="logo" href="https://microlink.io/logo.png">
101-
<img class="logo" href="https://microlink.io/logo.png">
102-
</div>
103-
104-
<img class="main-logo" href="https://microlink.io/logo.png">
105-
<p>Hello World </p>
106-
</body>
107-
</html>
108-
`
109-
110-
const rules = [
111-
{
112-
foo: [() => 'bar']
113-
}
114-
]
115-
116-
const metascraper = require('../..')([
117-
{
118-
foo: [() => false, () => false, () => 'baz']
119-
}
120-
])
68+
const metascraper = require('../..')([require('metascraper-image')()])
12169

12270
const meta = await metascraper({ url, html, rules })
123-
should(meta.foo).be.equal('baz')
71+
should(meta.image).be.equal('https://microlink.io/logo.png')
12472
})

0 commit comments

Comments
 (0)