Skip to content

Commit a20489a

Browse files
scarf005fregante
andauthored
Further improve CJK and encoded URL support (#61)
Co-authored-by: Federico <[email protected]>
1 parent 2f6bf27 commit a20489a

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

__snapshots__/index.test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ exports[`https://github.com/fregante/shorten-repo-url/blame/v0.12/.gitignore 1`]
4040

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

43+
exports[`https://github.com/fregante/shorten-repo-url/blob/main/한글.txt 1`] = `<code>main</code>/한글.txt`;
44+
4345
exports[`https://github.com/fregante/shorten-repo-url/blob/master/.gitignore 1`] = `<code>master</code>/.gitignore`;
4446

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

203205
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)`;
204206

207+
exports[`https://github.com/scarf005/hangul-test/wiki/한글-위키-페이지 1`] = `Wiki: 한글 위키 페이지 (scarf005/hangul-test)`;
208+
209+
exports[`https://github.com/scarf005/hangul-test/wiki/한글-위키-페이지#한글-헤딩 1`] = `Wiki: 한글 위키 페이지 (한글 헤딩) (scarf005/hangul-test)`;
210+
205211
exports[`https://github.com/settings/profile 1`] = `github.com/settings/profile`;
206212

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

263269
exports[`https://wwww.google.com/ 1`] = `wwww.google.com`;
270+
271+
exports[`https://한글로-된-경로.com/하위경로#한글-해시 1`] = `한글로-된-경로.com/하위경로#한글-해시`;

index.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,15 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
8181
const currentRepo = currentUrl.pathname.slice(1).split('/', 2).join('/');
8282

8383
/**
84-
* Parse URL
84+
* Parse URL manually to avoid URL encoding and punycode
8585
*/
86+
const origin = href.split('/', 3).join('/');
87+
const pathname = href.slice(origin.length).replace(/[?#].*/, '');
88+
const hash = /#.+$/.exec(href)?.[0] ?? '';
89+
90+
// Use URL exclusively for search parameters because they're too hard to parse
8691
const url = new URL(href);
87-
const {
88-
origin,
89-
pathname,
90-
search,
91-
searchParams,
92-
hash,
93-
} = url;
92+
const {search, searchParams} = url;
9493

9594
const pathnameParts = pathname.slice(1).split('/'); // ['user', 'repo', 'pull', '342']
9695
const repoPath = pathnameParts.slice(2).join('/'); // 'pull/342'
@@ -158,31 +157,24 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
158157
* Shorten URL
159158
*/
160159
if (isReserved || pathname === '/' || (!isLocal && !isRaw && !isRedirection)) {
161-
const parsedUrl = new URL(href);
162160
const cleanHref = [
163-
parsedUrl.origin
161+
origin
164162
.replace(/^https:[/][/]/, '')
165163
.replace(/^www[.]/, ''),
166-
parsedUrl.pathname
164+
pathname
167165
.replace(/[/]$/, ''),
168166
];
169167

170168
if (['issues', 'pulls'].includes(user) && !repo) {
171-
const query = pullQueryOut(parsedUrl.searchParams, parsedUrl.pathname);
172-
cleanHref.push(parsedUrl.search, query);
169+
const query = pullQueryOut(url.searchParams, url.pathname);
170+
cleanHref.push(url.search, query);
173171
} else {
174-
cleanHref.push(parsedUrl.search);
172+
cleanHref.push(url.search);
175173
}
176174

177-
cleanHref.push(parsedUrl.hash);
175+
cleanHref.push(decodeURI(url.hash));
178176

179-
// The user prefers seeing the URL as it was typed, so we need to decode it
180-
try {
181-
return decodeURI(cleanHref.join(''));
182-
} catch {
183-
// Decoding fails if the URL includes '%%'
184-
return href;
185-
}
177+
return cleanHref.join('');
186178
}
187179

188180
if (user && !repo) {
@@ -290,16 +282,18 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
290282
*/
291283
function safeDecode(url) {
292284
try {
293-
return decodeURIComponent(url);
285+
return new URL(url).href;
294286
} catch {
295287
return url;
296288
}
297289
}
298290

291+
/** @param a {HTMLAnchorElement} */
299292
function getLinkHref(a) {
300293
return a.dataset.originalHref ?? a.href;
301294
}
302295

296+
/** @param a {HTMLAnchorElement} */
303297
function isCustomLink(a) {
304298
const url = safeDecode(getLinkHref(a));
305299
const label = safeDecode(a.textContent);
@@ -321,7 +315,7 @@ export function applyToLink(a, currentUrl) {
321315
// And if there are no additional images in the link
322316
&& !a.firstElementChild
323317
) {
324-
const url = getLinkHref(a);
318+
const url = a.textContent;
325319
const shortened = shortenRepoUrl(url, currentUrl);
326320
a.replaceChildren(
327321
...shortened.split(

index.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const urls = [
3232
'https://github.com/fregante/shorten-repo-url/blob/master/.gitignore',
3333
'https://github.com/fregante/shorten-repo-url/blob/v0.12/.gitignore',
3434
'https://github.com/fregante/shorten-repo-url/blob/cc8fc46/.gitignore',
35+
'https://github.com/fregante/shorten-repo-url/blob/main/한글.txt',
3536
'https://github.com/nodejs/node/blob/master/.gitignore',
3637
'https://github.com/nodejs/node/blob/v0.12/.gitignore',
3738
'https://github.com/nodejs/node/blob/cc8fc46/.gitignore',
@@ -138,6 +139,8 @@ const urls = [
138139
'https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22#',
139140
'https://github.com/refined-github/refined-github/wiki/%22Can-you-add-this-feature%3F%22',
140141
'https://github.com/fregante/shorten-repo-url/wiki/%22Can-you-add-this-feature%3F%22',
142+
'https://github.com/scarf005/hangul-test/wiki/한글-위키-페이지',
143+
'https://github.com/scarf005/hangul-test/wiki/한글-위키-페이지#한글-헤딩',
141144
'https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement#parameters',
142145
'https://www.google.com/',
143146
'https://wwww.google.com/',
@@ -146,6 +149,7 @@ const urls = [
146149
'https://www.npmjs.com/packaasdge/node',
147150
'https://example.com/nodejs/node/blob/cc8fc46/.gitignore',
148151
'https://example.site/한글로-된-URL',
152+
'https://한글로-된-경로.com/하위경로#한글-해시',
149153
];
150154

151155
test.each(urls)('%s', url => {

0 commit comments

Comments
 (0)