diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2024-11-14 13:02:11 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-14 05:02:11 +0000 |
commit | 3f9c3e7bc394c115ccc4818d6505f1f68de350d2 (patch) | |
tree | e07e4a3dc07ce80104f7949af5cb180b7fce449e /routers/api/v1/misc | |
parent | 985e2a8af3d6468bac3ab178148c38bdbd8414f5 (diff) | |
download | gitea-3f9c3e7bc394c115ccc4818d6505f1f68de350d2.tar.gz gitea-3f9c3e7bc394c115ccc4818d6505f1f68de350d2.zip |
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
Diffstat (limited to 'routers/api/v1/misc')
-rw-r--r-- | routers/api/v1/misc/markup.go | 12 | ||||
-rw-r--r-- | routers/api/v1/misc/markup_test.go | 13 |
2 files changed, 12 insertions, 13 deletions
diff --git a/routers/api/v1/misc/markup.go b/routers/api/v1/misc/markup.go index 9699c79368..868ed92519 100644 --- a/routers/api/v1/misc/markup.go +++ b/routers/api/v1/misc/markup.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/common" "code.gitea.io/gitea/services/context" @@ -41,7 +42,8 @@ func Markup(ctx *context.APIContext) { return } - common.RenderMarkup(ctx.Base, ctx.Repo, form.Mode, form.Text, form.Context, form.FilePath, form.Wiki) + mode := util.Iif(form.Wiki, "wiki", form.Mode) //nolint:staticcheck + common.RenderMarkup(ctx.Base, ctx.Repo, mode, form.Text, form.Context, form.FilePath) } // Markdown render markdown document to HTML @@ -71,12 +73,8 @@ func Markdown(ctx *context.APIContext) { return } - mode := "markdown" - if form.Mode == "comment" || form.Mode == "gfm" { - mode = form.Mode - } - - common.RenderMarkup(ctx.Base, ctx.Repo, mode, form.Text, form.Context, "", form.Wiki) + mode := util.Iif(form.Wiki, "wiki", form.Mode) //nolint:staticcheck + common.RenderMarkup(ctx.Base, ctx.Repo, mode, form.Text, form.Context, "") } // MarkdownRaw render raw markdown HTML diff --git a/routers/api/v1/misc/markup_test.go b/routers/api/v1/misc/markup_test.go index abffdf3516..6b8c09034a 100644 --- a/routers/api/v1/misc/markup_test.go +++ b/routers/api/v1/misc/markup_test.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/contexttest" @@ -24,6 +25,7 @@ const AppURL = "http://localhost:3000/" func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expectedBody string, expectedCode int) { setting.AppURL = AppURL + defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)() context := "/gogits/gogs" if !wiki { context += path.Join("/src/branch/main", path.Dir(filePath)) @@ -38,13 +40,13 @@ func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expe ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markup") web.SetForm(ctx, &options) Markup(ctx) - actual := strings.ReplaceAll(resp.Body.String(), ` data-markdown-generated-content=""`, "") - assert.Equal(t, expectedBody, actual) + assert.Equal(t, expectedBody, resp.Body.String()) assert.Equal(t, expectedCode, resp.Code) resp.Body.Reset() } func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody string, responseCode int) { + defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableInternalAttributes, true)() setting.AppURL = AppURL context := "/gogits/gogs" if !wiki { @@ -59,8 +61,7 @@ func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown") web.SetForm(ctx, &options) Markdown(ctx) - actual := strings.ReplaceAll(resp.Body.String(), ` data-markdown-generated-content=""`, "") - assert.Equal(t, responseBody, actual) + assert.Equal(t, responseBody, resp.Body.String()) assert.Equal(t, responseCode, resp.Code) resp.Body.Reset() } @@ -158,8 +159,8 @@ Here are some links to the most important topics. You can find the full list of <a href="http://localhost:3000/gogits/gogs/media/branch/main/path/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs/media/branch/main/path/image.png" alt="Image"/></a></p> `, http.StatusOK) - testRenderMarkup(t, "file", true, "path/test.unknown", "## Test", "Unsupported render extension: .unknown\n", http.StatusUnprocessableEntity) - testRenderMarkup(t, "unknown", true, "", "## Test", "Unknown mode: unknown\n", http.StatusUnprocessableEntity) + testRenderMarkup(t, "file", false, "path/test.unknown", "## Test", "unsupported file to render: \"path/test.unknown\"\n", http.StatusUnprocessableEntity) + testRenderMarkup(t, "unknown", false, "", "## Test", "Unknown mode: unknown\n", http.StatusUnprocessableEntity) } var simpleCases = []string{ |