Skip to content

Commit 0c9e0a4

Browse files
committed
fix: rendering internal file links in org
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 930bae2 commit 0c9e0a4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

modules/markup/orgmode/orgmode.go

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

modules/markup/orgmode/orgmode_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@ func TestRender_StandardLinks(t *testing.T) {
3939
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`)
4040
}
4141

42+
func TestRender_InternalLinks(t *testing.T) {
43+
setting.AppURL = AppURL
44+
45+
test := func(input, expected string) {
46+
buffer, err := RenderString(&markup.RenderContext{
47+
Ctx: git.DefaultContext,
48+
Links: markup.Links{
49+
Base: "/relative-path",
50+
BranchPath: "branch/main",
51+
},
52+
}, input)
53+
assert.NoError(t, err)
54+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
55+
}
56+
57+
test("[[file:test.org][Test]]",
58+
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
59+
test("[[./test.org][Test]]",
60+
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
61+
test("[[test.org][Test]]",
62+
`<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
63+
test("[[path/to/test.org][Test]]",
64+
`<p><a href="/relative-path/src/branch/main/path/to/test.org">Test</a></p>`)
65+
}
66+
4267
func TestRender_Media(t *testing.T) {
4368
setting.AppURL = AppURL
4469

0 commit comments

Comments
 (0)