8000 [pull] main from miniflux:main by pull[bot] · Pull Request #310 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions internal/reader/json/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ func (j *JSONAdapter) BuildFeed(baseURL string) *model.Feed {
// The entry title is optional, so we need to find a fallback.
if entry.Title == "" {
for _, value := range []string{item.Summary, item.ContentText, item.ContentHTML} {
value = strings.TrimSpace(value)
if value != "" {
entry.Title = sanitizer.TruncateHTML(value, 100)
break
if value = sanitizer.TruncateHTML(value, 100); value == "" {
continue
}

entry.Title = value
break
}
}

Expand Down
10 changes: 3 additions & 7 deletions internal/reader/rdf/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type rdfAdapter struct {

func (r *rdfAdapter) buildFeed(baseURL string) *model.Feed {
feed := &model.Feed{
Title: stripTags(r.rdf.Channel.Title),
Title: sanitizer.StripTags(r.rdf.Channel.Title),
FeedURL: strings.TrimSpace(baseURL),
SiteURL: strings.TrimSpace(r.rdf.Channel.Link),
Description: strings.TrimSpace(r.rdf.Channel.Description),
Expand Down Expand Up @@ -95,17 +95,13 @@ func (r *rdfAdapter) buildFeed(baseURL string) *model.Feed {
// Populate the entry author.
switch {
case item.DublinCoreCreator != "":
entry.Author = stripTags(item.DublinCoreCreator)
entry.Author = sanitizer.StripTags(item.DublinCoreCreator)
case r.rdf.Channel.DublinCoreCreator != "":
entry.Author = stripTags(r.rdf.Channel.DublinCoreCreator)
entry.Author = sanitizer.StripTags(r.rdf.Channel.DublinCoreCreator)
}

feed.Entries = append(feed.Entries, entry)
}

return feed
}

func stripTags(value string) string {
return strings.TrimSpace(sanitizer.StripTags(value))
}
4 changes: 2 additions & 2 deletions internal/reader/rss/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func findFeedAuthor(rssChannel *rssChannel) string {
return ""
}

return strings.TrimSpace(sanitizer.StripTags(author))
return sanitizer.StripTags(author)
}

func findFeedTags(rssChannel *rssChannel) []string {
Expand Down Expand Up @@ -296,7 +296,7 @@ func findEntryAuthor(rssItem *rssItem) string {
return ""
}

return strings.TrimSpace(sanitizer.StripTags(author))
return sanitizer.StripTags(author)
}

func findEntryTags(rssItem *rssItem) []string {
Expand Down
2 changes: 1 addition & 1 deletion internal/reader/sanitizer/strip_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func StripTags(input string) string {
return ""
}

return dst.String()
return strings.TrimSpace(dst.String())
}

// stripIter iterates over the input [io.Reader] and calls the yield function for each [html.TextToken].
Expand Down
2 changes: 1 addition & 1 deletion internal/reader/sanitizer/strip_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "testing"

func TestStripTags(t *testing.T) {
input := `This <a href="/test.html">link is relative</a> and <strong>this</strong> image: <img src="../folder/image.png"/>`
expected := `This link is relative and this image: `
expected := `This link is relative and this image:`
output := StripTags(input)

if expected != output {
Expand Down
83 changes: 48 additions & 35 deletions internal/reader/subscription/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,47 +125,60 @@ func (f *subscriptionFinder) FindSubscriptions(websiteURL, rssBridgeURL string,
}

func (f *subscriptionFinder) findSubscriptionsFromWebPage(websiteURL string, doc *goquery.Document) (Subscriptions, *locale.LocalizedErrorWrapper) {
queries := map[string]string{
"link[type='application/rss+xml']": parser.FormatRSS,
"link[type='application/atom+xml']": parser.FormatAtom,
"link[type='application/feed+json']": parser.FormatJSON,

// Ignore JSON feed URLs that contain "/wp-json/" to avoid confusion
// with WordPress REST API endpoints.
"link[type='application/json']:not([href*='/wp-json/'])": parser.FormatJSON,
}

var subscriptions Subscriptions
subscriptionURLs := make(map[string]bool, len(queries))
for feedQuerySelector, feedFormat := range queries {
doc.Find(feedQuerySelector).Each(func(i int, s *goquery.Selection) {
subscription := new(subscription)
subscription.Type = feedFormat

if feedURL, exists := s.Attr("href"); exists && feedURL != "" {
var err error
subscription.URL, err = urllib.ResolveToAbsoluteURL(websiteURL, feedURL)
if err != nil {
return
}
} else {
return // without an url, there can be no subscription.
// There are 4 possible feed formats
subscriptionURLs := make(map[string]bool, 4)

// Single DOM walk over every <link> with a type attribute, then dispatch on
// the MIME type. This is better than doing a separate goquery.Find pass per
// type.
doc.Find("link[type]").Each(func(_ int, s *goquery.Selection) {
typeAttr, _ := s.Attr("type")
var feedFormat string
switch typeAttr {
case "application/rss+xml":
feedFormat = parser.FormatRSS
case "application/atom+xml":
feedFormat = parser.FormatAtom
case "application/feed+json":
feedFormat = parser.FormatJSON
case "application/json":
// Ignore JSON feed URLs that contain "/wp-json/" to avoid confusion
// with WordPress REST API endpoints.
if href, _ := s.Attr("href"); strings.Contains(href, "/wp-json/") {
return
}
feedFormat = parser.FormatJSON
default:
return
}

if title, exists := s.Attr("title"); exists {
subscription.Title = title
}
feedURL, _ := s.Attr("href")
if feedURL == "" {
return // without an url, there can be no subscription.
}

if subscription.Title == "" {
subscription.Title = subscription.URL
}
absoluteURL, err := urllib.ResolveToAbsoluteURL(websiteURL, feedURL)
if err != nil {
return
}

if !subscriptionURLs[subscription.URL] {
subscriptionURLs[subscription.URL] = true
subscriptions = append(subscriptions, subscription)
}
if subscriptionURLs[absoluteURL] {
return
}
subscriptionURLs[absoluteURL] = true

title, _ := s.Attr("title")
if title == "" {
title = absoluteURL
}

subscriptions = append(subscriptions, &subscription{
Type: feedFormat,
Title: title,
URL: absoluteURL,
})
}
})

return subscriptions, nil
}
Expand Down
Loading
0