html-get 2.26.5 → 2.26.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.
- package/package.json +2 -2
- package/src/html.js +12 -55
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "html-get",
|
|
3
3
|
"description": "Get the HTML from any website, fine-tuned for correction & speed",
|
|
4
4
|
"homepage": "https://nicedoc.com/microlinkhq/html-get",
|
|
5
|
-
"version": "2.26.
|
|
5
|
+
"version": "2.26.7",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"main": "src/index.js",
|
|
8
8
|
"bin": {
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@kikobeats/time-span": "~1.0.13",
|
|
49
|
-
"@metascraper/helpers": "~5.
|
|
49
|
+
"@metascraper/helpers": "~5.54.0",
|
|
50
50
|
"cheerio": "~1.2.0",
|
|
51
51
|
"content-type": "~1.0.5",
|
|
52
52
|
"css-url-regex": "~4.0.0",
|
package/src/html.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const debug = require('debug-logfmt')('html-get:rewrite')
|
|
4
|
-
const {
|
|
4
|
+
const { castArray, forEach } = require('lodash')
|
|
5
5
|
const isLocalAddress = require('is-local-address')
|
|
6
6
|
const { TAGS: URL_TAGS } = require('html-urls')
|
|
7
7
|
const isHTML = require('is-html-content')
|
|
@@ -11,13 +11,7 @@ const cheerio = require('cheerio')
|
|
|
11
11
|
const { URL } = require('url')
|
|
12
12
|
const path = require('path')
|
|
13
13
|
|
|
14
|
-
const {
|
|
15
|
-
date: toDate,
|
|
16
|
-
isMime,
|
|
17
|
-
isUrl,
|
|
18
|
-
mimeExtension,
|
|
19
|
-
parseUrl
|
|
20
|
-
} = require('@metascraper/helpers')
|
|
14
|
+
const { isMime, isUrl, mimeExtension, parseUrl } = require('@metascraper/helpers')
|
|
21
15
|
|
|
22
16
|
const { getContentType, getCharset } = require('./util')
|
|
23
17
|
|
|
@@ -25,20 +19,9 @@ const has = el => el.length !== 0
|
|
|
25
19
|
|
|
26
20
|
const upsert = (el, collection, item) => !has(el) && collection.push(item)
|
|
27
21
|
|
|
28
|
-
/**
|
|
29
|
-
* Infer timestamp from `last-modified`, `date`, or `age` response headers.
|
|
30
|
-
*/
|
|
31
|
-
const getDate = headers => {
|
|
32
|
-
const timestamp = get(headers, 'last-modified') || get(headers, 'date')
|
|
33
|
-
return timestamp
|
|
34
|
-
? toDate(timestamp)
|
|
35
|
-
: toDate(Date.now() - Number(get(headers, 'age')) * 1000)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
22
|
const addHead = ({ $, url, headers }) => {
|
|
39
23
|
const tags = []
|
|
40
24
|
const charset = getCharset(headers)
|
|
41
|
-
const date = getDate(headers)
|
|
42
25
|
const { domain } = parseUrl(url)
|
|
43
26
|
const head = $('head')
|
|
44
27
|
|
|
@@ -52,19 +35,7 @@ const addHead = ({ $, url, headers }) => {
|
|
|
52
35
|
)
|
|
53
36
|
}
|
|
54
37
|
|
|
55
|
-
|
|
56
|
-
upsert(
|
|
57
|
-
head.find('meta[property="article:published_time"]'),
|
|
58
|
-
tags,
|
|
59
|
-
`<meta name="date" content="${date}" />`
|
|
60
|
-
)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
upsert(
|
|
64
|
-
head.find('link[rel="canonical"]'),
|
|
65
|
-
tags,
|
|
66
|
-
`<link rel="canonical" href="${url}">`
|
|
67
|
-
)
|
|
38
|
+
upsert(head.find('link[rel="canonical"]'), tags, `<link rel="canonical" href="${url}">`)
|
|
68
39
|
|
|
69
40
|
if (charset) {
|
|
70
41
|
upsert(head.find('meta[charset]'), tags, `<meta charset="${charset}">`)
|
|
@@ -90,8 +61,7 @@ const addBody = ({ url, headers, html }) => {
|
|
|
90
61
|
return `<!DOCTYPE html><html><head></head><body>${element}</body></html>`
|
|
91
62
|
}
|
|
92
63
|
|
|
93
|
-
const isOpenGraph = (prop = '') =>
|
|
94
|
-
['og:', 'fb:', 'al:'].some(prefix => prop.startsWith(prefix))
|
|
64
|
+
const isOpenGraph = (prop = '') => ['og:', 'fb:', 'al:'].some(prefix => prop.startsWith(prefix))
|
|
95
65
|
|
|
96
66
|
const rewriteMetaTags = ({ $ }) => {
|
|
97
67
|
$('meta').each((_, element) => {
|
|
@@ -133,22 +103,16 @@ const rewriteHtmlUrls = ({ $, url }) => {
|
|
|
133
103
|
}
|
|
134
104
|
|
|
135
105
|
const replaceCssUrls = (url, stylesheet) => {
|
|
136
|
-
const cssUrls = Array.from(execall(cssUrl(), stylesheet)).reduce(
|
|
137
|
-
(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
},
|
|
141
|
-
new Set()
|
|
142
|
-
)
|
|
106
|
+
const cssUrls = Array.from(execall(cssUrl(), stylesheet)).reduce((acc, match) => {
|
|
107
|
+
match.subMatches.forEach(match => acc.add(match))
|
|
108
|
+
return acc
|
|
109
|
+
}, new Set())
|
|
143
110
|
|
|
144
111
|
cssUrls.forEach(cssUrl => {
|
|
145
112
|
if (/^(?:https?|data):/.test(cssUrl)) return
|
|
146
113
|
try {
|
|
147
114
|
const absoluteUrl = new URL(cssUrl, url).toString()
|
|
148
|
-
stylesheet = stylesheet.replaceAll(
|
|
149
|
-
`url(${cssUrl})`,
|
|
150
|
-
`url(${absoluteUrl})`
|
|
151
|
-
)
|
|
115
|
+
stylesheet = stylesheet.replaceAll(`url(${cssUrl})`, `url(${absoluteUrl})`)
|
|
152
116
|
} catch (_) {}
|
|
153
117
|
})
|
|
154
118
|
|
|
@@ -158,9 +122,7 @@ const replaceCssUrls = (url, stylesheet) => {
|
|
|
158
122
|
const rewriteCssUrls = ({ $, url }) => {
|
|
159
123
|
// Process <style> tags
|
|
160
124
|
// e.g., <style>body { background-image: url('/image.jpg'); }</style>
|
|
161
|
-
$('style').each((_, element) =>
|
|
162
|
-
$(element).html(replaceCssUrls(url, $(element).html()))
|
|
163
|
-
)
|
|
125
|
+
$('style').each((_, element) => $(element).html(replaceCssUrls(url, $(element).html())))
|
|
164
126
|
|
|
165
127
|
// Process elements with style attributes
|
|
166
128
|
// e.g., <div style="background-image: url('/image.jpg');"></div>
|
|
@@ -189,8 +151,7 @@ const injectScripts = ({ $, scripts, type }) =>
|
|
|
189
151
|
)
|
|
190
152
|
)
|
|
191
153
|
|
|
192
|
-
const addDocType = html =>
|
|
193
|
-
html.startsWith('<!') ? html : `<!DOCTYPE html>${html}`
|
|
154
|
+
const addDocType = html => (html.startsWith('<!') ? html : `<!DOCTYPE html>${html}`)
|
|
194
155
|
|
|
195
156
|
module.exports = ({
|
|
196
157
|
html,
|
|
@@ -204,9 +165,7 @@ module.exports = ({
|
|
|
204
165
|
scripts,
|
|
205
166
|
modules
|
|
206
167
|
}) => {
|
|
207
|
-
const content = addDocType(
|
|
208
|
-
isHTML(html) ? html : addBody({ url, headers, html })
|
|
209
|
-
)
|
|
168
|
+
const content = addDocType(isHTML(html) ? html : addBody({ url, headers, html }))
|
|
210
169
|
|
|
211
170
|
const $ = cheerio.load(content)
|
|
212
171
|
|
|
@@ -237,5 +196,3 @@ module.exports = ({
|
|
|
237
196
|
|
|
238
197
|
return rewriteUrls ? rewriteCssUrls({ $, url }) : $
|
|
239
198
|
}
|
|
240
|
-
|
|
241
|
-
module.exports.getDate = getDate
|