[BUG] Fix relative links on orgmode

- For regular non-image nonvideo links, they should be made relative,
this was done against `r.Ctx.Links.Base`, but since 637451a45e, that
should instead be done by `SrcLink()` if there's branch information set
in the context, because branch and treepath information are no longer
set in `r.Ctx.Links.Base`.
- This is consistent with how #2166 _fixed_ relative links.
- Media is not affected, `TestRender_Media` test doesn't fail.
- Adds unit tests.
- Ref https://codeberg.org/Codeberg/Community/issues/1485
This commit is contained in:
Gusted 2024-02-18 21:03:34 +01:00
parent 5240e27266
commit a2442793d2
No known key found for this signature in database
GPG key ID: FD821B732837125F
2 changed files with 55 additions and 1 deletions

View file

@ -147,11 +147,21 @@ func (r *Writer) resolveLink(node org.Node) string {
}
if len(link) > 0 && !markup.IsLinkStr(link) &&
link[0] != '#' && !strings.HasPrefix(link, mailto) {
base := r.Ctx.Links.Base
var base string
if r.Ctx.IsWiki {
base = r.Ctx.Links.WikiLink()
} else if r.Ctx.Links.HasBranchInfo() {
base = r.Ctx.Links.SrcLink()
} else {
base = r.Ctx.Links.Base
}
switch l.Kind() {
case "image", "video":
base = r.Ctx.Links.ResolveMediaLink(r.Ctx.IsWiki)
}
link = util.URLJoin(base, link)
}
return link

View file

@ -36,6 +36,10 @@ func TestRender_StandardLinks(t *testing.T) {
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
// No BranchPath or TreePath set.
test("[[file:comfy][comfy]]",
`<p><a href="http://localhost:3000/gogits/gogs/comfy">comfy</a></p>`)
test("[[https://google.com/]]",
`<p><a href="https://google.com/">https://google.com/</a></p>`)
@ -44,6 +48,46 @@ func TestRender_StandardLinks(t *testing.T) {
`<p><a href="`+lnk+`">WikiPage</a></p>`)
}
func TestRender_BaseLinks(t *testing.T) {
setting.AppURL = AppURL
setting.AppSubURL = AppSubURL
testBranch := func(input, expected string) {
buffer, err := RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
Links: markup.Links{
Base: setting.AppSubURL,
BranchPath: "branch/main",
},
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
testBranchTree := func(input, expected string) {
buffer, err := RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
Links: markup.Links{
Base: setting.AppSubURL,
BranchPath: "branch/main",
TreePath: "deep/nested/folder",
},
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
testBranch("[[file:comfy][comfy]]",
`<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/comfy">comfy</a></p>`)
testBranchTree("[[file:comfy][comfy]]",
`<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/deep/nested/folder/comfy">comfy</a></p>`)
testBranch("[[file:./src][./src/]]",
`<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/src">./src/</a></p>`)
testBranchTree("[[file:./src][./src/]]",
`<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/deep/nested/folder/src">./src/</a></p>`)
}
func TestRender_Media(t *testing.T) {
setting.AppURL = AppURL
setting.AppSubURL = AppSubURL