Skip to content

Commit

Permalink
add support for extensions in json1
Browse files Browse the repository at this point in the history
  • Loading branch information
alx committed Jul 17, 2017
1 parent 3f46291 commit 29a7b90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Feed {
this.items = []
this.categories = []
this.contributors = []
this.extensions = []
}

addItem(item) {
Expand All @@ -24,6 +25,10 @@ class Feed {
this.contributors.push(contributor)
}

addExtension(extension) {
this.extensions.push(extension)
}

render(format) {
console.warn('DEPRECATED: use atom1() or rss2() instead of render()');
if (format === 'atom-1.0') {
Expand Down Expand Up @@ -377,7 +382,7 @@ class Feed {
}

json1() {
const { options, items } = this
const { options, items, extensions } = this
let feed = {
version: 'https://jsonfeed.org/version/1',
title: options.title,
Expand Down Expand Up @@ -409,6 +414,11 @@ class Feed {
feed.author.url = options.author.link;
}
}

extensions.forEach(e => {
feed[e.name] = e.objects;
});

feed.items = items.map(item => {
let feedItem = {
id: item.id,
Expand Down
12 changes: 12 additions & 0 deletions src/feed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ feed.addItem({
image: 'https://example.com/hello-world.jpg'
})

feed.addExtension({
name: '_example_extension',
objects: {
about: 'just an extension example',
dummy: 'example',
}
});

test('it should generate an RSS 2.0 feed', () => {
let expected = `<?xml version=\"1.0\" encoding=\"utf-8\"?>
<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">
Expand Down Expand Up @@ -169,6 +177,10 @@ test('it should generate a JSON v1 Feed', () => {
"title": "Hello World",
"url": "https://example.com/hello-world"
}],
"_example_extension": {
"about": "just an extension example",
"dummy": "example"
},
"title": "Feed Title",
"version": "https://jsonfeed.org/version/1"
};
Expand Down

0 comments on commit 29a7b90

Please sign in to comment.