Skip to content

Commit

Permalink
Merge pull request #698 from microlinkhq/next
Browse files Browse the repository at this point in the history
fix(logo-favicon): ensure url exists
  • Loading branch information
Kikobeats committed Mar 14, 2024
2 parents 1eb7f77 + 126487a commit 357a543
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
4 changes: 3 additions & 1 deletion packages/metascraper-logo-favicon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ const getDomNodeSizes = (domNodes, attr, url) =>
.reduce((acc, domNode) => {
const relativeUrl = domNode.attribs[attr]
if (!relativeUrl) return acc
const normalizedUrl = normalizeUrl(url, relativeUrl)
if (!normalizedUrl) return acc
return [
...acc,
{
...domNode.attribs,
url: normalizeUrl(url, relativeUrl),
url: normalizedUrl,
size: getSize(url, domNode.attribs.sizes)
}
]
Expand Down
30 changes: 19 additions & 11 deletions packages/metascraper-logo-favicon/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ test('get the biggest icon possible', async t => {
'<link rel="icon" type="image/png" href="/logo/favicon-128.png" sizes="128x128">'
])
const metadata = await metascraper({ url, html })
t.is(
metadata.logo,
'https://cdn.microlink.io/logo/favicon-196x196.png'
)
t.is(metadata.logo, 'https://cdn.microlink.io/logo/favicon-196x196.png')
})

test('detect `rel="fluid-icon"`', async t => {
Expand Down Expand Up @@ -139,7 +136,10 @@ test('detect `rel="apple-touch-icon-precomposed"`', async t => {
'<link rel="apple-touch-icon-precomposed" sizes="144x144" href="logo/apple-touch-icon-144x144.png">'
])
const metadata = await metascraper({ url, html })
t.is(metadata.logo, 'https://cdn.microlink.io/logo/apple-touch-icon-144x144.png')
t.is(
metadata.logo,
'https://cdn.microlink.io/logo/apple-touch-icon-144x144.png'
)
})

test('detect `rel="apple-touch-icon"`', async t => {
Expand All @@ -149,7 +149,10 @@ test('detect `rel="apple-touch-icon"`', async t => {
'<link rel="apple-touch-icon" sizes="144x144" href="logo/apple-touch-icon-144x144.png">'
])
const metadata = await metascraper({ url, html })
t.is(metadata.logo, 'https://cdn.microlink.io/logo/apple-touch-icon-144x144.png')
t.is(
metadata.logo,
'https://cdn.microlink.io/logo/apple-touch-icon-144x144.png'
)
})

test('detect `rel="shortcut icon"`', async t => {
Expand All @@ -159,10 +162,7 @@ test('detect `rel="shortcut icon"`', async t => {
'<link rel="shortcut icon" href="logo/favicon.ico">'
])
const metadata = await metascraper({ url, html })
t.is(
metadata.logo,
'https://cdn.microlink.io/logo/favicon.ico'
)
t.is(metadata.logo, 'https://cdn.microlink.io/logo/favicon.ico')
})

test('detect `rel="mask icon"`', async t => {
Expand Down Expand Up @@ -236,10 +236,18 @@ test('resolve logo using from google associated with the domain', async t => {
t.true(metadata.logo.includes('gstatic'))
})

test('avoid data URI when data length is 0', async t => {
test('avoid empty data URI', async t => {
const url = 'https://www.adobe.com/'
const html = '<link rel="icon" href="data:,">'
const metascraper = createMetascraper()
const metadata = await metascraper({ url, html })
t.is(metadata.logo, 'https://www.adobe.com/favicon.ico')
})

test('avoid wrong data URI', async t => {
const url = 'https://www.adobe.com/'
const html = '<link rel="icon" href="data:">'
const metascraper = createMetascraper()
const metadata = await metascraper({ url, html })
t.is(metadata.logo, 'https://www.adobe.com/favicon.ico')
})
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Generated by [AVA](https://avajs.dev).
description: '[...] it’s natural to think that San Francisco’s new Nasdaq Entrepreneurial Center,…',
image: 'https://s.hdnux.com/photos/45/53/13/9876166/6/rawImage.jpg',
lang: 'en',
logo: 'https://www.sfchronicle.com/sites/premiumsfgate/apple-touch-icon-152x152.png',
logo: 'https://www.sfchronicle.com/sites/premiumsfgate/apple-touch-icon-196x196.png',
publisher: 'SFGATE',
title: 'Nasdaq center in SF offers free classes for entrepreneurs',
url: 'https://www.sfchronicle.com/business/article/Nasdaq-center-in-SF-offers-free-classes-for-7338290.php',
Expand Down
Binary file not shown.

0 comments on commit 357a543

Please sign in to comment.