Skip to content

Commit 5651cc7

Browse files
authored
Merge pull request #2119 from ethantkoenig/fix/wiki
Fix wiki preview links
2 parents 30787e4 + e58237e commit 5651cc7

3 files changed

Lines changed: 52 additions & 45 deletions

File tree

modules/markdown/markdown.go

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ import (
1414
"regexp"
1515
"strings"
1616

17-
"github.com/Unknwon/com"
18-
"github.com/russross/blackfriday"
19-
"golang.org/x/net/html"
20-
2117
"code.gitea.io/gitea/modules/base"
18+
"code.gitea.io/gitea/modules/log"
2219
"code.gitea.io/gitea/modules/markup"
2320
"code.gitea.io/gitea/modules/setting"
21+
22+
"github.com/Unknwon/com"
23+
"github.com/russross/blackfriday"
24+
"golang.org/x/net/html"
2425
)
2526

2627
// Issue name styles
@@ -213,36 +214,17 @@ func cutoutVerbosePrefix(prefix string) string {
213214
}
214215

215216
// URLJoin joins url components, like path.Join, but preserving contents
216-
func URLJoin(elem ...string) string {
217-
res := ""
218-
last := len(elem) - 1
219-
for i, item := range elem {
220-
res += item
221-
if i != last && !strings.HasSuffix(res, "/") {
222-
res += "/"
223-
}
224-
}
225-
cwdIndex := strings.Index(res, "/./")
226-
for cwdIndex != -1 {
227-
res = strings.Replace(res, "/./", "/", 1)
228-
cwdIndex = strings.Index(res, "/./")
229-
}
230-
upIndex := strings.Index(res, "/..")
231-
for upIndex != -1 {
232-
res = strings.Replace(res, "/..", "", 1)
233-
prevStart := -1
234-
for i := upIndex - 1; i >= 0; i-- {
235-
if res[i] == '/' {
236-
prevStart = i
237-
break
238-
}
239-
}
240-
if prevStart != -1 {
241-
res = res[:prevStart] + res[upIndex:]
242-
}
243-
upIndex = strings.Index(res, "/..")
244-
}
245-
return res
217+
func URLJoin(base string, elems ...string) string {
218+
u, err := url.Parse(base)
219+
if err != nil {
220+
log.Error(4, "URLJoin: Invalid base URL %s", base)
221+
return ""
222+
}
223+
joinArgs := make([]string, 0, len(elems)+1)
224+
joinArgs = append(joinArgs, u.Path)
225+
joinArgs = append(joinArgs, elems...)
226+
u.Path = path.Join(joinArgs...)
227+
return u.String()
246228
}
247229

248230
// RenderIssueIndexPattern renders issue indexes to corresponding links.

modules/markdown/markdown_test.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ func testRenderIssueIndexPattern(t *testing.T, input, expected string, metas map
5959
string(RenderIssueIndexPattern([]byte(input), AppSubURL, metas)))
6060
}
6161

