Skip to content

Commit

Permalink
feat(logo): support filter method (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Aug 30, 2023
1 parent c10dbbb commit b53b726
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/metascraper-audio/test/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,6 @@ test('transistor.fm (twitter:player)', async t => {

t.is(
metadata.audio,
'https://chrt.fm/track/637E/2.gum.fm/op3.dev/e/dts.podtrac.com/redirect.mp3/media.transistor.fm/e83b42d0/9e93424b.mp3?src=player'
'https://media.transistor.fm/e83b42d0/9e93424b.mp3?src=player'
)
})
2 changes: 2 additions & 0 deletions packages/metascraper-clearbit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ $ npm install metascraper-clearbit --save

##### gotOpts

Type: `object`

Any option provided here will passed to [got#options](https://github.com/sindresorhus/got#options).

In addition, these options are set by default:
Expand Down
20 changes: 20 additions & 0 deletions packages/metascraper-logo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@
$ npm install metascraper-logo --save
```

## API

### metascraper-logo([options])

#### options

##### filter

Type: `function`

A function to filter results in case it passes a test implemented by the provided function. It is the `url` as the first parameter:

```js
const metascraper = require('metascraper')([
require('metascraper-logo')({
filter: url => url.endsWith('.png')
})
])
```

## License

**metascraper-logo** © [Microlink](https://microlink.io), released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>
Expand Down
43 changes: 26 additions & 17 deletions packages/metascraper-logo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@
const { $jsonld, url: urlFn, toRule } = require('@metascraper/helpers')
const { eq, get } = require('lodash')

const toUrl = toRule(urlFn)

const toLogoUrl = ($, propName) => {
const logo = $jsonld(propName)($)
const width = get(logo, 'width')
const height = get(logo, 'height')
return width && height && eq(width, height) && get(logo, 'url')
}

module.exports = () => ({
logo: [
toUrl($ => $('meta[property="og:logo"]').attr('content')),
toUrl($ => $('meta[itemprop="logo"]').attr('content')),
toUrl($ => $('img[itemprop="logo"]').attr('src')),
toUrl($ => toLogoUrl($, 'brand.logo')),
toUrl($ => toLogoUrl($, 'organization.logo')),
toUrl($ => toLogoUrl($, 'place.logo')),
toUrl($ => toLogoUrl($, 'product.logo')),
toUrl($ => toLogoUrl($, 'service.logo')),
toUrl($ => toLogoUrl($, 'publisher.logo')),
toUrl($ => toLogoUrl($, 'logo.url')),
toUrl($ => toLogoUrl($, 'logo'))
]
})
module.exports = ({ filter } = {}) => {
const mapper = filter
? async value => {
const result = urlFn(value)
return typeof result === 'string' ? await filter(result) : result
}
: urlFn

const toUrl = toRule(mapper)

return {
logo: [
toUrl($ => $('meta[property="og:logo"]').attr('content')),
toUrl($ => $('meta[itemprop="logo"]').attr('content')),
toUrl($ => $('img[itemprop="logo"]').attr('src')),
toUrl($ => toLogoUrl($, 'brand.logo')),
toUrl($ => toLogoUrl($, 'organization.logo')),
toUrl($ => toLogoUrl($, 'place.logo')),
toUrl($ => toLogoUrl($, 'product.logo')),
toUrl($ => toLogoUrl($, 'service.logo')),
toUrl($ => toLogoUrl($, 'publisher.logo')),
toUrl($ => toLogoUrl($, 'logo.url')),
toUrl($ => toLogoUrl($, 'logo'))
]
}
}

0 comments on commit b53b726

Please sign in to comment.