Skip to content

Commit c090f87

Browse files
npease18techknowlogickyardenshohamlunnyyp05327
authored
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 <[email protected]> Co-authored-by: Yarden Shoham <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: yp05327 <[email protected]> Co-authored-by: Yarden Shoham <[email protected]>
1 parent b6fc2cd commit c090f87

File tree

7 files changed

+97
-38
lines changed

7 files changed

+97
-38
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
date: "2023-03-02T21:00:00+05:00"
3+
title: "Usage: Gitea Profile READMEs"
4+
slug: "profile-readme"
5+
weight: 12
6+
toc: false
7+
draft: false
8+
menu:
9+
sidebar:
10+
parent: "usage"
11+
name: "Gitea Profile READMEs"
12+
weight: 12
13+
identifier: "profile-readme"
14+
---
15+
16+
# Gitea Profile READMEs
17+
18+
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.
19+
20+
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.

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ starred = Starred Repositories
569569
watched = Watched Repositories
570570
code = Code
571571
projects = Projects
572+
overview = Overview
572573
following = Following
573574
follow = Follow
574575
unfollow = Unfollow

routers/web/shared/user/header.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
package user
55

66
import (
7+
repo_model "code.gitea.io/gitea/models/repo"
78
"code.gitea.io/gitea/modules/context"
9+
"code.gitea.io/gitea/modules/git"
810
"code.gitea.io/gitea/modules/setting"
911
)
1012

@@ -13,4 +15,24 @@ func RenderUserHeader(ctx *context.Context) {
1315
ctx.Data["IsPackageEnabled"] = setting.Packages.Enabled
1416
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
1517
ctx.Data["ContextUser"] = ctx.ContextUser
18+
tab := ctx.FormString("tab")
19+
ctx.Data["TabName"] = tab
20+
repo, err := repo_model.GetRepositoryByName(ctx.ContextUser.ID, ".profile")
21+
if err == nil && !repo.IsEmpty {
22+
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
23+
if err != nil {
24+
ctx.ServerError("OpenRepository", err)
25+
return
26+
}
27+
defer gitRepo.Close()
28+
commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
29+
if err != nil {
30+
ctx.ServerError("GetBranchCommit", err)
31+
return
32+
}
33+
blob, err := commit.GetBlobByPath("README.md")
34+
if err == nil && blob != nil {
35+
ctx.Data["ProfileReadme"] = true
36+
}
37+
}
1638
}

routers/web/user/profile.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
repo_model "code.gitea.io/gitea/models/repo"
1717
user_model "code.gitea.io/gitea/models/user"
1818
"code.gitea.io/gitea/modules/context"
19+
"code.gitea.io/gitea/modules/git"
1920
"code.gitea.io/gitea/modules/markup"
2021
"code.gitea.io/gitea/modules/markup/markdown"
2122
"code.gitea.io/gitea/modules/setting"
@@ -91,6 +92,38 @@ func Profile(ctx *context.Context) {
9192
ctx.Data["RenderedDescription"] = content
9293
}
9394

95+
repo, err := repo_model.GetRepositoryByName(ctx.ContextUser.ID, ".profile")
96+
if err == nil && !repo.IsEmpty {
97+
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
98+
if err != nil {
99+
ctx.ServerError("OpenRepository", err)
100+
return
101+
}
102+
defer gitRepo.Close()
103+
commit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
104+
if err != nil {
105+
ctx.ServerError("GetBranchCommit", err)
106+
return
107+
}
108+
blob, err := commit.GetBlobByPath("README.md")
109+
if err == nil {
110+
bytes, err := blob.GetBlobContent()
111+
if err != nil {
112+
ctx.ServerError("GetBlobContent", err)
113+
return
114+
}
115+
profileContent, err := markdown.RenderString(&markup.RenderContext{
116+
Ctx: ctx,
117+
GitRepo: gitRepo,
118+
}, bytes)
119+
if err != nil {
120+
ctx.ServerError("RenderString", err)
121+
return
122+
}
123+
ctx.Data["ProfileReadme"] = profileContent
124+
}
125+
}
126+
94127
showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
95128

