diff --git a/__snapshots__/index.test.js.snap b/__snapshots__/index.test.js.snap index 18f4639..dbe69ad 100644 --- a/__snapshots__/index.test.js.snap +++ b/__snapshots__/index.test.js.snap @@ -1,5 +1,7 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`http://example.com?utm_source=newsletter 1`] = `http://example.com?utm_source=newsletter`; + exports[`http://www.google.com/ 1`] = `http://www.google.com`; exports[`https://cdn.rawgit.com/fregante/shorten-repo-url/d71718db/.gitignore 1`] = `d71718db/.gitignore (raw)`; @@ -12,8 +14,14 @@ exports[`https://cdn.rawgit.com/nodejs/node/v0.12/.gitignore 1`] = `nodejs/node@ exports[`https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement#parameters 1`] = `developer.mozilla.org/en-US/docs/Web/API/Document/createElement#parameters`; +exports[`https://example.com#section 1`] = `example.com#section`; + +exports[`https://example.com/?id=42 1`] = `example.com?id=42`; + exports[`https://example.com/nodejs/node/blob/cc8fc46/.gitignore 1`] = `example.com/nodejs/node/blob/cc8fc46/.gitignore`; +exports[`https://example.com?utm_source=newsletter 1`] = `example.com?utm_source=newsletter`; + exports[`https://example.site/한글로-된-URL 1`] = `example.site/한글로-된-URL`; exports[`https://github.com 1`] = `github.com`; @@ -262,6 +270,8 @@ exports[`https://togithub.com/refined-github/refined-github/commit/4f270c4f50e0a exports[`https://togithub.com/refined-github/refined-github/commit/e81a9646b448d90c7e02ab41332cab0507dccbbd#commitcomment-60089354 1`] = `refined-github/refined-github@e81a964 (comment)`; +exports[`https://www.example.com?id=42 1`] = `example.com?id=42`; + exports[`https://www.google.com/ 1`] = `google.com`; exports[`https://www.npmjs.com/ 1`] = `npmjs.com`; diff --git a/fixtures/urls.js b/fixtures/urls.js index df5fc83..44424fe 100644 --- a/fixtures/urls.js +++ b/fixtures/urls.js @@ -135,4 +135,11 @@ export const urls = [ 'https://example.com/nodejs/node/blob/cc8fc46/.gitignore', 'https://example.site/한글로-된-URL', 'https://한글로-된-경로.com/하위경로#한글-해시', + + // Bare domain with a query/hash immediately after the host (no path) #regression + 'https://example.com?utm_source=newsletter', + 'http://example.com?utm_source=newsletter', + 'https://example.com#section', + 'https://www.example.com?id=42', + 'https://example.com/?id=42', ]; diff --git a/index.js b/index.js index e02e421..3eee3d3 100644 --- a/index.js +++ b/index.js @@ -86,7 +86,9 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') { /** * Parse URL manually to avoid URL encoding and punycode */ - const origin = href.split('/', 3).join('/'); + // Strip any `?query`/`#hash` that follows the host directly (no path), otherwise + // it gets absorbed into `origin` and later re-appended → duplicated query/hash + const origin = href.split('/', 3).join('/').replace(/[?#].*/, ''); const pathname = href.slice(origin.length).replace(/[?#].*/, '') || '/'; const hash = /#.+$/.exec(href)?.[0] ?? '';