From 599d23544b4f707b3c69a8eb43ffd8ade9961a82 Mon Sep 17 00:00:00 2001 From: httpjamesm Date: Wed, 28 Dec 2022 22:54:13 -0500 Subject: [PATCH] feat: unify comments finder, show post comments style: show post notices in a clean layout --- public/question.css | 8 ++++++ src/routes/question.go | 47 ++++------------------------------ src/utils/comments.go | 58 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 42 deletions(-) create mode 100644 src/utils/comments.go diff --git a/public/question.css b/public/question.css index ace1a7a..c66c8d8 100644 --- a/public/question.css +++ b/public/question.css @@ -119,6 +119,14 @@ img { font-size: 0.8rem; } +.s-notice { + background-color: var(--meta-bg); + padding: 1rem; + border-radius: 5px; + color: var(--text-color); + margin-bottom: 1rem; +} + @media only screen and (max-width: 800px) { body { padding-left: 1rem; diff --git a/src/routes/question.go b/src/routes/question.go index 8d87e24..22e5eab 100644 --- a/src/routes/question.go +++ b/src/routes/question.go @@ -42,6 +42,8 @@ func ViewQuestion(c *gin.Context) { questionText := questionTextParent.Children().First().Text() + questionPostLayout := doc.Find("div.post-layout").First() + questionBodyParent := doc.Find("div.s-prose") questionBodyParentHTML, err := questionBodyParent.Html() @@ -49,6 +51,8 @@ func ViewQuestion(c *gin.Context) { panic(err) } + questionBodyParentHTML = utils.FindAndReturnComments(questionBodyParentHTML, questionPostLayout) + // parse any code blocks and highlight them answerCodeBlocks := questionCodeBlockRegex.FindAllString(questionBodyParentHTML, -1) for _, codeBlock := range answerCodeBlocks { @@ -166,48 +170,7 @@ 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, "")) - } + answerBodyHTML = utils.FindAndReturnComments(answerBodyHTML, postLayout) answers = append(answers, template.HTML(answerBodyHTML)) }) diff --git a/src/utils/comments.go b/src/utils/comments.go new file mode 100644 index 0000000..ede9c16 --- /dev/null +++ b/src/utils/comments.go @@ -0,0 +1,58 @@ +package utils + +import ( + "fmt" + "strings" + + "github.com/PuerkitoBio/goquery" +) + +func FindAndReturnComments(inHtml string, postLayout *goquery.Selection) (outHtml string) { + outHtml = inHtml + + 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 { + outHtml = inHtml + fmt.Sprintf(`
Show %d comments
%s
`, len(comments), strings.Join(comments, "")) + } + + return + +}