From cb14f3c70ffb6536249817ae9fac337265878f91 Mon Sep 17 00:00:00 2001 From: katsusan Date: Wed, 27 Dec 2023 00:53:22 +0800 Subject: [PATCH 1/2] fix wrong link in user profile when using relative url --- routers/web/user/profile.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index ac278e300d701..e7668c284fcfc 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -7,6 +7,7 @@ package user import ( "fmt" "net/http" + "path" "strings" activities_model "code.gitea.io/gitea/models/activities" @@ -233,10 +234,20 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileGi if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil { log.Error("failed to GetBlobContent: %v", err) } else { + // When user opens someone's profile page, the router doesn't give the repo context, which makes it difficult to + // render the full link of media elements. + // the media link should be in the format of /[user]/[repoName]/src/branch/[branchName], + // Eg. /Tom/.profile/src/branch/main + // The branch shown on the profile page should be the 'main' branch, this need to be in sync with doc, see: + // https://docs.gitea.com/usage/profile-readme + const profileBranch = "main" + repoName := strings.TrimSuffix(path.Base(profileGitRepo.Path), ".git") + prefix := fmt.Sprintf("/%s/%s/src/branch/%s", ctx.ContextUser.Name, repoName, profileBranch) if profileContent, err := markdown.RenderString(&markup.RenderContext{ - Ctx: ctx, - GitRepo: profileGitRepo, - Metas: map[string]string{"mode": "document"}, + Ctx: ctx, + GitRepo: profileGitRepo, + URLPrefix: prefix, + Metas: map[string]string{"mode": "document"}, }, bytes); err != nil { log.Error("failed to RenderString: %v", err) } else { From 0abc1280610ba64951aa27142bac1b270b9cc283 Mon Sep 17 00:00:00 2001 From: katsusan Date: Wed, 27 Dec 2023 13:46:18 +0800 Subject: [PATCH 2/2] fix Organization profile and correct the way of generating urlprefix --- routers/web/org/home.go | 17 +++++++++++------ routers/web/shared/user/header.go | 6 +++--- routers/web/user/profile.go | 21 +++++++++------------ 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/routers/web/org/home.go b/routers/web/org/home.go index a9adfdc03cd8f..cdf280ed4acc3 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -18,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" shared_user "code.gitea.io/gitea/routers/web/shared/user" ) @@ -157,14 +158,14 @@ func Home(ctx *context.Context) { ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0 - profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer) + profileDbRepo, profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer) defer profileClose() - prepareOrgProfileReadme(ctx, profileGitRepo, profileReadmeBlob) + prepareOrgProfileReadme(ctx, profileGitRepo, profileDbRepo, profileReadmeBlob) ctx.HTML(http.StatusOK, tplOrgHome) } -func prepareOrgProfileReadme(ctx *context.Context, profileGitRepo *git.Repository, profileReadme *git.Blob) { +func prepareOrgProfileReadme(ctx *context.Context, profileGitRepo *git.Repository, profileDbRepo *repo_model.Repository, profileReadme *git.Blob) { if profileGitRepo == nil || profileReadme == nil { return } @@ -172,10 +173,14 @@ func prepareOrgProfileReadme(ctx *context.Context, profileGitRepo *git.Repositor if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil { log.Error("failed to GetBlobContent: %v", err) } else { + // Pass URLPrefix to markdown render for the full link of media elements. + // The profile of default branch would be shown. + prefix := profileDbRepo.Link() + "/src/branch/" + util.PathEscapeSegments(profileDbRepo.DefaultBranch) if profileContent, err := markdown.RenderString(&markup.RenderContext{ - Ctx: ctx, - GitRepo: profileGitRepo, - Metas: map[string]string{"mode": "document"}, + Ctx: ctx, + GitRepo: profileGitRepo, + URLPrefix: prefix, + Metas: map[string]string{"mode": "document"}, }, bytes); err != nil { log.Error("failed to RenderString: %v", err) } else { diff --git a/routers/web/shared/user/header.go b/routers/web/shared/user/header.go index 411d499eb4f70..919a080b42c65 100644 --- a/routers/web/shared/user/header.go +++ b/routers/web/shared/user/header.go @@ -87,7 +87,7 @@ func PrepareContextForProfileBigAvatar(ctx *context.Context) { } } -func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) { +func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) { profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, ".profile") if err == nil { perm, err := access_model.GetUserRepoPermission(ctx, profileDbRepo, doer) @@ -105,7 +105,7 @@ func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profile } else if !repo_model.IsErrRepoNotExist(err) { log.Error("FindUserProfileReadme failed to GetRepositoryByName: %v", err) } - return profileGitRepo, profileReadmeBlob, func() { + return profileDbRepo, profileGitRepo, profileReadmeBlob, func() { if profileGitRepo != nil { _ = profileGitRepo.Close() } @@ -115,7 +115,7 @@ func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profile func RenderUserHeader(ctx *context.Context) { prepareContextForCommonProfile(ctx) - _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer) + _, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer) defer profileClose() ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil } diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index e7668c284fcfc..a8ab3dde81d01 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -7,7 +7,6 @@ package user import ( "fmt" "net/http" - "path" "strings" activities_model "code.gitea.io/gitea/models/activities" @@ -65,17 +64,17 @@ func userProfile(ctx *context.Context) { ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data) } - profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer) + profileDbRepo, profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer) defer profileClose() showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID) - prepareUserProfileTabData(ctx, showPrivate, profileGitRepo, profileReadmeBlob) + prepareUserProfileTabData(ctx, showPrivate, profileDbRepo, profileGitRepo, profileReadmeBlob) // call PrepareContextForProfileBigAvatar later to avoid re-querying the NumFollowers & NumFollowing shared_user.PrepareContextForProfileBigAvatar(ctx) ctx.HTML(http.StatusOK, tplProfile) } -func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileGitRepo *git.Repository, profileReadme *git.Blob) { +func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadme *git.Blob) { // if there is a profile readme, default to "overview" page, otherwise, default to "repositories" page // if there is not a profile readme, the overview tab should be treated as the repositories tab tab := ctx.FormString("tab") @@ -234,15 +233,13 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileGi if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil { log.Error("failed to GetBlobContent: %v", err) } else { - // When user opens someone's profile page, the router doesn't give the repo context, which makes it difficult to - // render the full link of media elements. - // the media link should be in the format of /[user]/[repoName]/src/branch/[branchName], - // Eg. /Tom/.profile/src/branch/main - // The branch shown on the profile page should be the 'main' branch, this need to be in sync with doc, see: + // Give the URLPrefix to the markdown render for the full link of media element. + // the media link usually be like /[user]/[repoName]/media/branch/[branchName], + // Eg. /Tom/.profile/media/branch/main + // The branch shown on the profile page is the default branch, this need to be in sync with doc, see: // https://docs.gitea.com/usage/profile-readme - const profileBranch = "main" - repoName := strings.TrimSuffix(path.Base(profileGitRepo.Path), ".git") - prefix := fmt.Sprintf("/%s/%s/src/branch/%s", ctx.ContextUser.Name, repoName, profileBranch) + + prefix := profileDbRepo.Link() + "/src/branch/" + util.PathEscapeSegments(profileDbRepo.DefaultBranch) if profileContent, err := markdown.RenderString(&markup.RenderContext{ Ctx: ctx, GitRepo: profileGitRepo,