2024-11-04 10:59:50 +00:00
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package markup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/references"
|
|
|
|
"code.gitea.io/gitea/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]
|
2024-11-22 05:48:09 +00:00
|
|
|
teams, ok := ctx.RenderOptions.Metas["teams"]
|
2024-11-04 10:59:50 +00:00
|
|
|
// FIXME: util.URLJoin may not be necessary here:
|
|
|
|
// - setting.AppURL is defined to have a terminal '/' so unless mention[1:]
|
|
|
|
// is an AppSubURL link we can probably fallback to concatenation.
|
|
|
|
// team mention should follow @orgName/teamName style
|
|
|
|
if ok && strings.Contains(mention, "/") {
|
|
|
|
mentionOrgAndTeam := strings.Split(mention, "/")
|
2024-11-22 05:48:09 +00:00
|
|
|
if mentionOrgAndTeam[0][1:] == ctx.RenderOptions.Metas["org"] && strings.Contains(teams, ","+strings.ToLower(mentionOrgAndTeam[1])+",") {
|
2024-11-24 08:18:57 +00:00
|
|
|
link := ctx.RenderHelper.ResolveLink(util.URLJoin("org", ctx.RenderOptions.Metas["org"], "teams", mentionOrgAndTeam[1]), LinkTypeApp)
|
|
|
|
replaceContent(node, loc.Start, loc.End, createLink(ctx, link, mention, "" /*mention*/))
|
2024-11-04 10:59:50 +00:00
|
|
|
node = node.NextSibling.NextSibling
|
|
|
|
start = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
start = loc.End
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mentionedUsername := mention[1:]
|
|
|
|
|
2024-11-24 08:18:57 +00:00
|
|
|
if DefaultRenderHelperFuncs != nil && DefaultRenderHelperFuncs.IsUsernameMentionable(ctx, mentionedUsername) {
|
|
|
|
link := ctx.RenderHelper.ResolveLink(mentionedUsername, LinkTypeApp)
|
|
|
|
replaceContent(node, loc.Start, loc.End, createLink(ctx, link, mention, "" /*mention*/))
|
2024-11-04 10:59:50 +00:00
|
|
|
node = node.NextSibling.NextSibling
|
|
|
|
start = 0
|
|
|
|
} else {
|
|
|
|
start = loc.End
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|