From 0f3ea7ad0bf660e38816f7bbb5da43e84ae8e04b Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 21 Sep 2022 22:15:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/detail.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/actions/detail.go b/actions/detail.go index e15d498..c81159f 100644 --- a/actions/detail.go +++ b/actions/detail.go @@ -72,7 +72,7 @@ func Detail(c *gin.Context) { h["post"] = post h["showComment"] = showComment h["prev"] = prev - h["comments"] = formatComment(commentss, 1) + h["comments"] = formatComment(commentss, 1, 5) h["next"] = next } @@ -95,9 +95,9 @@ func (c Comments) Swap(i, j int) { c[i], c[j] = c[j], c[i] } -func formatComment(comments Comments, depth int) (html string) { +func formatComment(comments Comments, depth, maxDepth int) (html string) { s := strings.Builder{} - if depth > 5 { + if depth > maxDepth { comments = findComments(comments) } sort.Sort(comments) @@ -107,9 +107,8 @@ func formatComment(comments Comments, depth int) (html string) { eo = "odd" } p := "" - fl := false - if len(comment.Children) > 0 && depth < 6 { + if len(comment.Children) > 0 && depth < maxDepth+1 { p = "parent" fl = true } @@ -117,7 +116,7 @@ func formatComment(comments Comments, depth int) (html string) { if fl { depth++ s.WriteString(`
    `) - s.WriteString(formatComment(comment.Children, depth)) + s.WriteString(formatComment(comment.Children, depth, maxDepth)) s.WriteString(`
`) } s.WriteString("") @@ -134,9 +133,9 @@ func formatComment(comments Comments, depth int) (html string) { func findComments(comments Comments) Comments { var r Comments for _, comment := range comments { - tmp := comment + tmp := *comment tmp.Children = nil - r = append(r, tmp) + r = append(r, &tmp) if len(comment.Children) > 0 { t := findComments(comment.Children) r = append(r, t...)