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

add extra items support #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/__tests__/__snapshots__/atom1.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

exports[`atom 1.0 should generate a valid feed 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
<feed xmlns=\\"http://www.w3.org/2005/Atom\\">
<feed xmlns=\\"http://www.w3.org/2005/Atom\\" xmlns:dcterms=\\"https://purl.org/dc/terms/\\" xmlns:media=\\"https://search.yahoo.com/mrss/\\">
<id>http://example.com/</id>
<title>Feed Title</title>
<updated>2013-07-13T23:00:00.000Z</updated>
@@ -25,6 +25,8 @@ exports[`atom 1.0 should generate a valid feed 1`] = `
<email>[email protected]</email>
<uri>https://example.com/johancruyff</uri>
</contributor>
<media:content url=\\"https://v3spec.msn.com/image1.jpg\\" type=\\"image/jpeg\\" medium=\\"image\\"/>
<media:credit>Joe Gargery/Fabrikam Images</media:credit>
<entry>
<title type=\\"html\\"><![CDATA[Hello World]]></title>
<id>https://example.com/hello-world?id=this&amp;that=true</id>
@@ -58,6 +60,9 @@ exports[`atom 1.0 should generate a valid feed 1`] = `
<uri>https://example.com/reggiemiller</uri>
</contributor>
<published>2013-07-10T23:00:00.000Z</published>
<media:content url=\\"https://v3spec.msn.com/image1.jpg\\" type=\\"image/jpeg\\" medium=\\"image\\">
<media:credit>Joe Gargery/Fabrikam Images</media:credit>
</media:content>
</entry>
</feed>"
`;
28 changes: 24 additions & 4 deletions src/__tests__/__snapshots__/rss2.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

exports[`rss 2.0 should generate a valid feed 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
<rss version=\\"2.0\\" xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\">
<rss version=\\"2.0\\" xmlns:dcterms=\\"https://purl.org/dc/terms/\\" xmlns:media=\\"https://search.yahoo.com/mrss/\\" xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\">
<channel>
<title>Feed Title</title>
<link>http://example.com/</link>
@@ -20,6 +20,8 @@ exports[`rss 2.0 should generate a valid feed 1`] = `
<copyright>All rights reserved 2013, John Doe</copyright>
<category>Technology</category>
<atom:link href=\\"wss://example.com/\\" rel=\\"hub\\"/>
<media:content url=\\"https://v3spec.msn.com/image1.jpg\\" type=\\"image/jpeg\\" medium=\\"image\\"/>
<media:credit>Joe Gargery/Fabrikam Images</media:credit>
<item>
<title><![CDATA[Hello World]]></title>
<link>https://example.com/hello-world?link=sanitized&amp;value=2</link>
@@ -32,14 +34,17 @@ exports[`rss 2.0 should generate a valid feed 1`] = `
<category>Grateful Dead</category>
<category domain=\\"http://www.fool.com/cusips\\">MSFT</category>
<enclosure url=\\"https://example.com/hello-world.jpg\\" length=\\"0\\" type=\\"image/jpg\\"/>
<media:content url=\\"https://v3spec.msn.com/image1.jpg\\" type=\\"image/jpeg\\" medium=\\"image\\">
<media:credit>Joe Gargery/Fabrikam Images</media:credit>
</media:content>
</item>
</channel>
</rss>"
`;

exports[`rss 2.0 should generate a valid feed with audio 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
<rss version=\\"2.0\\" xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\">
<rss version=\\"2.0\\" xmlns:dcterms=\\"https://purl.org/dc/terms/\\" xmlns:media=\\"https://search.yahoo.com/mrss/\\" xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\">
<channel>
<title>Feed Title</title>
<link>http://example.com/</link>
@@ -57,6 +62,8 @@ exports[`rss 2.0 should generate a valid feed with audio 1`] = `
<copyright>All rights reserved 2013, John Doe</copyright>
<category>Technology</category>
<atom:link href=\\"wss://example.com/\\" rel=\\"hub\\"/>
<media:content url=\\"https://v3spec.msn.com/image1.jpg\\" type=\\"image/jpeg\\" medium=\\"image\\"/>
<media:credit>Joe Gargery/Fabrikam Images</media:credit>
<item>
<title><![CDATA[Hello World]]></title>
<link>https://example.com/hello-world?link=sanitized&amp;value=2</link>
@@ -69,6 +76,9 @@ exports[`rss 2.0 should generate a valid feed with audio 1`] = `
<category>Grateful Dead</category>
<category domain=\\"http://www.fool.com/cusips\\">MSFT</category>
<enclosure url=\\"https://example.com/hello-world.jpg\\" length=\\"0\\" type=\\"image/jpg\\"/>
<media:content url=\\"https://v3spec.msn.com/image1.jpg\\" type=\\"image/jpeg\\" medium=\\"image\\">
<media:credit>Joe Gargery/Fabrikam Images</media:credit>
</media:content>
</item>
<item>
<title><![CDATA[Hello World]]></title>
@@ -115,7 +125,7 @@ exports[`rss 2.0 should generate a valid feed with audio 1`] = `

exports[`rss 2.0 should generate a valid feed with enclosure 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
<rss version=\\"2.0\\" xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\">
<rss version=\\"2.0\\" xmlns:dcterms=\\"https://purl.org/dc/terms/\\" xmlns:media=\\"https://search.yahoo.com/mrss/\\" xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\">
<channel>
<title>Feed Title</title>
<link>http://example.com/</link>
@@ -133,6 +143,8 @@ exports[`rss 2.0 should generate a valid feed with enclosure 1`] = `
<copyright>All rights reserved 2013, John Doe</copyright>
<category>Technology</category>
<atom:link href=\\"wss://example.com/\\" rel=\\"hub\\"/>
<media:content url=\\"https://v3spec.msn.com/image1.jpg\\" type=\\"image/jpeg\\" medium=\\"image\\"/>
<media:credit>Joe Gargery/Fabrikam Images</media:credit>
<item>
<title><![CDATA[Hello World]]></title>
<link>https://example.com/hello-world?link=sanitized&amp;value=2</link>
@@ -145,6 +157,9 @@ exports[`rss 2.0 should generate a valid feed with enclosure 1`] = `
<category>Grateful Dead</category>
<category domain=\\"http://www.fool.com/cusips\\">MSFT</category>
<enclosure url=\\"https://example.com/hello-world.jpg\\" length=\\"0\\" type=\\"image/jpg\\"/>
<media:content url=\\"https://v3spec.msn.com/image1.jpg\\" type=\\"image/jpeg\\" medium=\\"image\\">
<media:credit>Joe Gargery/Fabrikam Images</media:credit>
</media:content>
</item>
<item>
<title><![CDATA[Hello World]]></title>
@@ -178,7 +193,7 @@ exports[`rss 2.0 should generate a valid feed with enclosure 1`] = `

exports[`rss 2.0 should generate a valid feed with image properties 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
<rss version=\\"2.0\\" xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\">
<rss version=\\"2.0\\" xmlns:dcterms=\\"https://purl.org/dc/terms/\\" xmlns:media=\\"https://search.yahoo.com/mrss/\\" xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\">
<channel>
<title>Feed Title</title>
<link>http://example.com/</link>
@@ -196,6 +211,8 @@ exports[`rss 2.0 should generate a valid feed with image properties 1`] = `
<copyright>All rights reserved 2013, John Doe</copyright>
<category>Technology</category>
<atom:link href=\\"wss://example.com/\\" rel=\\"hub\\"/>
<media:content url=\\"https://v3spec.msn.com/image1.jpg\\" type=\\"image/jpeg\\" medium=\\"image\\"/>
<media:credit>Joe Gargery/Fabrikam Images</media:credit>
<item>
<title><![CDATA[Hello World]]></title>
<link>https://example.com/hello-world?link=sanitized&amp;value=2</link>
@@ -208,6 +225,9 @@ exports[`rss 2.0 should generate a valid feed with image properties 1`] = `
<category>Grateful Dead</category>
<category domain=\\"http://www.fool.com/cusips\\">MSFT</category>
<enclosure url=\\"https://example.com/hello-world.jpg\\" length=\\"0\\" type=\\"image/jpg\\"/>
<media:content url=\\"https://v3spec.msn.com/image1.jpg\\" type=\\"image/jpeg\\" medium=\\"image\\">
<media:credit>Joe Gargery/Fabrikam Images</media:credit>
</media:content>
</item>
<item>
<title><![CDATA[Hello World]]></title>
28 changes: 28 additions & 0 deletions src/__tests__/setup.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,11 @@ export const sampleFeed = new Feed({
name: "John Doe",
email: "[email protected]",
link: "https://example.com/johndoe?link=sanitized&value=2"
},

namespaces: {
"xmlns:dcterms": "https://purl.org/dc/terms/",
"xmlns:media": "https://search.yahoo.com/mrss/"
}
});

@@ -97,6 +102,16 @@ sampleFeed.addItem({
image: "https://example.com/hello-world.jpg",
enclosure: { url: "https://example.com/hello-world.jpg", length: 12665, type: "image/jpeg" },
published,
extra: {
'media:content': {
_attributes: {
url: 'https://v3spec.msn.com/image1.jpg',
type: 'image/jpeg',
medium: 'image'
},
'media:credit': 'Joe Gargery/Fabrikam Images'
}
}
});

sampleFeed.addExtension({
@@ -106,3 +121,16 @@ sampleFeed.addExtension({
dummy: "example",
},
});

sampleFeed.addExtra(
'media:content',
{
_attributes: {
url: 'https://v3spec.msn.com/image1.jpg',
type: 'image/jpeg',
medium: 'image'
}
}
)

sampleFeed.addExtra('media:credit', 'Joe Gargery/Fabrikam Images')
16 changes: 16 additions & 0 deletions src/atom1.ts
Original file line number Diff line number Diff line change
@@ -22,6 +22,12 @@ export default (ins: Feed) => {
}
};


const namespaces = options.namespaces || {}
for (const key in namespaces) {
base.feed._attributes[key] = namespaces[key];
}

if (options.author) {
base.feed.author = formatAuthor(options.author);
}
@@ -79,6 +85,10 @@ export default (ins: Feed) => {

// icon

for (const key in ins.extra) {
base.feed[key] = ins.extra[key];
}

base.feed.entry = [];

/**************************************************************************
@@ -160,6 +170,12 @@ export default (ins: Feed) => {
entry.rights = item.copyright;
}

if (item.extra) {
for (const key in item.extra) {
entry[key] = item.extra[key];
}
}

base.feed.entry.push(entry);
});

10 changes: 9 additions & 1 deletion src/feed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import renderAtom from "./atom1";
import renderJSON from "./json";
import renderRSS from "./rss2";
import { Author, Extension, FeedOptions, Item } from "./typings";
import { Author, Extension, ExtraItem, FeedOptions, Item } from "./typings";

export { Author, Extension, FeedOptions, Item };

@@ -14,6 +14,7 @@ export class Feed {
categories: string[] = [];
contributors: Author[] = [];
extensions: Extension[] = [];
extra: { [key: string]: ExtraItem } = {};

constructor(options: FeedOptions) {
this.options = options;
@@ -43,6 +44,13 @@ export class Feed {
*/
public addExtension = (extension: Extension) => this.extensions.push(extension);

/**
* Adds an extra custom item
* @param key
* @param value
*/
public addExtra = (key: string, value: ExtraItem) => this.extra[key] = value;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExtraItem is just a xml-js object. Each user needs to make sure that the object conforms to xml-js specification.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm not sure how I should handle these extra items in the json format.

Copy link
Author

@yshrsmz yshrsmz Jul 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can simply ignore extra items when generating JSON? Just like extensions in atom1/rss2 format.


/**
* Returns a Atom 1.0 feed
*/
15 changes: 15 additions & 0 deletions src/rss2.ts
Original file line number Diff line number Diff line change
@@ -27,6 +27,11 @@ export default (ins: Feed) => {
},
};

const namespaces = options.namespaces || {}
for (const key in namespaces) {
base.rss._attributes[key] = namespaces[key];
}

/**
* Channel language
* https://validator.w3.org/feed/docs/rss2.html#ltlanguagegtSubelementOfLtchannelgt
@@ -109,6 +114,10 @@ export default (ins: Feed) => {
};
}

for (const key in ins.extra) {
base.rss.channel[key] = ins.extra[key];
}

/**
* Channel Categories
* https://validator.w3.org/feed/docs/rss2.html#hrelementsOfLtitemgt
@@ -193,6 +202,12 @@ export default (ins: Feed) => {
item.enclosure = formatEnclosure(entry.video, "video");
}

if (entry.extra) {
for (const key in entry.extra) {
item[key] = entry.extra[key];
}
}

base.rss.channel.item.push(item);
});

7 changes: 7 additions & 0 deletions src/typings/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Element, ElementCompact } from 'xml-js';

export interface Item {
title: string;
id?: string;
@@ -22,6 +24,8 @@ export interface Item {
copyright?: string;

extensions?: Extension[];

extra?: { [key: string]: ExtraItem };
}

export interface Enclosure {
@@ -52,6 +56,7 @@ export interface FeedOptions {
generator?: string;
language?: string;
ttl?: number;
namespaces?: Record<string, string>;

feed?: string;
feedLinks?: any;
@@ -70,3 +75,5 @@ export interface Extension {
name: string;
objects: any;
}

export type ExtraItem = Element | ElementCompact | string;