html-get 2.28.0 → 2.28.1

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 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.28.0",
5
+ "version": "2.28.1",
6
6
  "types": "index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "bin": {
package/src/html.js CHANGED
@@ -11,7 +11,7 @@ const cheerio = require('cheerio')
11
11
  const { URL } = require('url')
12
12
  const path = require('path')
13
13
 
14
- const { isMime, isUrl, mimeExtension, parseUrl } = require('@metascraper/helpers')
14
+ const { isMime, isUrl, parseUrl } = require('@metascraper/helpers')
15
15
 
16
16
  const { getContentType, getCharset } = require('./util')
17
17
 
@@ -70,6 +70,20 @@ const addHead = ({ $, url, headers, pdfMeta = {} }) => {
70
70
  tags.forEach(tag => head.append(tag))
71
71
  }
72
72
 
73
+ const escapeText = text =>
74
+ String(text ?? '')
75
+ .replace(/&/g, '&')
76
+ .replace(/</g, '&lt;')
77
+
78
+ const isText = contentType =>
79
+ typeof contentType === 'string' &&
80
+ (contentType.endsWith('/json') ||
81
+ contentType.endsWith('+json') ||
82
+ (contentType.startsWith('text/') && contentType !== 'text/html'))
83
+
84
+ const isDocument = ({ html, headers, mode, converted }) =>
85
+ isHTML(html) && (mode === 'prerender' || converted || !isText(getContentType(headers)))
86
+
73
87
  const addBody = ({ url, headers, html }) => {
74
88
  const contentType = getContentType(headers)
75
89
  let element = ''
@@ -80,8 +94,10 @@ const addBody = ({ url, headers, html }) => {
80
94
  element = `<video><source src="${url}" type="${contentType}"></source></video>`
81
95
  } else if (isMime(contentType, 'audio')) {
82
96
  element = `<audio><source src="${url}" type="${contentType}"></source></audio>`
83
- } else if (mimeExtension(contentType) === 'json') {
84
- element = `<pre>${html}</pre>`
97
+ } else if (isText(contentType)) {
98
+ const text = escapeText(html)
99
+ const preserved = text.startsWith('\n') ? `<!-- -->${text}` : text
100
+ element = `<pre>${preserved}</pre>`
85
101
  }
86
102
 
87
103
  return `<!DOCTYPE html><html><head></head><body>${element}</body></html>`
@@ -191,6 +207,8 @@ module.exports = ({
191
207
  html,
192
208
  url,
193
209
  headers = {},
210
+ mode,
211
+ converted,
194
212
  pdfMeta,
195
213
  styles,
196
214
  hide,
@@ -200,7 +218,9 @@ module.exports = ({
200
218
  scripts,
201
219
  modules
202
220
  }) => {
203
- const content = addDocType(isHTML(html) ? html : addBody({ url, headers, html }))
221
+ const content = addDocType(
222
+ isDocument({ html, headers, mode, converted }) ? html : addBody({ url, headers, html })
223
+ )
204
224
 
205
225
  const $ = cheerio.load(content)
206
226
 
package/src/index.js CHANGED
@@ -105,6 +105,7 @@ const fetch = PCancelable.fn(
105
105
  const res = await req
106
106
 
107
107
  let pdfMeta
108
+ let asDocument = false
108
109
  const html = await (async () => {
109
110
  const contentType = getContentType(res.headers)
110
111
 
@@ -137,6 +138,7 @@ const fetch = PCancelable.fn(
137
138
  })
138
139
  pdfMeta = { ...pdfMeta, image }
139
140
  }
141
+ asDocument = true
140
142
  return converted
141
143
  }
142
144
  debug('mutool:empty', { url: res.url })
@@ -155,7 +157,10 @@ const fetch = PCancelable.fn(
155
157
  await writeFile(file.path, res.body)
156
158
  try {
157
159
  const converted = await convertOffice(officeFormat, file.path, convertOpts())
158
- if (converted) return converted
160
+ if (converted) {
161
+ asDocument = true
162
+ return converted
163
+ }
159
164
  debug('pandoc:empty', { url: res.url, format: officeFormat })
160
165
  } catch (error) {
161
166
  debug('pandoc:error', {
@@ -176,6 +181,7 @@ const fetch = PCancelable.fn(
176
181
  headers: res.headers,
177
182
  html,
178
183
  pdfMeta,
184
+ converted: asDocument,
179
185
  mode: 'fetch',
180
186
  url: res.url,
181
187
  statusCode: res.statusCode,
@@ -446,10 +452,11 @@ const getContent = PCancelable.fn(
446
452
  onCancel(() => promise.cancel())
447
453
 
448
454
  return promise.then(content => {
449
- const { pdfMeta, ...rest } = content
455
+ const { pdfMeta, converted, ...rest } = content
450
456
  const $ = addHtml({
451
457
  ...rest,
452
458
  pdfMeta,
459
+ converted,
453
460
  ...(isFetchMode ? puppeteerOpts : undefined),
454
461
  rewriteUrls,
455
462
  rewriteHtml