Skip to content

Commit

Permalink
feat: ghost card rewrite rule
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-walker committed Jan 11, 2025
1 parent 79ec6ef commit 54f3e71
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions internal/reader/rewrite/rewrite_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,31 @@ func removeTables(entryContent string) string {
output, _ := doc.FindMatcher(goquery.Single("body")).Html()
return output
}

func fixGhostCards(entryContent string) string {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
if err != nil {
return entryContent
}

doc.Find("figure.kg-card").Each(func(i int, s *goquery.Selection) {
title := s.Find(".kg-bookmark-title").First().Text()
author := s.Find(".kg-bookmark-author").First().Text()
href := s.Find("a.kg-bookmark-container").First().AttrOr("href", "")

fmt.Printf("%s - %s => %s", title, author, href)

if href == "" {
return
}

if author == "" || strings.HasSuffix(title, author) {
s.SetHtml(fmt.Sprintf("<a href=\"%s\">%s</a>", href, title))
} else {
s.SetHtml(fmt.Sprintf("<a href=\"%s\">%s - %s</a>", href, title, author))
}
})

output, _ := doc.FindMatcher(goquery.Single("body")).Html()
return output
}
2 changes: 2 additions & 0 deletions internal/reader/rewrite/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func (rule rule) applyRule(entryURL string, entry *model.Entry) {
entry.Content = removeTables(entry.Content)
case "remove_clickbait":
entry.Title = titlelize(entry.Title)
case "fix_ghost_cards":
entry.Content = fixGhostCards(entry.Content)
}
}

Expand Down

0 comments on commit 54f3e71

Please sign in to comment.