Skip to content

Commit 2c6cc0b

Browse files
authored
Fix links for the menus in the view file page (#22795)
1 parent d5fa2e7 commit 2c6cc0b

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

templates/repo/view_file.tmpl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@
110110
</table>
111111
<div class="code-line-menu ui vertical pointing menu tippy-target">
112112
{{if $.Permission.CanRead $.UnitTypeIssues}}
113-
{{/* FIXME: Here we use HTMLURL but not link, see https://github.com/go-gitea/gitea/pull/21986/files#r1096532186*/}}
114-
<a class="item ref-in-new-issue" href="{{.RepoLink}}/issues/new?body={{.Repository.HTMLURL}}{{printf "/src/commit/"}}{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}" rel="nofollow noindex">{{.locale.Tr "repo.issues.context.reference_issue"}}</a>
113+
<a class="item ref-in-new-issue" data-url-issue-new="{{.RepoLink}}/issues/new" data-url-param-body-link="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}" rel="nofollow noindex">{{.locale.Tr "repo.issues.context.reference_issue"}}</a>
115114
{{end}}
116115
<a class="item view_git_blame" href="{{.Repository.Link}}/blame/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{.locale.Tr "repo.view_git_blame"}}</a>
117116
<a class="item copy-line-permalink" data-url="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{.locale.Tr "repo.file_copy_permalink"}}</a>

web_src/js/features/repo-code.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {svg} from '../svg.js';
33
import {invertFileFolding} from './file-fold.js';
44
import {createTippy} from '../modules/tippy.js';
55
import {copyToClipboard} from './clipboard.js';
6+
import {toAbsoluteUrl} from '../utils.js';
67

78
export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/;
89
export const rangeAnchorRegex = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/;
@@ -19,17 +20,18 @@ function selectRange($list, $select, $from) {
1920
$list.removeClass('active');
2021

2122
// add hashchange to permalink
22-
const $issue = $('a.ref-in-new-issue');
23+
const $refInNewIssue = $('a.ref-in-new-issue');
2324
const $copyPermalink = $('a.copy-line-permalink');
2425
const $viewGitBlame = $('a.view_git_blame');
2526

2627
const updateIssueHref = function (anchor) {
27-
if ($issue.length === 0) {
28+
if ($refInNewIssue.length === 0) {
2829
return;
2930
}
30-
let href = $issue.attr('href');
31-
href = `${href.replace(/%23L\d+$|%23L\d+-L\d+$/, '')}%23${anchor}`;
32-
$issue.attr('href', href);
31+
const urlIssueNew = $refInNewIssue.attr('data-url-issue-new');
32+
const urlParamBodyLink = $refInNewIssue.attr('data-url-param-body-link');
33+
const issueContent = `${toAbsoluteUrl(urlParamBodyLink)}#${anchor}`; // the default content for issue body
34+
$refInNewIssue.attr('href', `${urlIssueNew}?body=${encodeURIComponent(issueContent)}`);
3335
};
3436

3537
const updateViewGitBlameFragment = function (anchor) {
@@ -188,7 +190,7 @@ export function initRepoCodeView() {
188190
currentTarget.closest('tr').outerHTML = blob;
189191
});
190192
$(document).on('click', '.copy-line-permalink', async (e) => {
191-
const success = await copyToClipboard(e.currentTarget.getAttribute('data-url'));
193+
const success = await copyToClipboard(toAbsoluteUrl(e.currentTarget.getAttribute('data-url')));
192194
if (!success) return;
193195
document.querySelector('.code-line-button')?._tippy?.hide();
194196
});

web_src/js/utils.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,15 @@ export function convertImage(blob, mime) {
134134
});
135135
}
136136

137-
export function toAbsoluteUrl(relUrl) {
138-
if (relUrl.startsWith('http://') || relUrl.startsWith('https://')) {
139-
return relUrl;
137+
export function toAbsoluteUrl(url) {
138+
if (url.startsWith('http://') || url.startsWith('https://')) {
139+
return url;
140140
}
141-
return `${window.location.origin}${relUrl}`;
141+
if (url.startsWith('//')) {
142+
return `${window.location.protocol}${url}`; // it's also a somewhat absolute URL (with the current scheme)
143+
}
144+
if (url && !url.startsWith('/')) {
145+
throw new Error('unsupported url, it should either start with / or http(s)://');
146+
}
147+
return `${window.location.origin}${url}`;
142148
}

web_src/js/utils.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ test('blobToDataURI', async () => {
139139
});
140140

141141
test('toAbsoluteUrl', () => {
142+
expect(toAbsoluteUrl('//host/dir')).toEqual('http://host/dir');
143+
expect(toAbsoluteUrl('https://host/dir')).toEqual('https://host/dir');
144+
142145
expect(toAbsoluteUrl('')).toEqual('http://localhost:3000');
143146
expect(toAbsoluteUrl('/user/repo')).toEqual('http://localhost:3000/user/repo');
147+
148+
expect(() => toAbsoluteUrl('path')).toThrowError('unsupported');
144149
});

0 commit comments

Comments
 (0)