Skip to content

Commit 4b913e3

Browse files
authored
Add stylesheet support to rss and atom (#216)
1 parent 6745292 commit 4b913e3

7 files changed

Lines changed: 122 additions & 1 deletion

File tree

src/__tests__/__snapshots__/atom1.spec.ts.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,23 @@ exports[`atom 1.0 should generate a valid feed 1`] = `
6363
</entry>
6464
</feed>"
6565
`;
66+
67+
exports[`atom 1.0 should generate a valid feed with stylesheet 1`] = `
68+
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?><?xml-stylesheet href=\\"https://exmaple.com/rss.xsl\\" type=\\"text/xsl\\"?>
69+
<feed xmlns=\\"http://www.w3.org/2005/Atom\\">
70+
<id>http://example.com/</id>
71+
<title>Feed Title</title>
72+
<updated>2013-07-13T23:00:00.000Z</updated>
73+
<generator>https://github.com/jpmonette/feed</generator>
74+
<author>
75+
<name>John Doe</name>
76+
<email>johndoe@example.com</email>
77+
<uri>https://example.com/johndoe</uri>
78+
</author>
79+
<link rel=\\"alternate\\" href=\\"http://example.com/\\"/>
80+
<link rel=\\"hub\\" href=\\"wss://example.com/\\"/>
81+
<subtitle>This is my personnal feed!</subtitle>
82+
<logo>http://example.com/image.png</logo>
83+
<rights>All rights reserved 2013, John Doe</rights>
84+
</feed>"
85+
`;

src/__tests__/__snapshots__/rss2.spec.ts.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,29 @@ exports[`rss 2.0 should generate a valid feed with image properties 1`] = `
710710
</rss>"
711711
`;
712712
713+
exports[`rss 2.0 should generate a valid feed with stylesheet 1`] = `
714+
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?><?xml-stylesheet href=\\"https://exmaple.com/rss.xsl\\" type=\\"text/xsl\\"?>
715+
<rss version=\\"2.0\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\">
716+
<channel>
717+
<title>Feed Title</title>
718+
<link>http://example.com/</link>
719+
<description>This is my personnal feed!</description>
720+
<lastBuildDate>Sat, 13 Jul 2013 23:00:00 GMT</lastBuildDate>
721+
<docs>https://validator.w3.org/feed/docs/rss2.html</docs>
722+
<generator>https://github.com/jpmonette/feed</generator>
723+
<language>en</language>
724+
<ttl>60</ttl>
725+
<image>
726+
<title>Feed Title</title>
727+
<url>http://example.com/image.png</url>
728+
<link>http://example.com/</link>
729+
</image>
730+
<copyright>All rights reserved 2013, John Doe</copyright>
731+
<atom:link href=\\"wss://example.com/\\" rel=\\"hub\\"/>
732+
</channel>
733+
</rss>"
734+
`;
735+
713736
exports[`rss 2.0 should generate a valid feed with video 1`] = `
714737
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
715738
<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\\">

src/__tests__/atom1.spec.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1-
import { sampleFeed } from "./setup";
1+
import { Feed } from "../feed";
2+
import { sampleFeed, updated } from "./setup";
23

34
describe("atom 1.0", () => {
45
it("should generate a valid feed", () => {
56
const actual = sampleFeed.atom1();
67
expect(actual).toMatchSnapshot();
78
});
9+
10+
it("should generate a valid feed with stylesheet", () => {
11+
const sampleFeed = new Feed({
12+
title: "Feed Title",
13+
description: "This is my personnal feed!",
14+
link: "http://example.com/",
15+
stylesheet: "https://exmaple.com/rss.xsl",
16+
id: "http://example.com/",
17+
language: "en",
18+
ttl: 60,
19+
image: "http://example.com/image.png",
20+
copyright: "All rights reserved 2013, John Doe",
21+
hub: "wss://example.com/",
22+
updated, // optional, default = today
23+
24+
author: {
25+
name: "John Doe",
26+
email: "johndoe@example.com",
27+
link: "https://example.com/johndoe",
28+
},
29+
});
30+
const actual = sampleFeed.atom1();
31+
expect(actual).toMatchSnapshot();
32+
});
833
});

src/__tests__/rss2.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,28 @@ describe("rss 2.0", () => {
329329
const actual = sampleFeed.rss2();
330330
expect(actual).toMatchSnapshot();
331331
});
332+
333+
it("should generate a valid feed with stylesheet", () => {
334+
const sampleFeed = new Feed({
335+
title: "Feed Title",
336+
description: "This is my personnal feed!",
337+
link: "http://example.com/",
338+
stylesheet: "https://exmaple.com/rss.xsl",
339+
id: "http://example.com/",
340+
language: "en",
341+
ttl: 60,
342+
image: "http://example.com/image.png",
343+
copyright: "All rights reserved 2013, John Doe",
344+
hub: "wss://example.com/",
345+
updated, // optional, default = today
346+
347+
author: {
348+
name: "John Doe",
349+
email: "johndoe@example.com",
350+
link: "https://example.com/johndoe",
351+
},
352+
});
353+
const actual = sampleFeed.rss2();
354+
expect(actual).toMatchSnapshot();
355+
});
332356
});

src/atom1.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default (ins: Feed) => {
1313

1414
const base: any = {
1515
_declaration: { _attributes: { version: "1.0", encoding: "utf-8" } },
16+
_instruction: {},
1617
feed: {
1718
_attributes: { xmlns: "http://www.w3.org/2005/Atom" },
1819
id: options.id,
@@ -22,6 +23,19 @@ export default (ins: Feed) => {
2223
},
2324
};
2425

26+
if (options.stylesheet) {
27+
base._instruction = {
28+
"xml-stylesheet": {
29+
_attributes: {
30+
href: options.stylesheet,
31+
type: "text/xsl",
32+
},
33+
},
34+
};
35+
} else {
36+
delete base._instruction;
37+
}
38+
2539
if (options.author) {
2640
base.feed.author = formatAuthor(options.author);
2741
}

src/rss2.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default (ins: Feed) => {
1414

1515
const base: any = {
1616
_declaration: { _attributes: { version: "1.0", encoding: "utf-8" } },
17+
_instruction: {},
1718
rss: {
1819
_attributes: { version: "2.0" },
1920
channel: {
@@ -27,6 +28,19 @@ export default (ins: Feed) => {
2728
},
2829
};
2930

31+
if (options.stylesheet) {
32+
base._instruction = {
33+
"xml-stylesheet": {
34+
_attributes: {
35+
href: options.stylesheet,
36+
type: "text/xsl",
37+
},
38+
},
39+
};
40+
} else {
41+
delete base._instruction;
42+
}
43+
3044
/**
3145
* Channel language
3246
* https://validator.w3.org/feed/docs/rss2.html#ltlanguagegtSubelementOfLtchannelgt

src/typings/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface FeedOptions {
5353
generator?: string;
5454
language?: string;
5555
ttl?: number;
56+
stylesheet?: string;
5657

5758
feed?: string;
5859
feedLinks?: any;

0 commit comments

Comments
 (0)