Skip to content

Commit

Permalink
fix(helpers): ensure image is a string (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats authored Jun 29, 2022
1 parent 9b8d64b commit 57e6df5
Show file tree
Hide file tree
Showing 10 changed files with 24,710 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/metascraper-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ const author = (value, opts) =>
isAuthor(value) ? getAuthor(value, opts) : undefined

const url = (value, { url = '' } = {}) => {
if (isEmpty(value)) return
if (!isString(value) || isEmpty(value)) return

try {
const absoluteUrl = normalizeUrl(url, value)
Expand Down Expand Up @@ -344,7 +344,9 @@ const $jsonld = propName => $ => {

const image = (value, opts) => {
const urlValue = url(value, opts)
return !isAudioUrl(urlValue, opts) && !isVideoUrl(urlValue, opts)
return urlValue !== undefined &&
!isAudioUrl(urlValue, opts) &&
!isVideoUrl(urlValue, opts)
? urlValue
: undefined
}
Expand Down
16 changes: 16 additions & 0 deletions packages/metascraper-helpers/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
date,
description,
extension,
image,
isAudioExtension,
isAudioUrl,
isAuthor,
Expand Down Expand Up @@ -143,6 +144,21 @@ test('.isVideoUrl', t => {
t.true(isVideoUrl('https://microlink.io/demo.mp4'))
})

test('.image', t => {
t.is(image('https://microlink.io/demo.png'), 'https://microlink.io/demo.png')
t.is(image('/demo.png'), undefined)
t.is(
image('/demo.png', { url: 'https://microlink.io' }),
'https://microlink.io/demo.png'
)
t.is(image('https://microlink.io/demo.mp4'), undefined)
t.is(image('https://microlink.io/demo.mp3'), undefined)
t.is(
image({ '@id': 'https://www.milanocittastato.it/#/schema/logo/image/' }),
undefined
)
})

test('.isImageUrl', t => {
t.false(isVideoUrl('demo.png'))
t.false(isVideoUrl('/demo.png'))
Expand Down
31 changes: 31 additions & 0 deletions packages/metascraper/test/integration/belfusto/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const { readFile } = require('fs').promises
const test = require('ava')
const { resolve } = require('path')

const metascraper = require('../../..')([
require('metascraper-author')(),
require('metascraper-date')(),
require('metascraper-description')(),
require('metascraper-video')(),
require('metascraper-image')(),
require('metascraper-lang')(),
require('metascraper-logo')(),
require('metascraper-logo-favicon')(),
require('metascraper-manifest')(),
require('metascraper-publisher')(),
require('metascraper-title')(),
require('metascraper-url')(),
require('metascraper-readability')()
])

const url = 'https://www.belfusto.com/tendencia/ergowear/'

test('belfusto', async t => {
const html = await readFile(resolve(__dirname, 'input.html'))
const metadata = await metascraper({ html, url })

console.log(metadata)
t.snapshot(metadata)
})
Loading

0 comments on commit 57e6df5

Please sign in to comment.