Support JSON feed version 1.1 - #247
Conversation
| if (options.author.avatar) { | ||
| feed.author.avatar = options.author.avatar; | ||
| } | ||
| feed.authors = toArray(options.author).map((author) => ({ |
There was a problem hiding this comment.
| feed.authors = toArray(options.author).map((author) => ({ | |
| feed.authors = (Array.isArray(options.author) ? options.author : [options.author]).map((author) => ({ |
There was a problem hiding this comment.
I've switched the implementation to use both a author and authors property for the feed options, but I've essentially made this change, it's just checking for the different properties instead of checking if one is an array
| if (options.author && Array.isArray(options.author)) { | ||
| options.author = options.author[0]; | ||
| } |
There was a problem hiding this comment.
I'm skeptical this is valid - can you provide a reference?
There was a problem hiding this comment.
After researching this, multiple authors in RSS is a huge mess. It took me a while to notice that this library only renders feed author in the case of podcasts. Because of that, I think it's enough to fallback to the first authors in case multiple are specified.
| if (item.author) { | ||
| let author: Author | Author[] = item.author; | ||
| if (author instanceof Array) { | ||
| // json feed only supports 1 author per post | ||
| author = author[0]; | ||
| } | ||
| feedItem.author = {}; | ||
| if (author.name) { | ||
| feedItem.author.name = author.name; | ||
| } | ||
| if (author.link) { | ||
| feedItem.author.url = author.link; | ||
| } | ||
| if (author.avatar) { | ||
| feedItem.author.avatar = author.avatar; | ||
| } | ||
| feedItem.authors = item.author.map((author) => ({ | ||
| name: author.name, | ||
| url: author.link, | ||
| avatar: author.avatar, | ||
| })); |
There was a problem hiding this comment.
Could you preserve existing behavior if 1.0 is used, to be backward compatible? Same for the one at the top-level.
There was a problem hiding this comment.
I think I've done this. item.author is already an array and I've updated my changes so that feed.author is always a single author. For multiple authors, I've added an FeedOptions.authors property like you suggested in a comment below. So if you decide to merge this it should be able to come out as a minor, because the library's API is the same as before and behavior is the same for the same inputs.
Unless I'm misunderstanding and you mean we should have options for generating both a JSON Feed 1.0 and 1.1. In that sense, my changes aren't backwards compatible, because they always create a 1.1 feed. I assumed readers generally support 1.1 since it's almost 6 years old now, but I might be wrong.
| if (options.author) { | ||
| base.feed.author = formatAuthor(options.author); | ||
| base.feed.author = formatAuthor(Array.isArray(options.author) ? options.author[0] : options.author); |
There was a problem hiding this comment.
I'd expect this change to be covered in tests?
There was a problem hiding this comment.
You're right, sorry. I realized Atom supports multiple authors so that's what I implemented instead, and I added a test to verify that my implementation works. Let me know what you think
| category?: string; | ||
|
|
||
| author?: Author; | ||
| author?: Author | Author[]; |
There was a problem hiding this comment.
| author?: Author | Author[]; | |
| /** | |
| * @deprecated Use authors instead. This will be removed in next major. | |
| */ | |
| author?: Author; | |
| authors?: Author[]; |
There was a problem hiding this comment.
This is way nicer than what I did originally. I changed the deprecation comment a bit, but otherwise I've done this
| export const createSampleFeed = ({ | ||
| authors, | ||
| itemAuthors, | ||
| }: Partial<{ authors: FeedOptions["author"]; itemAuthors: Item["author"] }> = {}) => { |
There was a problem hiding this comment.
As noted in a previous comment, I ended up keeping FeedOptions.author the same and added a FeedOptions.authors property. If I'm understanding correctly, that keeps the library API the same.
I hope it's okay that I added some optional helper arguments to the createSampleFeed function to make some tests nicer, which is what is present in this diff. Those arguments now reflect FeedOptions["authors"]
| id?: string; | ||
| link: string; | ||
| date: Date; | ||
| language?: string; |
There was a problem hiding this comment.
Atom seems to support this as well, so please make sure it's added to the entries.
Could you also do a loose validation that it is ISO 639-1? Combination of lowercase + /^[a-z]{2}(-[a-z]{2})?$/.test(code).
There was a problem hiding this comment.
Good point, thanks. I added a validateLanguageCode utility to be used when adding language to JSON feeds and the xml:lang attribute in Atom.
|
@Kiwow Thanks for the contribution - please address the review. |
Conflicts: src/__tests__/__snapshots__/json.spec.ts.snap src/json.ts
Thanks for the review @jpmonette! Sorry about the time it took me to respond. I've found some time for open source again, so I should be more responsive now. I changed the PR quite a bit based on your feedback. Please let me know what you think / if I misunderstood anything. There's also a few seemingly unrelated changes caused by the move to vitest and biome. P.S. Don't be fooled by GitHub saying I added the new commits five days ago. I just pushed them |
Closes #246
Thanks for your hard work maintaining this! I had a go at implementing support for the JSON feed version 1.1 spec.
I've added:
"authors"field, because the spec has deprecated"author"and recommends only using the array field."language"field for both feeds and itemsFeedOptions.authorto include an array of authors. When multiple feed authors are provided, I've made it so that only the first one is used in case of RSS and Atom. This could be addressed in the future, although I'm not sure about how/if RSS and Atom support multiple feed authorsI'm not sure if provided contributors should be merged into the authors array, but I haven't done so yet. In the (current) version 1 implementation, contributors are ignored for JSON feed as well. If it makes sense to you to merge contributors in, I don't have a problem with adding that bit.