mirror of
https://github.com/miniflux/v2.git
synced 2025-09-30 19:22:11 +00:00
fix(jsonfeed): fallback to external_url
when url
is missing
This commit is contained in:
parent
79b0d0b9cc
commit
ff07f02716
2 changed files with 66 additions and 4 deletions
|
@ -68,11 +68,16 @@ func (j *JSONAdapter) BuildFeed(baseURL string) *model.Feed {
|
|||
for _, item := range j.jsonFeed.Items {
|
||||
entry := model.NewEntry()
|
||||
entry.Title = strings.TrimSpace(item.Title)
|
||||
entry.URL = strings.TrimSpace(item.URL)
|
||||
|
||||
// Make sure the entry URL is absolute.
|
||||
if entryURL, err := urllib.AbsoluteURL(feed.SiteURL, entry.URL); err == nil {
|
||||
entry.URL = entryURL
|
||||
for _, itemURL := range []string{item.URL, item.ExternalURL} {
|
||||
itemURL = strings.TrimSpace(itemURL)
|
||||
if itemURL != "" {
|
||||
// Make sure the entry URL is absolute.
|
||||
if entryURL, err := urllib.AbsoluteURL(feed.SiteURL, itemURL); err == nil {
|
||||
entry.URL = entryURL
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// The entry title is optional, so we need to find a fallback.
|
||||
|
|
|
@ -415,6 +415,63 @@ func TestParseItemWithRelativeURL(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestParseItemWithExternalURLAndNoURL(t *testing.T) {
|
||||
data := `{
|
||||
"version": "https://jsonfeed.org/version/1",
|
||||
"title": "Example",
|
||||
"home_page_url": "https://example.org/",
|
||||
"feed_url": "https://example.org/feed.json",
|
||||
"items": [
|
||||
{
|
||||
"id": "1234259",
|
||||
"external_url": "some_page.html"
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(feed.Entries) != 1 {
|
||||
t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
|
||||
}
|
||||
|
||||
if feed.Entries[0].URL != "https://example.org/some_page.html" {
|
||||
t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseItemWithExternalURLAndURL(t *testing.T) {
|
||||
data := `{
|
||||
"version": "https://jsonfeed.org/version/1",
|
||||
"title": "Example",
|
||||
"home_page_url": "https://example.org/",
|
||||
"feed_url": "https://example.org/feed.json",
|
||||
"items": [
|
||||
{
|
||||
"id": "1234259",
|
||||
"url": "https://example.org/article",
|
||||
"external_url": "https://example.org/another-article"
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
feed, err := Parse("https://example.org/feed.json", bytes.NewBufferString(data))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(feed.Entries) != 1 {
|
||||
t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries))
|
||||
}
|
||||
|
||||
if feed.Entries[0].URL != "https://example.org/article" {
|
||||
t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseItemWithLegacyAuthorField(t *testing.T) {
|
||||
data := `{
|
||||
"version": "https://jsonfeed.org/version/1",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue