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

Append (comment) when a link points at a comment rather than the whole issue #23734

Merged
merged 27 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bab57d6
add comment to render and init tippy
HesterG Mar 24, 2023
895d34d
skip files changed hash comment
HesterG Mar 24, 2023
ea92068
add new line
HesterG Mar 24, 2023
43563af
comments
HesterG Mar 24, 2023
12d9df9
handle path including pr or issue
HesterG Mar 24, 2023
0cd84ed
adjust condition
HesterG Mar 24, 2023
259ff80
Merge branch 'go-gitea:main' into issue-commet-render-23671
HesterG Mar 24, 2023
60643cb
Merge branch 'go-gitea:main' into issue-commet-render-23671
HesterG Mar 27, 2023
e5d109d
add popup when preview changes
HesterG Mar 27, 2023
a2b91d7
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 27, 2023
13f0515
move if to attachTippyToRefIssues
HesterG Mar 27, 2023
76dbc97
move filesChangedm
HesterG Mar 27, 2023
b3839f2
remove unnecessary ifs and fix order
HesterG Mar 27, 2023
bb58eda
try add locale
HesterG Mar 28, 2023
262ef11
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 28, 2023
d5f70c6
pass language to metas
HesterG Mar 28, 2023
c682983
use context key for locale
wxiaoguang Mar 28, 2023
4919558
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 28, 2023
42a57bf
fine tune variable name and comments
wxiaoguang Mar 28, 2023
8f471ea
Update modules/markup/html.go
wxiaoguang Mar 28, 2023
fad4ae7
Merge branch 'main' into issue-commet-render-23671
silverwind Mar 28, 2023
ab7231b
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 29, 2023
434364a
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 29, 2023
bbea3ba
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 30, 2023
9007732
Merge branch 'main' into issue-commet-render-23671
HesterG Mar 31, 2023
c0bde72
Merge branch 'main' into issue-commet-render-23671
HesterG Apr 3, 2023
b6fb404
Merge branch 'main' into issue-commet-render-23671
lunny Apr 3, 2023
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
29 changes: 27 additions & 2 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ var issueFullPattern *regexp.Regexp
// Once for to prevent races
var issueFullPatternOnce sync.Once

// regexp for full links to hash comment in pull request files changed tab
var filesChangedFullPattern *regexp.Regexp

// Once for to prevent races
var filesChangedFullPatternOnce sync.Once

func getIssueFullPattern() *regexp.Regexp {
issueFullPatternOnce.Do(func() {
issueFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
Expand All @@ -105,6 +111,14 @@ func getIssueFullPattern() *regexp.Regexp {
return issueFullPattern
}

func getFilesChangedFullPattern() *regexp.Regexp {
filesChangedFullPatternOnce.Do(func() {
filesChangedFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
`[\w_.-]+/[\w_.-]+/pulls/((?:\w{1,10}-)?[1-9][0-9]*)/files([\?|#](\S+)?)?\b`)
})
return filesChangedFullPattern
}

// CustomLinkURLSchemes allows for additional schemes to be detected when parsing links within text
func CustomLinkURLSchemes(schemes []string) {
schemes = append(schemes, "http", "https")
Expand Down Expand Up @@ -797,21 +811,32 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
next := node.NextSibling
for node != nil && node != next {
m := getIssueFullPattern().FindStringSubmatchIndex(node.Data)
filesChangedm := getFilesChangedFullPattern().FindStringSubmatchIndex(node.Data)
lunny marked this conversation as resolved.
Show resolved Hide resolved
if m == nil {
return
}

// if the link is from files changed tab in pull requests, leave it as it is
if filesChangedm != nil {
return
}

link := node.Data[m[0]:m[1]]
id := "#" + node.Data[m[2]:m[3]]

// if m[4] and m[5] is not -1, then link is to a comment
// indicate that in the text by appending (comment)
if m[4] != -1 && m[5] != -1 {
id += " (comment)"
HesterG marked this conversation as resolved.
Show resolved Hide resolved
}

// extract repo and org name from matched link like
// http://localhost:3000/gituser/myrepo/issues/1
linkParts := strings.Split(link, "/")
matchOrg := linkParts[len(linkParts)-4]
matchRepo := linkParts[len(linkParts)-3]

if matchOrg == ctx.Metas["user"] && matchRepo == ctx.Metas["repo"] {
// TODO if m[4]:m[5] is not nil, then link is to a comment,
// and we should indicate that in the text somehow
replaceContent(node, m[0], m[1], createLink(link, id, "ref-issue"))
} else {
orgRepoID := matchOrg + "/" + matchRepo + id
Expand Down
8 changes: 6 additions & 2 deletions modules/markup/html_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,17 @@ func TestRender_FullIssueURLs(t *testing.T) {
test("Look here http://localhost:3000/person/repo/issues/4",
`Look here <a href="http://localhost:3000/person/repo/issues/4" class="ref-issue">person/repo#4</a>`)
test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
`<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4</a>`)
`<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4 (comment)</a>`)
test("http://localhost:3000/gogits/gogs/issues/4",
`<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a>`)
test("http://localhost:3000/gogits/gogs/issues/4 test",
`<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a> test`)
test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123 test",
`<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&amp;b=2#comment-123" class="ref-issue">#4</a> test`)
`<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&amp;b=2#comment-123" class="ref-issue">#4 (comment)</a> test`)
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24",
"http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24")
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files",
"http://localhost:3000/testOrg/testOrgRepo/pulls/2/files")
}

func TestRegExp_sha1CurrentPattern(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions web_src/js/features/comp/MarkupContentPreview.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import $ from 'jquery';
import {initMarkupContent} from '../../markup/content.js';
import {attachTippyToRefIssues} from '../contextpopup.js';

const {csrfToken} = window.config;

Expand All @@ -15,8 +16,12 @@ export function initCompMarkupContentPreviewTab($form) {
text: $form.find(`.tab[data-tab="${$tabMenu.data('write')}"] textarea`).val()
}, (data) => {
const $previewPanel = $form.find(`.tab[data-tab="${$tabMenu.data('preview')}"]`);
const refIssues = $previewPanel.find('p .ref-issue');
$previewPanel.html(data);
initMarkupContent();
if (refIssues) {
attachTippyToRefIssues(refIssues);
}
});
});
}
3 changes: 3 additions & 0 deletions web_src/js/features/contextpopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {createTippy} from '../modules/tippy.js';
export function initContextPopups() {
const refIssues = $('.ref-issue');
if (!refIssues.length) return;
attachTippyToRefIssues(refIssues);
}

export function attachTippyToRefIssues(refIssues) {
refIssues.each(function () {
if ($(this).hasClass('ref-external-issue')) {
return;
Expand Down
7 changes: 7 additions & 0 deletions web_src/js/features/repo-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {initCompReactionSelector} from './comp/ReactionSelector.js';
import {initRepoSettingBranches} from './repo-settings.js';
import {initRepoPullRequestMergeForm} from './repo-issue-pr-form.js';
import {hideElem, showElem} from '../utils/dom.js';
import {attachTippyToRefIssues} from './contextpopup.js';

const {csrfToken} = window.config;

Expand Down Expand Up @@ -439,6 +440,12 @@ async function onEditContent(event) {
} else {
$renderContent.html(data.content);
$rawContent.text($textarea.val());
// check if the content contains ref-issue
// if there are ref issues, attach the tippy
const refIssues = $renderContent.find('p .ref-issue');
if (refIssues) {
attachTippyToRefIssues(refIssues);
}
}
const $content = $segment;
if (!$content.find('.dropzone-attachments').length) {
Expand Down