enhance(markup): highlight mentions of the current user (#40474)

Render user mentions like GitHub, semibold in the surrounding text
color, and highlight mentions of the signed-in user in yellow so they
stand out in long threads.

The renderer adds `user-mention` and `current-user` classes, the styling
is pure CSS. CODEOWNERS and OAuth creator links use the same class, team
mentions are unchanged.

| Before | After |
| --- | --- |
| <img width="291" height="29" alt="before"
src="https://github.com/user-attachments/assets/6c451e7a-dfce-4e95-a386-f9aa9d28c721"
/> | <img width="291" height="29" alt="after"
src="https://github.com/user-attachments/assets/6b137c71-4692-46f2-adf8-d77ee87f3077"
/> |

Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
silverwind authored and GitHub committed 2026-11-21 14:39:11 +00:00
1 parent 6f8ebc0b2f
commit 535a05345b
16 files changed
+50 -23

No files matched your search

+1 -1
View File
@@ -29,7 +29,7 @@ func TestRepoComment(t *testing.T) {
assert.Equal(t,
`<p><a href="/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d" rel="nofollow"><code>65f1bf27bc</code></a><br/>
<a href="/user2/repo1/issues/1" class="ref-issue" rel="nofollow">#1</a><br/>
<a href="/user2" rel="nofollow">@user2</a></p>
<a href="/user2" class="user-mention" rel="nofollow">@user2</a></p>
`, rendered)
})
+1 -1
View File
@@ -31,7 +31,7 @@ func TestRepoFile(t *testing.T) {
assert.Equal(t,
`<p><a href="/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d" rel="nofollow"><code>65f1bf27bc</code></a>
#1
<a href="/user2" rel="nofollow">@user2</a></p>
<a href="/user2" class="user-mention" rel="nofollow">@user2</a></p>
`, rendered)
})
+1 -1
View File
@@ -28,7 +28,7 @@ func TestRepoWiki(t *testing.T) {
assert.Equal(t,
`<p><a href="/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d" rel="nofollow"><code>65f1bf27bc</code></a>
<a href="/user2/repo1/issues/1" class="ref-issue" rel="nofollow">#1</a>
<a href="/user2" rel="nofollow">@user2</a></p>
<a href="/user2" class="user-mention" rel="nofollow">@user2</a></p>
`, rendered)
})
+1 -1
View File
@@ -27,7 +27,7 @@ func TestSimpleDocument(t *testing.T) {
assert.Equal(t,
`<p>65f1bf27bc3bf70f64657658635e66094edbcb4d
#1
<a href="/user2" rel="nofollow">@user2</a></p>
<a href="/user2" class="user-mention" rel="nofollow">@user2</a></p>
<p><a href="/base/test" rel="nofollow">/test</a>
<a href="/base/test" rel="nofollow">./test</a>
<a href="/base/image" target="_blank" rel="nofollow noopener"><img src="/base/image" alt="/image"/></a>
+2 -1
View File
@@ -46,7 +46,8 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) {
if DefaultRenderHelperFuncs != nil && DefaultRenderHelperFuncs.IsUsernameMentionable(ctx, mentionedUsername) {
link := "/:root/" + url.PathEscape(mentionedUsername)
replaceContent(node, loc.Start, loc.End, createLink(ctx, link, mention, "" /*mention*/))
isCurrentUser := DefaultRenderHelperFuncs.IsCurrentUsername != nil && DefaultRenderHelperFuncs.IsCurrentUsername(ctx, mentionedUsername)
replaceContent(node, loc.Start, loc.End, createLink(ctx, link, mention, "user-mention"+util.Iif(isCurrentUser, " current-user", "")))
node = node.NextSibling.NextSibling
start = 0
} else {
+2 -2
View File
@@ -172,8 +172,8 @@ This PR has been generated by [Renovate Bot](https://github.com/renovatebot/reno
<p>See commit <a href="/` + testRepoOwnerName + `/` + testRepoName + `/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p>
<p>Ideas and codes</p>
<ul>
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" class="ref-issue" rel="nofollow">ocornut/imgui#786</a></li>
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="` + FullURL + `issues/786" class="ref-issue" rel="nofollow">#786</a></li>
<li>Bezier widget (by <a href="/r-lyeh" class="user-mention" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" class="ref-issue" rel="nofollow">ocornut/imgui#786</a></li>
<li>Bezier widget (by <a href="/r-lyeh" class="user-mention" rel="nofollow">@r-lyeh</a>) <a href="` + FullURL + `issues/786" class="ref-issue" rel="nofollow">#786</a></li>
<li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li>
<li><a href="` + baseURL + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
<li><a href="` + baseURL + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
+1
View File
@@ -35,6 +35,7 @@ type RenderHelper interface {
// services/markup: some real helper functions could only be provided here because it needs to access various services & templates
type RenderHelperFuncs struct {
IsUsernameMentionable func(ctx context.Context, username string) bool
IsCurrentUsername func(ctx context.Context, username string) bool
RenderRepoFileCodePreview func(ctx context.Context, options RenderCodePreviewOptions) (template.HTML, error)
RenderRepoIssueIconTitle func(ctx context.Context, options RenderIssueIconTitleOptions) (template.HTML, error)
}
+11 -8
View File
@@ -56,7 +56,10 @@ func TestMain(m *testing.M) {
setting.Markdown.RenderOptionsComment.ShortIssuePattern = true
markup.Init(&markup.RenderHelperFuncs{
IsUsernameMentionable: func(ctx context.Context, username string) bool {
return username == "mention-user"
return username == "mention-user" || username == "current-user"
},
IsCurrentUsername: func(_ctx context.Context, username string) bool {
return username == "current-user"
},
})
os.Exit(m.Run())
@@ -108,19 +111,19 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a582
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
<span class="emoji" data-alias="+1">👍</span>
<a href="mailto:mail@domain.com">mail@domain.com</a>
<a href="/mention-user">@mention-user</a> test
<a href="/mention-user" class="user-mention">@mention-user</a> test
<a href="/user13/repo11/issues/123" class="ref-issue">#123</a>
space`
assert.Equal(t, expected, string(newTestRenderUtils(t).RenderCommitBody(testInput(), mockRepo)))
})
t.Run("RenderCommitMessage", func(t *testing.T) {
expected := `space <a href="/mention-user" data-markdown-generated-content="">@mention-user</a>`
expected := `space <a href="/mention-user" data-markdown-generated-content="" class="user-mention">@mention-user</a>`
assert.EqualValues(t, expected, newTestRenderUtils(t).RenderCommitMessage(testInput(), mockRepo))
})
t.Run("RenderCommitMessageLinkSubject", func(t *testing.T) {
expected := `<span class="title-full-link-hover"><a href="https://example.com/link" class="muted title-full-link">space </a><a href="/mention-user" data-markdown-generated-content="">@mention-user</a></span>`
expected := `<span class="title-full-link-hover"><a href="https://example.com/link" class="muted title-full-link">space </a><a href="/mention-user" data-markdown-generated-content="" class="user-mention">@mention-user</a></span>`
assert.EqualValues(t, expected, newTestRenderUtils(t).RenderCommitMessageLinkSubject(testInput(), "https://example.com/link", mockRepo))
})
@@ -215,7 +218,7 @@ func TestRenderIssueTitleCodeSpan(t *testing.T) {
func TestRenderMarkdownToHtml(t *testing.T) {
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
expected := `<p>space <a href="/mention-user" rel="nofollow">@mention-user</a><br/>
expected := `<p>space <a href="/mention-user" class="user-mention" rel="nofollow">@mention-user</a><br/>
/just/a/path.bin
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a>
<a href="/file.bin" rel="nofollow">local link</a>
@@ -232,7 +235,7 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a582
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
<span class="emoji" data-alias="+1">👍</span>
<a href="mailto:mail@domain.com" rel="nofollow">mail@domain.com</a>
<a href="/mention-user" rel="nofollow">@mention-user</a> test
<a href="/mention-user" class="user-mention" rel="nofollow">@mention-user</a> test
#123
space</p>
`
@@ -315,8 +318,8 @@ func TestRenderLabels(t *testing.T) {
func TestUserMention(t *testing.T) {
defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
rendered := newTestRenderUtils(t).MarkdownToHtml("@no-such-user @mention-user @mention-user")
assert.Equal(t, `<p>@no-such-user <a href="/mention-user" rel="nofollow">@mention-user</a> <a href="/mention-user" rel="nofollow">@mention-user</a></p>`, strings.TrimSpace(string(rendered)))
rendered := newTestRenderUtils(t).MarkdownToHtml("@no-such-user @mention-user @mention-user @current-user")
assert.Equal(t, `<p>@no-such-user <a href="/mention-user" class="user-mention" rel="nofollow">@mention-user</a> <a href="/mention-user" class="user-mention" rel="nofollow">@mention-user</a> <a href="/current-user" class="user-mention current-user" rel="nofollow">@current-user</a></p>`, strings.TrimSpace(string(rendered)))
}
func TestAvatarStack(t *testing.T) {
+1 -1
View File
@@ -84,7 +84,7 @@ func TestAPI_RenderGFM(t *testing.T) {
<ul>
<li><a href="http://localhost:3000/user2/repo1/wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
<li><a href="http://localhost:3000/user2/repo1/wiki/Tips" rel="nofollow">Tips</a></li>
<li>Bezier widget (by <a href="http://localhost:3000/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="https://github.com/ocornut/imgui/issues/786" rel="nofollow">https://github.com/ocornut/imgui/issues/786</a></li>
<li>Bezier widget (by <a href="http://localhost:3000/r-lyeh" class="user-mention" rel="nofollow">@r-lyeh</a>) <a href="https://github.com/ocornut/imgui/issues/786" rel="nofollow">https://github.com/ocornut/imgui/issues/786</a></li>
</ul>
`,
// Guard wiki sidebar: special syntax
+1 -1
View File
@@ -369,7 +369,7 @@ func AuthorizeOAuth(ctx *context.Context) {
ctx.Data["Scope"] = form.Scope
ctx.Data["Nonce"] = form.Nonce
if user != nil {
ctx.Data["ApplicationCreatorLinkHTML"] = template.HTML(fmt.Sprintf(`<a href="%s">@%s</a>`, html.EscapeString(user.HomeLink()), html.EscapeString(user.Name)))
ctx.Data["ApplicationCreatorLinkHTML"] = template.HTML(fmt.Sprintf(`<a class="user-mention" href="%s">@%s</a>`, html.EscapeString(user.HomeLink()), html.EscapeString(user.Name)))
} else {
ctx.Data["ApplicationCreatorLinkHTML"] = template.HTML(fmt.Sprintf(`<a href="%s">%s</a>`, html.EscapeString(setting.AppSubURL+"/"), html.EscapeString(setting.AppName)))
}
+5 -4
View File
@@ -158,6 +158,7 @@ func handleFileViewRenderImage(ctx *context.Context, fInfo *fileInfo, prefetchBu
type fileCodeOwner struct {
CodeOwnerName string
CodeOwnerLink string
IsUser bool
}
func prepareFileCodeOwners(ctx *context.Context) {
@@ -177,22 +178,22 @@ func prepareFileCodeOwners(ctx *context.Context) {
var owners []fileCodeOwner
seen := make(map[string]bool)
add := func(name, link string) {
add := func(name, link string, isUser bool) {
if !seen[link] {
owners = append(owners, fileCodeOwner{name, link})
owners = append(owners, fileCodeOwner{name, link, isUser})
seen[link] = true
}
}
for _, rule := range matching {
for _, user := range rule.Users {
add("@"+user.Name, user.HomeLink())
add("@"+user.Name, user.HomeLink(), true)
}
for _, team := range rule.Teams {
org, err := org_model.GetOrgByID(ctx, team.OrgID)
if err != nil {
continue
}
add("@"+org.Name+"/"+team.Name, org.HomeLink()+"/teams/"+util.PathEscapeSegments(team.LowerName))
add("@"+org.Name+"/"+team.Name, org.HomeLink()+"/teams/"+util.PathEscapeSegments(team.LowerName), false)
}
}
ctx.Data["FileCodeOwners"] = owners
+5
View File
@@ -5,6 +5,7 @@ package markup
import (
"context"
"strings"
"gitea.dev/models/user"
"gitea.dev/modules/markup"
@@ -30,5 +31,9 @@ func FormalRenderHelperFuncs() *markup.RenderHelperFuncs {
// when using gitea context (web context), use user's visibility and user's permission to check
return user.IsUserVisibleToViewer(giteaCtx, mentionedUser, giteaCtx.Doer)
},
IsCurrentUsername: func(ctx context.Context, username string) bool {
giteaCtx := gitea_context.GetWebContext(ctx)
return giteaCtx != nil && giteaCtx.Doer != nil && strings.EqualFold(giteaCtx.Doer.Name, username)
},
}
}
@@ -43,9 +43,13 @@ func TestRenderHelperMention(t *testing.T) {
assert.True(t, FormalRenderHelperFuncs().IsUsernameMentionable(giteaCtx, userPublic))
assert.False(t, FormalRenderHelperFuncs().IsUsernameMentionable(giteaCtx, userPrivate))
assert.False(t, FormalRenderHelperFuncs().IsCurrentUsername(giteaCtx, userPublic))
giteaCtx.Doer, err = user.GetUserByName(t.Context(), userPrivate)
assert.NoError(t, err)
assert.True(t, FormalRenderHelperFuncs().IsUsernameMentionable(giteaCtx, userPublic))
assert.True(t, FormalRenderHelperFuncs().IsUsernameMentionable(giteaCtx, userPrivate))
assert.True(t, FormalRenderHelperFuncs().IsCurrentUsername(giteaCtx, "USER31"))
assert.False(t, FormalRenderHelperFuncs().IsCurrentUsername(giteaCtx, userPublic))
assert.False(t, FormalRenderHelperFuncs().IsCurrentUsername(t.Context(), userPrivate))
}
+1 -1
View File
@@ -27,7 +27,7 @@
<div class="file-codeowners ui segment flex-text-block tw-flex-wrap">
{{svg "octicon-shield-check" 16}} <span>{{ctx.Locale.Tr "repo.codeowners"}}</span>
{{range $one := .FileCodeOwners}}
<a href="{{$one.CodeOwnerLink}}">{{$one.CodeOwnerName}}</a>
<a {{if $one.IsUser}}class="user-mention" {{end}}href="{{$one.CodeOwnerLink}}">{{$one.CodeOwnerName}}</a>
{{end}}
</div>
{{end}}
+1 -1
View File
@@ -158,7 +158,7 @@ func TestPullView_CodeOwner(t *testing.T) {
require.NoError(t, err)
resp = session.MakeRequest(t, NewRequest(t, "GET", "/user2/test_codeowner/src/branch/file-owners/README.md"), http.StatusOK)
doc = NewHTMLParser(t, resp.Body)
assert.Equal(t, "@user8", doc.Find(".file-codeowners a[href='/user8']").Text())
assert.Equal(t, "@user8", doc.Find(".file-codeowners a.user-mention[href='/user8']").Text())
assert.Equal(t, "@org3/team1", doc.Find(".file-codeowners a[href='/org3/teams/team1']").Text())
resp = session.MakeRequest(t, NewRequest(t, "GET", "/user2/test_codeowner/src/branch/file-owners/CODEOWNERS"), http.StatusOK)
doc = NewHTMLParser(t, resp.Body)
+12
View File
@@ -327,6 +327,18 @@ a.suppressed:hover {
color: inherit;
}
a.user-mention {
color: inherit;
font-weight: var(--font-weight-semibold);
border-radius: 2px;
white-space: nowrap;
}
a.user-mention.current-user {
color: var(--color-warning-text);
background: var(--color-warning-bg);
}
/* keyboard focus ring for interactive elements */
a:focus-visible,
button:focus-visible,