|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { |
| 4 | + $jsonld, |
| 5 | + memoizeOne, |
| 6 | + author, |
| 7 | + date, |
| 8 | + description, |
| 9 | + parseUrl, |
| 10 | + toRule |
| 11 | +} = require('@metascraper/helpers') |
| 12 | + |
| 13 | +const toAuthor = toRule(author) |
| 14 | +const toDate = toRule(date) |
| 15 | +const toDescription = toRule(description) |
| 16 | + |
| 17 | +const test = memoizeOne(url => parseUrl(url).domain === 'dribbble.com') |
| 18 | + |
| 19 | +const parseMetaDescription = memoizeOne(description => { |
| 20 | + if (!description || !description.includes(' | ')) { |
| 21 | + return { author: null, description: null } |
| 22 | + } |
| 23 | + |
| 24 | + const parts = description.split(' | ') |
| 25 | + // Remove the last part if it's "Connect with them on Dribbble..." |
| 26 | + if ( |
| 27 | + parts.length > 1 && |
| 28 | + parts[parts.length - 1].includes('Connect with them on Dribbble') |
| 29 | + ) { |
| 30 | + parts.pop() |
| 31 | + } |
| 32 | + |
| 33 | + return { |
| 34 | + author: parts.length > 0 ? parts[0] : null, |
| 35 | + description: parts.length > 1 ? parts.slice(1).join(' | ') : null |
| 36 | + } |
| 37 | +}) |
| 38 | + |
| 39 | +module.exports = () => { |
| 40 | + const rules = { |
| 41 | + author: [ |
| 42 | + toAuthor($jsonld('creator.name')), |
| 43 | + toAuthor($ => { |
| 44 | + const description = $('meta[name="description"]').attr('content') |
| 45 | + const { author } = parseMetaDescription(description) |
| 46 | + return author |
| 47 | + }), |
| 48 | + toAuthor($ => $('h1').first().text()) |
| 49 | + ], |
| 50 | + description: [ |
| 51 | + toDescription($ => { |
| 52 | + const desc = $('meta[name="description"]').attr('content') |
| 53 | + const { description } = parseMetaDescription(desc) |
| 54 | + return description |
| 55 | + }) |
| 56 | + ], |
| 57 | + date: [toDate($jsonld('uploadDate'))], |
| 58 | + publisher: () => 'Dribbble' |
| 59 | + } |
| 60 | + |
| 61 | + rules.test = ({ url }) => test(url) |
| 62 | + |
| 63 | + rules.pkgName = 'metascraper-dribbble' |
| 64 | + |
| 65 | + return rules |
| 66 | +} |
| 67 | + |
| 68 | +module.exports.test = test |
0 commit comments