Skip to content

Commit

Permalink
fix: decode unicode in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Aug 26, 2024
1 parent 94111a9 commit 1c6a81f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
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(punycode.toUnicode(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
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

0 comments on commit 1c6a81f

Please sign in to comment.