Skip to content

Commit c125b41

Browse files
authored
Shorten global issue/PR lists (#50)
1 parent e4fe9c8 commit c125b41

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

index.js

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ const dependentsRegex = /^network[/]dependents[/]?$/;
1313
const dependenciesRegex = /^network[/]dependencies[/]?$/;
1414
const wikiRegex = /^wiki[/](.+)$/;
1515

16+
function pullQueryOut(searchParameters, pathname) {
17+
let query = searchParameters.get('q');
18+
19+
if (!query) {
20+
return '';
21+
}
22+
23+
searchParameters.delete('q');
24+
if (pathname.endsWith('/issues')) {
25+
query = query.replace('is:issue', '');
26+
}
27+
28+
if (pathname.endsWith('/pulls')) {
29+
query = query.replace('is:pr', '');
30+
}
31+
32+
return ` (${query.replaceAll(/\s+/g, ' ').trim()})`;
33+
}
34+
1635
function styleRevision(revision) {
1736
if (!revision) {
1837
return;
@@ -127,12 +146,24 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
127146
/**
128147
* Shorten URL
129148
*/
130-
131149
if (isReserved || pathname === '/' || (!isLocal && !isRaw && !isRedirection)) {
132-
return href
133-
.replace(/^https:[/][/]/, '')
134-
.replace(/^www[.]/, '')
135-
.replace(/[/]$/, '');
150+
const parsedUrl = new URL(href);
151+
const cleanHref = [
152+
parsedUrl.origin
153+
.replace(/^https:[/][/]/, '')
154+
.replace(/^www[.]/, ''),
155+
parsedUrl.pathname
156+
.replace(/[/]$/, ''),
157+
];
158+
159+
if (['issues', 'pulls'].includes(user) && !repo) {
160+
const query = pullQueryOut(parsedUrl.searchParams, parsedUrl.pathname);
161+
cleanHref.push(parsedUrl.search, query);
162+
} else {
163+
cleanHref.push(parsedUrl.search);
164+
}
165+
166+
return cleanHref.join('');
136167
}
137168

138169
if (user && !repo) {
@@ -224,17 +255,7 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
224255
}
225256
}
226257

227-
let query = searchParams.get('q') ?? '';
228-
if (query) {
229-
searchParams.delete('q');
230-
if (pathname.endsWith('/issues')) {
231-
query = query.replace('is:issue', '');
232-
} else if (pathname.endsWith('/pulls')) {
233-
query = query.replace('is:pr', '');
234-
}
235-
236-
query = ` (${query.replaceAll(/\s+/g, ' ').trim()})`;
237-
}
258+
const query = pullQueryOut(searchParams, pathname);
238259

239260
if (searchParams.get('tab') === 'readme-ov-file') {
240261
searchParams.delete('tab');

index.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,14 @@ test('GitHub.com URLs', urlMatcherMacro, new Map([
442442
'https://github.com/fregante/shorten-repo-url/issues?q=is%3Aissue++is%3Aopen+sort%3Aupdated-desc+&unrelated=true',
443443
'fregante/shorten-repo-url/issues?unrelated=true (is:open sort:updated-desc)',
444444
],
445+
[
446+
'https://github.com/issues?q=is%3Aissue++is%3Aopen+sort%3Aupdated-desc+&unrelated=true',
447+
'github.com/issues?unrelated=true (is:open sort:updated-desc)',
448+
],
449+
[
450+
'https://github.com/pulls?q=is%3Apr++is%3Aopen+sort%3Aupdated-desc+&unrelated=true',
451+
'github.com/pulls?unrelated=true (is:open sort:updated-desc)',
452+
],
445453
[
446454
'https://github.com/sindresorhus/notifier-for-github/pull/253/files/6b4489d417c9425dc27c5fb8d6b4a8518debd035..60cdcf3c3646164441bf8f037cef620479cdec59',
447455
'<code>6b4489d4..60cdcf3c</code> (#253)',

0 commit comments

Comments
 (0)