From 84ba0886cdf1da1b4aac90d992ef0d745d61e272 Mon Sep 17 00:00:00 2001 From: httpjamesm Date: Wed, 28 Dec 2022 22:48:02 -0500 Subject: [PATCH] feat: show answer comments under collapsible --- public/globals.css | 4 ++++ public/question.css | 31 +++++++++++++++++++++++------ src/routes/question.go | 45 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/public/globals.css b/public/globals.css index 4bca3f1..83b2785 100644 --- a/public/globals.css +++ b/public/globals.css @@ -47,3 +47,7 @@ html { border-radius: 50%; padding: .25rem; } + +details { + cursor: pointer; +} \ No newline at end of file diff --git a/public/question.css b/public/question.css index edf21bd..ace1a7a 100644 --- a/public/question.css +++ b/public/question.css @@ -8,7 +8,7 @@ body { background-color: var(--main-bg); color: var(--text-color); font-family: sans-serif; - + display: flex; justify-content: center; @@ -31,7 +31,7 @@ body { code { background-color: var(--code-bg); - padding: .15rem; + padding: 0.15rem; border-radius: 5px; color: white; } @@ -45,7 +45,6 @@ pre { line-height: 1.35; } - .timestamp { color: var(--muted-text-color); font-size: 0.8rem; @@ -85,10 +84,10 @@ img { .answer-author { width: fit-content; background-color: var(--meta-bg); - padding: .5rem; + padding: 0.5rem; border-radius: 5px; text-align: right; - font-size: .8rem; + font-size: 0.8rem; } .logo-link { @@ -100,9 +99,29 @@ img { height: 2rem; } +.comments-parent { + display: flex; + flex-direction: column; + gap: 1rem; + margin-top: 1rem; +} + +.comment-parent { + cursor: text; +} + +.comment-body { + font-size: 0.95rem; +} + +.comment-author { + color: var(--muted-text-color); + font-size: 0.8rem; +} + @media only screen and (max-width: 800px) { body { padding-left: 1rem; padding-right: 1rem; } -} \ No newline at end of file +} diff --git a/src/routes/question.go b/src/routes/question.go index a96fbf6..8d87e24 100644 --- a/src/routes/question.go +++ b/src/routes/question.go @@ -152,7 +152,7 @@ func ViewQuestion(c *gin.Context) { }) // append
Answered %s by %s
to the bottom of the answer - answerBodyHTML += fmt.Sprintf(`
Answered at %s by %s
`, answerTimestamp, answerAuthorURL, answerAuthorName) + answerBodyHTML += fmt.Sprintf(`
Answered %s by %s
`, answerTimestamp, answerAuthorURL, answerAuthorName) // parse any code blocks and highlight them answerCodeBlocks := codeBlockRegex.FindAllString(answerBodyHTML, -1) @@ -166,6 +166,49 @@ func ViewQuestion(c *gin.Context) { answerBodyHTML = strings.Replace(answerBodyHTML, codeBlock, highlightedCodeBlock, 1) } + comments := []string{} + + commentsComponent := postLayout.Find("div.js-post-comments-component") + + commentsList := commentsComponent.Find("div.comments") + commentsList2 := commentsList.Find("ul.comments-list") + + allComments := commentsList2.Find("li.comment") + + allComments.Each(func(i int, s *goquery.Selection) { + commentText := s.Find("div.comment-text") + + commentBody := commentText.Find("div.comment-body") + + commentCopy, err := commentBody.Find("span.comment-copy").Html() + if err != nil { + return + } + + commentAuthorURL := "" + + commentAuthor := commentBody.Find("span.comment-user") + if commentAuthor.Length() == 0 { + commentAuthor = commentBody.Find("a.comment-user") + if commentAuthor.Length() == 0 { + return + } + + commentAuthorURL = commentAuthor.AttrOr("href", "") + } + + commentTimestamp := commentBody.Find("span.relativetime-clean").Text() + + comment := fmt.Sprintf(`
%s
Commented %s by %s.
`, commentCopy, commentTimestamp, commentAuthorURL, commentAuthor.Text()) + + comments = append(comments, comment) + + }) + + if len(comments) > 0 { + answerBodyHTML += fmt.Sprintf(`
Show %d comments
%s
`, len(comments), strings.Join(comments, "")) + } + answers = append(answers, template.HTML(answerBodyHTML)) })