|
| 1 | +/* global document */ |
1 | 2 | import reservedNames from 'github-reserved-names/reserved-names.json' with { type: 'json' }; |
2 | 3 |
|
3 | | -const patchDiffRegex = /[.](patch|diff)$/; |
4 | | -const releaseRegex = /^releases[/]tag[/]([^/]+)/; |
5 | | -const labelRegex = /^labels[/]([^/]+)/; |
6 | | -const compareRegex = /^compare[/]([^/]+)/; |
7 | | -const pullRegex = /^pull[/](?<pull>\d+)(?:[/](?<pullPage>[^/]+))?(?:[/](?<pullPartialStart>[\da-f]{40})[.][.](?<pullPartialEnd>[\da-f]{40}))?$/; |
8 | | -const issueRegex = /^issues[/](\d+)$/; |
9 | | -const commitRegex = /^commit[/]([\da-f]{40})$/; |
10 | | -const releaseArchiveRegex = /^archive[/](.+)([.]zip|[.]tar[.]gz)/; |
11 | | -const releaseDownloadRegex = /^releases[/]download[/]([^/]+)[/](.+)/; |
12 | | -const dependentsRegex = /^network[/]dependents[/]?$/; |
13 | | -const dependenciesRegex = /^network[/]dependencies[/]?$/; |
14 | | -const wikiRegex = /^wiki[/](.+)$/; |
| 4 | +const patchDiffRegex = /\.(patch|diff)$/; |
| 5 | +const releaseRegex = /^releases\/tag\/([^/]+)/; |
| 6 | +const labelRegex = /^labels\/([^/]+)/; |
| 7 | +const compareRegex = /^compare\/([^/]+)/; |
| 8 | +const pullRegex = /^pull\/(?<pull>\d+)(?:\/(?<pullPage>[^/]+))?(?:\/(?<pullPartialStart>[\da-f]{40})\.\.(?<pullPartialEnd>[\da-f]{40}))?$/; |
| 9 | +const issueRegex = /^issues\/(\d+)$/; |
| 10 | +const commitRegex = /^commit\/([\da-f]{40})$/; |
| 11 | +const releaseArchiveRegex = /^archive\/(.+)(\.zip|\.tar\.gz)/; |
| 12 | +const releaseDownloadRegex = /^releases\/download\/([^/]+)\/(.+)/; |
| 13 | +const dependentsRegex = /^network\/dependents\/?$/; |
| 14 | +const dependenciesRegex = /^network\/dependencies\/?$/; |
| 15 | +const wikiRegex = /^wiki\/(.+)$/; |
15 | 16 |
|
16 | 17 | /** @type {(searchParameters: URLSearchParams, pathname: string) => string} */ |
17 | 18 | function pullQueryOut(searchParameters, pathname) { |
@@ -40,7 +41,7 @@ function styleRevision(revision) { |
40 | 41 | } |
41 | 42 |
|
42 | 43 | revision = revision.replace(patchDiffRegex, ''); |
43 | | - if (/^[0-9a-f]{40}$/.test(revision)) { |
| 44 | + if (/^[\da-f]{40}$/.test(revision)) { |
44 | 45 | revision = revision.slice(0, 7); |
45 | 46 | } |
46 | 47 |
|
@@ -72,6 +73,7 @@ function joinValues(array, delimiter = '/') { |
72 | 73 | * @param href {string} |
73 | 74 | * @param currentUrl {string} |
74 | 75 | */ |
| 76 | +/* eslint-disable complexity */ |
75 | 77 | function shortenRepoUrl(href, currentUrl = 'https://github.com') { |
76 | 78 | if (!href) { |
77 | 79 | return; |
@@ -159,10 +161,10 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') { |
159 | 161 | if (isReserved || pathname === '/' || (!isLocal && !isRaw && !isRedirection)) { |
160 | 162 | const cleanHref = [ |
161 | 163 | origin |
162 | | - .replace(/^https:[/][/]/, '') |
163 | | - .replace(/^www[.]/, ''), |
| 164 | + .replace(/^https:\/\//, '') |
| 165 | + .replace(/^www\./, ''), |
164 | 166 | pathname |
165 | | - .replace(/[/]$/, ''), |
| 167 | + .replace(/\/$/, ''), |
166 | 168 | ]; |
167 | 169 |
|
168 | 170 | if (['issues', 'pulls'].includes(user) && !repo) { |
@@ -273,8 +275,9 @@ function shortenRepoUrl(href, currentUrl = 'https://github.com') { |
273 | 275 | } |
274 | 276 |
|
275 | 277 | // Drop leading and trailing slash of relative path |
276 | | - return pathname.replaceAll(/^[/]|[/]$/g, '') + url.search + hash + query; |
| 278 | + return pathname.replaceAll(/^\/|\/$/g, '') + url.search + hash + query; |
277 | 279 | } |
| 280 | +/* eslint-enable complexity */ |
278 | 281 |
|
279 | 282 | /** |
280 | 283 | * Without this, <a>%%</a> would throw an error |
|
0 commit comments