get-canonical-url
Advanced tools
Comparing version 1.0.1 to 1.1.0
import type { Options as NormalizeOptions } from "normalize-url"; | ||
export interface Options { | ||
/** | ||
* Clean-up and normalize the determined canonical URL. | ||
* | ||
* @default false | ||
*/ | ||
readonly normalize?: boolean; | ||
/** | ||
* Options passed directly to [`normalize-url`](https://github.com/sindresorhus/normalize-url#options). | ||
* | ||
* Requires `options.normalize = true`. | ||
* | ||
* @default { stripWWW: false, stripHash: true, removeQueryParameters: true, removeTrailingSlash: false } | ||
*/ | ||
readonly normalizeOptions?: NormalizeOptions; | ||
/** | ||
* Make an educated guess using other clues if canonical isn't explicitly set in the page's `<head>`. | ||
* | ||
* @default false | ||
*/ | ||
readonly guess?: boolean; | ||
/** | ||
* Clean-up and normalize the determined canonical URL. | ||
* | ||
* @default false | ||
*/ | ||
readonly normalize?: boolean; | ||
/** | ||
* Options passed directly to [`normalize-url`](https://github.com/sindresorhus/normalize-url#options). | ||
* | ||
* Requires `options.normalize = true`. | ||
* | ||
* @default { stripWWW: false, stripHash: true, removeQueryParameters: true, removeTrailingSlash: false } | ||
*/ | ||
readonly normalizeOptions?: NormalizeOptions; | ||
/** | ||
* Make an educated guess using other clues if canonical isn't explicitly set in the page's `<head>`. | ||
* | ||
* @default false | ||
*/ | ||
readonly guess?: boolean; | ||
} | ||
/** | ||
@@ -29,0 +25,0 @@ * Returns the current page's canonical URL. |
@@ -1,40 +0,2 @@ | ||
/*! get-canonical-url v1.0.1 | MIT | https://github.com/jakejarvis/get-canonical-url */ | ||
import normalizeUrl from 'normalize-url'; | ||
function canonicalUrl(options = {}) { | ||
options = { | ||
normalize: false, | ||
normalizeOptions: { | ||
// A few sensible normalize-url defaults: | ||
// https://github.com/sindresorhus/normalize-url#options | ||
stripWWW: false, | ||
stripHash: true, | ||
removeQueryParameters: true, | ||
removeTrailingSlash: false | ||
}, | ||
guess: false, | ||
...options | ||
}; // Start with a blank slate | ||
let url = undefined; // Look for a <link rel="canonical"> tag in the page's <head> | ||
const linkElement = document.head.querySelector("link[rel='canonical']"); | ||
if (linkElement !== null) { | ||
// Easy peasy, there was a <link rel="canonical"> tag! | ||
url = linkElement.href; | ||
} else if (options.guess) { | ||
// We've been told to make an educated guess if canonical isn't explicitly set | ||
url = document.documentURI || document.URL || window.location.href; | ||
} | ||
if (options.normalize) { | ||
// Pass either custom options or defaults (above) directly to normalize-url | ||
url = normalizeUrl(url, options.normalizeOptions); | ||
} // Some sort of URL has been determined by this point, unless it's impossible | ||
return url; | ||
} | ||
export { canonicalUrl as default }; | ||
function e(){return e=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a])}return e},e.apply(this,arguments)}const t=(e,t)=>t.some(t=>t instanceof RegExp?t.test(e):t===e);function r(r){void 0===r&&(r={}),r=e({normalize:!1,normalizeOptions:{stripWWW:!1,stripHash:!0,removeQueryParameters:!0,removeTrailingSlash:!1},guess:!1},r);var a=void 0,o=document.head.querySelector("link[rel='canonical']");return null!==o?a=o.href:r.guess&&(a=document.documentURI||document.URL||window.location.href),a&&r.normalize&&(a=function(e,r){if(r={defaultProtocol:"http:",normalizeProtocol:!0,forceHttp:!1,forceHttps:!1,stripAuthentication:!0,stripHash:!1,stripTextFragment:!0,stripWWW:!0,removeQueryParameters:[/^utm_\w+/i],removeTrailingSlash:!0,removeSingleSlash:!0,removeDirectoryIndex:!1,sortQueryParameters:!0,...r},e=e.trim(),/^data:/i.test(e))return((e,{stripHash:t})=>{const r=/^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(e);if(!r)throw new Error(`Invalid URL: ${e}`);let{type:a,data:o,hash:s}=r.groups;const n=a.split(";");s=t?"":s;let i=!1;"base64"===n[n.length-1]&&(n.pop(),i=!0);const h=(n.shift()||"").toLowerCase(),p=[...n.map(e=>{let[t,r=""]=e.split("=").map(e=>e.trim());return"charset"===t&&(r=r.toLowerCase(),"us-ascii"===r)?"":`${t}${r?`=${r}`:""}`}).filter(Boolean)];return i&&p.push("base64"),(p.length>0||h&&"text/plain"!==h)&&p.unshift(h),`data:${p.join(";")},${i?o.trim():o}${s?`#${s}`:""}`})(e,r);if(/^view-source:/i.test(e))throw new Error("`view-source:` is not supported as it is a non-standard protocol");const a=e.startsWith("//");!a&&/^\.*\//.test(e)||(e=e.replace(/^(?!(?:\w+:)?\/\/)|^\/\//,r.defaultProtocol));const o=new URL(e);if(r.forceHttp&&r.forceHttps)throw new Error("The `forceHttp` and `forceHttps` options cannot be used together");if(r.forceHttp&&"https:"===o.protocol&&(o.protocol="http:"),r.forceHttps&&"http:"===o.protocol&&(o.protocol="https:"),r.stripAuthentication&&(o.username="",o.password=""),r.stripHash?o.hash="":r.stripTextFragment&&(o.hash=o.hash.replace(/#?:~:text.*?$/i,"")),o.pathname){const e=/\b[a-z][a-z\d+\-.]{1,50}:\/\//g;let t=0,r="";for(;;){const a=e.exec(o.pathname);if(!a)break;const s=a[0],n=a.index;r+=o.pathname.slice(t,n).replace(/\/{2,}/g,"/"),r+=s,t=n+s.length}r+=o.pathname.slice(t,o.pathname.length).replace(/\/{2,}/g,"/"),o.pathname=r}if(o.pathname)try{o.pathname=decodeURI(o.pathname)}catch{}if(!0===r.removeDirectoryIndex&&(r.removeDirectoryIndex=[/^index\.[a-z]+$/]),Array.isArray(r.removeDirectoryIndex)&&r.removeDirectoryIndex.length>0){let e=o.pathname.split("/");t(e[e.length-1],r.removeDirectoryIndex)&&(e=e.slice(0,-1),o.pathname=e.slice(1).join("/")+"/")}if(o.hostname&&(o.hostname=o.hostname.replace(/\.$/,""),r.stripWWW&&/^www\.(?!www\.)[a-z\-\d]{1,63}\.[a-z.\-\d]{2,63}$/.test(o.hostname)&&(o.hostname=o.hostname.replace(/^www\./,""))),Array.isArray(r.removeQueryParameters))for(const e of[...o.searchParams.keys()])t(e,r.removeQueryParameters)&&o.searchParams.delete(e);!0===r.removeQueryParameters&&(o.search=""),r.sortQueryParameters&&o.searchParams.sort(),r.removeTrailingSlash&&(o.pathname=o.pathname.replace(/\/$/,""));const s=e;return e=o.toString(),r.removeSingleSlash||"/"!==o.pathname||s.endsWith("/")||""!==o.hash||(e=e.replace(/\/$/,"")),(r.removeTrailingSlash||"/"===o.pathname)&&""===o.hash&&r.removeSingleSlash&&(e=e.replace(/\/$/,"")),a&&!r.normalizeProtocol&&(e=e.replace(/^http:\/\//,"//")),r.stripProtocol&&(e=e.replace(/^(?:https?:)?\/\//,"")),e}(a,r.normalizeOptions)),a}export{r as default}; | ||
//# sourceMappingURL=get-canonical-url.esm.js.map |
@@ -1,2 +0,2 @@ | ||
/*! get-canonical-url v1.0.1 | MIT | https://github.com/jakejarvis/get-canonical-url */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).canonicalUrl=t()}(this,(function(){"use strict";function e(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,o)}return r}function t(t){for(var o=1;o<arguments.length;o++){var a=null!=arguments[o]?arguments[o]:{};o%2?e(Object(a),!0).forEach((function(e){r(t,e,a[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(a)):e(Object(a)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(a,e))}))}return t}function r(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}const o=(e,t)=>t.some((t=>t instanceof RegExp?t.test(e):t===e));function a(e,t){if(t={defaultProtocol:"http:",normalizeProtocol:!0,forceHttp:!1,forceHttps:!1,stripAuthentication:!0,stripHash:!1,stripTextFragment:!0,stripWWW:!0,removeQueryParameters:[/^utm_\w+/i],removeTrailingSlash:!0,removeSingleSlash:!0,removeDirectoryIndex:!1,sortQueryParameters:!0,...t},e=e.trim(),/^data:/i.test(e))return((e,{stripHash:t})=>{const r=/^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(e);if(!r)throw new Error(`Invalid URL: ${e}`);let{type:o,data:a,hash:n}=r.groups;const s=o.split(";");n=t?"":n;let i=!1;"base64"===s[s.length-1]&&(s.pop(),i=!0);const c=(s.shift()||"").toLowerCase(),p=[...s.map((e=>{let[t,r=""]=e.split("=").map((e=>e.trim()));return"charset"===t&&(r=r.toLowerCase(),"us-ascii"===r)?"":`${t}${r?`=${r}`:""}`})).filter(Boolean)];return i&&p.push("base64"),(p.length>0||c&&"text/plain"!==c)&&p.unshift(c),`data:${p.join(";")},${i?a.trim():a}${n?`#${n}`:""}`})(e,t);if(/^view-source:/i.test(e))throw new Error("`view-source:` is not supported as it is a non-standard protocol");const r=e.startsWith("//");!r&&/^\.*\//.test(e)||(e=e.replace(/^(?!(?:\w+:)?\/\/)|^\/\//,t.defaultProtocol));const a=new URL(e);if(t.forceHttp&&t.forceHttps)throw new Error("The `forceHttp` and `forceHttps` options cannot be used together");if(t.forceHttp&&"https:"===a.protocol&&(a.protocol="http:"),t.forceHttps&&"http:"===a.protocol&&(a.protocol="https:"),t.stripAuthentication&&(a.username="",a.password=""),t.stripHash?a.hash="":t.stripTextFragment&&(a.hash=a.hash.replace(/#?:~:text.*?$/i,"")),a.pathname){const e=/\b[a-z][a-z\d+\-.]{1,50}:\/\//g;let t=0,r="";for(;;){const o=e.exec(a.pathname);if(!o)break;const n=o[0],s=o.index;r+=a.pathname.slice(t,s).replace(/\/{2,}/g,"/"),r+=n,t=s+n.length}r+=a.pathname.slice(t,a.pathname.length).replace(/\/{2,}/g,"/"),a.pathname=r}if(a.pathname)try{a.pathname=decodeURI(a.pathname)}catch{}if(!0===t.removeDirectoryIndex&&(t.removeDirectoryIndex=[/^index\.[a-z]+$/]),Array.isArray(t.removeDirectoryIndex)&&t.removeDirectoryIndex.length>0){let e=a.pathname.split("/");const r=e[e.length-1];o(r,t.removeDirectoryIndex)&&(e=e.slice(0,-1),a.pathname=e.slice(1).join("/")+"/")}if(a.hostname&&(a.hostname=a.hostname.replace(/\.$/,""),t.stripWWW&&/^www\.(?!www\.)[a-z\-\d]{1,63}\.[a-z.\-\d]{2,63}$/.test(a.hostname)&&(a.hostname=a.hostname.replace(/^www\./,""))),Array.isArray(t.removeQueryParameters))for(const e of[...a.searchParams.keys()])o(e,t.removeQueryParameters)&&a.searchParams.delete(e);!0===t.removeQueryParameters&&(a.search=""),t.sortQueryParameters&&a.searchParams.sort(),t.removeTrailingSlash&&(a.pathname=a.pathname.replace(/\/$/,""));const n=e;return e=a.toString(),t.removeSingleSlash||"/"!==a.pathname||n.endsWith("/")||""!==a.hash||(e=e.replace(/\/$/,"")),(t.removeTrailingSlash||"/"===a.pathname)&&""===a.hash&&t.removeSingleSlash&&(e=e.replace(/\/$/,"")),r&&!t.normalizeProtocol&&(e=e.replace(/^http:\/\//,"//")),t.stripProtocol&&(e=e.replace(/^(?:https?:)?\/\//,"")),e}return function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};e=t({normalize:!1,normalizeOptions:{stripWWW:!1,stripHash:!0,removeQueryParameters:!0,removeTrailingSlash:!1},guess:!1},e);var r=void 0,o=document.head.querySelector("link[rel='canonical']");return null!==o?r=o.href:e.guess&&(r=document.documentURI||document.URL||window.location.href),e.normalize&&(r=a(r,e.normalizeOptions)),r}})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e||self).canonicalUrl=t()}(this,function(){function e(){return e=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a])}return e},e.apply(this,arguments)}const t=(e,t)=>t.some(t=>t instanceof RegExp?t.test(e):t===e);return function(r){void 0===r&&(r={}),r=e({normalize:!1,normalizeOptions:{stripWWW:!1,stripHash:!0,removeQueryParameters:!0,removeTrailingSlash:!1},guess:!1},r);var a=void 0,o=document.head.querySelector("link[rel='canonical']");return null!==o?a=o.href:r.guess&&(a=document.documentURI||document.URL||window.location.href),a&&r.normalize&&(a=function(e,r){if(r={defaultProtocol:"http:",normalizeProtocol:!0,forceHttp:!1,forceHttps:!1,stripAuthentication:!0,stripHash:!1,stripTextFragment:!0,stripWWW:!0,removeQueryParameters:[/^utm_\w+/i],removeTrailingSlash:!0,removeSingleSlash:!0,removeDirectoryIndex:!1,sortQueryParameters:!0,...r},e=e.trim(),/^data:/i.test(e))return((e,{stripHash:t})=>{const r=/^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(e);if(!r)throw new Error(`Invalid URL: ${e}`);let{type:a,data:o,hash:n}=r.groups;const s=a.split(";");n=t?"":n;let i=!1;"base64"===s[s.length-1]&&(s.pop(),i=!0);const c=(s.shift()||"").toLowerCase(),p=[...s.map(e=>{let[t,r=""]=e.split("=").map(e=>e.trim());return"charset"===t&&(r=r.toLowerCase(),"us-ascii"===r)?"":`${t}${r?`=${r}`:""}`}).filter(Boolean)];return i&&p.push("base64"),(p.length>0||c&&"text/plain"!==c)&&p.unshift(c),`data:${p.join(";")},${i?o.trim():o}${n?`#${n}`:""}`})(e,r);if(/^view-source:/i.test(e))throw new Error("`view-source:` is not supported as it is a non-standard protocol");const a=e.startsWith("//");!a&&/^\.*\//.test(e)||(e=e.replace(/^(?!(?:\w+:)?\/\/)|^\/\//,r.defaultProtocol));const o=new URL(e);if(r.forceHttp&&r.forceHttps)throw new Error("The `forceHttp` and `forceHttps` options cannot be used together");if(r.forceHttp&&"https:"===o.protocol&&(o.protocol="http:"),r.forceHttps&&"http:"===o.protocol&&(o.protocol="https:"),r.stripAuthentication&&(o.username="",o.password=""),r.stripHash?o.hash="":r.stripTextFragment&&(o.hash=o.hash.replace(/#?:~:text.*?$/i,"")),o.pathname){const e=/\b[a-z][a-z\d+\-.]{1,50}:\/\//g;let t=0,r="";for(;;){const a=e.exec(o.pathname);if(!a)break;const n=a[0],s=a.index;r+=o.pathname.slice(t,s).replace(/\/{2,}/g,"/"),r+=n,t=s+n.length}r+=o.pathname.slice(t,o.pathname.length).replace(/\/{2,}/g,"/"),o.pathname=r}if(o.pathname)try{o.pathname=decodeURI(o.pathname)}catch{}if(!0===r.removeDirectoryIndex&&(r.removeDirectoryIndex=[/^index\.[a-z]+$/]),Array.isArray(r.removeDirectoryIndex)&&r.removeDirectoryIndex.length>0){let e=o.pathname.split("/");t(e[e.length-1],r.removeDirectoryIndex)&&(e=e.slice(0,-1),o.pathname=e.slice(1).join("/")+"/")}if(o.hostname&&(o.hostname=o.hostname.replace(/\.$/,""),r.stripWWW&&/^www\.(?!www\.)[a-z\-\d]{1,63}\.[a-z.\-\d]{2,63}$/.test(o.hostname)&&(o.hostname=o.hostname.replace(/^www\./,""))),Array.isArray(r.removeQueryParameters))for(const e of[...o.searchParams.keys()])t(e,r.removeQueryParameters)&&o.searchParams.delete(e);!0===r.removeQueryParameters&&(o.search=""),r.sortQueryParameters&&o.searchParams.sort(),r.removeTrailingSlash&&(o.pathname=o.pathname.replace(/\/$/,""));const n=e;return e=o.toString(),r.removeSingleSlash||"/"!==o.pathname||n.endsWith("/")||""!==o.hash||(e=e.replace(/\/$/,"")),(r.removeTrailingSlash||"/"===o.pathname)&&""===o.hash&&r.removeSingleSlash&&(e=e.replace(/\/$/,"")),a&&!r.normalizeProtocol&&(e=e.replace(/^http:\/\//,"//")),r.stripProtocol&&(e=e.replace(/^(?:https?:)?\/\//,"")),e}(a,r.normalizeOptions)),a}}); | ||
//# sourceMappingURL=get-canonical-url.min.js.map |
{ | ||
"name": "get-canonical-url", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "🔗 Determines the current page's canonical URL and optionally normalizes it for consistency.", | ||
"license": "MIT", | ||
"homepage": "https://github.com/jakejarvis/get-canonical-url", | ||
"repository": "jakejarvis/get-canonical-url", | ||
"author": { | ||
@@ -12,6 +12,2 @@ "name": "Jake Jarvis", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jakejarvis/get-canonical-url.git" | ||
}, | ||
"type": "module", | ||
@@ -21,14 +17,14 @@ "files": [ | ||
], | ||
"main": "./dist/get-canonical-url.cjs.js", | ||
"source": "./src/get-canonical-url.ts", | ||
"main": "./dist/get-canonical-url.cjs", | ||
"module": "./dist/get-canonical-url.esm.js", | ||
"unpkg": "./dist/get-canonical-url.min.js", | ||
"types": "./dist/get-canonical-url.d.ts", | ||
"exports": { | ||
"require": "./dist/get-canonical-url.cjs.js", | ||
"require": "./dist/get-canonical-url.cjs", | ||
"import": "./dist/get-canonical-url.esm.js", | ||
"browser": "./dist/get-canonical-url.min.js" | ||
}, | ||
"types": "./dist/get-canonical-url.d.ts", | ||
"scripts": { | ||
"build": "rollup -c", | ||
"watch": "rollup -c -w", | ||
"build": "microbundle --format cjs,esm,umd --name 'canonicalUrl'", | ||
"test": "mocha", | ||
@@ -38,27 +34,17 @@ "lint": "eslint .", | ||
}, | ||
"dependencies": { | ||
"normalize-url": "^7.0.2" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@babel/core": "^7.15.8", | ||
"@babel/eslint-parser": "^7.15.8", | ||
"@babel/preset-env": "^7.15.8", | ||
"@jakejarvis/eslint-config": "*", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"@rollup/plugin-eslint": "^8.0.1", | ||
"@rollup/plugin-node-resolve": "^13.0.5", | ||
"@types/chai": "^4.2.22", | ||
"@types/jsdom": "^16.2.13", | ||
"@types/mocha": "^9.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.3.1", | ||
"@typescript-eslint/parser": "^5.3.1", | ||
"chai": "^4.3.4", | ||
"eslint": "^7.32.0", | ||
"eslint-plugin-compat": "~3.13.0", | ||
"eslint-plugin-import": "~2.24.2", | ||
"jsdom": "^18.0.0", | ||
"mocha": "^9.1.2", | ||
"rollup": "^2.58.0", | ||
"rollup-plugin-copy": "^3.4.0", | ||
"rollup-plugin-delete": "^2.0.0", | ||
"rollup-plugin-filesize": "^9.1.1", | ||
"rollup-plugin-terser": "^7.0.2" | ||
"eslint": "^8.2.0", | ||
"jsdom": "^18.0.1", | ||
"microbundle": "^0.14.1", | ||
"mocha": "^9.1.3", | ||
"normalize-url": "^7.0.2", | ||
"typescript": "^4.4.4" | ||
}, | ||
@@ -65,0 +51,0 @@ "keywords": [ |
# 🔗 get-canonical-url | ||
[![CI](https://github.com/jakejarvis/get-canonical-url/actions/workflows/ci.yml/badge.svg)](https://github.com/jakejarvis/get-canonical-url/actions/workflows/ci.yml) | ||
[![npm](https://img.shields.io/npm/v/get-canonical-url?logo=npm)](https://www.npmjs.com/package/get-canonical-url) | ||
[![MIT License](https://img.shields.io/github/license/jakejarvis/get-canonical-url?color=violet)](LICENSE) | ||
[![npm](https://img.shields.io/npm/v/get-canonical-url)](https://www.npmjs.com/package/get-canonical-url) | ||
@@ -17,2 +16,8 @@ Determines the current page's canonical URL and optionally normalizes it via [normalize-url](https://github.com/sindresorhus/normalize-url) for consistency. | ||
Or pull directly from [unpkg](https://unpkg.com/browse/get-canonical-url/) in-browser: | ||
```html | ||
<script src="https://unpkg.com/get-canonical-url/dist/get-canonical-url.min.js"></script> | ||
``` | ||
## Usage | ||
@@ -19,0 +24,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
61554
0
13
10
107
84
1
1
1
- Removednormalize-url@^7.0.2
- Removednormalize-url@7.2.0(transitive)