diff --git a/internal/reader/rewrite/rewrite_functions.go b/internal/reader/rewrite/rewrite_functions.go index 878167c1444..e128a22a416 100644 --- a/internal/reader/rewrite/rewrite_functions.go +++ b/internal/reader/rewrite/rewrite_functions.go @@ -462,22 +462,48 @@ func fixGhostCards(entryContent string) string { return entryContent } - doc.Find("figure.kg-card").Each(func(i int, s *goquery.Selection) { + const cardSelector = "figure.kg-card" + var currentList *goquery.Selection + + doc.Find(cardSelector).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 == "" { + // if there is no link or title, skip processing + if href == "" || title == "" { return } + link := "" if author == "" || strings.HasSuffix(title, author) { - s.ReplaceWithHtml(fmt.Sprintf("%s", href, title)) + link = fmt.Sprintf("%s", href, title) + } else { + link = fmt.Sprintf("%s - %s", href, title, author) + } + + next := s.Next() + + // if the next element is also a card, start a list + if next.Is(cardSelector) && currentList == nil { + currentList = s.BeforeHtml("
This separates the two cards
+ `, + } + + controlEntry := &model.Entry{ + Title: `A title`, + Content: `Example Article 1 - Example +This separates the two cards
+ Example Article 2 - Example`, + } + Rewriter("https://example.org/article", testEntry, `fix_ghost_cards`) + + if !reflect.DeepEqual(testEntry, controlEntry) { + t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry) + } +}