Files
gitea-MIRROR/modules/markup/html_mention.go
silverwind 535a05345b 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
2026-11-21 14:39:11 +00:00

58 lines
1.9 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markup
import (
"fmt"
"net/url"
"strings"
"gitea.dev/modules/references"
"gitea.dev/modules/util"
"golang.org/x/net/html"
)
func mentionProcessor(ctx *RenderContext, node *html.Node) {
start := 0
nodeStop := node.NextSibling
for node != nodeStop {
found, loc := references.FindFirstMentionBytes(util.UnsafeStringToBytes(node.Data[start:]))
if !found {
node = node.NextSibling
start = 0
continue
}
loc.Start += start
loc.End += start
mention := node.Data[loc.Start:loc.End]
orgLowerTeams, checkOrgTeams := ctx.RenderOptions.Metas["teams"] // in format ",team1,team2,...,team-n,", always lowercase
if checkOrgTeams && strings.Contains(mention, "/") {
mentionOrg, teamName, _ := strings.Cut(mention, "/")
orgName := mentionOrg[1:] // remove the '@' prefix
if strings.EqualFold(orgName, ctx.RenderOptions.Metas["org"]) && strings.Contains(orgLowerTeams, ","+strings.ToLower(teamName)+",") {
link := fmt.Sprintf("/:root/org/%s/teams/%s", url.PathEscape(orgName), url.PathEscape(teamName))
replaceContent(node, loc.Start, loc.End, createLink(ctx, link, mention, "" /*mention*/))
node = node.NextSibling.NextSibling
start = 0
continue
}
start = loc.End
continue
}
mentionedUsername := mention[1:]
if DefaultRenderHelperFuncs != nil && DefaultRenderHelperFuncs.IsUsernameMentionable(ctx, mentionedUsername) {
link := "/:root/" + url.PathEscape(mentionedUsername)
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 {
start = loc.End
}
}
}