Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further improve CJK and encoded URL support #61

Merged
merged 11 commits into from
Aug 29, 2024
8 changes: 8 additions & 0 deletions __snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ exports[`https://github.com/fregante/shorten-repo-url/blame/v0.12/.gitignore 1`]

exports[`https://github.com/fregante/shorten-repo-url/blob/cc8fc46/.gitignore 1`] = `<code>cc8fc46</code>/.gitignore`;

exports[`https://github.com/fregante/shorten-repo-url/blob/main/한글.txt 1`] = `<code>main</code>/한글.txt`;

exports[`https://github.com/fregante/shorten-repo-url/blob/master/.gitignore 1`] = `<code>master</code>/.gitignore`;

exports[`https://github.com/fregante/shorten-repo-url/blob/v0.12/.gitignore 1`] = `<code>v0.12</code>/.gitignore`;
Expand Down Expand Up @@ -202,6 +204,10 @@ exports[`https://github.com/refined-github/refined-github/wiki/%22Can-you-add-th

exports[`https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22#3-it-doesnt-require-options 1`] = `Wiki: "Can you add this feature?" (3 it doesnt require options) (refined-github/refined-github)`;

exports[`https://github.com/scarf005/hangul-test/wiki/한글-위키-페이지 1`] = `Wiki: 한글 위키 페이지 (scarf005/hangul-test)`;

exports[`https://github.com/scarf005/hangul-test/wiki/한글-위키-페이지#한글-헤딩 1`] = `Wiki: 한글 위키 페이지 (한글 헤딩) (scarf005/hangul-test)`;

exports[`https://github.com/settings/profile 1`] = `github.com/settings/profile`;

exports[`https://github.com/sindresorhus 1`] = `@sindresorhus`;
Expand Down Expand Up @@ -261,3 +267,5 @@ exports[`https://www.npmjs.com/ 1`] = `npmjs.com`;
exports[`https://www.npmjs.com/packaasdge/node 1`] = `npmjs.com/packaasdge/node`;

exports[`https://wwww.google.com/ 1`] = `wwww.google.com`;

exports[`https://한글로-된-경로.com/하위경로#한글-해시 1`] = `한글로-된-경로.com/하위경로#한글-해시`;
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import reservedNames from 'github-reserved-names/reserved-names.json' with { type: 'json' };
import punycode from 'punycode.js';

const patchDiffRegex = /[.](patch|diff)$/;
const releaseRegex = /^releases[/]tag[/]([^/]+)/;
Expand Down Expand Up @@ -86,11 +87,11 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
const url = new URL(href);
const {
origin,
pathname,
search,
searchParams,
hash,
} = url;
const pathname = decodeURIComponent(url.pathname);

const pathnameParts = pathname.slice(1).split('/'); // ['user', 'repo', 'pull', '342']
const repoPath = pathnameParts.slice(2).join('/'); // 'pull/342'
Expand Down Expand Up @@ -178,7 +179,7 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {

// The user prefers seeing the URL as it was typed, so we need to decode it
try {
return decodeURI(cleanHref.join(''));
return decodeURI(cleanHref.map(x => punycode.toUnicode(x)).join(''));
} catch {
// Decoding fails if the URL includes '%%'
return href;
Expand Down
4 changes: 4 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'https://github.com/fregante/shorten-repo-url/blob/master/.gitignore',
'https://github.com/fregante/shorten-repo-url/blob/v0.12/.gitignore',
'https://github.com/fregante/shorten-repo-url/blob/cc8fc46/.gitignore',
'https://github.com/fregante/shorten-repo-url/blob/main/한글.txt',
'https://github.com/nodejs/node/blob/master/.gitignore',
'https://github.com/nodejs/node/blob/v0.12/.gitignore',
'https://github.com/nodejs/node/blob/cc8fc46/.gitignore',
Expand Down Expand Up @@ -138,6 +139,8 @@
'https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22#',
'https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22',
'https://github.com/fregante/shorten-repo-url/wiki/%22Can-you-add-this-feature%3F%22',
'https://github.com/scarf005/hangul-test/wiki/한글-위키-페이지',
'https://github.com/scarf005/hangul-test/wiki/한글-위키-페이지#한글-헤딩',
'https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement#parameters',
'https://www.google.com/',
'https://wwww.google.com/',
Expand All @@ -146,6 +149,7 @@
'https://www.npmjs.com/packaasdge/node',
'https://example.com/nodejs/node/blob/cc8fc46/.gitignore',
'https://example.site/한글로-된-URL',
'https://한글로-된-경로.com/하위경로#한글-해시',
fregante marked this conversation as resolved.
Show resolved Hide resolved
];

test.each(urls)('%s', url => {
Expand All @@ -161,6 +165,6 @@
a.textContent = url;
applyToLink(a, currentLocation);
expect(a.href, 'applyToLink should not alter the href').toBe(correctlyEncodedUrl);
expect(a.innerHTML).toBe(shortened);

Check failure on line 168 in index.test.js

View workflow job for this annotation

GitHub Actions / Test

index.test.js > https://한글로-된-경로.com/하위경로#한글-해시

AssertionError: expected 'https://한글로-된-경로.com/하위경로#한글-해시' to be '한글로-된-경로.com/하위경로#한글-해시' // Object.is equality Expected: "한글로-된-경로.com/하위경로#한글-해시" Received: "https://한글로-된-경로.com/하위경로#한글-해시" ❯ index.test.js:168:22
});

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
}
},
"dependencies": {
"github-reserved-names": "^2.0.5"
"github-reserved-names": "^2.0.5",
"punycode.js": "^2.3.1"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^5.0.0",
Expand Down
Loading