Skip to content

Commit

Permalink
Drop need for punycode
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Aug 26, 2024
1 parent c7118d7 commit 55f75aa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
18 changes: 6 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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 @@ -159,12 +158,13 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
* Shorten URL
*/
if (isReserved || pathname === '/' || (!isLocal && !isRaw && !isRedirection)) {
const origin = href.split('/', 3).join('/');
const parsedUrl = new URL(href);
const cleanHref = [
parsedUrl.origin
origin
.replace(/^https:[/][/]/, '')
.replace(/^www[.]/, ''),
parsedUrl.pathname
decodeURI(parsedUrl.pathname)
.replace(/[/]$/, ''),
];

Expand All @@ -175,15 +175,9 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
cleanHref.push(parsedUrl.search);
}

cleanHref.push(parsedUrl.hash);
cleanHref.push(decodeURI(parsedUrl.hash));

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

if (user && !repo) {
Expand Down Expand Up @@ -324,7 +318,7 @@ export function applyToLink(a, currentUrl) {
// And if there are no additional images in the link
&& !a.firstElementChild
) {
const url = getLinkHref(a);
const url = a.textContent;
const shortened = shortenRepoUrl(url, currentUrl);
a.replaceChildren(
...shortened.split(
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
}
},
"dependencies": {
"github-reserved-names": "^2.0.5",
"punycode.js": "^2.3.1"
"github-reserved-names": "^2.0.5"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^5.0.0",
Expand Down

0 comments on commit 55f75aa

Please sign in to comment.