62+
func TestURLJoin(t *testing.T) {
63+
type test struct {
64+
Expected string
65+
Base string
66+
Elements []string
67+
}
68+
newTest := func(expected, base string, elements ...string) test {
69+
return test{Expected: expected, Base: base, Elements: elements}
70+
}
71+
for _, test := range []test{
72+
newTest("https://try.gitea.io/a/b/c",
73+
"https://try.gitea.io", "a/b", "c"),
74+
newTest("https://try.gitea.io/a/b/c",
75+
"https://try.gitea.io/", "/a/b/", "/c/"),
76+
newTest("https://try.gitea.io/a/c",
77+
"https://try.gitea.io/", "/a/./b/", "../c/"),
78+
newTest("a/b/c",
79+
"a", "b/c/"),
80+
newTest("a/b/d",
81+
"a/", "b/c/", "/../d/"),
82+
} {
83+
assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
84+
}
85+
}
86+
6287
func TestRender_IssueIndexPattern(t *testing.T) {
6388
// numeric: render inputs without valid mentions
6489
test := func(s string) {
@@ -641,17 +666,17 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
641666
`<p>Wiki! Enjoy :)</p>
642667
643668
<ul>
644-
<li><a href="` + baseURLContent + `Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
645-
<li><a href="` + baseURLContent + `Tips" rel="nofollow">Tips</a></li>
669+
<li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
670+
<li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
646671
</ul>
647672
648673
<p>Ideas and codes</p>
649674
650675
<ul>
651676
<li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>)<a href="https://github.com/ocornut/imgui/issues/786" rel="nofollow">#786</a></li>
652677
<li>Node graph editors<a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">#306</a></li>
653-
<li><a href="` + baseURLContent + `memory_editor_example" rel="nofollow">Memory Editor</a></li>
654-
<li><a href="` + baseURLContent + `plot_var_example" rel="nofollow">Plot var helper</a></li>
678+
<li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
679+
<li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
655680
</ul>
656681
`,
657682
`<h2>What is Wine Staging?</h2>
@@ -665,15 +690,15 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
665690
<table>
666691
<thead>
667692
<tr>
668-
<th><a href="` + baseURLImages + `images/icon-install.png" rel="nofollow"><img src="` + baseURLImages + `images/icon-install.png" alt="images/icon-install.png" title="icon-install.png"/></a></th>
669-
<th><a href="` + baseURLContent + `Installation" rel="nofollow">Installation</a></th>
693+
<th><a href="` + baseURLImages + `/images/icon-install.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-install.png" alt="images/icon-install.png" title="icon-install.png"/></a></th>
694+
<th><a href="` + baseURLContent + `/Installation" rel="nofollow">Installation</a></th>
670695
</tr>
671696
</thead>
672697
673698
<tbody>
674699
<tr>
675-
<td><a href="` + baseURLImages + `images/icon-usage.png" rel="nofollow"><img src="` + baseURLImages + `images/icon-usage.png" alt="images/icon-usage.png" title="icon-usage.png"/></a></td>
676-
<td><a href="` + baseURLContent + `Usage" rel="nofollow">Usage</a></td>
700+
<td><a href="` + baseURLImages + `/images/icon-usage.png" rel="nofollow"><img src="` + baseURLImages + `/images/icon-usage.png" alt="images/icon-usage.png" title="icon-usage.png"/></a></td>
701+
<td><a href="` + baseURLContent + `/Usage" rel="nofollow">Usage</a></td>
677702
</tr>
678703
</tbody>
679704
</table>
@@ -682,9 +707,9 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
682707
683708
<ol>
684709
<li><a href="https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#packaging-for-the-desktop" rel="nofollow">Package your libGDX application</a>
685-
<a href="` + baseURLImages + `images/1.png" rel="nofollow"><img src="` + baseURLImages + `images/1.png" alt="images/1.png" title="1.png"/></a></li>
710+
<a href="` + baseURLImages + `/images/1.png" rel="nofollow"><img src="` + baseURLImages + `/images/1.png" alt="images/1.png" title="1.png"/></a></li>
686711
<li>Perform a test run by hitting the Run! button.
687-
<a href="` + baseURLImages + `images/2.png" rel="nofollow"><img src="` + baseURLImages + `images/2.png" alt="images/2.png" title="2.png"/></a></li>
712+
<a href="` + baseURLImages + `/images/2.png" rel="nofollow"><img src="` + baseURLImages + `/images/2.png" alt="images/2.png" title="2.png"/></a></li>
688713
</ol>
689714
`,
690715
}

templates/repo/wiki/new.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<input name="title" value="{{.title}}" autofocus required>
1919
</div>
2020
<div class="field">
21-
<textarea id="edit_area" name="content" data-id="wiki-{{.old_title}}" data-url="{{AppSubUrl}}/api/v1/markdown" data-context="{{.RepoLink}}">{{if .PageIsWikiEdit}}{{.content}}{{else}}{{.i18n.Tr "repo.wiki.welcome"}}{{end}}</textarea required>
21+
<textarea id="edit_area" name="content" data-id="wiki-{{.old_title}}" data-url="{{AppSubUrl}}/api/v1/markdown" data-context="{{.RepoLink}}/wiki">{{if .PageIsWikiEdit}}{{.content}}{{else}}{{.i18n.Tr "repo.wiki.welcome"}}{{end}}</textarea required>
2222
</div>
2323
<div class="field">
2424
<input name="message" placeholder="{{.i18n.Tr "repo.wiki.default_commit_message"}}">

0 commit comments

Comments
 (0)