diff options
author | guillep2k <18600385+guillep2k@users.noreply.github.com> | 2019-12-01 10:57:05 -0300 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-12-01 15:57:05 +0200 |
commit | 6a90c7e3dd2a571b0f6ea9081633b372073ca197 (patch) | |
tree | abad0ab8177049d50974f2c516484ac4fae20d03 /modules/markup | |
parent | 2011a5b8183fe227f9f57f861dbe42abbd7abf42 (diff) | |
download | gitea-6a90c7e3dd2a571b0f6ea9081633b372073ca197.tar.gz gitea-6a90c7e3dd2a571b0f6ea9081633b372073ca197.zip |
Alternate syntax for cross references (#9116)
* Add support for local vs. remote xrefs
* Add doc for references
* Docs: fix cases not currently supported
* One more doc fix
* Doc: mentions for teams and orgs
* Change !num ref concept, no change in functionality
* Fix test
* Improve table of issue reference types
* Fix paragraph mark
Diffstat (limited to 'modules/markup')
-rw-r--r-- | modules/markup/html.go | 36 | ||||
-rw-r--r-- | modules/markup/html_internal_test.go | 58 |
2 files changed, 64 insertions, 30 deletions
diff --git a/modules/markup/html.go b/modules/markup/html.go index e5a475aff8..b06b6da7ae 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -640,10 +640,19 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) { ref *references.RenderizableReference ) - if ctx.metas["style"] == IssueNameStyleAlphanumeric { - found, ref = references.FindRenderizableReferenceAlphanumeric(node.Data) - } else { - found, ref = references.FindRenderizableReferenceNumeric(node.Data) + _, exttrack := ctx.metas["format"] + alphanum := ctx.metas["style"] == IssueNameStyleAlphanumeric + + // Repos with external issue trackers might still need to reference local PRs + // We need to concern with the first one that shows up in the text, whichever it is + found, ref = references.FindRenderizableReferenceNumeric(node.Data, exttrack && alphanum) + if exttrack && alphanum { + if found2, ref2 := references.FindRenderizableReferenceAlphanumeric(node.Data); found2 { + if !found || ref2.RefLocation.Start < ref.RefLocation.Start { + found = true + ref = ref2 + } + } } if !found { return @@ -651,13 +660,22 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) { var link *html.Node reftext := node.Data[ref.RefLocation.Start:ref.RefLocation.End] - if _, ok := ctx.metas["format"]; ok { + if exttrack && !ref.IsPull { ctx.metas["index"] = ref.Issue link = createLink(com.Expand(ctx.metas["format"], ctx.metas), reftext, "issue") - } else if ref.Owner == "" { - link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "issues", ref.Issue), reftext, "issue") } else { - link = createLink(util.URLJoin(setting.AppURL, ref.Owner, ref.Name, "issues", ref.Issue), reftext, "issue") + // Path determines the type of link that will be rendered. It's unknown at this point whether + // the linked item is actually a PR or an issue. Luckily it's of no real consequence because + // Gitea will redirect on click as appropriate. + path := "issues" + if ref.IsPull { + path = "pulls" + } + if ref.Owner == "" { + link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], path, ref.Issue), reftext, "issue") + } else { + link = createLink(util.URLJoin(setting.AppURL, ref.Owner, ref.Name, path, ref.Issue), reftext, "issue") + } } if ref.Action == references.XRefActionNone { @@ -667,7 +685,7 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) { // Decorate action keywords if actionable var keyword *html.Node - if references.IsXrefActionable(ref.Action) { + if references.IsXrefActionable(ref, exttrack, alphanum) { keyword = createKeyword(node.Data[ref.ActionLocation.Start:ref.ActionLocation.End]) } else { keyword = &html.Node{ diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go index 9722063e17..2746dec2cf 100644 --- a/modules/markup/html_internal_test.go +++ b/modules/markup/html_internal_test.go @@ -25,8 +25,8 @@ func alphanumIssueLink(baseURL, class, name string) string { } // numericLink an HTML to a numeric-style issue -func numericIssueLink(baseURL, class string, index int) string { - return link(util.URLJoin(baseURL, strconv.Itoa(index)), class, fmt.Sprintf("#%d", index)) +func numericIssueLink(baseURL, class string, index int, marker string) string { + return link(util.URLJoin(baseURL, strconv.Itoa(index)), class, fmt.Sprintf("%s%d", marker, index)) } // link an HTML link @@ -75,8 +75,12 @@ func TestRender_IssueIndexPattern(t *testing.T) { test("#abcd") test("test#1234") test("#1234test") - test(" test #1234test") + test("#abcd") + test("test!1234") + test("!1234test") + test(" test !1234test") test("/home/gitea/#1234") + test("/home/gitea/!1234") // should not render issue mention without leading space test("test#54321 issue") @@ -90,42 +94,54 @@ func TestRender_IssueIndexPattern2(t *testing.T) { setting.AppSubURL = AppSubURL // numeric: render inputs with valid mentions - test := func(s, expectedFmt string, indices ...int) { + test := func(s, expectedFmt, marker string, indices ...int) { + var path, prefix string + if marker == "!" { + path = "pulls" + prefix = "http://localhost:3000/someUser/someRepo/pulls/" + } else { + path = "issues" + prefix = "https://someurl.com/someUser/someRepo/" + } + links := make([]interface{}, len(indices)) for i, index := range indices { - links[i] = numericIssueLink(util.URLJoin(setting.AppSubURL, "issues"), "issue", index) + links[i] = numericIssueLink(util.URLJoin(setting.AppSubURL, path), "issue", index, marker) } expectedNil := fmt.Sprintf(expectedFmt, links...) testRenderIssueIndexPattern(t, s, expectedNil, &postProcessCtx{metas: localMetas}) for i, index := range indices { - links[i] = numericIssueLink("https://someurl.com/someUser/someRepo/", "issue", index) + links[i] = numericIssueLink(prefix, "issue", index, marker) } expectedNum := fmt.Sprintf(expectedFmt, links...) testRenderIssueIndexPattern(t, s, expectedNum, &postProcessCtx{metas: numericMetas}) } // should render freestanding mentions - test("#1234 test", "%s test", 1234) - test("test #8 issue", "test %s issue", 8) - test("test issue #1234", "test issue %s", 1234) - test("fixes issue #1234.", "fixes issue %s.", 1234) + test("#1234 test", "%s test", "#", 1234) + test("test #8 issue", "test %s issue", "#", 8) + test("!1234 test", "%s test", "!", 1234) + test("test !8 issue", "test %s issue", "!", 8) + test("test issue #1234", "test issue %s", "#", 1234) + test("fixes issue #1234.", "fixes issue %s.", "#", 1234) // should render mentions in parentheses / brackets - test("(#54321 issue)", "(%s issue)", 54321) - test("[#54321 issue]", "[%s issue]", 54321) - test("test (#9801 extra) issue", "test (%s extra) issue", 9801) - test("test (#1)", "test (%s)", 1) + test("(#54321 issue)", "(%s issue)", "#", 54321) + test("[#54321 issue]", "[%s issue]", "#", 54321) + test("test (#9801 extra) issue", "test (%s extra) issue", "#", 9801) + test("test (!9801 extra) issue", "test (%s extra) issue", "!", 9801) + test("test (#1)", "test (%s)", "#", 1) // should render multiple issue mentions in the same line - test("#54321 #1243", "%s %s", 54321, 1243) - test("wow (#54321 #1243)", "wow (%s %s)", 54321, 1243) - test("(#4)(#5)", "(%s)(%s)", 4, 5) - test("#1 (#4321) test", "%s (%s) test", 1, 4321) + test("#54321 #1243", "%s %s", "#", 54321, 1243) + test("wow (#54321 #1243)", "wow (%s %s)", "#", 54321, 1243) + test("(#4)(#5)", "(%s)(%s)", "#", 4, 5) + test("#1 (#4321) test", "%s (%s) test", "#", 1, 4321) // should render with : - test("#1234: test", "%s: test", 1234) - test("wow (#54321: test)", "wow (%s: test)", 54321) + test("#1234: test", "%s: test", "#", 1234) + test("wow (#54321: test)", "wow (%s: test)", "#", 54321) } func TestRender_IssueIndexPattern3(t *testing.T) { @@ -201,7 +217,7 @@ func TestRender_AutoLink(t *testing.T) { // render valid issue URLs test(util.URLJoin(setting.AppSubURL, "issues", "3333"), - numericIssueLink(util.URLJoin(setting.AppSubURL, "issues"), "issue", 3333)) + numericIssueLink(util.URLJoin(setting.AppSubURL, "issues"), "issue", 3333, "#")) // render valid commit URLs tmp := util.URLJoin(AppSubURL, "commit", "d8a994ef243349f321568f9e36d5c3f444b99cae") |