Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support JSON Feed 1.1 closes #91 #104

Merged
merged 1 commit into from
Sep 26, 2023
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
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ applications.

[atom]: https://tools.ietf.org/html/rfc4287
[rss]: http://www.rssboard.org/rss-specification
[jsonfeed]: https://jsonfeed.org/version/1
[jsonfeed]: https://jsonfeed.org/version/1.1

### Usage

Expand Down Expand Up @@ -151,13 +151,18 @@ Outputs:
</rss>

{
"version": "https://jsonfeed.org/version/1",
"version": "https://jsonfeed.org/version/1.1",
"title": "jmoiron.net blog",
"home_page_url": "http://jmoiron.net/blog",
"description": "discussion about tech, footie, photos",
"author": {
"name": "Jason Moiron"
},
"authors": [
{
"name": "Jason Moiron"
}
],
"items": [
{
"id": "",
Expand All @@ -167,7 +172,12 @@ Outputs:
"date_published": "2013-01-16T03:22:24.530817846-05:00",
"author": {
"name": "Jason Moiron"
}
},
"authors": [
{
"name": "Jason Moiron"
}
]
},
{
"id": "",
Expand Down
23 changes: 19 additions & 4 deletions feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,18 @@ var rssOutput = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:
</rss>`

var jsonOutput = `{
"version": "https://jsonfeed.org/version/1",
"version": "https://jsonfeed.org/version/1.1",
"title": "jmoiron.net blog",
"home_page_url": "http://jmoiron.net/blog",
"description": "discussion about tech, footie, photos",
"author": {
"name": "Jason Moiron"
},
"authors": [
{
"name": "Jason Moiron"
}
],
"items": [
{
"id": "",
Expand All @@ -138,7 +143,12 @@ var jsonOutput = `{
"date_published": "2013-01-16T21:52:35-05:00",
"author": {
"name": "Jason Moiron"
}
},
"authors": [
{
"name": "Jason Moiron"
}
]
},
{
"id": "",
Expand Down Expand Up @@ -369,13 +379,18 @@ var rssOutputSorted = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
</rss>`

var jsonOutputSorted = `{
"version": "https://jsonfeed.org/version/1",
"version": "https://jsonfeed.org/version/1.1",
"title": "jmoiron.net blog",
"home_page_url": "http://jmoiron.net/blog",
"description": "discussion about tech, footie, photos",
"author": {
"name": "Jason Moiron"
},
"authors": [
{
"name": "Jason Moiron"
}
],
"items": [
{
"id": "",
Expand Down Expand Up @@ -572,7 +587,7 @@ func TestJSONHub(t *testing.T) {
Version: "https://jsonfeed.org/version/1",
Title: "feed title",
Hubs: []*JSONHub{
&JSONHub{
{
Type: "WebSub",
Url: "https://websub-hub.example",
},
Expand Down
41 changes: 24 additions & 17 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

const jsonFeedVersion = "https://jsonfeed.org/version/1"
const jsonFeedVersion = "https://jsonfeed.org/version/1.1"

// JSONAuthor represents the author of the feed or of an individual item
// in the feed
Expand Down Expand Up @@ -77,7 +77,8 @@ type JSONItem struct {
BannerImage string `json:"banner_,omitempty"`
PublishedDate *time.Time `json:"date_published,omitempty"`
ModifiedDate *time.Time `json:"date_modified,omitempty"`
Author *JSONAuthor `json:"author,omitempty"`
Author *JSONAuthor `json:"author,omitempty"` // deprecated in JSON Feed v1.1, keeping for backwards compatibility
Authors []*JSONAuthor `json:"authors,omitempty"`
Tags []string `json:"tags,omitempty"`
Attachments []JSONAttachment `json:"attachments,omitempty"`
}
Expand All @@ -92,19 +93,21 @@ type JSONHub struct {
// JSONFeed represents a syndication feed in the JSON Feed Version 1 format.
// Matching the specification found here: https://jsonfeed.org/version/1.
type JSONFeed struct {
Version string `json:"version"`
Title string `json:"title"`
HomePageUrl string `json:"home_page_url,omitempty"`
FeedUrl string `json:"feed_url,omitempty"`
Description string `json:"description,omitempty"`
UserComment string `json:"user_comment,omitempty"`
NextUrl string `json:"next_url,omitempty"`
Icon string `json:"icon,omitempty"`
Favicon string `json:"favicon,omitempty"`
Author *JSONAuthor `json:"author,omitempty"`
Expired *bool `json:"expired,omitempty"`
Hubs []*JSONHub `json:"hubs,omitempty"`
Items []*JSONItem `json:"items,omitempty"`
Version string `json:"version"`
Title string `json:"title"`
Language string `json:"language,omitempty"`
HomePageUrl string `json:"home_page_url,omitempty"`
FeedUrl string `json:"feed_url,omitempty"`
Description string `json:"description,omitempty"`
UserComment string `json:"user_comment,omitempty"`
NextUrl string `json:"next_url,omitempty"`
Icon string `json:"icon,omitempty"`
Favicon string `json:"favicon,omitempty"`
Author *JSONAuthor `json:"author,omitempty"` // deprecated in JSON Feed v1.1, keeping for backwards compatibility
Authors []*JSONAuthor `json:"authors,omitempty"`
Expired *bool `json:"expired,omitempty"`
Hubs []*JSONHub `json:"hubs,omitempty"`
Items []*JSONItem `json:"items,omitempty"`
}

// JSON is used to convert a generic Feed to a JSONFeed.
Expand Down Expand Up @@ -139,9 +142,11 @@ func (f *JSON) JSONFeed() *JSONFeed {
feed.HomePageUrl = f.Link.Href
}
if f.Author != nil {
feed.Author = &JSONAuthor{
author := &JSONAuthor{
Name: f.Author.Name,
}
feed.Author = author
feed.Authors = []*JSONAuthor{author}
}
for _, e := range f.Items {
feed.Items = append(feed.Items, newJSONItem(e))
Expand All @@ -165,9 +170,11 @@ func newJSONItem(i *Item) *JSONItem {
item.ExternalUrl = i.Source.Href
}
if i.Author != nil {
item.Author = &JSONAuthor{
author := &JSONAuthor{
Name: i.Author.Name,
}
item.Author = author
item.Authors = []*JSONAuthor{author}
}
if !i.Created.IsZero() {
item.PublishedDate = &i.Created
Expand Down
Loading