@metascraper/helpers 5.43.3 → 5.43.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +39 -26
  2. package/package.json +3 -2
package/index.js CHANGED
@@ -11,6 +11,7 @@ const _normalizeUrl = require('normalize-url')
11
11
  const smartquotes = require('smartquotes')
12
12
  const { decodeHTML } = require('entities')
13
13
  const iso6393 = require('iso-639-3/to-1')
14
+ const dataUri = require('data-uri-utils')
14
15
  const hasValues = require('has-values')
15
16
  const chrono = require('chrono-node')
16
17
  const isIso = require('isostring')
@@ -28,12 +29,9 @@ const urlRegex = require('url-regex-safe')({
28
29
 
29
30
  const {
30
31
  chain,
31
- eq,
32
32
  flow,
33
33
  get,
34
- includes,
35
34
  invoke,
36
- isArray,
37
35
  isBoolean,
38
36
  isDate,
39
37
  isEmpty,
@@ -78,6 +76,7 @@ const toTitle = str =>
78
76
  const VIDEO = 'video'
79
77
  const AUDIO = 'audio'
80
78
  const IMAGE = 'image'
79
+ const PDF = 'pdf'
81
80
 
82
81
  const imageExtensions = chain(require('image-extensions'))
83
82
  .concat(['avif'])
@@ -106,7 +105,8 @@ const videoExtensions = chain(require('video-extensions'))
106
105
  const EXTENSIONS = {
107
106
  ...imageExtensions,
108
107
  ...audioExtensions,
109
- ...videoExtensions
108
+ ...videoExtensions,
109
+ [PDF]: PDF
110
110
  }
111
111
 
112
112
  const REGEX_BY = /^[\s\n]*by[\s\n]+|@[\s\n]*/i
@@ -203,31 +203,39 @@ const protocol = url => {
203
203
  return protocol.replace(':', '')
204
204
  }
205
205
 
206
- const isMediaTypeUrl = (url, type, { ext, ...opts } = {}) =>
207
- isUrl(url, opts) && isMediaTypeExtension(url, type, ext)
206
+ const isExtension = (url, type, ext = extension(url)) =>
207
+ type === EXTENSIONS[ext]
208
208
 
209
- const isMediaTypeExtension = (url, type, ext) =>
210
- eq(type, get(EXTENSIONS, ext || extension(url)))
209
+ const isExtensionUrl = (url, type, { ext, ...opts } = {}) =>
210
+ isUrl(url, opts) && isExtension(url, type, ext)
211
+
212
+ const createIsUrl = type => (url, opts) => isExtensionUrl(url, type, opts)
213
+
214
+ const isVideoUrl = createIsUrl(VIDEO)
215
+
216
+ const isAudioUrl = createIsUrl(AUDIO)
217
+
218
+ const isImageUrl = createIsUrl(IMAGE)
219
+
220
+ const isPdfUrl = createIsUrl(PDF)
211
221
 
212
222
  const isMediaUrl = memoizeOne(
213
223
  (url, opts) =>
214
224
  isImageUrl(url, opts) || isVideoUrl(url, opts) || isAudioUrl(url, opts)
215
225
  )
216
226
 
217
- const isVideoUrl = (url, opts) => isMediaTypeUrl(url, VIDEO, opts)
218
-
219
- const isAudioUrl = (url, opts) => isMediaTypeUrl(url, AUDIO, opts)
220
-
221
- const isImageUrl = (url, opts) => isMediaTypeUrl(url, IMAGE, opts)
222
-
223
227
  const isMediaExtension = url =>
224
228
  isImageExtension(url) || isVideoExtension(url) || isAudioExtension(url)
225
229
 
226
- const isVideoExtension = url => isMediaTypeExtension(url, VIDEO)
230
+ const createIsExtension = type => url => isExtension(url, type)
227
231
 
228
- const isAudioExtension = url => isMediaTypeExtension(url, AUDIO)
232
+ const isVideoExtension = createIsExtension(VIDEO)
229
233
 
230
- const isImageExtension = url => isMediaTypeExtension(url, IMAGE)
234
+ const isAudioExtension = createIsExtension(AUDIO)
235
+
236
+ const isImageExtension = createIsExtension(IMAGE)
237
+
238
+ const isPdfExtension = createIsExtension(PDF)
231
239
 
232
240
  const isContentType =
233
241
  extensions =>
@@ -323,16 +331,14 @@ const lang = input => {
323
331
  const key = toLower(condenseWhitespace(input))
324
332
  if (input.length === 3) return iso6393[key]
325
333
  const lang = toLower(key.substring(0, 2))
326
- return includes(iso6393Values, lang) ? lang : undefined
334
+ return iso6393Values.includes(lang) ? lang : undefined
327
335
  }
328
336
 
329
337
  const title = (value, { removeSeparator = false, ...opts } = {}) =>
330
338
  isString(value) ? titleize(value, { removeSeparator, ...opts }) : undefined
331
339
 
332
- const isMime = (contentType, type) => {
333
- const ext = mimeExtension(contentType)
334
- return eq(type, get(EXTENSIONS, ext))
335
- }
340
+ const isMime = (contentType, type) =>
341
+ type === get(EXTENSIONS, mimeExtension(contentType))
336
342
 
337
343
  memoizeOne.EqualityUrlAndHtmlDom = (newArgs, oldArgs) =>
338
344
  newArgs[0] === oldArgs[0] && newArgs[1].html() === oldArgs[1].html()
@@ -370,11 +376,17 @@ const $jsonld = propName => $ => {
370
376
 
371
377
  const image = (value, opts) => {
372
378
  const urlValue = url(value, opts)
373
- return urlValue !== undefined &&
379
+
380
+ const result =
381
+ urlValue !== undefined &&
374
382
  !isAudioUrl(urlValue, opts) &&
375
383
  !isVideoUrl(urlValue, opts)
376
- ? urlValue
377
- : undefined
384
+ ? urlValue
385
+ : undefined
386
+
387
+ if (!dataUri.test(result)) return result
388
+ const buffer = dataUri.toBuffer(dataUri.normalize(result))
389
+ return buffer.length ? result : undefined
378
390
  }
379
391
 
380
392
  const logo = image
@@ -490,7 +502,6 @@ module.exports = {
490
502
  has,
491
503
  image,
492
504
  imageExtensions,
493
- isArray,
494
505
  isAudioExtension,
495
506
  isAudioUrl,
496
507
  isAuthor,
@@ -499,6 +510,8 @@ module.exports = {
499
510
  isMediaExtension,
500
511
  isMediaUrl,
501
512
  isMime,
513
+ isPdfExtension,
514
+ isPdfUrl,
502
515
  isString,
503
516
  isUrl,
504
517
  isVideoExtension,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@metascraper/helpers",
3
3
  "description": "Collection of helper functions used by metascraper",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-helpers",
5
- "version": "5.43.3",
5
+ "version": "5.43.7",
6
6
  "main": "index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -25,6 +25,7 @@
25
25
  "audio-extensions": "0.0.0",
26
26
  "chrono-node": "~2.7.4",
27
27
  "condense-whitespace": "~2.0.0",
28
+ "data-uri-utils": "~1.0.7",
28
29
  "entities": "~4.5.0",
29
30
  "file-extension": "~4.0.5",
30
31
  "has-values": "~2.0.1",
@@ -62,5 +63,5 @@
62
63
  "publishConfig": {
63
64
  "access": "public"
64
65
  },
65
- "gitHead": "ba67a534c6c0695a538c03d83cc5387db63e3afd"
66
+ "gitHead": "9a22b5bf54e2cb7465c38191dd927128d9cf2706"
66
67
  }