From c3da3fd3887af675b3d76aeaa8205ce4edadfcbf Mon Sep 17 00:00:00 2001 From: Artur Galyamov Date: Thu, 3 Nov 2022 17:24:52 +0500 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5/=D0=B2=D1=8B=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=B5=D1=82=D0=B5=D0=BD?= =?UTF-8?q?=D1=86=D0=B8=D0=B9=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=BD=D0=BE?= =?UTF-8?q?=20#128?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit в сырое содержимое выводим преобразованную информацию с найти. При сохранении изменений в БД удаляем ссылку на найти. В сырое содержимое (rawContent) выводим ссылку на найти, иначе неправильно будут вычисляться data-source-position. Был альтернативный вариант, вычислять data-source-position на основании трансформированного содержимого (с найти), но сохранять в сырое содержимое изначальное, нетрансформированное, содержимое. --- routers/web/repo/competence.go | 19 +++++++++++++++++-- routers/web/repo/resource.go | 8 ++++---- templates/repo/competences/list.tmpl | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/routers/web/repo/competence.go b/routers/web/repo/competence.go index 2002cbc41b..4ad4f175a2 100644 --- a/routers/web/repo/competence.go +++ b/routers/web/repo/competence.go @@ -7,6 +7,7 @@ import ( "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "net/http" + "regexp" ) const ( @@ -18,7 +19,7 @@ func Competences(ctx *context.Context) { repo := ctx.Data["Repository"] var err error - ctx.Data["RenderedCompetences"], err = GetRenderedTrustPropsWithSearchLinks(ctx, repo, "Competences") + ctx.Data["RenderedCompetences"], ctx.Data["TransformedTrustProps"], err = GetRenderedTrustPropsWithSearchLinks(ctx, repo, "Competences") if err != nil { ctx.ServerError("Render", err) @@ -48,7 +49,12 @@ func ToggleCompetences(ctx *context.Context) { return } - repo.Competences = toggledContent + repo.Competences, err = trimSearchLinks(toggledContent) + if err != nil { + ctx.ServerError("trimSearchLinks", err) + return + } + err = repo_model.UpdateRepositoryCols(repo, "competences") if err != nil { ctx.ServerError("ToggleCompetences", err) @@ -64,3 +70,12 @@ func ToggleCompetences(ctx *context.Context) { responseBody, ) } + +func trimSearchLinks(content string) (string, error) { + regExp, err := regexp.Compile(`\s*\[найти\]\(.+\s*\)`) + if err != nil { + return "", err + } + trimmedSearchLink := regExp.ReplaceAllString(content, "") + return trimmedSearchLink, nil +} diff --git a/routers/web/repo/resource.go b/routers/web/repo/resource.go index bcf7f76595..a5ca592219 100644 --- a/routers/web/repo/resource.go +++ b/routers/web/repo/resource.go @@ -19,7 +19,7 @@ func Resources(ctx *context.Context) { repo := ctx.Data["Repository"] var err error - ctx.Data["RenderedResources"], err = GetRenderedTrustPropsWithSearchLinks(ctx, repo, "Resources") + ctx.Data["RenderedResources"], ctx.Data["TransformedTrustProps"], err = GetRenderedTrustPropsWithSearchLinks(ctx, repo, "Resources") if err != nil { ctx.ServerError("Render", err) @@ -29,7 +29,7 @@ func Resources(ctx *context.Context) { ctx.HTML(http.StatusOK, tplResources) } -func GetRenderedTrustPropsWithSearchLinks(ctx *context.Context, repo interface{}, fieldName string) (string, error) { +func GetRenderedTrustPropsWithSearchLinks(ctx *context.Context, repo interface{}, fieldName string) (string, string, error) { trustProps := user.GetTextField(repo, fieldName) var regExp *regexp.Regexp @@ -37,7 +37,7 @@ func GetRenderedTrustPropsWithSearchLinks(ctx *context.Context, repo interface{} regExp, err = regexp.Compile(`- \[ \] (.+)`) if err != nil { - return "", err + return "", "", err } trustPropNamesMatches := regExp.FindAllStringSubmatch(trustProps, -1) @@ -66,5 +66,5 @@ func GetRenderedTrustPropsWithSearchLinks(ctx *context.Context, repo interface{} var renderedTrustPropsWithSafeURLs string renderedTrustPropsWithSafeURLs, err = user.GetRenderedTextFieldByValue(ctx, repo, transformedTrustProps) renderedTrustPropsWithTargetBlank := strings.ReplaceAll(renderedTrustPropsWithSafeURLs, " {{$.RenderedCompetences | Str2html}} -
{{.Repository.Competences}}
+
{{$.TransformedTrustProps}}