Add Gitea Profile Readmes (#23260)
Implements displaying a README.md file present in a users ```.profile``` repository on the users profile page. If no such repository/file is present, the user's profile page remains unchanged. Example of user with ```.profile/README.md```  Example of user without ```.profile/README.md```  This pull request closes the feature request in #12233 Special thanks to @techknowlogick for the help in the Gitea discord! --------- Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: Yarden Shoham <hrsi88@gmail.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: yp05327 <576951401@qq.com> Co-authored-by: Yarden Shoham <git@yardenshoham.com>
This commit is contained in:
parent
b6fc2cdf82
commit
c090f87a8d
7 changed files with 97 additions and 38 deletions
|
@ -4,7 +4,9 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
|
@ -13,4 +15,24 @@ func RenderUserHeader(ctx *context.Context) {
|
|||
ctx.Data["IsPackageEnabled"] = setting.Packages.Enabled
|
||||
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
||||
ctx.Data["ContextUser"] = ctx.ContextUser
|
||||
tab := ctx.FormString("tab")
|
||||
ctx.Data["TabName"] = tab
|
||||
repo, err := repo_model.GetRepositoryByName(ctx.ContextUser.ID, ".profile")
|
||||
if err == nil && !repo.IsEmpty {
|
||||
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
|
||||
if err != nil {
|
||||
ctx.ServerError("OpenRepository", err)
|
||||
return
|
||||
}
|
||||
defer gitRepo.Close()
|
||||
commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetBranchCommit", err)
|
||||
return
|
||||
}
|
||||
blob, err := commit.GetBlobByPath("README.md")
|
||||
if err == nil && blob != nil {
|
||||
ctx.Data["ProfileReadme"] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/markup"
|
||||
"code.gitea.io/gitea/modules/markup/markdown"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
@ -91,6 +92,38 @@ func Profile(ctx *context.Context) {
|
|||
ctx.Data["RenderedDescription"] = content
|
||||
}
|
||||
|
||||
repo, err := repo_model.GetRepositoryByName(ctx.ContextUser.ID, ".profile")
|
||||
if err == nil && !repo.IsEmpty {
|
||||
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
|
||||
if err != nil {
|
||||
ctx.ServerError("OpenRepository", err)
|
||||
return
|
||||
}
|
||||
defer gitRepo.Close()
|
||||
commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetBranchCommit", err)
|
||||
return
|
||||
}
|
||||
blob, err := commit.GetBlobByPath("README.md")
|
||||
if err == nil {
|
||||
bytes, err := blob.GetBlobContent()
|
||||
if err != nil {
|
||||
ctx.ServerError("GetBlobContent", err)
|
||||
return
|
||||
}
|
||||
profileContent, err := markdown.RenderString(&markup.RenderContext{
|
||||
Ctx: ctx,
|
||||
GitRepo: gitRepo,
|
||||
}, bytes)
|
||||
if err != nil {
|
||||
ctx.ServerError("RenderString", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["ProfileReadme"] = profileContent
|
||||
}
|
||||
}
|
||||
|
||||
showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
|
||||
|
||||
orgs, err := organization.FindOrgs(organization.FindOrgOptions{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue