Skip to content

Commit

Permalink
Ensure favicon path is absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 29, 2017
1 parent 2aab9e7 commit 266b404
Show file tree
Hide file tree
Showing 7 changed files with 13,957 additions and 6 deletions.
14 changes: 14 additions & 0 deletions packages/metascraper-logo-favicon/.metascraperrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"rules": [
"metascraper-amazon",
"metascraper-author",
"metascraper-date",
"metascraper-description",
"metascraper-image",
"metascraper-logo",
"metascraper-logo-favicon",
"metascraper-publisher",
"metascraper-title",
"metascraper-url"
]
}
10 changes: 10 additions & 0 deletions packages/metascraper-logo-favicon/__snapshots__/index.js.snap-shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
exports['create an absolute faivcon url if the logo is not present 1'] = {
"author": "Mat Follas",
"title": "Vegetable Perfection: 100 delicious recipes for roots, bulbs, shoots and stems",
"publisher": "Amazon",
"image": "https://images-eu.ssl-images-amazon.com/images/I/61Q2c36lYeL._SX218_BO1,204,203,200_QL40_.jpg",
"description": "Buy Vegetable Perfection: 100 delicious recipes for roots, bulbs, shoots and stems by Mat Follas (ISBN: 9781849757096) from Amazon’s Book Store. Everyday low prices and free delivery on eligible orders.",
"logo": "https://www.amazon.co.uk/favicon.ico",
"url": "https://www.amazon.co.uk/Vegetable-Perfection-tasty-recipes-shoots/dp/1849757097/ref=asap_bc?ie=UTF8"
}

9 changes: 7 additions & 2 deletions packages/metascraper-logo-favicon/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict'

const { getUrl } = require('@metascraper/helpers')
const { resolve: resolveUrl, URL } = require('url')

const getFaviconUrl = url => {
const {origin} = new URL(url)
return resolveUrl(origin, 'favicon.ico')
}

module.exports = () => ({
logo: [({ htmlDom: $, meta, url }) => getUrl(url, `favicon.ico`)]
logo: [({ htmlDom: $, meta, url }) => getFaviconUrl(url)]
})
11 changes: 7 additions & 4 deletions packages/metascraper-logo-favicon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
"clearbit",
"metascraper"
],
"dependencies": {
"@metascraper/helpers": "^3.3.0"
},
"devDependencies": {
"clear-module": "latest",
"lodash": "latest",
"mocha": "latest",
"nyc": "latest",
"should": "latest",
"snap-shot": "latest",
"standard": "latest"
},
"engines": {
Expand All @@ -33,7 +36,7 @@
"index.js"
],
"scripts": {
"test": "exit 0"
"test": "NODE_PATH=.. TZ=UTC NODE_ENV=test nyc mocha test"
},
"license": "MIT",
"lint-staged": {
Expand Down
13,881 changes: 13,881 additions & 0 deletions packages/metascraper-logo-favicon/test/fixtures/input.html

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions packages/metascraper-logo-favicon/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const clearModule = require('clear-module')
const snapshot = require('snap-shot')
const { promisify } = require('util')
const { resolve } = require('path')
const { omit } = require('lodash')
const fs = require('fs')

const readFile = promisify(fs.readFile)

describe('metascraper amazon integration', () => {
before(() => {
clearModule.all()
process.env.METASCRAPER_CONFIG_CWD = __dirname
})

after(() => {
clearModule.all()
delete process.env.METASCRAPER_CONFIG_CWD
})

describe('metascraper logo favicon', () => {
it('create an absolute faivcon url if the logo is not present', async () => {
const metascraper = require('metascraper')
const html = await readFile(resolve(__dirname, 'fixtures/input.html'))
const url = 'https://www.amazon.co.uk/Vegetable-Perfection-tasty-recipes-shoots/dp/1849757097/ref=asap_bc?ie=UTF8'
const meta = omit(await metascraper({ html, url }), ['date'])
snapshot(meta)
})
})
})
6 changes: 6 additions & 0 deletions packages/metascraper-logo-favicon/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--require should
--reporter spec
--timeout 120000
--slow 300
--bail
--recursive

0 comments on commit 266b404

Please sign in to comment.