mirror of
				https://github.com/nkanaev/yarr.git
				synced 2025-10-30 22:43:29 +00:00 
			
		
		
		
	show article content in the list if the title is missing
This commit is contained in:
		| @@ -4,6 +4,7 @@ import ( | |||||||
| 	"bytes" | 	"bytes" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"strings" | 	"strings" | ||||||
|  | 	"unicode" | ||||||
|  |  | ||||||
| 	"golang.org/x/net/html" | 	"golang.org/x/net/html" | ||||||
| ) | ) | ||||||
| @@ -61,3 +62,16 @@ func ExtractText(content string) string { | |||||||
| 	text = whitespaceRegex.ReplaceAllLiteralString(text, " ") | 	text = whitespaceRegex.ReplaceAllLiteralString(text, " ") | ||||||
| 	return text | 	return text | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TruncateText(input string, size int) string { | ||||||
|  | 	runes := []rune(input) | ||||||
|  | 	if len(runes) <= size { | ||||||
|  | 		return input | ||||||
|  | 	} | ||||||
|  | 	for i := size - 1; i > 0; i-- { | ||||||
|  | 		if unicode.IsSpace(runes[i]) { | ||||||
|  | 			return string(runes[:i]) + " ..." | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return input | ||||||
|  | } | ||||||
|   | |||||||
| @@ -24,3 +24,21 @@ func TestExtractText(t *testing.T) { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestTruncateText(t *testing.T) { | ||||||
|  | 	input := "Lorem ipsum — классический текст-«рыба»" | ||||||
|  |  | ||||||
|  | 	size := 30 | ||||||
|  | 	want := "Lorem ipsum — классический ..." | ||||||
|  | 	have := TruncateText(input, size) | ||||||
|  | 	if want != have { | ||||||
|  | 		t.Errorf("\nsize: %d\nwant: %#v\nhave: %#v", size, want, have) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	size = 1000 | ||||||
|  | 	want = input | ||||||
|  | 	have = TruncateText(input, size) | ||||||
|  | 	if want != have { | ||||||
|  | 		t.Errorf("\nsize: %d\nwant: %#v\nhave: %#v", size, want, have) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
| @@ -371,12 +371,19 @@ func (s *Server) handleItemList(c *router.Context) { | |||||||
| 		} | 		} | ||||||
| 		newestFirst := query.Get("oldest_first") != "true" | 		newestFirst := query.Get("oldest_first") != "true" | ||||||
|  |  | ||||||
| 		items := s.db.ListItems(filter, perPage+1, newestFirst, false) | 		items := s.db.ListItems(filter, perPage+1, newestFirst, true) | ||||||
| 		hasMore := false | 		hasMore := false | ||||||
| 		if len(items) == perPage+1 { | 		if len(items) == perPage+1 { | ||||||
| 			hasMore = true | 			hasMore = true | ||||||
| 			items = items[:perPage] | 			items = items[:perPage] | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		for i, item := range items { | ||||||
|  | 			if item.Title == "" { | ||||||
|  | 				text := htmlutil.ExtractText(item.Content) | ||||||
|  | 				items[i].Title = htmlutil.TruncateText(text, 140) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
| 		c.JSON(http.StatusOK, map[string]interface{}{ | 		c.JSON(http.StatusOK, map[string]interface{}{ | ||||||
| 			"list":     items, | 			"list":     items, | ||||||
| 			"has_more": hasMore, | 			"has_more": hasMore, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user