From c090f87a8db5b51e0aa9c7278b38ddc862c048ac Mon Sep 17 00:00:00 2001 From: Nicholas Pease <34464552+LAX18@users.noreply.github.com> Date: Tue, 9 May 2023 01:57:24 -0400 Subject: [PATCH] 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``` ![image](https://user-images.githubusercontent.com/34464552/222757202-5d53ac62-60d9-432f-b9e3-2537ffa91041.png) Example of user without ```.profile/README.md``` ![image](https://user-images.githubusercontent.com/34464552/222759972-576e058b-acd4-47ac-be33-38a7cb58cc81.png) This pull request closes the feature request in #12233 Special thanks to @techknowlogick for the help in the Gitea discord! --------- Co-authored-by: techknowlogick Co-authored-by: Yarden Shoham Co-authored-by: Lunny Xiao Co-authored-by: yp05327 <576951401@qq.com> Co-authored-by: Yarden Shoham --- .../content/doc/usage/profile-readme.en-us.md | 20 ++++++++++ options/locale/locale_en-US.ini | 1 + routers/web/shared/user/header.go | 22 +++++++++++ routers/web/user/profile.go | 33 ++++++++++++++++ templates/user/overview/header.tmpl | 13 +++++-- templates/user/profile.tmpl | 39 ++----------------- web_src/css/user.css | 7 ++++ 7 files changed, 97 insertions(+), 38 deletions(-) create mode 100644 docs/content/doc/usage/profile-readme.en-us.md diff --git a/docs/content/doc/usage/profile-readme.en-us.md b/docs/content/doc/usage/profile-readme.en-us.md new file mode 100644 index 000000000..802451f0b --- /dev/null +++ b/docs/content/doc/usage/profile-readme.en-us.md @@ -0,0 +1,20 @@ +--- +date: "2023-03-02T21:00:00+05:00" +title: "Usage: Gitea Profile READMEs" +slug: "profile-readme" +weight: 12 +toc: false +draft: false +menu: + sidebar: + parent: "usage" + name: "Gitea Profile READMEs" + weight: 12 + identifier: "profile-readme" +--- + +# Gitea Profile READMEs + +To display a markdown file in your Gitea profile page, simply make a repository named ".profile" and edit the README.md file inside. Gitea will automatically pull this file in and display it above your repositories. + +Note. You are welcome to make this repository private. Doing so will hide your source files from public viewing and allow you to privitize certain files. However, the README.md file will be the only file present on your profile. If you wish to have an entirely private .profile repository, remove or rename the README.md file. diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 9bfc0033b..f82522083 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -569,6 +569,7 @@ starred = Starred Repositories watched = Watched Repositories code = Code projects = Projects +overview = Overview following = Following follow = Follow unfollow = Unfollow diff --git a/routers/web/shared/user/header.go b/routers/web/shared/user/header.go index 05e45f999..9594e6975 100644 --- a/routers/web/shared/user/header.go +++ b/routers/web/shared/user/header.go @@ -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 + } + } } diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index ef91d89d1..42ae37e3b 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -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{ diff --git a/templates/user/overview/header.tmpl b/templates/user/overview/header.tmpl index 10227a3eb..6016aca44 100644 --- a/templates/user/overview/header.tmpl +++ b/templates/user/overview/header.tmpl @@ -1,6 +1,7 @@ -{{with .ContextUser}} -
+{{if or (.IsPackagesPage) (.PageIsViewProjects)}} + {{with .ContextUser}} +
@@ -14,11 +15,17 @@
+ {{end}} {{end}}
{{if eq .TabName "activity"}} @@ -177,10 +144,12 @@ {{template "repo/user_cards" .}} {{else if eq .TabName "followers"}} {{template "repo/user_cards" .}} - {{else}} + {{else if or (eq .TabName "repositories") (not .ProfileReadme)}} {{template "explore/repo_search" .}} {{template "explore/repo_list" .}} {{template "base/paginate" .}} + {{else if .ProfileReadme}} +
{{$.ProfileReadme|Str2html}}
{{end}}
diff --git a/web_src/css/user.css b/web_src/css/user.css index cdf295656..0a8b49b03 100644 --- a/web_src/css/user.css +++ b/web_src/css/user.css @@ -140,6 +140,13 @@ border: none; } +#readme_profile { + padding: 10px; + border-radius: 0.28571429rem; + background: var(--color-card); + border: 1px solid var(--color-secondary); +} + #notification_table tr { cursor: default; }