mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Refactor markup render system (#32612)
This PR removes (almost) all path tricks, and introduces "renderhelper" package. Now we can clearly see the rendering behaviors for comment/file/wiki, more details are in "renderhelper" tests. Fix #31411 , fix #18592, fix #25632 and maybe more problems. (ps: fix #32608 by the way)
This commit is contained in:
@@ -13,7 +13,6 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/markup"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/alecthomas/chroma/v2"
|
||||
"github.com/alecthomas/chroma/v2/lexers"
|
||||
@@ -142,19 +141,11 @@ func (r *Writer) resolveLink(kind, link string) string {
|
||||
// so we need to try to guess the link kind again here
|
||||
kind = org.RegularLink{URL: link}.Kind()
|
||||
}
|
||||
|
||||
base := r.Ctx.RenderOptions.Links.Base
|
||||
if r.Ctx.IsMarkupContentWiki() {
|
||||
base = r.Ctx.RenderOptions.Links.WikiLink()
|
||||
} else if r.Ctx.RenderOptions.Links.HasBranchInfo() {
|
||||
base = r.Ctx.RenderOptions.Links.SrcLink()
|
||||
}
|
||||
|
||||
if kind == "image" || kind == "video" {
|
||||
base = r.Ctx.RenderOptions.Links.ResolveMediaLink(r.Ctx.IsMarkupContentWiki())
|
||||
link = r.Ctx.RenderHelper.ResolveLink(link, markup.LinkTypeMedia)
|
||||
} else {
|
||||
link = r.Ctx.RenderHelper.ResolveLink(link, markup.LinkTypeDefault)
|
||||
}
|
||||
|
||||
link = util.URLJoin(base, link)
|
||||
}
|
||||
return link
|
||||
}
|
||||
|
@@ -10,7 +10,6 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/modules/markup"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -22,34 +21,21 @@ func TestMain(m *testing.M) {
|
||||
}
|
||||
|
||||
func TestRender_StandardLinks(t *testing.T) {
|
||||
test := func(input, expected string, isWiki bool) {
|
||||
buffer, err := RenderString(markup.NewTestRenderContext(
|
||||
markup.Links{
|
||||
Base: "/relative-path",
|
||||
BranchPath: "branch/main",
|
||||
},
|
||||
map[string]string{"markupContentMode": util.Iif(isWiki, "wiki", "")},
|
||||
), input)
|
||||
test := func(input, expected string) {
|
||||
buffer, err := RenderString(markup.NewTestRenderContext("/relative-path/media/branch/main/"), 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)
|
||||
`<p><a href="https://google.com/">https://google.com/</a></p>`)
|
||||
test("[[ImageLink.svg][The Image Desc]]",
|
||||
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`, false)
|
||||
`<p><a href="/relative-path/media/branch/main/ImageLink.svg">The Image Desc</a></p>`)
|
||||
}
|
||||
|
||||
func TestRender_InternalLinks(t *testing.T) {
|
||||
test := func(input, expected string) {
|
||||
buffer, err := RenderString(markup.NewTestRenderContext(
|
||||
markup.Links{
|
||||
Base: "/relative-path",
|
||||
BranchPath: "branch/main",
|
||||
},
|
||||
), input)
|
||||
buffer, err := RenderString(markup.NewTestRenderContext("/relative-path/src/branch/main"), input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
||||
}
|
||||
@@ -66,7 +52,7 @@ func TestRender_InternalLinks(t *testing.T) {
|
||||
|
||||
func TestRender_Media(t *testing.T) {
|
||||
test := func(input, expected string) {
|
||||
buffer, err := RenderString(markup.NewTestRenderContext(markup.Links{Base: "./relative-path"}), input)
|
||||
buffer, err := RenderString(markup.NewTestRenderContext("./relative-path"), input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
||||
}
|
||||
|
Reference in New Issue
Block a user