unprint 0.18.24 → 0.18.25

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 (3) hide show
  1. package/README.md +2 -1
  2. package/package.json +1 -1
  3. package/src/app.js +17 -7
package/README.md CHANGED
@@ -121,7 +121,8 @@ Options
121
121
 
122
122
  Options
123
123
  * `origin`: The hostname to prefix when it is not included in the URL (`/path`).
124
- * `protocol`: The protocol to use when it is not included in the URL (`:www.example.com`, default `http`).
124
+ * `protocol`: The protocol to use when it is not included in the URL (`//www.example.com`, default `http`). Set to `null` to disable this behavior.
125
+ * `forceProtocol`: Overwrite the input or origin protocol with the specified `protocol`.
125
126
 
126
127
  Returns the `href` from an anchor element (or any other specified target) as a string.
127
128
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unprint",
3
- "version": "0.18.24",
3
+ "version": "0.18.25",
4
4
  "description": "Simplify common web scraping tasks while staying in control of the data.",
5
5
  "main": "src/app.js",
6
6
  "scripts": {},
package/src/app.js CHANGED
@@ -365,25 +365,35 @@ function queryTexts(context, selector, customOptions) {
365
365
  }
366
366
 
367
367
  function prefixUrl(urlPath, originUrl, customOptions) {
368
+ const options = {
369
+ protocol: 'https',
370
+ forceProtocol: false,
371
+ ...customOptions,
372
+ };
373
+
368
374
  if (!urlPath) {
369
375
  return null;
370
376
  }
371
377
 
372
378
  if (/^http/.test(urlPath)) {
373
379
  // this is already a complete URL
374
- return urlPath;
380
+ return options.forceProtocol && options.protocol
381
+ ? urlPath?.replace(/^.*?:/, `${options.protocol}:`)
382
+ : urlPath;
375
383
  }
376
384
 
377
385
  if (!originUrl) {
386
+ // path without protocol, i.e. //www.example.com
387
+ if (options.protocol && /^\/\//.test(urlPath)) {
388
+ return `${options.protocol}:${urlPath}`;
389
+ }
390
+
378
391
  return null;
379
392
  }
380
393
 
381
- const options = {
382
- protocol: 'https',
383
- ...customOptions,
384
- };
385
-
386
- const origin = originUrl?.replace(/^.*?:/, `${options.protocol}:`);
394
+ const origin = options.forceProtocol && options.protocol
395
+ ? originUrl?.replace(/^.*?:/, `${options.protocol}:`)
396
+ : originUrl;
387
397
 
388
398
  try {
389
399
  return new URL(urlPath, origin).href;