Merge branch 'feat/1.0.8/tag' into test

This commit is contained in:
aichy126 2023-04-10 15:21:19 +08:00
commit 9945c9f96f
6 changed files with 30 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import (
"time"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/controller"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/pkg/converter"
"github.com/answerdev/answer/pkg/day"
@ -41,6 +42,9 @@ var funcMap = template.FuncMap{
"templateHTML": func(data string) template.HTML {
return template.HTML(data)
},
"formatLinkNofollow": func(data string) template.HTML {
return template.HTML(FormatLinkNofollow(data))
},
"translator": func(la i18n.Language, data string, params ...interface{}) string {
trans := translator.GlobalTrans.Tr(la, data)
@ -116,3 +120,21 @@ var funcMap = template.FuncMap{
return htmltext.UrlTitle(title)
},
}
func FormatLinkNofollow(html string) string {
var hrefRegexp = regexp.MustCompile("(?m)<a.*?[^<]>.*?</a>")
match := hrefRegexp.FindAllString(html, -1)
if match != nil {
for _, v := range match {
hasNofollow := strings.Contains(v, "rel=\"nofollow\"")
hasSiteUrl := strings.Contains(v, controller.SiteUrl)
if !hasSiteUrl {
if !hasNofollow {
nofollowUrl := strings.Replace(v, "<a", "<a rel=\"nofollow\"", 1)
html = strings.Replace(html, v, nofollowUrl, 1)
}
}
}
}
return html
}

View File

@ -23,6 +23,8 @@ import (
"github.com/segmentfault/pacman/log"
)
var SiteUrl = ""
type TemplateController struct {
scriptPath string
cssPath string
@ -67,6 +69,7 @@ func (tc *TemplateController) SiteInfo(ctx *gin.Context) *schema.TemplateSiteInf
if err != nil {
log.Error(err)
}
SiteUrl = resp.General.SiteUrl
resp.Interface, err = tc.siteInfoService.GetSiteInterface(ctx)
if err != nil {
log.Error(err)

View File

@ -37,6 +37,7 @@ func Markdown2HTML(source string) string {
filter.AllowStyling()
filter.RequireNoFollowOnLinks(false)
filter.RequireParseableURLs(false)
filter.RequireNoFollowOnFullyQualifiedLinks(false)
html = filter.Sanitize(html)
return html
}

View File

@ -3,7 +3,7 @@
<div class="border-bottom py-2 comment-item border-top">
<div class="d-block">
<div class="fmt fs-14">
{{templateHTML .ParsedText}}
{{formatLinkNofollow .ParsedText}}
</div>
</div>
<div class="d-flex justify-content-between fs-14">

View File

@ -28,7 +28,7 @@
{{end}}
</div>
<article class="fmt text-break text-wrap mt-4">
{{templateHTML .detail.HTML}}
{{formatLinkNofollow .detail.HTML}}
</article>
<div class="mt-4">
<div role="group" class="btn-group">
@ -98,7 +98,7 @@
{{range .answers}}
<div id="10020000000000930" class="answer-item py-4">
<article class="fmt">
{{templateHTML .HTML}}
{{formatLinkNofollow .HTML}}
</article>
<div class="d-flex align-items-center mt-4">
<div class="">

View File

@ -8,7 +8,7 @@
>{{$.tag.SlugName}}</a
>
</h3>
<p class="text-break">{{templateHTML $.tag.ParsedText}}</p>
<p class="text-break">{{formatLinkNofollow $.tag.ParsedText}}</p>
</div>
<div>
<div class="mb-3 d-flex flex-wrap justify-content-between">