This commit is contained in:
xing 2022-09-21 22:15:20 +08:00
parent 00b09ac1f9
commit 0f3ea7ad0b

View File

@ -72,7 +72,7 @@ func Detail(c *gin.Context) {
h["post"] = post h["post"] = post
h["showComment"] = showComment h["showComment"] = showComment
h["prev"] = prev h["prev"] = prev
h["comments"] = formatComment(commentss, 1) h["comments"] = formatComment(commentss, 1, 5)
h["next"] = next h["next"] = next
} }
@ -95,9 +95,9 @@ func (c Comments) Swap(i, j int) {
c[i], c[j] = c[j], c[i] 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{} s := strings.Builder{}
if depth > 5 { if depth > maxDepth {
comments = findComments(comments) comments = findComments(comments)
} }
sort.Sort(comments) sort.Sort(comments)
@ -107,9 +107,8 @@ func formatComment(comments Comments, depth int) (html string) {
eo = "odd" eo = "odd"
} }
p := "" p := ""
fl := false fl := false
if len(comment.Children) > 0 && depth < 6 { if len(comment.Children) > 0 && depth < maxDepth+1 {
p = "parent" p = "parent"
fl = true fl = true
} }
@ -117,7 +116,7 @@ func formatComment(comments Comments, depth int) (html string) {
if fl { if fl {
depth++ depth++
s.WriteString(`<ol class="children">`) s.WriteString(`<ol class="children">`)
s.WriteString(formatComment(comment.Children, depth)) s.WriteString(formatComment(comment.Children, depth, maxDepth))
s.WriteString(`</ol>`) s.WriteString(`</ol>`)
} }
s.WriteString("</li><!-- #comment-## -->") s.WriteString("</li><!-- #comment-## -->")
@ -134,9 +133,9 @@ func formatComment(comments Comments, depth int) (html string) {
func findComments(comments Comments) Comments { func findComments(comments Comments) Comments {
var r Comments var r Comments
for _, comment := range comments { for _, comment := range comments {
tmp := comment tmp := *comment
tmp.Children = nil tmp.Children = nil
r = append(r, tmp) r = append(r, &tmp)
if len(comment.Children) > 0 { if len(comment.Children) > 0 {
t := findComments(comment.Children) t := findComments(comment.Children)
r = append(r, t...) r = append(r, t...)