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.
- package/README.md +2 -1
- package/package.json +1 -1
- 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 (
|
|
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
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
|
|
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
|
|
382
|
-
protocol
|
|
383
|
-
|
|
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;
|