From da59f8af8d6bff50753ce544f07d9ce2b77831d0 Mon Sep 17 00:00:00 2001 From: Artur Galyamov Date: Fri, 16 Sep 2022 21:49:03 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D0=B1=D0=BE=D0=BB=D0=B5=D0=B5=20=D1=82?= =?UTF-8?q?=D1=80=D1=83=D0=B4=D0=BD=D1=8B=D0=B9=20=D1=81=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B0=D0=B9,=20=D0=BA=D0=BE=D0=B3=D0=B4=D0=B0=20=D0=B2=20?= =?UTF-8?q?=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D0=B8=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D0=B8=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D1=81=D0=B2=D0=BE=D0=B9=D1=81=D1=82=D0=B2=D0=B0?= =?UTF-8?q?=20=D0=B5=D1=81=D1=82=D1=8C=20=D0=BF=D1=80=D0=BE=D0=B1=D0=B5?= =?UTF-8?q?=D0=BB=20#65?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit трудность в том, что нужно учесть это в URL. Сделать путём регулярных выражений НЕ получилось. Т.к. непонятно, как сделать глобальную замену содержимого обратных ссылок. --- routers/web/repo/resource.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) 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 }