Skip to content

Commit 42149ff

Browse files
authored
fix wrong link in user and organization profile when using relative url (#28617)
fix #28436. the doc https://docs.gitea.com/usage/profile-readme maybe also need to be updated to tell that the main branch is necessary,which means the following three conditions should be satisfied: - repo: **.profile** - branch: **[default branch]** - markdown: **README.md**
1 parent baf0d40 commit 42149ff

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

routers/web/org/home.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/markup"
1919
"code.gitea.io/gitea/modules/markup/markdown"
2020
"code.gitea.io/gitea/modules/setting"
21+
"code.gitea.io/gitea/modules/util"
2122
shared_user "code.gitea.io/gitea/routers/web/shared/user"
2223
)
2324

@@ -157,25 +158,29 @@ func Home(ctx *context.Context) {
157158

158159
ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0
159160

160-
profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer)
161+
profileDbRepo, profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer)
161162
defer profileClose()
162-
prepareOrgProfileReadme(ctx, profileGitRepo, profileReadmeBlob)
163+
prepareOrgProfileReadme(ctx, profileGitRepo, profileDbRepo, profileReadmeBlob)
163164

164165
ctx.HTML(http.StatusOK, tplOrgHome)
165166
}
166167

167-
func prepareOrgProfileReadme(ctx *context.Context, profileGitRepo *git.Repository, profileReadme *git.Blob) {
168+
func prepareOrgProfileReadme(ctx *context.Context, profileGitRepo *git.Repository, profileDbRepo *repo_model.Repository, profileReadme *git.Blob) {
168169
if profileGitRepo == nil || profileReadme == nil {
169170
return
170171
}
171172

172173
if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
173174
log.Error("failed to GetBlobContent: %v", err)
174175
} else {
176+
// Pass URLPrefix to markdown render for the full link of media elements.
177+
// The profile of default branch would be shown.
178+
prefix := profileDbRepo.Link() + "/src/branch/" + util.PathEscapeSegments(profileDbRepo.DefaultBranch)
175179
if profileContent, err := markdown.RenderString(&markup.RenderContext{
176-
Ctx: ctx,
177-
GitRepo: profileGitRepo,
178-
Metas: map[string]string{"mode": "document"},
180+
Ctx: ctx,
181+
GitRepo: profileGitRepo,
182+
URLPrefix: prefix,
183+
Metas: map[string]string{"mode": "document"},
179184
}, bytes); err != nil {
180185
log.Error("failed to RenderString: %v", err)
181186
} else {

routers/web/shared/user/header.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func PrepareContextForProfileBigAvatar(ctx *context.Context) {
8787
}
8888
}
8989

90-
func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) {
90+
func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadmeBlob *git.Blob, profileClose func()) {
9191
profileDbRepo, err := repo_model.GetRepositoryByName(ctx, ctx.ContextUser.ID, ".profile")
9292
if err == nil {
9393
perm, err := access_model.GetUserRepoPermission(ctx, profileDbRepo, doer)
@@ -105,7 +105,7 @@ func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profile
105105
} else if !repo_model.IsErrRepoNotExist(err) {
106106
log.Error("FindUserProfileReadme failed to GetRepositoryByName: %v", err)
107107
}
108-
return profileGitRepo, profileReadmeBlob, func() {
108+
return profileDbRepo, profileGitRepo, profileReadmeBlob, func() {
109109
if profileGitRepo != nil {
110110
_ = profileGitRepo.Close()
111111
}
@@ -115,7 +115,7 @@ func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profile
115115
func RenderUserHeader(ctx *context.Context) {
116116
prepareContextForCommonProfile(ctx)
117117

118-
_, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer)
118+
_, _, profileReadmeBlob, profileClose := FindUserProfileReadme(ctx, ctx.Doer)
119119
defer profileClose()
120120
ctx.Data["HasProfileReadme"] = profileReadmeBlob != nil
121121
}

routers/web/user/profile.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ func userProfile(ctx *context.Context) {
6464
ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data)
6565
}
6666

67-
profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer)
67+
profileDbRepo, profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer)
6868
defer profileClose()
6969

7070
showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
71-
prepareUserProfileTabData(ctx, showPrivate, profileGitRepo, profileReadmeBlob)
71+
prepareUserProfileTabData(ctx, showPrivate, profileDbRepo, profileGitRepo, profileReadmeBlob)
7272
// call PrepareContextForProfileBigAvatar later to avoid re-querying the NumFollowers & NumFollowing
7373
shared_user.PrepareContextForProfileBigAvatar(ctx)
7474
ctx.HTML(http.StatusOK, tplProfile)
7575
}
7676

77-
func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileGitRepo *git.Repository, profileReadme *git.Blob) {
77+
func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadme *git.Blob) {
7878
// if there is a profile readme, default to "overview" page, otherwise, default to "repositories" page
7979
// if there is not a profile readme, the overview tab should be treated as the repositories tab
8080
tab := ctx.FormString("tab")
@@ -233,10 +233,18 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileGi
233233
if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
234234
log.Error("failed to GetBlobContent: %v", err)
235235
} else {
236+
// Give the URLPrefix to the markdown render for the full link of media element.
237+
// the media link usually be like /[user]/[repoName]/media/branch/[branchName],
238+
// Eg. /Tom/.profile/media/branch/main
239+
// The branch shown on the profile page is the default branch, this need to be in sync with doc, see:
240+
// https://docs.gitea.com/usage/profile-readme
241+
242+
prefix := profileDbRepo.Link() + "/src/branch/" + util.PathEscapeSegments(profileDbRepo.DefaultBranch)
236243
if profileContent, err := markdown.RenderString(&markup.RenderContext{
237-
Ctx: ctx,
238-
GitRepo: profileGitRepo,
239-
Metas: map[string]string{"mode": "document"},
244+
Ctx: ctx,
245+
GitRepo: profileGitRepo,
246+
URLPrefix: prefix,
247+
Metas: map[string]string{"mode": "document"},
240248
}, bytes); err != nil {
241249
log.Error("failed to RenderString: %v", err)
242250
} else {

0 commit comments

Comments
 (0)