Skip to content

Commit

Permalink
Shorten global issue/PR lists (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jun 23, 2024
1 parent e4fe9c8 commit c125b41
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
53 changes: 37 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ const dependentsRegex = /^network[/]dependents[/]?$/;
const dependenciesRegex = /^network[/]dependencies[/]?$/;
const wikiRegex = /^wiki[/](.+)$/;

function pullQueryOut(searchParameters, pathname) {
let query = searchParameters.get('q');

if (!query) {
return '';
}

searchParameters.delete('q');
if (pathname.endsWith('/issues')) {
query = query.replace('is:issue', '');
}

if (pathname.endsWith('/pulls')) {
query = query.replace('is:pr', '');
}

return ` (${query.replaceAll(/\s+/g, ' ').trim()})`;
}

function styleRevision(revision) {
if (!revision) {
return;
Expand Down Expand Up @@ -127,12 +146,24 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') {
/**
* Shorten URL
*/

if (isReserved || pathname === '/' || (!isLocal && !isRaw && !isRedirection)) {
return href
.replace(/^https:[/][/]/, '')
.replace(/^www[.]/, '')
.replace(/[/]$/, '');
const parsedUrl = new URL(href);
const cleanHref = [
parsedUrl.origin
.replace(/^https:[/][/]/, '')
.replace(/^www[.]/, ''),
parsedUrl.pathname
.replace(/[/]$/, ''),
];

if (['issues', 'pulls'].includes(user) && !repo) {
const query = pullQueryOut(parsedUrl.searchParams, parsedUrl.pathname);
cleanHref.push(parsedUrl.search, query);
} else {
cleanHref.push(parsedUrl.search);
}

return cleanHref.join('');
}

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

let query = searchParams.get('q') ?? '';
if (query) {
searchParams.delete('q');
if (pathname.endsWith('/issues')) {
query = query.replace('is:issue', '');
} else if (pathname.endsWith('/pulls')) {
query = query.replace('is:pr', '');
}

query = ` (${query.replaceAll(/\s+/g, ' ').trim()})`;
}
const query = pullQueryOut(searchParams, pathname);

if (searchParams.get('tab') === 'readme-ov-file') {
searchParams.delete('tab');
Expand Down
8 changes: 8 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@ test('GitHub.com URLs', urlMatcherMacro, new Map([
'https://github.com/fregante/shorten-repo-url/issues?q=is%3Aissue++is%3Aopen+sort%3Aupdated-desc+&unrelated=true',
'fregante/shorten-repo-url/issues?unrelated=true (is:open sort:updated-desc)',
],
[
'https://github.com/issues?q=is%3Aissue++is%3Aopen+sort%3Aupdated-desc+&unrelated=true',
'github.com/issues?unrelated=true (is:open sort:updated-desc)',
],
[
'https://github.com/pulls?q=is%3Apr++is%3Aopen+sort%3Aupdated-desc+&unrelated=true',
'github.com/pulls?unrelated=true (is:open sort:updated-desc)',
],
[
'https://github.com/sindresorhus/notifier-for-github/pull/253/files/6b4489d417c9425dc27c5fb8d6b4a8518debd035..60cdcf3c3646164441bf8f037cef620479cdec59',
'<code>6b4489d4..60cdcf3c</code> (#253)',
Expand Down

0 comments on commit c125b41

Please sign in to comment.