Skip to content

Commit

Permalink
Format partial PR files views (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheap-glitch authored Oct 16, 2021
1 parent 756b4ae commit 499b25f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const patchDiffRegex = /[.](patch|diff)$/;
const releaseRegex = /^releases[/]tag[/]([^/]+)/;
const labelRegex = /^labels[/]([^/]+)/;
const compareRegex = /^compare[/]([^/]+)/;
const pullRegex = /^pull[/](\d+)[/]([^/]+)$/;
const pullRegex = /^pull[/](\d+)[/]([^/]+)(?:[/]([\da-f]{40})[.][.]([\da-f]{40}))?$/;
const releaseArchiveRegex = /^archive[/](.+)([.]zip|[.]tar[.]gz)/;
const releaseDownloadRegex = /^releases[/]download[/]([^/]+)[/](.+)/;
const dependentsRegex = /^network[/]dependents[/]?$/;
Expand Down Expand Up @@ -90,7 +90,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
const [, downloadTag, downloadFilename] = repoPath.match(releaseDownloadRegex) || [];
const [, label] = repoPath.match(labelRegex) || [];
const [, compare] = repoPath.match(compareRegex) || [];
const [, pull, pullPage] = repoPath.match(pullRegex) || [];
const [, pull, pullPage, pullPartialStart, pullPartialEnd] = repoPath.match(pullRegex) || [];
const isFileOrDir = revision && [
'raw',
'tree',
Expand Down Expand Up @@ -164,8 +164,14 @@ function shortenURL(href, currentUrl = 'https://github.com') {
return `${user}/${repo} (dependencies)`;
}

if (pull && pullPage) {
return `${repoUrl}#${pull} (${pullPage})`;
if (pull) {
if (pullPage === 'files' && pullPartialStart && pullPartialEnd) {
return `<code>${pullPartialStart.slice(0, 8)}..${pullPartialEnd.slice(0, 8)}</code> (#${pull})`;
}

if (pullPage) {
return `${repoUrl}#${pull} (${pullPage})`;
}
}

if (compare) {
Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,8 @@ 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/sindresorhus/notifier-for-github/pull/253/files/6b4489d417c9425dc27c5fb8d6b4a8518debd035..60cdcf3c3646164441bf8f037cef620479cdec59',
'<code>6b4489d4..60cdcf3c</code> (#253)',
],
]));

0 comments on commit 499b25f

Please sign in to comment.