@metascraper/helpers 5.55.0 → 5.56.2

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +59 -3
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.55.0",
5
+ "version": "5.56.2",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -73,5 +73,5 @@
73
73
  "publishConfig": {
74
74
  "access": "public"
75
75
  },
76
- "gitHead": "c6fdffea348dcee4ff5ce22613518c4c68f50f2a"
76
+ "gitHead": "63168a1c6ab9fa67552a13fd9e4a60ad4c10f953"
77
77
  }
package/src/index.js CHANGED
@@ -176,14 +176,66 @@ const protocol = url => {
176
176
  return ''
177
177
  }
178
178
 
179
- const sanetizeUrl = (url, opts) =>
180
- _normalizeUrl(url, {
179
+ /**
180
+ * `normalize-url` treats `+` as a space in query keys that have no `=`
181
+ * (CGI-style `?241+ful+HB1288+pdf`) and emits `%20`. Restore those pluses —
182
+ * they are delimiters, not encoded spaces.
183
+ *
184
+ * @param {string} original
185
+ * @param {string} normalized
186
+ * @returns {string}
187
+ */
188
+ const restoreCgiPlus = (original, normalized) => {
189
+ const origMark = original.indexOf('?')
190
+ const normMark = normalized.indexOf('?')
191
+ if (origMark === -1 || normMark === -1) return normalized
192
+
193
+ const origHash = original.indexOf('#', origMark)
194
+ const origSearch = original.slice(
195
+ origMark + 1,
196
+ origHash === -1 ? undefined : origHash
197
+ )
198
+ if (!origSearch.includes('+')) return normalized
199
+
200
+ const origByEncoded = new Map()
201
+ for (const orig of origSearch.split('&')) {
202
+ if (!orig.includes('=') && orig.includes('+')) {
203
+ origByEncoded.set(orig.replaceAll('+', '%20'), orig)
204
+ }
205
+ }
206
+ if (origByEncoded.size === 0) return normalized
207
+
208
+ const normHash = normalized.indexOf('#', normMark)
209
+ const normSearch = normalized.slice(
210
+ normMark + 1,
211
+ normHash === -1 ? undefined : normHash
212
+ )
213
+ let changed = false
214
+ const restored = normSearch.split('&').map(norm => {
215
+ const orig = origByEncoded.get(norm)
216
+ if (!orig) return norm
217
+ changed = true
218
+ return orig
219
+ })
220
+ if (!changed) return normalized
221
+
222
+ const hash = normHash === -1 ? '' : normalized.slice(normHash)
223
+ return `${normalized.slice(0, normMark + 1)}${restored.join('&')}${hash}`
224
+ }
225
+
226
+ const sanetizeUrl = (url, opts) => {
227
+ const normalized = _normalizeUrl(url, {
181
228
  stripWWW: false,
182
229
  sortQueryParameters: false,
183
230
  removeSingleSlash: false,
184
231
  removeTrailingSlash: false,
185
232
  ...opts
186
233
  })
234
+ const queryStart = url.indexOf('?')
235
+ return queryStart !== -1 && url.indexOf('+', queryStart) !== -1
236
+ ? restoreCgiPlus(url, normalized)
237
+ : normalized
238
+ }
187
239
 
188
240
  const normalizeUrl = (baseUrl, relativePath, opts) => {
189
241
  try {
@@ -504,6 +556,9 @@ const $jsonld = propName => $ => {
504
556
  return isString(value) ? decodeHTML(value) : value
505
557
  }
506
558
 
559
+ const $meta = attr => $ =>
560
+ $(`meta[name="${attr}" i], meta[property="${attr}" i]`).attr('content')
561
+
507
562
  const image = (value, opts) => {
508
563
  const urlValue = url(value, opts)
509
564
 
@@ -677,7 +732,7 @@ const withIframe = (rules, getIframe, propName) => {
677
732
  }
678
733
  }
679
734
 
680
- const twitter = $('meta[name="twitter:player"]').attr('content')
735
+ const twitter = $meta('twitter:player')($)
681
736
  if (twitter) return probe(twitter, args)
682
737
  })
683
738
  }
@@ -685,6 +740,7 @@ const withIframe = (rules, getIframe, propName) => {
685
740
  module.exports = {
686
741
  $filter,
687
742
  $jsonld,
743
+ $meta,
688
744
  absoluteUrl,
689
745
  audio,
690
746
  audioExtensions,