Refactor render system (#32492)
There were too many patches to the Render system, it's really difficult to make further improvements. This PR clears the legacy problems and fix TODOs. 1. Rename `RenderContext.Type` to `RenderContext.MarkupType` to clarify its usage. 2. Use `ContentMode` to replace `meta["mode"]` and `IsWiki`, to clarify the rendering behaviors. 3. Use "wiki" mode instead of "mode=gfm + wiki=true" 4. Merge `renderByType` and `renderByFile` 5. Add more comments ---- The problem of "mode=document": in many cases it is not set, so many non-comment places use comment's hard line break incorrectly
This commit is contained in:
@ -16,6 +16,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/markup/markdown"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/svg"
|
||||
"code.gitea.io/gitea/modules/test"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -74,7 +75,7 @@ func TestRender_StandardLinks(t *testing.T) {
|
||||
Links: markup.Links{
|
||||
Base: FullURL,
|
||||
},
|
||||
IsWiki: true,
|
||||
ContentMode: markup.RenderContentAsWiki,
|
||||
}, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
|
||||
@ -296,23 +297,22 @@ This PR has been generated by [Renovate Bot](https://github.com/renovatebot/reno
|
||||
}
|
||||
|
||||
func TestTotal_RenderWiki(t *testing.T) {
|
||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.ForceHardLineBreak, true)()
|
||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
|
||||
setting.AppURL = AppURL
|
||||
|
||||
answers := testAnswers(util.URLJoin(FullURL, "wiki"), util.URLJoin(FullURL, "wiki", "raw"))
|
||||
|
||||
for i := 0; i < len(sameCases); i++ {
|
||||
line, err := markdown.RenderString(&markup.RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
Links: markup.Links{
|
||||
Base: FullURL,
|
||||
},
|
||||
Repo: newMockRepo(testRepoOwnerName, testRepoName),
|
||||
Metas: localMetas,
|
||||
IsWiki: true,
|
||||
Repo: newMockRepo(testRepoOwnerName, testRepoName),
|
||||
Metas: localMetas,
|
||||
ContentMode: markup.RenderContentAsWiki,
|
||||
}, sameCases[i])
|
||||
assert.NoError(t, err)
|
||||
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
|
||||
assert.Equal(t, answers[i], actual)
|
||||
assert.Equal(t, answers[i], string(line))
|
||||
}
|
||||
|
||||
testCases := []string{
|
||||
@ -334,19 +334,18 @@ func TestTotal_RenderWiki(t *testing.T) {
|
||||
Links: markup.Links{
|
||||
Base: FullURL,
|
||||
},
|
||||
IsWiki: true,
|
||||
ContentMode: markup.RenderContentAsWiki,
|
||||
}, testCases[i])
|
||||
assert.NoError(t, err)
|
||||
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
|
||||
assert.EqualValues(t, testCases[i+1], actual)
|
||||
assert.EqualValues(t, testCases[i+1], string(line))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTotal_RenderString(t *testing.T) {
|
||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.ForceHardLineBreak, true)()
|
||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
|
||||
setting.AppURL = AppURL
|
||||
|
||||
answers := testAnswers(util.URLJoin(FullURL, "src", "master"), util.URLJoin(FullURL, "media", "master"))
|
||||
|
||||
for i := 0; i < len(sameCases); i++ {
|
||||
line, err := markdown.RenderString(&markup.RenderContext{
|
||||
Ctx: git.DefaultContext,
|
||||
@ -358,8 +357,7 @@ func TestTotal_RenderString(t *testing.T) {
|
||||
Metas: localMetas,
|
||||
}, sameCases[i])
|
||||
assert.NoError(t, err)
|
||||
actual := strings.ReplaceAll(string(line), ` data-markdown-generated-content=""`, "")
|
||||
assert.Equal(t, answers[i], actual)
|
||||
assert.Equal(t, answers[i], string(line))
|
||||
}
|
||||
|
||||
testCases := []string{}
|
||||
@ -428,6 +426,7 @@ func TestRenderSiblingImages_Issue12925(t *testing.T) {
|
||||
expected := `<p><a href="/image1" target="_blank" rel="nofollow noopener"><img src="/image1" alt="image1"></a><br>
|
||||
<a href="/image2" target="_blank" rel="nofollow noopener"><img src="/image2" alt="image2"></a></p>
|
||||
`
|
||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.ForceHardLineBreak, true)()
|
||||
res, err := markdown.RenderRawString(&markup.RenderContext{Ctx: git.DefaultContext}, testcase)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, res)
|
||||
@ -996,11 +995,16 @@ space</p>
|
||||
},
|
||||
}
|
||||
|
||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.ForceHardLineBreak, true)()
|
||||
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)()
|
||||
for i, c := range cases {
|
||||
result, err := markdown.RenderString(&markup.RenderContext{Ctx: context.Background(), Links: c.Links, IsWiki: c.IsWiki}, input)
|
||||
result, err := markdown.RenderString(&markup.RenderContext{
|
||||
Ctx: context.Background(),
|
||||
Links: c.Links,
|
||||
ContentMode: util.Iif(c.IsWiki, markup.RenderContentAsWiki, markup.RenderContentAsDefault),
|
||||
}, input)
|
||||
assert.NoError(t, err, "Unexpected error in testcase: %v", i)
|
||||
actual := strings.ReplaceAll(string(result), ` data-markdown-generated-content=""`, "")
|
||||
assert.Equal(t, c.Expected, actual, "Unexpected result in testcase %v", i)
|
||||
assert.Equal(t, c.Expected, string(result), "Unexpected result in testcase %v", i)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user