@metascraper/helpers 5.52.0 → 5.54.0
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.
- package/package.json +12 -4
- package/src/index.js +97 -10
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metascraper/helpers",
|
|
3
|
-
"description": "
|
|
3
|
+
"description": "Shared helper utilities used across metascraper rules for parsing and normalizing metadata.",
|
|
4
4
|
"homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-helpers",
|
|
5
|
-
"version": "5.
|
|
5
|
+
"version": "5.54.0",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -19,7 +19,15 @@
|
|
|
19
19
|
},
|
|
20
20
|
"keywords": [
|
|
21
21
|
"helpers",
|
|
22
|
-
"
|
|
22
|
+
"html",
|
|
23
|
+
"json-ld",
|
|
24
|
+
"metadata",
|
|
25
|
+
"metascraper",
|
|
26
|
+
"normalize",
|
|
27
|
+
"open-graph",
|
|
28
|
+
"parse",
|
|
29
|
+
"scraper",
|
|
30
|
+
"utils"
|
|
23
31
|
],
|
|
24
32
|
"dependencies": {
|
|
25
33
|
"audio-extensions": "0.0.0",
|
|
@@ -65,5 +73,5 @@
|
|
|
65
73
|
"publishConfig": {
|
|
66
74
|
"access": "public"
|
|
67
75
|
},
|
|
68
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "23db3208c1dcc6367a705bdb9bf723fbaae259e0"
|
|
69
77
|
}
|
package/src/index.js
CHANGED
|
@@ -123,6 +123,8 @@ const REGEX_LOCATION = /^[A-Z\s]+\s+[-—–]\s+/
|
|
|
123
123
|
|
|
124
124
|
const REGEX_TITLE_SEPARATOR = /^[^|\-/•—]+/
|
|
125
125
|
|
|
126
|
+
const REGEX_URL_TAB_OR_NEWLINE = /[\t\n\r]/g
|
|
127
|
+
|
|
126
128
|
const AUTHOR_MAX_LENGTH = 128
|
|
127
129
|
|
|
128
130
|
const removeLocation = value => replace(value, REGEX_LOCATION, '')
|
|
@@ -143,6 +145,37 @@ const absoluteUrl = (baseUrl, relativePath) => {
|
|
|
143
145
|
return urlObject(relativePath, baseUrl).toString()
|
|
144
146
|
}
|
|
145
147
|
|
|
148
|
+
// https://url.spec.whatwg.org/#scheme-state
|
|
149
|
+
const protocol = url => {
|
|
150
|
+
if (!isString(url)) return ''
|
|
151
|
+
const { length } = url
|
|
152
|
+
let start = 0
|
|
153
|
+
while (start < length && url.charCodeAt(start) <= 0x20) start++
|
|
154
|
+
const head = url.charCodeAt(start)
|
|
155
|
+
let isLowerCased = head >= 0x61 && head <= 0x7a
|
|
156
|
+
if (!isLowerCased && !(head >= 0x41 && head <= 0x5a)) return ''
|
|
157
|
+
for (let index = start + 1; index < length; index++) {
|
|
158
|
+
const code = url.charCodeAt(index)
|
|
159
|
+
if (code === 0x3a) {
|
|
160
|
+
const scheme = url.slice(start, index)
|
|
161
|
+
return isLowerCased ? scheme : scheme.toLowerCase()
|
|
162
|
+
}
|
|
163
|
+
if (code >= 0x61 && code <= 0x7a) continue
|
|
164
|
+
if (code >= 0x41 && code <= 0x5a) {
|
|
165
|
+
isLowerCased = false
|
|
166
|
+
continue
|
|
167
|
+
}
|
|
168
|
+
if (code >= 0x30 && code <= 0x39) continue
|
|
169
|
+
if (code === 0x2b || code === 0x2d || code === 0x2e) continue
|
|
170
|
+
// `mai\tlto:a@b` declares `mailto`
|
|
171
|
+
if (code === 0x09 || code === 0x0a || code === 0x0d) {
|
|
172
|
+
return protocol(url.replace(REGEX_URL_TAB_OR_NEWLINE, ''))
|
|
173
|
+
}
|
|
174
|
+
return ''
|
|
175
|
+
}
|
|
176
|
+
return ''
|
|
177
|
+
}
|
|
178
|
+
|
|
146
179
|
const sanetizeUrl = (url, opts) =>
|
|
147
180
|
_normalizeUrl(url, {
|
|
148
181
|
stripWWW: false,
|
|
@@ -156,7 +189,7 @@ const normalizeUrl = (baseUrl, relativePath, opts) => {
|
|
|
156
189
|
try {
|
|
157
190
|
const absolute = absoluteUrl(baseUrl, relativePath)
|
|
158
191
|
// normalize-url v9 no longer rejects `javascript:` URLs; keep them out
|
|
159
|
-
if (
|
|
192
|
+
if (protocol(absolute) === 'javascript') return undefined
|
|
160
193
|
return sanetizeUrl(absolute, opts)
|
|
161
194
|
} catch (_) {}
|
|
162
195
|
}
|
|
@@ -205,11 +238,6 @@ const isAuthor = (str, opts = { relative: false }) =>
|
|
|
205
238
|
const getAuthor = (str, { removeBy = true, ...opts } = {}) =>
|
|
206
239
|
titleize(str, { removeBy, ...opts })
|
|
207
240
|
|
|
208
|
-
const protocol = url => {
|
|
209
|
-
const { protocol = '' } = urlObject(url)
|
|
210
|
-
return protocol.replace(':', '')
|
|
211
|
-
}
|
|
212
|
-
|
|
213
241
|
const isExtension = (url, type, ext = extension(url)) =>
|
|
214
242
|
type === EXTENSIONS[ext]
|
|
215
243
|
|
|
@@ -520,9 +548,23 @@ const validator = {
|
|
|
520
548
|
|
|
521
549
|
const truthyTest = () => true
|
|
522
550
|
|
|
523
|
-
const
|
|
551
|
+
const isValidValue = async (rule, value, args) => {
|
|
552
|
+
try {
|
|
553
|
+
return await rule.validate(value, args, debug)
|
|
554
|
+
} catch (error) {
|
|
555
|
+
debug('validate:error', {
|
|
556
|
+
pkgName: rule.pkgName,
|
|
557
|
+
errorName: error?.name,
|
|
558
|
+
errorMessage: error?.message
|
|
559
|
+
})
|
|
560
|
+
return false
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
const findRule = async (rules, args = {}, propName) => {
|
|
524
565
|
let index = 0
|
|
525
566
|
let value
|
|
567
|
+
let hasValue = false
|
|
526
568
|
|
|
527
569
|
do {
|
|
528
570
|
const rule = rules[index++]
|
|
@@ -530,9 +572,20 @@ const findRule = async (rules, args, propName) => {
|
|
|
530
572
|
if (test(args)) {
|
|
531
573
|
const duration = debug.duration()
|
|
532
574
|
value = await rule(args)
|
|
533
|
-
|
|
575
|
+
hasValue = has(value)
|
|
576
|
+
const isRejected =
|
|
577
|
+
hasValue && rule.validate && !(await isValidValue(rule, value, args))
|
|
578
|
+
if (isRejected) {
|
|
579
|
+
value = undefined
|
|
580
|
+
hasValue = false
|
|
581
|
+
}
|
|
582
|
+
duration(
|
|
583
|
+
`${rule.pkgName}:${propName}:${index - 1}:${hasValue}${
|
|
584
|
+
isRejected ? ':rejected' : ''
|
|
585
|
+
}`
|
|
586
|
+
)
|
|
534
587
|
}
|
|
535
|
-
} while (!
|
|
588
|
+
} while (!hasValue && index < rules.length)
|
|
536
589
|
|
|
537
590
|
return value
|
|
538
591
|
}
|
|
@@ -596,6 +649,39 @@ const createGetIframeCached = getIframe => {
|
|
|
596
649
|
}
|
|
597
650
|
}
|
|
598
651
|
|
|
652
|
+
const withIframe = (rules, getIframe, propName) => {
|
|
653
|
+
const probe = async (src, args) => {
|
|
654
|
+
try {
|
|
655
|
+
return await findRule(
|
|
656
|
+
rules,
|
|
657
|
+
{ ...args, htmlDom: await getIframe(args.url, args.htmlDom, src) },
|
|
658
|
+
propName
|
|
659
|
+
)
|
|
660
|
+
} catch (_) {}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
return rules.concat(async args => {
|
|
664
|
+
const { htmlDom: $, url } = args
|
|
665
|
+
const srcs = $('iframe[src^="http"], iframe[src^="/"]')
|
|
666
|
+
.map((_, el) => $(el).attr('src'))
|
|
667
|
+
.get()
|
|
668
|
+
|
|
669
|
+
if (srcs.length > 0) {
|
|
670
|
+
const seen = new Set()
|
|
671
|
+
for (const src of srcs) {
|
|
672
|
+
const normalized = normalizeUrl(url, src)
|
|
673
|
+
if (!normalized || seen.has(normalized)) continue
|
|
674
|
+
seen.add(normalized)
|
|
675
|
+
const value = await probe(normalized, args)
|
|
676
|
+
if (has(value)) return value
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
const twitter = $('meta[name="twitter:player"]').attr('content')
|
|
681
|
+
if (twitter) return probe(twitter, args)
|
|
682
|
+
})
|
|
683
|
+
}
|
|
684
|
+
|
|
599
685
|
module.exports = {
|
|
600
686
|
$filter,
|
|
601
687
|
$jsonld,
|
|
@@ -648,5 +734,6 @@ module.exports = {
|
|
|
648
734
|
url,
|
|
649
735
|
validator,
|
|
650
736
|
video,
|
|
651
|
-
videoExtensions
|
|
737
|
+
videoExtensions,
|
|
738
|
+
withIframe
|
|
652
739
|
}
|