Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorten global issue/PR lists #50

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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