From dd79f0ce2b30d975f6c93024d81d32ed10632a6c Mon Sep 17 00:00:00 2001 From: oliverpool Date: Tue, 17 Jun 2025 13:35:46 +0200 Subject: [PATCH] blob: use NewTruncatedReader for markdown --- modules/markup/markdown/markdown.go | 7 +- routers/web/org/home.go | 10 ++- routers/web/user/profile.go | 10 ++- tests/integration/org_profile_test.go | 108 +++++++++++++++++++++++++ tests/integration/user_profile_test.go | 41 ++++++++++ 5 files changed, 167 insertions(+), 9 deletions(-) create mode 100644 tests/integration/org_profile_test.go diff --git a/modules/markup/markdown/markdown.go b/modules/markup/markdown/markdown.go index 717da464b9..e811d29994 100644 --- a/modules/markup/markdown/markdown.go +++ b/modules/markup/markdown/markdown.go @@ -267,8 +267,13 @@ func Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error // RenderString renders Markdown string to HTML with all specific handling stuff and return string func RenderString(ctx *markup.RenderContext, content string) (template.HTML, error) { + return RenderReader(ctx, strings.NewReader(content)) +} + +// RenderReader renders Markdown io.Reader to HTML with all specific handling stuff and return string +func RenderReader(ctx *markup.RenderContext, input io.Reader) (template.HTML, error) { var buf strings.Builder - if err := Render(ctx, strings.NewReader(content), &buf); err != nil { + if err := Render(ctx, input, &buf); err != nil { return "", err } return template.HTML(buf.String()), nil diff --git a/routers/web/org/home.go b/routers/web/org/home.go index a3823565ed..8f14f8899c 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -175,10 +175,12 @@ func prepareOrgProfileReadme(ctx *context.Context, profileGitRepo *git.Repositor return } - if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil { - log.Error("failed to GetBlobContent: %v", err) + if rc, _, err := profileReadme.NewTruncatedReader(setting.UI.MaxDisplayFileSize); err != nil { + log.Error("failed to NewTruncatedReader: %v", err) } else { - if profileContent, err := markdown.RenderString(&markup.RenderContext{ + defer rc.Close() + + if profileContent, err := markdown.RenderReader(&markup.RenderContext{ Ctx: ctx, GitRepo: profileGitRepo, Links: markup.Links{ @@ -188,7 +190,7 @@ func prepareOrgProfileReadme(ctx *context.Context, profileGitRepo *git.Repositor BranchPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)), }, Metas: map[string]string{"mode": "document"}, - }, bytes); err != nil { + }, rc); err != nil { log.Error("failed to RenderString: %v", err) } else { ctx.Data["ProfileReadme"] = profileContent diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 58f6d4a5f2..78dd6c5e7c 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -264,10 +264,12 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb total = int(count) case "overview": - if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil { - log.Error("failed to GetBlobContent: %v", err) + if rc, _, err := profileReadme.NewTruncatedReader(setting.UI.MaxDisplayFileSize); err != nil { + log.Error("failed to NewTruncatedReader: %v", err) } else { - if profileContent, err := markdown.RenderString(&markup.RenderContext{ + defer rc.Close() + + if profileContent, err := markdown.RenderReader(&markup.RenderContext{ Ctx: ctx, GitRepo: profileGitRepo, Links: markup.Links{ @@ -280,7 +282,7 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb BranchPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)), }, Metas: map[string]string{"mode": "document"}, - }, bytes); err != nil { + }, rc); err != nil { log.Error("failed to RenderString: %v", err) } else { ctx.Data["ProfileReadme"] = profileContent diff --git a/tests/integration/org_profile_test.go b/tests/integration/org_profile_test.go new file mode 100644 index 0000000000..2211a8b3d2 --- /dev/null +++ b/tests/integration/org_profile_test.go @@ -0,0 +1,108 @@ +// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved. +// SPDX-License-Identifier: MIT + +package integration + +import ( + "net/http" + "net/url" + "strings" + "testing" + + "forgejo.org/models/unittest" + user_model "forgejo.org/models/user" + "forgejo.org/modules/setting" + "forgejo.org/modules/test" + files_service "forgejo.org/services/repository/files" + "forgejo.org/tests" + + "github.com/stretchr/testify/assert" +) + +func TestOrgProfile(t *testing.T) { + onGiteaRun(t, func(t *testing.T, u *url.URL) { + checkReadme := func(t *testing.T, title, readmeFilename string, expectedCount int) { + t.Run(title, func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + // Prepare the test repository + org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) + + var ops []*files_service.ChangeRepoFile + op := "create" + if readmeFilename != "README.md" { + ops = append(ops, &files_service.ChangeRepoFile{ + Operation: "delete", + TreePath: "README.md", + }) + } else { + op = "update" + } + if readmeFilename != "" { + ops = append(ops, &files_service.ChangeRepoFile{ + Operation: op, + TreePath: readmeFilename, + ContentReader: strings.NewReader("# Hi!\n"), + }) + } + + _, _, f := tests.CreateDeclarativeRepo(t, org3, ".profile", nil, nil, ops) + defer f() + + // Perform the test + req := NewRequest(t, "GET", "/org3") + resp := MakeRequest(t, req, http.StatusOK) + + doc := NewHTMLParser(t, resp.Body) + readmeCount := doc.Find("#readme_profile").Length() + + assert.Equal(t, expectedCount, readmeCount) + }) + } + + checkReadme(t, "No readme", "", 0) + checkReadme(t, "README.md", "README.md", 1) + checkReadme(t, "readme.md", "readme.md", 1) + checkReadme(t, "ReadMe.mD", "ReadMe.mD", 1) + checkReadme(t, "readme.org does not render", "README.org", 0) + + t.Run("readme-size", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + // Prepare the test repository + org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}) + + _, _, f := tests.CreateDeclarativeRepo(t, org3, ".profile", nil, nil, []*files_service.ChangeRepoFile{ + { + Operation: "update", + TreePath: "README.md", + ContentReader: strings.NewReader(`## Lorem ipsum +dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +## Ut enim ad minim veniam +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum`), + }, + }) + defer f() + + t.Run("full", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + defer test.MockVariableValue(&setting.UI.MaxDisplayFileSize, 500)() + + req := NewRequest(t, "GET", "/org3") + resp := MakeRequest(t, req, http.StatusOK) + assert.Contains(t, resp.Body.String(), "Ut enim ad minim veniam") + assert.Contains(t, resp.Body.String(), "mollit anim id est laborum") + }) + + t.Run("truncated", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + defer test.MockVariableValue(&setting.UI.MaxDisplayFileSize, 146)() + + req := NewRequest(t, "GET", "/org3") + resp := MakeRequest(t, req, http.StatusOK) + assert.Contains(t, resp.Body.String(), "Ut enim ad minim") + assert.NotContains(t, resp.Body.String(), "veniam") + }) + }) + }) +} diff --git a/tests/integration/user_profile_test.go b/tests/integration/user_profile_test.go index 04d87c49ab..fb96efcd64 100644 --- a/tests/integration/user_profile_test.go +++ b/tests/integration/user_profile_test.go @@ -11,6 +11,8 @@ import ( "forgejo.org/models/unittest" user_model "forgejo.org/models/user" + "forgejo.org/modules/setting" + "forgejo.org/modules/test" files_service "forgejo.org/services/repository/files" "forgejo.org/tests" @@ -63,5 +65,44 @@ func TestUserProfile(t *testing.T) { checkReadme(t, "readme.md", "readme.md", 1) checkReadme(t, "ReadMe.mD", "ReadMe.mD", 1) checkReadme(t, "readme.org does not render", "README.org", 0) + + t.Run("readme-size", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + // Prepare the test repository + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + + _, _, f := tests.CreateDeclarativeRepo(t, user2, ".profile", nil, nil, []*files_service.ChangeRepoFile{ + { + Operation: "update", + TreePath: "README.md", + ContentReader: strings.NewReader(`## Lorem ipsum +dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +## Ut enim ad minim veniam +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum`), + }, + }) + defer f() + + t.Run("full", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + defer test.MockVariableValue(&setting.UI.MaxDisplayFileSize, 500)() + + req := NewRequest(t, "GET", "/user2") + resp := MakeRequest(t, req, http.StatusOK) + assert.Contains(t, resp.Body.String(), "Ut enim ad minim veniam") + assert.Contains(t, resp.Body.String(), "mollit anim id est laborum") + }) + + t.Run("truncated", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + defer test.MockVariableValue(&setting.UI.MaxDisplayFileSize, 146)() + + req := NewRequest(t, "GET", "/user2") + resp := MakeRequest(t, req, http.StatusOK) + assert.Contains(t, resp.Body.String(), "Ut enim ad minim") + assert.NotContains(t, resp.Body.String(), "veniam") + }) + }) }) }