diff --git a/routers/web/repo/resource.go b/routers/web/repo/resource.go index d0e5ea569d..b056c70048 100644 --- a/routers/web/repo/resource.go +++ b/routers/web/repo/resource.go @@ -4,7 +4,6 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/routers/web/user" - "errors" "fmt" "net/http" "regexp" @@ -41,15 +40,29 @@ func GetRenderedResourcesWithSearchLinks(ctx *context.Context, repo interface{}, return "", err } - resourceSubstitutionPattern := fmt.Sprintf(`- \[ \] $1 [найти](/explore/%s?tab=&q=$1)`, strings.ToLower(fieldName)) - resourcesWithLinks := regExp.ReplaceAll([]byte(resources), []byte(resourceSubstitutionPattern)) + resourceNamesMatches := regExp.FindAllStringSubmatch(resources, -1) + if resourceNamesMatches == nil { + return "", err + } + + var resourcesWithSafeURLs = strings.Clone(resources) + var resourceName string + var searchQS string + + for _, matches := range resourceNamesMatches { + resourceName = matches[1] + searchQS = strings.ReplaceAll(resourceName, " ", "+") + resourceSubstitutionPattern := fmt.Sprintf( + `- \[ \] %s [найти](/explore/%s?tab=&q=%s)`, + resourceName, + strings.ToLower(fieldName), + searchQS) - if resourcesWithLinks == nil { - return "", errors.New("not found matches in resources") + resourcesWithSafeURLs = strings.Replace(resourcesWithSafeURLs, resourceName, resourceSubstitutionPattern, -1) } - var renderedResourcesWithLinks string - renderedResourcesWithLinks, err = user.GetRenderedTextFieldByValue(ctx, repo, string(resourcesWithLinks)) + var renderedResourcesWithSafeURLs string + renderedResourcesWithSafeURLs, err = user.GetRenderedTextFieldByValue(ctx, repo, resourcesWithSafeURLs) - return renderedResourcesWithLinks, err + return renderedResourcesWithSafeURLs, err }