From ae0a4f0731c430f7bc254b1b167f666c19cfbfe7 Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 30 Jan 2023 20:48:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=B4=E5=83=8F=E6=98=AF=E5=90=A6=E4=BD=BF?= =?UTF-8?q?=E7=94=A8https?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/plugins/comment.go | 23 ++++++++++++++----- .../theme/twentyseventeen/twentyseventeen.go | 4 ++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/internal/plugins/comment.go b/internal/plugins/comment.go index edf61ca..d3e3cff 100644 --- a/internal/plugins/comment.go +++ b/internal/plugins/comment.go @@ -4,6 +4,7 @@ import ( "github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/gin-gonic/gin" + "net/url" "strconv" "strings" ) @@ -13,6 +14,7 @@ type CommentHandler struct { comments []*Comments maxDepth int depth int + isTls bool i CommentHtml } @@ -23,16 +25,25 @@ type Comments struct { type CommentHtml interface { Sort(i, j *Comments) bool - FormatLi(c *gin.Context, m models.Comments, depth int, eo, parent string) string + FormatLi(c *gin.Context, m models.Comments, depth int, isTls bool, eo, parent string) string } func FormatComments(c *gin.Context, i CommentHtml, comments []models.Comments, maxDepth int) string { tree := treeComments(comments) + u := c.Request.Header.Get("Referer") + var isTls bool + if u != "" { + uu, _ := url.Parse(u) + if uu.Scheme == "https" { + isTls = true + } + } h := CommentHandler{ Context: c, comments: tree, maxDepth: maxDepth, depth: 1, + isTls: isTls, i: i, } return h.formatComment(h.comments, true) @@ -55,7 +66,7 @@ func (d CommentHandler) formatComment(comments []*Comments, isTop bool) (html st parent = "parent" fl = true } - s.WriteString(d.i.FormatLi(d.Context, comment.Comments, d.depth, eo, parent)) + s.WriteString(d.i.FormatLi(d.Context, comment.Comments, d.depth, d.isTls, eo, parent)) if fl { d.depth++ s.WriteString(`
    `) @@ -130,8 +141,8 @@ func (c CommonCommentFormat) Sort(i, j *Comments) bool { return i.CommentDate.UnixNano() < j.CommentDate.UnixNano() } -func (c CommonCommentFormat) FormatLi(ctx *gin.Context, m models.Comments, depth int, eo, parent string) string { - return FormatLi(CommonLi(), ctx, m, depth, eo, parent) +func (c CommonCommentFormat) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls bool, eo, parent string) string { + return FormatLi(CommonLi(), ctx, m, depth, isTls, eo, parent) } var li = ` @@ -171,11 +182,11 @@ var li = ` ` -func FormatLi(li string, c *gin.Context, comments models.Comments, depth int, eo, parent string) string { +func FormatLi(li string, c *gin.Context, comments models.Comments, depth int, isTls bool, eo, parent string) string { for k, v := range map[string]string{ "{{CommentId}}": strconv.FormatUint(comments.CommentId, 10), "{{Depth}}": strconv.Itoa(depth), - "{{Gravatar}}": Gravatar(comments.CommentAuthorEmail, c.Request.TLS != nil), + "{{Gravatar}}": Gravatar(comments.CommentAuthorEmail, isTls), "{{CommentAuthorUrl}}": comments.CommentAuthorUrl, "{{CommentAuthor}}": comments.CommentAuthor, "{{PostId}}": strconv.FormatUint(comments.CommentPostId, 10), diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index b93f6ac..48e934e 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -110,7 +110,7 @@ type comment struct { plugins.CommonCommentFormat } -func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, eo, parent string) string { +func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls bool, eo, parent string) string { templ := plugins.CommonLi() templ = strings.ReplaceAll(templ, `回复`) - return plugins.FormatLi(templ, ctx, m, depth, eo, parent) + return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent) } func postThumbnail(posts []models.Posts, scene int) []models.Posts {