forked from forgejo/forgejo
Backport #26745 Fixes #26548 This PR refactors the rendering of markup links. The old code uses `strings.Replace` to change some urls while the new code uses more context to decide which link should be generated. The added tests should ensure the same output for the old and new behaviour (besides the bug). We may need to refactor the rendering a bit more to make it clear how the different helper methods render the input string. There are lots of options (resolve links / images / mentions / git hashes / emojis / ...) but you don't really know what helper uses which options. For example, we currently support images in the user description which should not be allowed I think: <details> <summary>Profile</summary> https://try.gitea.io/KN4CK3R  </details> (cherry picked from commit022552d5b6
)
This commit is contained in:
parent
5124be72aa
commit
875b398e5e
42 changed files with 966 additions and 388 deletions
|
@ -42,8 +42,10 @@ func TestRender_Commits(t *testing.T) {
|
|||
buffer, err := RenderString(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
RelativePath: ".md",
|
||||
URLPrefix: TestRepoURL,
|
||||
Metas: localMetas,
|
||||
Links: Links{
|
||||
Base: TestRepoURL,
|
||||
},
|
||||
Metas: localMetas,
|
||||
}, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
||||
|
@ -93,8 +95,10 @@ func TestRender_CrossReferences(t *testing.T) {
|
|||
buffer, err := RenderString(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
RelativePath: "a.md",
|
||||
URLPrefix: setting.AppSubURL,
|
||||
Metas: localMetas,
|
||||
Links: Links{
|
||||
Base: setting.AppSubURL,
|
||||
},
|
||||
Metas: localMetas,
|
||||
}, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
||||
|
@ -138,7 +142,9 @@ func TestRender_links(t *testing.T) {
|
|||
buffer, err := RenderString(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
RelativePath: "a.md",
|
||||
URLPrefix: TestRepoURL,
|
||||
Links: Links{
|
||||
Base: TestRepoURL,
|
||||
},
|
||||
}, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
||||
|
@ -238,7 +244,9 @@ func TestRender_email(t *testing.T) {
|
|||
res, err := RenderString(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
RelativePath: "a.md",
|
||||
URLPrefix: TestRepoURL,
|
||||
Links: Links{
|
||||
Base: TestRepoURL,
|
||||
},
|
||||
}, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res))
|
||||
|
@ -309,7 +317,9 @@ func TestRender_emoji(t *testing.T) {
|
|||
buffer, err := RenderString(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
RelativePath: "a.md",
|
||||
URLPrefix: TestRepoURL,
|
||||
Links: Links{
|
||||
Base: TestRepoURL,
|
||||
},
|
||||
}, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
||||
|
@ -371,29 +381,34 @@ func TestRender_ShortLinks(t *testing.T) {
|
|||
|
||||
test := func(input, expected, expectedWiki string) {
|
||||
buffer, err := markdown.RenderString(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: tree,
|
||||
Ctx: git.DefaultContext,
|
||||
Links: Links{
|
||||
Base: TestRepoURL,
|
||||
BranchPath: "master",
|
||||
},
|
||||
}, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
||||
buffer, err = markdown.RenderString(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: TestRepoURL,
|
||||
Metas: localMetas,
|
||||
IsWiki: true,
|
||||
Ctx: git.DefaultContext,
|
||||
Links: Links{
|
||||
Base: TestRepoURL,
|
||||
},
|
||||
Metas: localMetas,
|
||||
IsWiki: true,
|
||||
}, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer))
|
||||
}
|
||||
|
||||
rawtree := util.URLJoin(TestRepoURL, "raw", "master")
|
||||
mediatree := util.URLJoin(TestRepoURL, "media", "master")
|
||||
url := util.URLJoin(tree, "Link")
|
||||
otherURL := util.URLJoin(tree, "Other-Link")
|
||||
encodedURL := util.URLJoin(tree, "Link%3F")
|
||||
imgurl := util.URLJoin(rawtree, "Link.jpg")
|
||||
otherImgurl := util.URLJoin(rawtree, "Link+Other.jpg")
|
||||
encodedImgurl := util.URLJoin(rawtree, "Link+%23.jpg")
|
||||
notencodedImgurl := util.URLJoin(rawtree, "some", "path", "Link+#.jpg")
|
||||
imgurl := util.URLJoin(mediatree, "Link.jpg")
|
||||
otherImgurl := util.URLJoin(mediatree, "Link+Other.jpg")
|
||||
encodedImgurl := util.URLJoin(mediatree, "Link+%23.jpg")
|
||||
notencodedImgurl := util.URLJoin(mediatree, "some", "path", "Link+#.jpg")
|
||||
urlWiki := util.URLJoin(TestRepoURL, "wiki", "Link")
|
||||
otherURLWiki := util.URLJoin(TestRepoURL, "wiki", "Other-Link")
|
||||
encodedURLWiki := util.URLJoin(TestRepoURL, "wiki", "Link%3F")
|
||||
|
@ -475,21 +490,25 @@ func TestRender_ShortLinks(t *testing.T) {
|
|||
|
||||
func TestRender_RelativeImages(t *testing.T) {
|
||||
setting.AppURL = TestAppURL
|
||||
tree := util.URLJoin(TestRepoURL, "src", "master")
|
||||
|
||||
test := func(input, expected, expectedWiki string) {
|
||||
buffer, err := markdown.RenderString(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: tree,
|
||||
Metas: localMetas,
|
||||
Ctx: git.DefaultContext,
|
||||
Links: Links{
|
||||
Base: TestRepoURL,
|
||||
BranchPath: "master",
|
||||
},
|
||||
Metas: localMetas,
|
||||
}, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
|
||||
buffer, err = markdown.RenderString(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: TestRepoURL,
|
||||
Metas: localMetas,
|
||||
IsWiki: true,
|
||||
Ctx: git.DefaultContext,
|
||||
Links: Links{
|
||||
Base: TestRepoURL,
|
||||
},
|
||||
Metas: localMetas,
|
||||
IsWiki: true,
|
||||
}, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer))
|
||||
|
@ -521,9 +540,11 @@ func Test_ParseClusterFuzz(t *testing.T) {
|
|||
|
||||
var res strings.Builder
|
||||
err := PostProcess(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: "https://example.com",
|
||||
Metas: localMetas,
|
||||
Ctx: git.DefaultContext,
|
||||
Links: Links{
|
||||
Base: "https://example.com",
|
||||
},
|
||||
Metas: localMetas,
|
||||
}, strings.NewReader(data), &res)
|
||||
assert.NoError(t, err)
|
||||
assert.NotContains(t, res.String(), "<html")
|
||||
|
@ -532,9 +553,11 @@ func Test_ParseClusterFuzz(t *testing.T) {
|
|||
|
||||
res.Reset()
|
||||
err = PostProcess(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: "https://example.com",
|
||||
Metas: localMetas,
|
||||
Ctx: git.DefaultContext,
|
||||
Links: Links{
|
||||
Base: "https://example.com",
|
||||
},
|
||||
Metas: localMetas,
|
||||
}, strings.NewReader(data), &res)
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
@ -543,6 +566,7 @@ func Test_ParseClusterFuzz(t *testing.T) {
|
|||
|
||||
func TestPostProcess_RenderDocument(t *testing.T) {
|
||||
setting.AppURL = TestAppURL
|
||||
setting.StaticURLPrefix = TestAppURL // can't run standalone
|
||||
|
||||
localMetas := map[string]string{
|
||||
"user": "go-gitea",
|
||||
|
@ -553,9 +577,11 @@ func TestPostProcess_RenderDocument(t *testing.T) {
|
|||
test := func(input, expected string) {
|
||||
var res strings.Builder
|
||||
err := PostProcess(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: "https://example.com",
|
||||
Metas: localMetas,
|
||||
Ctx: git.DefaultContext,
|
||||
Links: Links{
|
||||
Base: "https://example.com",
|
||||
},
|
||||
Metas: localMetas,
|
||||
}, strings.NewReader(input), &res)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res.String()))
|
||||
|
@ -589,9 +615,8 @@ func TestIssue16020(t *testing.T) {
|
|||
|
||||
var res strings.Builder
|
||||
err := PostProcess(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: "https://example.com",
|
||||
Metas: localMetas,
|
||||
Ctx: git.DefaultContext,
|
||||
Metas: localMetas,
|
||||
}, strings.NewReader(data), &res)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, data, res.String())
|
||||
|
@ -606,9 +631,8 @@ func BenchmarkEmojiPostprocess(b *testing.B) {
|
|||
for i := 0; i < b.N; i++ {
|
||||
var res strings.Builder
|
||||
err := PostProcess(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: "https://example.com",
|
||||
Metas: localMetas,
|
||||
Ctx: git.DefaultContext,
|
||||
Metas: localMetas,
|
||||
}, strings.NewReader(data), &res)
|
||||
assert.NoError(b, err)
|
||||
}
|
||||
|
@ -617,8 +641,10 @@ func BenchmarkEmojiPostprocess(b *testing.B) {
|
|||
func TestFuzz(t *testing.T) {
|
||||
s := "t/l/issues/8#/../../a"
|
||||
renderContext := RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: "https://example.com/go-gitea/gitea",
|
||||
Ctx: git.DefaultContext,
|
||||
Links: Links{
|
||||
Base: "https://example.com/go-gitea/gitea",
|
||||
},
|
||||
Metas: map[string]string{
|
||||
"user": "go-gitea",
|
||||
"repo": "gitea",
|
||||
|
@ -635,9 +661,8 @@ func TestIssue18471(t *testing.T) {
|
|||
|
||||
var res strings.Builder
|
||||
err := PostProcess(&RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
URLPrefix: "https://example.com",
|
||||
Metas: localMetas,
|
||||
Ctx: git.DefaultContext,
|
||||
Metas: localMetas,
|
||||
}, strings.NewReader(data), &res)
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue