Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions __snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -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`] = `<code>d71718db</code>/.gitignore (raw)`;
Expand All @@ -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`;
Expand Down Expand Up @@ -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@<code>e81a964</code> (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`;
Expand Down
7 changes: 7 additions & 0 deletions fixtures/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] ?? '';

Expand Down
Loading