-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid use generic publisher (#141)
* fix: avoid use generic publisher * fix: move rules into favicon package * fix: add keywords meta * test: update snapshots * test: add missing dependency * test: update snapshots
- Loading branch information
Showing
98 changed files
with
225 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
}, | ||
"keywords": [ | ||
"clearbit", | ||
"logo", | ||
"metascraper" | ||
], | ||
"dependencies": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,4 @@ | ||
exports['html lang property 1'] = { | ||
"lang": "pl", | ||
"author": "Jakub Majmurek", | ||
"title": "Churchill, bohater naszych czasów / Film / dwutygodnik.com", | ||
"publisher": "Dwutygodnik", | ||
"image": "http://www.dwutygodnik.com/public/media/article/image_full/7615.png", | ||
"date": "2018-01-01T12:00:00.000Z", | ||
"description": "Wysyp filmów o Churchillu w pobrexitowej Brytanii, wydaje się czymś zrozumiałym. Uosabia on ostatni moment prawdziwej wielkości Zjednoczonego Królestwa – wspomnienie tej historycznej chwili pozwala oswoić traumy i lęki", | ||
"logo": "http://www.dwutygodnik.com/public/frontend/image_v4/favicon.ico", | ||
"url": "http://www.dwutygodnik.com/artykul/7615-churchill-bohater-naszych-czasow.html" | ||
"lang": "pl" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,73 @@ | ||
'use strict' | ||
|
||
const { flow, first, toNumber, split, chain, concat } = require('lodash') | ||
const { resolve: resolveUrl, URL } = require('url') | ||
const { url: urlFn } = require('@metascraper/helpers') | ||
const got = require('got') | ||
|
||
const getFaviconUrl = url => { | ||
const {origin} = new URL(url) | ||
return resolveUrl(origin, 'favicon.ico') | ||
const getSize = flow([str => split(str, 'x'), first, toNumber]) | ||
|
||
const getDomNodeSizes = (domNodes, attr) => | ||
chain(domNodes) | ||
.map(({ attribs }) => ({ | ||
size: getSize(attribs.sizes), | ||
link: attribs[attr] | ||
})) | ||
.value() | ||
|
||
const getSizes = ($, collection) => | ||
chain(collection) | ||
.reduce((acc, { tag, attr }) => { | ||
const domNodes = $(tag).get() | ||
const selectors = getDomNodeSizes(domNodes, attr) | ||
return concat(acc, selectors) | ||
}, []) | ||
.sortBy(({ size }) => -size) | ||
.value() | ||
|
||
const sizeSelectors = [ | ||
{ tag: 'link[rel="apple-touch-icon"]', attr: 'href' }, | ||
{ tag: 'link[rel="apple-touch-icon-precomposed"]', attr: 'href' }, | ||
{ tag: 'meta[name="msapplication-TileImage"]', attr: 'content' }, | ||
{ tag: 'link[rel="icon"]', attr: 'href' }, | ||
{ tag: 'link[rel="shortcut icon"]', attr: 'href' } | ||
] | ||
|
||
/** | ||
* Wrap a rule with validation and formatting logic. | ||
* | ||
* @param {Function} rule | ||
* @return {Function} wrapped | ||
*/ | ||
|
||
const wrap = rule => ({ htmlDom, url }) => { | ||
const value = rule(htmlDom) | ||
return urlFn(value, { url }) | ||
} | ||
|
||
/** | ||
* Rules. | ||
*/ | ||
module.exports = () => ({ | ||
logo: [({ htmlDom: $, meta, url }) => getFaviconUrl(url)] | ||
logo: [ | ||
wrap($ => { | ||
const sizes = getSizes($, sizeSelectors) | ||
const size = chain(sizes) | ||
.first() | ||
.get('link') | ||
.value() | ||
return size | ||
}), | ||
async ({ url }) => { | ||
const { origin } = new URL(url) | ||
const logoUrl = resolveUrl(origin, 'favicon.ico') | ||
|
||
try { | ||
await got.head(logoUrl) | ||
return logoUrl | ||
} catch (err) { | ||
return null | ||
} | ||
} | ||
] | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.