-
-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathindex.js
More file actions
652 lines (544 loc) · 15.7 KB
/
Copy pathindex.js
File metadata and controls
652 lines (544 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
'use strict'
const memoizeOne = require('memoize-one').default || require('memoize-one')
const debug = require('debug-logfmt')('metascraper:find-rule')
const condenseWhitespace = require('condense-whitespace').default
const mime = require('mime').default
const mimeExtension = mime.getExtension.bind(mime)
const capitalize = require('microsoft-capitalize')
const isRelativeUrl = require('is-relative-url').default
const fileExtension = require('file-extension')
const _normalizeUrl = require('normalize-url').default
const { jsonrepair } = require('jsonrepair')
const smartquotes = require('smartquotes')
const { decodeHTML } = require('entities')
const { iso6393To1: iso6393 } = require('iso-639-3/iso6393-to-1.js')
const dataUri = require('data-uri-utils')
const hasValues = require('has-values')
const chrono = require('chrono-node')
const isIso = require('isostring')
const isUri = require('is-uri')
const { URL } = require('url')
const tldts = require('tldts')
const METASCRAPER_RE2 = process.env.METASCRAPER_RE2
? process.env.METASCRAPER_RE2 === 'true'
: undefined
const urlRegexForTest = require('url-regex-safe')({
exact: true,
parens: true,
re2: METASCRAPER_RE2
})
const urlRegexForMatch = require('url-regex-safe')({
re2: METASCRAPER_RE2
})
const {
chain,
flow,
get,
invoke,
isBoolean,
isDate,
isEmpty,
isNumber,
isString,
lte,
memoize,
replace,
size,
toLower,
toString
} = require('lodash')
const iso6393Values = Object.values(iso6393)
const parseUrl = memoize(tldts.parse)
const toTitle = str =>
capitalize(str, [
'CLI',
'API',
'HTTP',
'HTTPS',
'JSX',
'DNS',
'URL',
'CI',
'CDN',
'package.json',
'GitHub',
'GitLab',
'CSS',
'JS',
'JavaScript',
'TypeScript',
'HTML',
'WordPress',
'JavaScript',
'Node.js'
])
const VIDEO = 'video'
const AUDIO = 'audio'
const IMAGE = 'image'
const PDF = 'pdf'
const imageExtensions = chain(require('image-extensions'))
.concat(['avif'])
.reduce((acc, ext) => {
acc[ext] = IMAGE
return acc
}, {})
.value()
const audioExtensions = chain(require('audio-extensions'))
.concat(['mpga'])
.difference(['mp4'])
.reduce((acc, ext) => {
acc[ext] = AUDIO
return acc
}, {})
.value()
const videoExtensions = chain(require('video-extensions').default)
.reduce((acc, ext) => {
acc[ext] = VIDEO
return acc
}, {})
.value()
const EXTENSIONS = {
...imageExtensions,
...audioExtensions,
...videoExtensions,
[PDF]: PDF
}
const REGEX_BY = /^[\s\n]*by[\s\n]+|@[\s\n]*/i
const REGEX_LOCATION = /^[A-Z\s]+\s+[-—–]\s+/
const REGEX_TITLE_SEPARATOR = /^[^|\-/•—]+/
const AUTHOR_MAX_LENGTH = 128
const removeLocation = value => replace(value, REGEX_LOCATION, '')
const isUrl = (url, { relative = false } = {}) =>
relative ? isRelativeUrl(url) : urlRegexForTest.test(url)
const urlObject = (...args) => {
try {
return new URL(...args)
} catch (_) {
return { toString: () => '' }
}
}
const absoluteUrl = (baseUrl, relativePath) => {
if (isEmpty(relativePath)) return urlObject(baseUrl).toString()
return urlObject(relativePath, baseUrl).toString()
}
const sanetizeUrl = (url, opts) =>
_normalizeUrl(url, {
stripWWW: false,
sortQueryParameters: false,
removeSingleSlash: false,
removeTrailingSlash: false,
...opts
})
const normalizeUrl = (baseUrl, relativePath, opts) => {
try {
const absolute = absoluteUrl(baseUrl, relativePath)
// normalize-url v9 no longer rejects `javascript:` URLs; keep them out
if (urlObject(absolute).protocol === 'javascript:') return undefined
return sanetizeUrl(absolute, opts)
} catch (_) {}
}
const removeBy = flow([
value => value.replace(REGEX_BY, ''),
condenseWhitespace
])
const removeSeparator = title =>
condenseWhitespace((REGEX_TITLE_SEPARATOR.exec(title) || [])[0] || title)
const createTitle = flow([condenseWhitespace, smartquotes])
const titleize = (src, opts = {}) => {
let title = createTitle(src)
if (opts.removeBy) title = removeBy(title)
if (opts.removeSeparator) title = removeSeparator(title)
if (opts.capitalize) title = toTitle(title)
return title
}
const $filter = ($, matchedEl, fn = $filter.fn) => {
let matched
matchedEl.each(function () {
const result = fn($(this))
if (result) {
matched = result
return false
}
})
return matched
}
$filter.fn = el => condenseWhitespace(el.text())
const isAuthor = (str, opts = { relative: false }) =>
!isUrl(str, opts) &&
!isEmpty(str) &&
isString(str) &&
lte(size(str), AUTHOR_MAX_LENGTH)
const getAuthor = (str, { removeBy = true, ...opts } = {}) =>
titleize(str, { removeBy, ...opts })
const protocol = url => {
const { protocol = '' } = urlObject(url)
return protocol.replace(':', '')
}
const isExtension = (url, type, ext = extension(url)) =>
type === EXTENSIONS[ext]
const isExtensionUrl = (url, type, { ext, ...opts } = {}) =>
isUrl(url, opts) && isExtension(url, type, ext)
const createIsUrl = type => (url, opts) => isExtensionUrl(url, type, opts)
const isVideoUrl = createIsUrl(VIDEO)
const isAudioUrl = createIsUrl(AUDIO)
const isImageUrl = createIsUrl(IMAGE)
const isPdfUrl = createIsUrl(PDF)
const isMediaUrl = (url, opts) =>
isImageUrl(url, opts) || isVideoUrl(url, opts) || isAudioUrl(url, opts)
const isMediaExtension = url =>
isImageExtension(url) || isVideoExtension(url) || isAudioExtension(url)
const createIsExtension = type => url => isExtension(url, type)
const isVideoExtension = createIsExtension(VIDEO)
const isAudioExtension = createIsExtension(AUDIO)
const isImageExtension = createIsExtension(IMAGE)
const isPdfExtension = createIsExtension(PDF)
const isContentType =
extensions =>
({ type = '' } = {}) =>
extensions.some(extension => type.endsWith(extension))
const isVideoContentType = isContentType(Object.keys(videoExtensions))
const isAudioContentType = isContentType(Object.keys(audioExtensions))
const extension = (str = '') => {
const url = urlObject(
str,
isRelativeUrl(str) ? 'http://localhost' : undefined
)
url.hash = ''
url.search = ''
return fileExtension(url.toString())
}
const description = (value, opts) =>
isString(value) ? getDescription(value, opts) : undefined
const getDescription = (
str,
{ truncateLength = Number.MAX_SAFE_INTEGER, ellipsis = '…', ...opts } = {}
) => {
let truncated = str.slice(0, truncateLength)
if (truncated.length < str.length) truncated = truncated.trim() + ellipsis
const description = removeLocation(truncated)
return titleize(description, opts).replace(/\s?\.\.\.?$/, ellipsis)
}
const publisher = value =>
isString(value) ? condenseWhitespace(value) : undefined
const author = (value, opts) =>
isAuthor(value) ? getAuthor(value, opts) : undefined
const url = (value, { url = '' } = {}) => {
if (!isString(value) || isEmpty(value)) return
try {
const absoluteUrl = normalizeUrl(url, value)
if (isUrl(absoluteUrl)) return absoluteUrl
} catch (_) {}
let sanitizedValue = value
if (value.startsWith('data:')) {
if (!dataUri.test(value)) return undefined
const [header, data] = value.split(',')
const cleanData = data.replace(/\s+/g, '')
sanitizedValue = `${header},${cleanData}`
}
return isUri(sanitizedValue) ? sanitizedValue : undefined
}
const getISODate = date =>
date && !Number.isNaN(date.getTime()) ? date.toISOString() : undefined
const date = value => {
if (isDate(value)) return value.toISOString()
if (!(isString(value) || isNumber(value))) return
// remove whitespace for easier parsing
if (isString(value)) value = condenseWhitespace(value)
// convert isodates to restringify, because sometimes they are truncated
if (isIso(value)) return new Date(value).toISOString()
if (/^\d{4}$/.test(value)) return new Date(toString(value)).toISOString()
let isoDate
if (isString(value)) {
for (const item of value.split('\n').filter(Boolean)) {
const parsed = chrono.parseDate(item)
isoDate = getISODate(parsed)
if (isoDate) break
}
} else {
if (value >= 1e16 || value <= -1e16) {
// nanoseconds
value = Math.floor(value / 1000000)
} else if (value >= 1e14 || value <= -1e14) {
// microseconds
value = Math.floor(value / 1000)
} else if (!(value >= 1e11) || value <= -3e10) {
// seconds
value = value * 1000
}
isoDate = getISODate(new Date(value))
}
return isoDate
}
const lang = input => {
if (isEmpty(input) || !isString(input)) return
const key = toLower(condenseWhitespace(input))
if (input.length === 3) return iso6393[key]
const lang = toLower(key.substring(0, 2))
return iso6393Values.includes(lang) ? lang : undefined
}
const title = (value, { removeSeparator = false, ...opts } = {}) =>
isString(value) ? titleize(value, { removeSeparator, ...opts }) : undefined
const isMime = (contentType, type) =>
type === get(EXTENSIONS, mimeExtension(contentType))
const isSameHtmlDom = (newHtmlDom, oldHtmlDom) => newHtmlDom === oldHtmlDom
memoizeOne.EqualityUrlAndHtmlDom = (newArgs, oldArgs) =>
newArgs[0] === oldArgs[0] && isSameHtmlDom(newArgs[1], oldArgs[1])
memoizeOne.EqualityFirstArgument = (newArgs, oldArgs) =>
newArgs[0] === oldArgs[0]
const parseJSON = text => {
try {
return JSON.parse(text)
} catch {
try {
return JSON.parse(jsonrepair(text))
} catch {
return undefined
}
}
}
const jsonld = memoizeOne(
$ =>
$('script[type="application/ld+json"]')
.map((_, element) => {
const el = $(element)
const text = $(el).contents().text()
const json = parseJSON(text)
if (!json) return false
const { '@graph': graph, ...props } = json
return Array.isArray(graph)
? graph.map(item => ({ ...props, ...item }))
: graph
? props
: json
})
.get()
.filter(Boolean),
(newArgs, oldArgs) => isSameHtmlDom(newArgs[0], oldArgs[0])
)
/**
* Recursive schema.org search with array traversal and object-to-name resolution.
* Returns array of primitive values (strings, numbers, booleans) for the given path.
*/
function searchSchemaResults (data, props, isExact) {
if (data === null || data === undefined) return []
if (
typeof data === 'string' ||
typeof data === 'number' ||
typeof data === 'boolean'
) {
return props.length === 0 ? [data] : []
}
if (Array.isArray(data)) {
const current = props[0]
if (current && /^\[\d+\]$/.test(current)) {
const index = parseInt(current.slice(1, -1), 10)
if (data[index] !== undefined) {
return searchSchemaResults(data[index], props.slice(1), isExact)
}
return []
}
if (
props.length === 0 &&
data.every(item => typeof item === 'string' || typeof item === 'number')
) {
return data.map(String)
}
return data.flatMap(item => searchSchemaResults(item, props, isExact))
}
if (typeof data === 'object') {
const [currentProp, ...rest] = props
if (!currentProp) {
if (data.name != null && typeof data.name === 'string') return [data.name]
return []
}
if (Object.prototype.hasOwnProperty.call(data, currentProp)) {
const result = searchSchemaResults(data[currentProp], rest, isExact)
if (result.length > 0) return result
}
if (!isExact) {
const nested = []
for (const key of Object.keys(data)) {
if (key.startsWith('@')) continue
nested.push(...searchSchemaResults(data[key], props, false))
}
return nested
}
return []
}
return []
}
const $jsonld = propName => $ => {
const collection = jsonld($)
const props = propName.split('.')
let value
let fallback
for (const item of collection) {
value = get(item, propName)
if (!isEmpty(value) || isNumber(value) || isBoolean(value)) break
if (value != null) continue
if (fallback !== undefined) continue
const exactResults = searchSchemaResults(item, props, true)
if (exactResults.length > 0) {
const filtered = exactResults.filter(v => v != null)
fallback = filtered.length > 1 ? filtered.join(' and ') : filtered[0]
} else {
const fuzzyResults = searchSchemaResults(item, props, false)
if (fuzzyResults.length > 0) {
fallback = fuzzyResults.find(v => v != null)
}
}
}
if (value == null && !isNumber(value) && !isBoolean(value)) {
value = fallback
}
return isString(value) ? decodeHTML(value) : value
}
const image = (value, opts) => {
const urlValue = url(value, opts)
const result =
urlValue !== undefined &&
!isAudioUrl(urlValue, opts) &&
!isVideoUrl(urlValue, opts)
? urlValue
: undefined
if (!dataUri.test(result)) return result
const buffer = dataUri.toBuffer(dataUri.normalize(result))
return buffer.length ? result : undefined
}
const logo = image
const media = (urlValidator, contentTypeValidator) => (value, opts) => {
const urlValue = url(value, opts)
return urlValidator(urlValue, opts) || contentTypeValidator(opts)
? urlValue
: undefined
}
const video = media(isVideoUrl, isVideoContentType)
const audio = media(isAudioUrl, isAudioContentType)
const validator = {
audio,
author,
date,
description,
image,
lang,
logo,
publisher,
title,
url,
video
}
const truthyTest = () => true
const findRule = async (rules, args, propName) => {
let index = 0
let value
do {
const rule = rules[index++]
const test = rule.test || truthyTest
if (test(args)) {
const duration = debug.duration()
value = await rule(args)
duration(`${rule.pkgName}:${propName}:${index - 1}:${has(value)}`)
}
} while (!has(value) && index < rules.length)
return value
}
const toRule =
(mapper, opts) =>
rule =>
async ({ htmlDom, url }) => {
const value = await rule(htmlDom, url)
return mapper(value, { url, ...opts })
}
const composeRule =
rule =>
({ from, to = from, ...opts }) =>
async ({ htmlDom, url }) => {
const data = await rule(htmlDom, url)
const value = get(data, from)
return invoke(validator, to, value, { url, ...opts })
}
const has = value =>
value !== undefined && !Number.isNaN(value) && hasValues(value)
const getUrls = input => String(input).match(urlRegexForMatch) ?? []
const htmlCache = new WeakMap()
const getHtml = htmlDom => {
if (!htmlCache.has(htmlDom)) {
htmlCache.set(htmlDom, htmlDom.html())
}
return htmlCache.get(htmlDom)
}
const loadIframe = require('./load-iframe')
const defaultGetIframe = (url, $, { src }) =>
loadIframe(url, $.load(`<iframe src="${src}"></iframe>`))
const createGetIframeCached = getIframe => {
const cacheByHtmlDom = new WeakMap()
return async (url, $, src) => {
let cacheBySrc = cacheByHtmlDom.get($)
if (!cacheBySrc) {
cacheBySrc = new Map()
cacheByHtmlDom.set($, cacheBySrc)
}
const cachedHtmlDom = cacheBySrc.get(src)
if (cachedHtmlDom) return cachedHtmlDom
const pendingHtmlDom = getIframe(url, $, { src }).catch(error => {
cacheBySrc.delete(src)
throw error
})
cacheBySrc.set(src, pendingHtmlDom)
return pendingHtmlDom
}
}
module.exports = {
$filter,
$jsonld,
absoluteUrl,
audio,
audioExtensions,
author,
composeRule,
createGetIframeCached,
date,
defaultGetIframe,
description,
extension,
fileExtension,
findRule,
getHtml,
getUrls,
has,
image,
imageExtensions,
isAudioExtension,
isAudioUrl,
isAuthor,
isImageExtension,
isImageUrl,
isMediaExtension,
isMediaUrl,
isMime,
isPdfExtension,
isPdfUrl,
isString,
isUrl,
isVideoExtension,
isVideoUrl,
iso6393,
jsonld,
lang,
loadIframe,
logo,
memoizeOne,
mimeExtension,
normalizeUrl,
parseUrl,
protocol,
publisher,
sanetizeUrl,
title,
titleize,
toRule,
url,
validator,
video,
videoExtensions
}