From cee8cea08d2aee58cc725e917f59b4b8f5e087e4 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 | 26 ++++++++++++++++++++ internal/reader/rewrite/rewriter.go | 2 ++ 2 files changed, 28 insertions(+) diff --git a/internal/reader/rewrite/rewrite_functions.go b/internal/reader/rewrite/rewrite_functions.go index 1b48eb9b388..b8f4a5cc857 100644 --- a/internal/reader/rewrite/rewrite_functions.go +++ b/internal/reader/rewrite/rewrite_functions.go @@ -455,3 +455,29 @@ 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", "") + + 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) } }