@metascraper/helpers 5.58.11 → 5.58.13
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 +7 -5
- package/src/index.js +11 -14
- package/src/url-regex.js +44 -0
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@metascraper/helpers",
|
|
3
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.58.
|
|
5
|
+
"version": "5.58.13",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -36,9 +36,9 @@
|
|
|
36
36
|
"data-uri-utils": "~1.0.15",
|
|
37
37
|
"debug-logfmt": "~1.4.15",
|
|
38
38
|
"entities": "~8.1.0",
|
|
39
|
-
"file-extension": "~4.0.5",
|
|
40
39
|
"has-values": "~2.0.1",
|
|
41
40
|
"image-extensions": "~1.1.0",
|
|
41
|
+
"ip-regex": "4.3.0",
|
|
42
42
|
"is-relative-url": "~4.1.1",
|
|
43
43
|
"is-uri": "~1.2.14",
|
|
44
44
|
"iso-639-3": "~3.0.1",
|
|
@@ -52,13 +52,15 @@
|
|
|
52
52
|
"normalize-url": "9",
|
|
53
53
|
"re2": "~1.27.0",
|
|
54
54
|
"smartquotes": "~2.3.2",
|
|
55
|
+
"tlds": "~1.261.0",
|
|
55
56
|
"tldts": "~7.6.1",
|
|
56
|
-
"url-regex-safe": "~4.0.0",
|
|
57
57
|
"video-extensions": "2"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"ava": "8",
|
|
61
|
-
"cheerio": "latest"
|
|
61
|
+
"cheerio": "latest",
|
|
62
|
+
"file-extension": "~4.0.5",
|
|
63
|
+
"url-regex-safe": "~4.0.0"
|
|
62
64
|
},
|
|
63
65
|
"engines": {
|
|
64
66
|
"node": ">= 22.13"
|
|
@@ -73,5 +75,5 @@
|
|
|
73
75
|
"publishConfig": {
|
|
74
76
|
"access": "public"
|
|
75
77
|
},
|
|
76
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "62841e86839aa2a9ae0dee7316c2b01982b9836d"
|
|
77
79
|
}
|
package/src/index.js
CHANGED
|
@@ -7,7 +7,6 @@ const mime = require('mime').default
|
|
|
7
7
|
const mimeExtension = mime.getExtension.bind(mime)
|
|
8
8
|
const capitalize = require('microsoft-capitalize')
|
|
9
9
|
const isRelativeUrl = require('is-relative-url').default
|
|
10
|
-
const fileExtension = require('file-extension')
|
|
11
10
|
const _normalizeUrl = require('normalize-url').default
|
|
12
11
|
const { jsonrepair } = require('jsonrepair')
|
|
13
12
|
const smartquotes = require('smartquotes')
|
|
@@ -21,19 +20,7 @@ const isUri = require('is-uri')
|
|
|
21
20
|
const { URL } = require('url')
|
|
22
21
|
const tldts = require('tldts')
|
|
23
22
|
|
|
24
|
-
const
|
|
25
|
-
? process.env.METASCRAPER_RE2 === 'true'
|
|
26
|
-
: undefined
|
|
27
|
-
|
|
28
|
-
const urlRegexForTest = require('url-regex-safe')({
|
|
29
|
-
exact: true,
|
|
30
|
-
parens: true,
|
|
31
|
-
re2: METASCRAPER_RE2
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
const urlRegexForMatch = require('url-regex-safe')({
|
|
35
|
-
re2: METASCRAPER_RE2
|
|
36
|
-
})
|
|
23
|
+
const { urlRegexForTest, urlRegexForMatch } = require('./url-regex')
|
|
37
24
|
|
|
38
25
|
const {
|
|
39
26
|
chain,
|
|
@@ -331,6 +318,16 @@ const isVideoContentType = isContentType(Object.keys(videoExtensions))
|
|
|
331
318
|
|
|
332
319
|
const isAudioContentType = isContentType(Object.keys(audioExtensions))
|
|
333
320
|
|
|
321
|
+
const lastSeparatorIndex = str =>
|
|
322
|
+
Math.max(str.lastIndexOf('.'), str.lastIndexOf('/'), str.lastIndexOf('\\'))
|
|
323
|
+
|
|
324
|
+
const fileExtension = (filename, opts) => {
|
|
325
|
+
if (!filename) return ''
|
|
326
|
+
const str = `${filename}`
|
|
327
|
+
const ext = str.slice(lastSeparatorIndex(str) + 1)
|
|
328
|
+
return opts?.preserveCase ? ext : ext.toLowerCase()
|
|
329
|
+
}
|
|
330
|
+
|
|
334
331
|
const extension = (str = '') => {
|
|
335
332
|
const url = urlObject(
|
|
336
333
|
str,
|
package/src/url-regex.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const ipRegex = require('ip-regex')
|
|
4
|
+
const tlds = require('tlds')
|
|
5
|
+
|
|
6
|
+
const loadRegExp = () => {
|
|
7
|
+
if (process.env.METASCRAPER_RE2 !== 'true') return RegExp
|
|
8
|
+
try {
|
|
9
|
+
return require('re2')
|
|
10
|
+
} catch {
|
|
11
|
+
return RegExp
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const UrlRegExp = loadRegExp()
|
|
16
|
+
|
|
17
|
+
const LABEL_CHAR = '[a-z\\u00a1-\\uffff0-9]'
|
|
18
|
+
|
|
19
|
+
const HOST = `(?:${LABEL_CHAR}(?:[-_]*${LABEL_CHAR})*)`
|
|
20
|
+
|
|
21
|
+
const DOMAIN = `(?:\\.${LABEL_CHAR}(?:-*${LABEL_CHAR})*)*`
|
|
22
|
+
|
|
23
|
+
const TLD = `(?:\\.(?:${[...tlds]
|
|
24
|
+
.sort((a, b) => b.length - a.length)
|
|
25
|
+
.join('|')}))`
|
|
26
|
+
|
|
27
|
+
const PROTOCOL = '(?:(?:[a-z]+:)?//)?'
|
|
28
|
+
|
|
29
|
+
const PORT = '(?::\\d{2,5})?'
|
|
30
|
+
|
|
31
|
+
const pathPattern = ({ parens }) => {
|
|
32
|
+
const disallowed = parens ? '\\s"\'' : '\\s"\\)\''
|
|
33
|
+
return `(?:(?:[/?#][^${disallowed}]*[^${disallowed}.?!])|[/])?`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const urlPattern = options =>
|
|
37
|
+
`(?:${PROTOCOL}|www\\.)(?:localhost|${ipRegex.v4().source}|${
|
|
38
|
+
ipRegex.v6().source
|
|
39
|
+
}|${HOST}${DOMAIN}${TLD})${PORT}${pathPattern(options)}`
|
|
40
|
+
|
|
41
|
+
module.exports = {
|
|
42
|
+
urlRegexForTest: new UrlRegExp(`(?:^${urlPattern({ parens: true })}$)`, 'i'),
|
|
43
|
+
urlRegexForMatch: new UrlRegExp(urlPattern({ parens: false }), 'ig')
|
|
44
|
+
}
|