Skip to content

Commit c4fee63

Browse files
ankitrgadiyaGiteaBot
authored andcommitted
fix: rendering internal file links in org (go-gitea#29669)
The internal links to other files in the repository were not rendering with the Src Prefix (/src/branch-name/file-path). This commit fixes that by using the `SrcLink` as base if available. Resolves go-gitea#29668
1 parent e8b6d28 commit c4fee63

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

modules/markup/orgmode/orgmode.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,18 @@ func (r *Writer) resolveLink(kind, link string) string {
143143
// so we need to try to guess the link kind again here
144144
kind = org.RegularLink{URL: link}.Kind()
145145
}
146+
146147
base := r.Ctx.Links.Base
148+
if r.Ctx.IsWiki {
149+
base = r.Ctx.Links.WikiLink()
150+
} else if r.Ctx.Links.HasBranchInfo() {
151+
base = r.Ctx.Links.SrcLink()
152+
}
153+
147154
if kind == "image" || kind == "video" {
148155
base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki)
149156
}
157+
150158
link = util.URLJoin(base, link)
151159
}
152160
return link

modules/markup/orgmode/orgmode_test.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,50 @@ const AppURL = "http://localhost:3000/"
1919
func TestRender_StandardLinks(t *testing.T) {
2020
setting.AppURL = AppURL
2121

22-
test := func(input, expected string) {
22+
test := func(input, expected string, isWiki bool) {
2323
buffer, err := RenderString(&markup.RenderContext{
2424
Ctx: git.DefaultContext,
2525
Links: markup.Links{
2626
Base: "/relative-path",
2727
BranchPath: "branch/main",
2828
},
29+
IsWiki: isWiki,
2930
}, input)
3031
assert.NoError(t, err)
3132
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
3233
}
3334

3435
test("[[https://google.com/]]",
35-
`<p><a href="https://google.com/">https://google.com/</a></p>`)
36+
`<p><a href="https://google.com/">https://google.com/</a></p>`, false)
3637
test("[[WikiPage][The WikiPage Desc]]",
37-
`<p><a href="/relative-path/WikiPage">The WikiPage Desc</a></p>`)
38+
`<p><a href="/relative-path/wiki/WikiPage">The WikiPage Desc</a></p>`, true)
3839
test("[[ImageLink.svg][The Image Desc]]",
39-
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`)
40+
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`, false)
41+
}
42+
43+
func TestRender_InternalLinks(t *testing.T) {
44+
setting.AppURL = AppURL
45+
46+
test := func(input, expected string) {
47+
buffer, err := RenderString(&markup.RenderContext{
48+
Ctx: git.DefaultContext,
49+
Links: markup.Links{
50+
Base: "/relative-path",
51+
BranchPath: "branch/main",
52+
},
53+
}, input)
54+
assert.NoError(t, err)
55+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
56+
}
57+
58+
test("[[file:test.org][Test]]",
59+
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
60+
test("[[./test.org][Test]]",
61+
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
62+
test("[[test.org][Test]]",
63+
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
64+
test("[[path/to/test.org][Test]]",
65+
`<p><a href="/relative-path/src/branch/main/path/to/test.org">Test</a></p>`)
4066
}
4167

4268
func TestRender_Media(t *testing.T) {

0 commit comments

Comments
 (0)