Skip to content

Commit 2c4fadf

Browse files
authored
Shorten no-referral GitHub URLs too (#33)
1 parent 98c6175 commit 2c4fadf

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

index.js

+39-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const patchDiffRegex = /[.](patch|diff)$/;
44
const releaseRegex = /^releases[/]tag[/]([^/]+)/;
55
const labelRegex = /^labels[/]([^/]+)/;
66
const compareRegex = /^compare[/]([^/]+)/;
7-
const pullRegex = /^pull[/](\d+)[/]([^/]+)(?:[/]([\da-f]{40})[.][.]([\da-f]{40}))?$/;
7+
const pullRegex = /^pull[/](\d+)(?:[/]([^/]+))?(?:[/]([\da-f]{40})[.][.]([\da-f]{40}))?$/;
8+
const issueRegex = /^issues[/](\d+)$/;
9+
const commitRegex = /^commit[/]([\da-f]{40})$/;
810
const releaseArchiveRegex = /^archive[/](.+)([.]zip|[.]tar[.]gz)/;
911
const releaseDownloadRegex = /^releases[/]download[/]([^/]+)[/](.+)/;
1012
const dependentsRegex = /^network[/]dependents[/]?$/;
@@ -23,6 +25,18 @@ function styleRevision(revision) {
2325
return `<code>${revision}</code>`;
2426
}
2527

28+
function commentIndicator(hash) {
29+
if (hash.startsWith('#issue-') || hash.startsWith('#commitcomment-')) {
30+
return ' (comment)';
31+
}
32+
33+
if (hash.startsWith('#pullrequestreview-')) {
34+
return ' (review)';
35+
}
36+
37+
return '';
38+
}
39+
2640
// Filter out null values
2741
function joinValues(array, delimiter = '/') {
2842
return array.filter(s => s).join(delimiter);
@@ -57,6 +71,11 @@ function shortenURL(href, currentUrl = 'https://github.com') {
5771
'https://rawgit.com',
5872
].includes(origin);
5973

74+
const isRedirection = [
75+
'https://togithub.com', // Renovate
76+
'https://github-redirect.dependabot.com', // Dependabot
77+
].includes(origin);
78+
6079
let [
6180
user,
6281
repo,
@@ -80,7 +99,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
8099
filePath = filePath.join('/');
81100

82101
const isLocal = origin === currentUrl.origin;
83-
const isThisRepo = (isLocal || isRaw) && currentRepo === `${user}/${repo}`;
102+
const isThisRepo = (isLocal || isRaw || isRedirection) && currentRepo === `${user}/${repo}`;
84103
const isReserved = reservedPaths.includes(user);
85104
const isDependents = dependentsRegex.test(repoPath);
86105
const isDependencies = dependenciesRegex.test(repoPath);
@@ -91,6 +110,8 @@ function shortenURL(href, currentUrl = 'https://github.com') {
91110
const [, label] = repoPath.match(labelRegex) || [];
92111
const [, compare] = repoPath.match(compareRegex) || [];
93112
const [, pull, pullPage, pullPartialStart, pullPartialEnd] = repoPath.match(pullRegex) || [];
113+
const [, issue] = isRedirection ? repoPath.match(issueRegex) || [] : [];
114+
const [, commit] = isRedirection ? repoPath.match(commitRegex) || [] : [];
94115
const isFileOrDir = revision && [
95116
'raw',
96117
'tree',
@@ -105,7 +126,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
105126
* Shorten URL
106127
*/
107128

108-
if (isReserved || pathname === '/' || (!isLocal && !isRaw)) {
129+
if (isReserved || pathname === '/' || (!isLocal && !isRaw && !isRedirection)) {
109130
return href
110131
.replace(/^https:[/][/]/, '')
111132
.replace(/^www[.]/, '')
@@ -179,6 +200,21 @@ function shortenURL(href, currentUrl = 'https://github.com') {
179200
return `${partial}${search}${hash} (compare)`;
180201
}
181202

203+
// Shorten URLs that would otherwise be natively shortened
204+
if (isRedirection) {
205+
if (issue) {
206+
return `${repoUrl}#${issue}${commentIndicator(hash)}`;
207+
}
208+
209+
if (pull) {
210+
return `${repoUrl}#${pull}${commentIndicator(hash)}`;
211+
}
212+
213+
if (commit) {
214+
return joinValues([repoUrl, `<code>${commit.slice(0, 7)}</code>`], '@') + commentIndicator(hash);
215+
}
216+
}
217+
182218
let query = searchParams.get('q') ?? '';
183219
if (query) {
184220
searchParams.delete('q');

test.js

+40
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,44 @@ test('GitHub.com URLs', urlMatcherMacro, new Map([
438438
'https://github.com/sindresorhus/notifier-for-github/pull/253/files/6b4489d417c9425dc27c5fb8d6b4a8518debd035..60cdcf3c3646164441bf8f037cef620479cdec59',
439439
'<code>6b4489d4..60cdcf3c</code> (#253)',
440440
],
441+
[
442+
'https://togithub.com/fregante/shorten-repo-url/issues/25',
443+
'#25',
444+
],
445+
[
446+
'https://togithub.com/fregante/shorten-repo-url/issues/28#issue-850900171',
447+
'#28 (comment)',
448+
],
449+
[
450+
'https://togithub.com/fregante/shorten-repo-url/pull/32',
451+
'#32',
452+
],
453+
[
454+
'https://togithub.com/fregante/shorten-repo-url/pull/32/files',
455+
'#32 (files)',
456+
],
457+
[
458+
'https://togithub.com/fregante/shorten-repo-url/pull/33#pullrequestreview-801229042',
459+
'#33 (review)',
460+
],
461+
[
462+
'https://togithub.com/nodejs/node/pull/123',
463+
'nodejs/node#123',
464+
],
465+
[
466+
'https://togithub.com/nodejs/node/pull/123/files',
467+
'nodejs/node#123 (files)',
468+
],
469+
[
470+
'https://togithub.com/fregante/shorten-repo-url/commit/98c6175b0cbd4caca71d24e68e57b942b0dfb549',
471+
'<code>98c6175</code>',
472+
],
473+
[
474+
'https://togithub.com/refined-github/refined-github/commit/4f270c4f50e0a2a20085a6e92095117f10340322',
475+
'refined-github/refined-github@<code>4f270c4</code>',
476+
],
477+
[
478+
'https://togithub.com/refined-github/refined-github/commit/e81a9646b448d90c7e02ab41332cab0507dccbbd#commitcomment-60089354',
479+
'refined-github/refined-github@<code>e81a964</code> (comment)',
480+
],
441481
]));

0 commit comments

Comments
 (0)