96129
orgs, err := organization.FindOrgs(organization.FindOrgOptions{

templates/user/overview/header.tmpl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!-- TODO: make template org and user can share -->
2-
{{with .ContextUser}}
3-
<div class="ui container">
2+
{{if or (.IsPackagesPage) (.PageIsViewProjects)}}
3+
{{with .ContextUser}}
4+
<div class="ui container">
45
<div class="ui vertically grid head">
56
<div class="column">
67
<div class="ui header">
@@ -14,11 +15,17 @@
1415
</div>
1516
</div>
1617
</div>
18+
{{end}}
1719
{{end}}
1820

1921
<div class="ui tabs container">
2022
<div class="ui secondary stackable pointing menu">
21-
<a class="item" href="{{.ContextUser.HomeLink}}">
23+
{{if .ProfileReadme}}
24+
<a class='{{if or (eq .TabName "overview") (and (eq .TabName "") (not .IsPackagesPage) (not .PageIsViewProjects))}}active {{end}}item' href="{{.ContextUser.HomeLink}}?tab=overview">
25+
{{svg "octicon-info"}} {{.locale.Tr "user.overview"}}
26+
</a>
27+
{{end}}
28+
<a class="{{if or (eq .TabName "repositories") (and (eq .TabName "") (not .IsPackagesPage) (not .PageIsViewProjects) (not .ProfileReadme))}}active {{end}} item" href="{{.ContextUser.HomeLink}}?tab=repositories">
2229
{{svg "octicon-repo"}} {{.locale.Tr "user.repositories"}}
2330
{{if .ContextUser.NumRepos}}
2431
<div class="ui small label">{{.ContextUser.NumRepos}}</div>

templates/user/profile.tmpl

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -121,40 +121,7 @@
121121
</div>
122122
<div class="ui eleven wide column">
123123
<div class="ui secondary stackable pointing tight menu">
124-
<a class='{{if and (ne .TabName "activity") (ne .TabName "following") (ne .TabName "followers") (ne .TabName "stars") (ne .TabName "watching") (ne .TabName "projects") (ne .TabName "code")}}active {{end}}item' href="{{.ContextUser.HomeLink}}">
125-
{{svg "octicon-repo"}} {{.locale.Tr "user.repositories"}}
126-
{{if .ContextUser.NumRepos}}
127-
<div class="ui small label">{{.ContextUser.NumRepos}}</div>
128-
{{end}}
129-
</a>
130-
<a href="{{.ContextUser.HomeLink}}/-/projects" class="{{if eq .TabName "projects"}}active {{end}}item">
131-
{{svg "octicon-project-symlink"}} {{.locale.Tr "user.projects"}}
132-
</a>
133-
{{if .IsPackageEnabled}}
134-
<a class='{{if eq .TabName "packages"}}active {{end}}item' href="{{.ContextUser.HomeLink}}/-/packages">
135-
{{svg "octicon-package"}} {{.locale.Tr "packages.title"}}
136-
</a>
137-
{{end}}
138-
{{if and (not $.UnitTypeCode.UnitGlobalDisabled) .IsRepoIndexerEnabled}}
139-
<a class='{{if eq .TabName "code"}}active {{end}}item' href="{{.ContextUser.HomeLink}}/-/code">
140-
{{svg "octicon-code"}} {{.locale.Tr "user.code"}}
141-
</a>
142-
{{end}}
143-
<a class='{{if eq .TabName "activity"}}active {{end}}item' href="{{.ContextUser.HomeLink}}?tab=activity">
144-
{{svg "octicon-rss"}} {{.locale.Tr "user.activity"}}
145-
</a>
146-
{{if not .DisableStars}}
147-
<a class='{{if eq .TabName "stars"}}active {{end}}item' href="{{.ContextUser.HomeLink}}?tab=stars">
148-
{{svg "octicon-star"}} {{.locale.Tr "user.starred"}}
149-
{{if .ContextUser.NumStars}}
150-
<div class="ui small label">{{.ContextUser.NumStars}}</div>
151-
{{end}}
152-
</a>
153-
{{else}}
154-
<a class='{{if eq .TabName "watching"}}active {{end}}item' href="{{.ContextUser.HomeLink}}?tab=watching">
155-
{{svg "octicon-eye"}} {{.locale.Tr "user.watched"}}
156-
</a>
157-
{{end}}
124+
{{template "user/overview/header" .}}
158125
</div>
159126

160127
{{if eq .TabName "activity"}}
@@ -177,10 +144,12 @@
177144
{{template "repo/user_cards" .}}
178145
{{else if eq .TabName "followers"}}
179146
{{template "repo/user_cards" .}}
180-
{{else}}
147+
{{else if or (eq .TabName "repositories") (not .ProfileReadme)}}
181148
{{template "explore/repo_search" .}}
182149
{{template "explore/repo_list" .}}
183150
{{template "base/paginate" .}}
151+
{{else if .ProfileReadme}}
152+
<div id="readme_profile" class="render-content markup"> {{$.ProfileReadme|Str2html}} </div>
184153
{{end}}
185154
</div>
186155
</div>

web_src/css/user.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@
140140
border: none;
141141
}
142142

143+
#readme_profile {
144+
padding: 10px;
145+
border-radius: 0.28571429rem;
146+
background: var(--color-card);
147+
border: 1px solid var(--color-secondary);
148+
}
149+
143150
#notification_table tr {
144151
cursor: default;
145152
}

0 commit comments

Comments
 (0)