Browse Source

fix(jsonfeed): stop the title fallback loop at the first non-empty value

Frédéric Guillot 2 months ago
parent
commit
33b780d9c0
2 changed files with 20 additions and 17 deletions
  1. 2 0
      internal/reader/json/adapter.go
  2. 18 17
      internal/reader/json/parser_test.go

+ 2 - 0
internal/reader/json/adapter.go

@@ -83,8 +83,10 @@ 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
 				}
 			}
 		}

+ 18 - 17
internal/reader/json/parser_test.go

@@ -5,7 +5,6 @@ package json // import "miniflux.app/v2/internal/reader/json"
 
 import (
 	"bytes"
-	"strings"
 	"testing"
 	"time"
 )
@@ -662,7 +661,7 @@ func TestParseItemWithInvalidDate(t *testing.T) {
 	}
 }
 
-func TestParseItemWithoutTitleButWithURL(t *testing.T) {
+func TestParseItemWithMissingTitleUsesSummaryFallback(t *testing.T) {
 	data := `{
 		"version": "https://jsonfeed.org/version/1",
 		"title": "My Example Feed",
@@ -670,7 +669,9 @@ func TestParseItemWithoutTitleButWithURL(t *testing.T) {
 		"feed_url": "https://example.org/feed.json",
 		"items": [
 			{
-				"url": "https://example.org/item"
+				"summary": "Summary title",
+				"content_text": "Content text title",
+				"content_html": "<p>HTML title</p>"
 			}
 		]
 	}`
@@ -684,12 +685,12 @@ func TestParseItemWithoutTitleButWithURL(t *testing.T) {
 		t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
 	}
 
-	if feed.Entries[0].Title != "https://example.org/item" {
+	if feed.Entries[0].Title != "Summary title" {
 		t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
 	}
 }
 
-func TestParseItemWithoutTitleButWithSummary(t *testing.T) {
+func TestParseItemWithMissingTitleUsesContentTextFallback(t *testing.T) {
 	data := `{
 		"version": "https://jsonfeed.org/version/1",
 		"title": "My Example Feed",
@@ -697,7 +698,9 @@ func TestParseItemWithoutTitleButWithSummary(t *testing.T) {
 		"feed_url": "https://example.org/feed.json",
 		"items": [
 			{
-				"summary": "This is some text content."
+				"summary": " ",
+				"content_text": "Content text title",
+				"content_html": "<p>HTML title</p>"
 			}
 		]
 	}`
@@ -711,12 +714,12 @@ func TestParseItemWithoutTitleButWithSummary(t *testing.T) {
 		t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
 	}
 
-	if feed.Entries[0].Title != "This is some text content." {
+	if feed.Entries[0].Title != "Content text title" {
 		t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
 	}
 }
 
-func TestParseItemWithoutTitleButWithHTMLContent(t *testing.T) {
+func TestParseItemWithMissingTitleUsesHTMLFallback(t *testing.T) {
 	data := `{
 		"version": "https://jsonfeed.org/version/1",
 		"title": "My Example Feed",
@@ -724,7 +727,9 @@ func TestParseItemWithoutTitleButWithHTMLContent(t *testing.T) {
 		"feed_url": "https://example.org/feed.json",
 		"items": [
 			{
-				"content_html": "This is <strong>HTML</strong>."
+				"summary": "",
+				"content_text": "",
+				"content_html": "<p>HTML title</p>"
 			}
 		]
 	}`
@@ -738,12 +743,12 @@ func TestParseItemWithoutTitleButWithHTMLContent(t *testing.T) {
 		t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
 	}
 
-	if feed.Entries[0].Title != "This is HTML." {
+	if feed.Entries[0].Title != "HTML title" {
 		t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
 	}
 }
 
-func TestParseItemWithoutTitleButWithTextContent(t *testing.T) {
+func TestParseItemWithMissingTitleUsesURLFallback(t *testing.T) {
 	data := `{
 		"version": "https://jsonfeed.org/version/1",
 		"title": "My Example Feed",
@@ -751,7 +756,7 @@ func TestParseItemWithoutTitleButWithTextContent(t *testing.T) {
 		"feed_url": "https://example.org/feed.json",
 		"items": [
 			{
-				"content_text": "` + strings.Repeat("a", 200) + `"
+				"url": "https://example.org/item"
 			}
 		]
 	}`
@@ -765,11 +770,7 @@ func TestParseItemWithoutTitleButWithTextContent(t *testing.T) {
 		t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries))
 	}
 
-	if len(feed.Entries[0].Title) != 103 {
-		t.Errorf("Incorrect entry title, got: %d", len(feed.Entries[0].Title))
-	}
-
-	if len([]rune(feed.Entries[0].Title)) != 101 {
+	if feed.Entries[0].Title != "https://example.org/item" {
 		t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title)
 	}
 }