fix: rendering internal file links in org (#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 #29668
This commit is contained in:
Ankit R Gadiya 2024-03-10 22:00:14 +05:30 committed by GitHub
parent 6e8762f962
commit 5665a0212b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 6 deletions

View File

@ -142,10 +142,18 @@ func (r *Writer) resolveLink(kind, link string) string {
// so we need to try to guess the link kind again here // so we need to try to guess the link kind again here
kind = org.RegularLink{URL: link}.Kind() kind = org.RegularLink{URL: link}.Kind()
} }
base := r.Ctx.Links.Base base := r.Ctx.Links.Base
if r.Ctx.IsWiki {
base = r.Ctx.Links.WikiLink()
} else if r.Ctx.Links.HasBranchInfo() {
base = r.Ctx.Links.SrcLink()
}
if kind == "image" || kind == "video" { if kind == "image" || kind == "video" {
base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki) base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki)
} }
link = util.URLJoin(base, link) link = util.URLJoin(base, link)
} }
return link return link

View File

@ -19,6 +19,30 @@ const AppURL = "http://localhost:3000/"
func TestRender_StandardLinks(t *testing.T) { func TestRender_StandardLinks(t *testing.T) {
setting.AppURL = AppURL setting.AppURL = AppURL
test := func(input, expected string, isWiki bool) {
buffer, err := RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
Links: markup.Links{
Base: "/relative-path",
BranchPath: "branch/main",
},
IsWiki: isWiki,
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
test("[[https://google.com/]]",
`<p><a href="https://google.com/">https://google.com/</a></p>`, false)
test("[[WikiPage][The WikiPage Desc]]",
`<p><a href="/relative-path/wiki/WikiPage">The WikiPage Desc</a></p>`, true)
test("[[ImageLink.svg][The Image Desc]]",
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`, false)
}
func TestRender_InternalLinks(t *testing.T) {
setting.AppURL = AppURL
test := func(input, expected string) { test := func(input, expected string) {
buffer, err := RenderString(&markup.RenderContext{ buffer, err := RenderString(&markup.RenderContext{
Ctx: git.DefaultContext, Ctx: git.DefaultContext,
@ -31,12 +55,14 @@ func TestRender_StandardLinks(t *testing.T) {
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
} }
test("[[https://google.com/]]", test("[[file:test.org][Test]]",
`<p><a href="https://google.com/">https://google.com/</a></p>`) `<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
test("[[WikiPage][The WikiPage Desc]]", test("[[./test.org][Test]]",
`<p><a href="/relative-path/WikiPage">The WikiPage Desc</a></p>`) `<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
test("[[ImageLink.svg][The Image Desc]]", test("[[test.org][Test]]",
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`) `<p><a href="/relative-path/src/branch/main/test.org">Test</a></p>`)
test("[[path/to/test.org][Test]]",
`<p><a href="/relative-path/src/branch/main/path/to/test.org">Test</a></p>`)
} }
func TestRender_Media(t *testing.T) { func TestRender_Media(t *testing.T) {