gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup
  4. import (
  5. "strings"
  6. "code.gitea.io/gitea/modules/references"
  7. "code.gitea.io/gitea/modules/util"
  8. "golang.org/x/net/html"
  9. )
  10. func mentionProcessor(ctx *RenderContext, node *html.Node) {
  11. start := 0
  12. nodeStop := node.NextSibling
  13. for node != nodeStop {
  14. found, loc := references.FindFirstMentionBytes(util.UnsafeStringToBytes(node.Data[start:]))
  15. if !found {
  16. node = node.NextSibling
  17. start = 0
  18. continue
  19. }
  20. loc.Start += start
  21. loc.End += start
  22. mention := node.Data[loc.Start:loc.End]
  23. teams, ok := ctx.RenderOptions.Metas["teams"]
  24. // FIXME: util.URLJoin may not be necessary here:
  25. // - setting.AppURL is defined to have a terminal '/' so unless mention[1:]
  26. // is an AppSubURL link we can probably fallback to concatenation.
  27. // team mention should follow @orgName/teamName style
  28. if ok && strings.Contains(mention, "/") {
  29. mentionOrgAndTeam := strings.Split(mention, "/")
  30. if mentionOrgAndTeam[0][1:] == ctx.RenderOptions.Metas["org"] && strings.Contains(teams, ","+strings.ToLower(mentionOrgAndTeam[1])+",") {
  31. link := "/:root/" + util.URLJoin("org", ctx.RenderOptions.Metas["org"], "teams", mentionOrgAndTeam[1])
  32. replaceContent(node, loc.Start, loc.End, createLink(ctx, link, mention, "" /*mention*/))
  33. node = node.NextSibling.NextSibling
  34. start = 0
  35. continue
  36. }
  37. start = loc.End
  38. continue
  39. }
  40. mentionedUsername := mention[1:]
  41. if DefaultRenderHelperFuncs != nil && DefaultRenderHelperFuncs.IsUsernameMentionable(ctx, mentionedUsername) {
  42. link := "/:root/" + mentionedUsername
  43. replaceContent(node, loc.Start, loc.End, createLink(ctx, link, mention, "" /*mention*/))
  44. node = node.NextSibling.NextSibling
  45. start = 0
  46. } else {
  47. start = loc.End
  48. }
  49. }
  50. }