diff options
author | mrsdizzie <info@mrsdizzie.com> | 2019-04-12 01:53:34 -0400 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-04-12 08:53:34 +0300 |
commit | 3ff0a126e12109b6c3aceaa229dd1bf229b6ad4b (patch) | |
tree | d3201fa81e7b6dc01d238ec7b833456e47463d6c /modules | |
parent | 3186ef554cdbf54e1a3328ffcb35ea18105d7cb1 (diff) | |
download | gitea-3ff0a126e12109b6c3aceaa229dd1bf229b6ad4b.tar.gz gitea-3ff0a126e12109b6c3aceaa229dd1bf229b6ad4b.zip |
Improve issue autolinks (#6273)
* Improve issue autolinks
Update autolinks to match what github does here:
Issue in same repo: #1
Issue in different repo: org/repo#1
Fixes #6264
* Use setting.AppURL when parsing URL
Using setting.AppURL here is a more reliable way of parsing the current
URL and what other functions in this file seem to use.
* Make ComposeMetas always return a valid context
* Add per repository markdown renderers for better context
* Update for use of context metas
Now that we include the user and repo name inside context metas, update
various code and tests for this new logic
Diffstat (limited to 'modules')
-rw-r--r-- | modules/markup/html.go | 34 | ||||
-rw-r--r-- | modules/markup/html_internal_test.go | 20 | ||||
-rw-r--r-- | modules/markup/markdown/markdown_test.go | 14 |
3 files changed, 52 insertions, 16 deletions
diff --git a/modules/markup/html.go b/modules/markup/html.go index 136f76d301..930c6b3a3e 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -551,20 +551,37 @@ func shortLinkProcessorFull(ctx *postProcessCtx, node *html.Node, noLink bool) { } func fullIssuePatternProcessor(ctx *postProcessCtx, node *html.Node) { + if ctx.metas == nil { + return + } m := getIssueFullPattern().FindStringSubmatchIndex(node.Data) if m == nil { return } link := node.Data[m[0]:m[1]] id := "#" + node.Data[m[2]:m[3]] - // 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)) + + // extract repo and org name from matched link like + // http://localhost:3000/gituser/myrepo/issues/1 + linkParts := strings.Split(path.Clean(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)) + + } else { + orgRepoID := matchOrg + "/" + matchRepo + id + replaceContent(node, m[0], m[1], createLink(link, orgRepoID)) + } } func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) { - prefix := cutoutVerbosePrefix(ctx.urlPrefix) - + if ctx.metas == nil { + return + } // default to numeric pattern, unless alphanumeric is requested. pattern := issueNumericPattern if ctx.metas["style"] == IssueNameStyleAlphanumeric { @@ -575,11 +592,10 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) { if match == nil { return } + id := node.Data[match[2]:match[3]] var link *html.Node - if ctx.metas == nil { - link = createLink(util.URLJoin(prefix, "issues", id[1:]), id) - } else { + if _, ok := ctx.metas["format"]; ok { // Support for external issue tracker if ctx.metas["style"] == IssueNameStyleAlphanumeric { ctx.metas["index"] = id @@ -587,6 +603,8 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) { ctx.metas["index"] = id[1:] } link = createLink(com.Expand(ctx.metas["format"], ctx.metas), id) + } else { + link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "issues", id[1:]), id) } replaceContent(node, match[2], match[3], link) } diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go index c71948593d..135a8e103c 100644 --- a/modules/markup/html_internal_test.go +++ b/modules/markup/html_internal_test.go @@ -53,6 +53,12 @@ var alphanumericMetas = map[string]string{ "style": IssueNameStyleAlphanumeric, } +// these values should match the Repo const above +var localMetas = map[string]string{ + "user": "gogits", + "repo": "gogs", +} + func TestRender_IssueIndexPattern(t *testing.T) { // numeric: render inputs without valid mentions test := func(s string) { @@ -91,7 +97,7 @@ func TestRender_IssueIndexPattern2(t *testing.T) { links[i] = numericIssueLink(util.URLJoin(setting.AppSubURL, "issues"), index) } expectedNil := fmt.Sprintf(expectedFmt, links...) - testRenderIssueIndexPattern(t, s, expectedNil, nil) + testRenderIssueIndexPattern(t, s, expectedNil, &postProcessCtx{metas: localMetas}) for i, index := range indices { links[i] = numericIssueLink("https://someurl.com/someUser/someRepo/", index) @@ -171,6 +177,7 @@ func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *post if ctx.urlPrefix == "" { ctx.urlPrefix = AppSubURL } + res, err := ctx.postProcess([]byte(input)) assert.NoError(t, err) assert.Equal(t, expected, string(res)) @@ -181,10 +188,10 @@ func TestRender_AutoLink(t *testing.T) { setting.AppSubURL = AppSubURL test := func(input, expected string) { - buffer, err := PostProcess([]byte(input), setting.AppSubURL, nil, false) + buffer, err := PostProcess([]byte(input), setting.AppSubURL, localMetas, false) assert.Equal(t, err, nil) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer))) - buffer, err = PostProcess([]byte(input), setting.AppSubURL, nil, true) + buffer, err = PostProcess([]byte(input), setting.AppSubURL, localMetas, true) assert.Equal(t, err, nil) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer))) } @@ -214,6 +221,7 @@ func TestRender_FullIssueURLs(t *testing.T) { if ctx.urlPrefix == "" { ctx.urlPrefix = AppSubURL } + ctx.metas = localMetas result, err := ctx.postProcess([]byte(input)) assert.NoError(t, err) assert.Equal(t, expected, string(result)) @@ -221,9 +229,11 @@ func TestRender_FullIssueURLs(t *testing.T) { test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6", "Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6") test("Look here http://localhost:3000/person/repo/issues/4", - `Look here <a href="http://localhost:3000/person/repo/issues/4">#4</a>`) + `Look here <a href="http://localhost:3000/person/repo/issues/4">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">#4</a>`) + `<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234">person/repo#4</a>`) + test("http://localhost:3000/gogits/gogs/issues/4", + `<a href="http://localhost:3000/gogits/gogs/issues/4">#4</a>`) } func TestRegExp_issueNumericPattern(t *testing.T) { diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 5aa9c3d7d2..8ba51e6a1b 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -19,6 +19,12 @@ const AppURL = "http://localhost:3000/" const Repo = "gogits/gogs" const AppSubURL = AppURL + Repo + "/" +// these values should match the Repo const above +var localMetas = map[string]string{ + "user": "gogits", + "repo": "gogs", +} + func TestRender_StandardLinks(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL @@ -100,7 +106,8 @@ func testAnswers(baseURLContent, baseURLImages string) []string { <p>Ideas and codes</p> <ul> -<li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" rel="nofollow">#786</a></li> +<li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" rel="nofollow">ocornut/imgui#786</a></li> +<li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/gogits/gogs/issues/786" rel="nofollow">#786</a></li> <li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li> <li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li> <li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li> @@ -188,6 +195,7 @@ var sameCases = []string{ Ideas and codes - Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786 +- Bezier widget (by @r-lyeh) ` + AppURL + `gogits/gogs/issues/786 - Node graph editors https://github.com/ocornut/imgui/issues/306 - [[Memory Editor|memory_editor_example]] - [[Plot var helper|plot_var_example]]`, @@ -243,7 +251,7 @@ func TestTotal_RenderWiki(t *testing.T) { answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/")) for i := 0; i < len(sameCases); i++ { - line := RenderWiki([]byte(sameCases[i]), AppSubURL, nil) + line := RenderWiki([]byte(sameCases[i]), AppSubURL, localMetas) assert.Equal(t, answers[i], line) } @@ -270,7 +278,7 @@ func TestTotal_RenderString(t *testing.T) { answers := testAnswers(util.URLJoin(AppSubURL, "src", "master/"), util.URLJoin(AppSubURL, "raw", "master/")) for i := 0; i < len(sameCases); i++ { - line := RenderString(sameCases[i], util.URLJoin(AppSubURL, "src", "master/"), nil) + line := RenderString(sameCases[i], util.URLJoin(AppSubURL, "src", "master/"), localMetas) assert.Equal(t, answers[i], line) } |