Skip to content

Commit

Permalink
minor: clean comments of <br> on the sides and all of <wbr>
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 19, 2023
1 parent 7dca443 commit 26c709d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions internal/fourchan/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func toThread(apiThread *api.Thread) *Thread {
// explodePost explodes based on "quotelink"
func explodePost(post Post) (posts []Post) {
findings := reQuote.FindAllString(post.comment, -1)
replies := reQuote.Split(post.comment, -1)
replies := cleanComments(reQuote.Split(post.comment, -1))

// Whole post is "not replying"
if len(findings) == 0 && len(replies) == 1 {
Expand All @@ -66,7 +66,7 @@ func explodePost(post Post) (posts []Post) {
return
}

// Add simple 1/1 reply/comment
// Add simple 1/1 reply/comment messages
if len(replies) == len(findings) && (len(replies) == 1 || !containsEmptyString(replies)) {
for i, reply := range replies {
parentId := getParentId(findings[i])
Expand Down Expand Up @@ -95,6 +95,24 @@ func explodePost(post Post) (posts []Post) {
return
}

func cleanComments(comments []string) (ret []string) {
for _, comment := range comments {
ret = append(ret, cleanComment(comment))
}
return
}

func cleanComment(comment string) string {
for strings.HasPrefix(comment, "<br>") {
comment = strings.TrimPrefix(comment, "<br>")
}
for strings.HasSuffix(comment, "<br>") {
comment = strings.TrimSuffix(comment, "<br>")
}
comment = strings.ReplaceAll(comment, "<wbr>", "") // NOTE: breaks links
return comment
}

func getParentId(finding string) int {
if strings.Contains(finding, `/g/thread/`) {
return 0
Expand Down
2 changes: 1 addition & 1 deletion internal/fourchan/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Post struct {
id int
parentId int
replies []Post
subject string
subject string // NOTE: mainly used for debug on testing
}

type Attachment struct {
Expand Down

0 comments on commit 26c709d

Please sign in to comment.