Skip to content

Commit 62d18dc

Browse files
committed
fix: decode unicode in URL
1 parent 66c9d5f commit 62d18dc

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import reservedNames from 'github-reserved-names/reserved-names.json' with { type: 'json' };
2+
import punycode from 'punycode.js';
23

34
const patchDiffRegex = /[.](patch|diff)$/;
45
const releaseRegex = /^releases[/]tag[/]([^/]+)/;
@@ -86,11 +87,11 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
8687
const url = new URL(href);
8788
const {
8889
origin,
89-
pathname,
9090
search,
9191
searchParams,
9292
hash,
9393
} = url;
94+
const pathname = decodeURIComponent(punycode.toUnicode(url.pathname));
9495

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

179180
// The user prefers seeing the URL as it was typed, so we need to decode it
180181
try {
181-
return decodeURI(cleanHref.join(''));
182+
return decodeURI(cleanHref.map(x => punycode.toUnicode(x)).join(''));
182183
} catch {
183184
// Decoding fails if the URL includes '%%'
184185
return href;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
}
4343
},
4444
"dependencies": {
45-
"github-reserved-names": "^2.0.5"
45+
"github-reserved-names": "^2.0.5",
46+
"punycode.js": "^2.3.1"
4647
},
4748
"devDependencies": {
4849
"@sindresorhus/tsconfig": "^5.0.0",

0 commit comments

Comments
 (0)