From 54f3e7121f535fcf6d8daa6ea280d0017cec83aa Mon Sep 17 00:00:00 2001 From: Jake Walker Date: Sat, 11 Jan 2025 20:11:02 +0000 Subject: [PATCH] feat: ghost card rewrite rule --- internal/reader/rewrite/rewrite_functions.go | 28 ++++++++++++++++++++ internal/reader/rewrite/rewriter.go | 2 ++ 2 files changed, 30 insertions(+) diff --git a/internal/reader/rewrite/rewrite_functions.go b/internal/reader/rewrite/rewrite_functions.go index 1b48eb9b388..45cc8799349 100644 --- a/internal/reader/rewrite/rewrite_functions.go +++ b/internal/reader/rewrite/rewrite_functions.go @@ -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("%s", href, title)) + } else { + s.SetHtml(fmt.Sprintf("%s - %s", href, title, author)) + } + }) + + output, _ := doc.FindMatcher(goquery.Single("body")).Html() + return output +} diff --git a/internal/reader/rewrite/rewriter.go b/internal/reader/rewrite/rewriter.go index e2c26b6c6f9..35395ac9c8a 100644 --- a/internal/reader/rewrite/rewriter.go +++ b/internal/reader/rewrite/rewriter.go @@ -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) } }