From b1d7348a20d7d3927d526b15c1847db6dd7767fa Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Thu, 6 Jul 2017 12:05:35 -0400 Subject: [PATCH 1/3] Fix wiki preview links --- modules/markdown/markdown.go | 40 +++++++++--------------------- modules/markdown/markdown_test.go | 41 +++++++++++++++++++++++-------- templates/repo/wiki/new.tmpl | 2 +- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index 1f890cc82..d360dfb84 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -19,6 +19,7 @@ import ( "golang.org/x/net/html" "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" ) @@ -213,36 +214,17 @@ func cutoutVerbosePrefix(prefix string) string { } // URLJoin joins url components, like path.Join, but preserving contents -func URLJoin(elem ...string) string { - res := "" - last := len(elem) - 1 - for i, item := range elem { - res += item - if i != last && !strings.HasSuffix(res, "/") { - res += "/" - } +func URLJoin(base string, elems ...string) string { + u, err := url.Parse(base) + if err != nil { + log.Error(4, "URLJoin: Invalid base URL %s", base) + return "" } - cwdIndex := strings.Index(res, "/./") - for cwdIndex != -1 { - res = strings.Replace(res, "/./", "/", 1) - cwdIndex = strings.Index(res, "/./") - } - upIndex := strings.Index(res, "/..") - for upIndex != -1 { - res = strings.Replace(res, "/..", "", 1) - prevStart := -1 - for i := upIndex - 1; i >= 0; i-- { - if res[i] == '/' { - prevStart = i - break - } - } - if prevStart != -1 { - res = res[:prevStart] + res[upIndex:] - } - upIndex = strings.Index(res, "/..") - } - return res + joinArgs := make([]string, 0, len(elems)+1) + joinArgs = append(joinArgs, u.Path) + joinArgs = append(joinArgs, elems...) + u.Path = path.Join(joinArgs...) + return u.String() } // RenderIssueIndexPattern renders issue indexes to corresponding links. diff --git a/modules/markdown/markdown_test.go b/modules/markdown/markdown_test.go index e6bc3683c..9a1dafa71 100644 --- a/modules/markdown/markdown_test.go +++ b/modules/markdown/markdown_test.go @@ -59,6 +59,27 @@ func testRenderIssueIndexPattern(t *testing.T, input, expected string, metas map string(RenderIssueIndexPattern([]byte(input), AppSubURL, metas))) } +func TestURLJoin(t *testing.T) { + type test struct { + Expected string + Base string + Elements []string + } + newTest := func(expected, base string, elements ...string) test { + return test{Expected: expected, Base: base, Elements: elements} + } + for _, test := range []test{ + newTest("https://try.gitea.io/a/b/c", + "https://try.gitea.io", "a/b", "c"), + newTest("https://try.gitea.io/a/b/c", + "https://try.gitea.io/", "/a/b/", "/c/"), + newTest("https://try.gitea.io/a/c", + "https://try.gitea.io/", "/a/./b/", "../c/"), + } { + assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...)) + } +} + func TestRender_IssueIndexPattern(t *testing.T) { // numeric: render inputs without valid mentions test := func(s string) { @@ -641,8 +662,8 @@ func testAnswers(baseURLContent, baseURLImages string) []string { `

Wiki! Enjoy :)

Ideas and codes

@@ -650,8 +671,8 @@ func testAnswers(baseURLContent, baseURLImages string) []string { `, `

What is Wine Staging?

@@ -665,15 +686,15 @@ func testAnswers(baseURLContent, baseURLImages string) []string { - - + + - - + +
images/icon-install.pngInstallationimages/icon-install.pngInstallation
images/icon-usage.pngUsageimages/icon-usage.pngUsage
@@ -682,9 +703,9 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
  1. Package your libGDX application -images/1.png
  2. +images/1.png
  3. Perform a test run by hitting the Run! button. -images/2.png
  4. +images/2.png
`, } diff --git a/templates/repo/wiki/new.tmpl b/templates/repo/wiki/new.tmpl index af87e9c43..cb928b283 100644 --- a/templates/repo/wiki/new.tmpl +++ b/templates/repo/wiki/new.tmpl @@ -18,7 +18,7 @@
- +
From e14ea9979bf0e9a5d4dbfbf3e933997de8ac2987 Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Thu, 6 Jul 2017 12:42:56 -0400 Subject: [PATCH 2/3] Relative URL tests --- modules/markdown/markdown_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/markdown/markdown_test.go b/modules/markdown/markdown_test.go index 9a1dafa71..c622009e8 100644 --- a/modules/markdown/markdown_test.go +++ b/modules/markdown/markdown_test.go @@ -75,6 +75,10 @@ func TestURLJoin(t *testing.T) { "https://try.gitea.io/", "/a/b/", "/c/"), newTest("https://try.gitea.io/a/c", "https://try.gitea.io/", "/a/./b/", "../c/"), + newTest("a/b/c", + "a", "b/c/"), + newTest("a/b/d", + "a/", "b/c/", "/../d/"), } { assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...)) } From e58237ee3f5be04e2ef2e155eca71077097d0c01 Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Thu, 6 Jul 2017 16:38:38 -0400 Subject: [PATCH 3/3] Fix import order --- modules/markdown/markdown.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index d360dfb84..9773e8c2f 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -14,14 +14,14 @@ import ( "regexp" "strings" - "github.com/Unknwon/com" - "github.com/russross/blackfriday" - "golang.org/x/net/html" - "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" + + "github.com/Unknwon/com" + "github.com/russross/blackfriday" + "golang.org/x/net/html" ) // Issue name styles