get-canonical-url
Version:
🔗 Determines the current page's canonical URL and optionally normalizes it for consistency.
3 lines (2 loc) • 3.64 kB
JavaScript
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);module.exports=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: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};
//# sourceMappingURL=get-canonical-url.cjs.map