Skip to content

Commit a96c968

Browse files
authored
Merge pull request #24 from backlineint/rss-filters
Add filters for RSS Feeds
2 parents 0c4b8de + b369e0f commit a96c968

File tree

7 files changed

+116
-3
lines changed

7 files changed

+116
-3
lines changed

.eleventy.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const pkg = require("./package.json");
22
const dateRfc3339 = require("./src/dateRfc3339");
3+
const dateRfc822 = require("./src/dateRfc822");
34
const absoluteUrl = require("./src/absoluteUrl");
45
const convertHtmlToAbsoluteUrls = require("./src/htmlToAbsoluteUrls");
56
const getNewestCollectionItemDate = require("./src/getNewestCollectionItemDate");
@@ -32,6 +33,7 @@ module.exports = function(eleventyConfig, options = {}) {
3233
// Dates
3334
eleventyConfig.addNunjucksFilter("getNewestCollectionItemDate", getNewestCollectionItemDate);
3435
eleventyConfig.addNunjucksFilter("dateToRfc3339", dateRfc3339);
36+
eleventyConfig.addNunjucksFilter("dateToRfc822", dateRfc822);
3537

3638
// Deprecated, these names are incorrect! Issue #8
3739
eleventyConfig.addNunjucksFilter("rssLastUpdatedDate", collection => {
@@ -43,6 +45,7 @@ module.exports = function(eleventyConfig, options = {}) {
4345
};
4446

4547
module.exports.dateToRfc3339 = dateRfc3339;
48+
module.exports.dateToRfc822 = dateRfc822;
4649
module.exports.getNewestCollectionItemDate = getNewestCollectionItemDate;
4750
module.exports.absoluteUrl = absoluteUrl;
4851
module.exports.convertHtmlToAbsoluteUrls = convertHtmlToAbsoluteUrls;

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
# eleventy-plugin-rss 🕚⚡️🎈🐀
44

5-
A pack of [Eleventy](https://github.com/11ty/eleventy) filters for generating Atom and JSON feeds using the Nunjucks templating engine.
5+
A pack of [Eleventy](https://github.com/11ty/eleventy) filters for generating Atom, JSON and RSS feeds using the Nunjucks templating engine.
66

7-
_Note: the plugin is called RSS but does not technically include an example of an RSS feed. Generally feed readers that support RSS also support Atom. If you’d like to contribute an example, a pull request would be welcome!_
87

9-
See `sample/feed.njk` for an example Atom feed template or `sample/feed.json` for an example JSON feed template.
8+
See `sample/feed.njk` for an example Atom feed template, `sample/feed.json` for an example JSON feed template, or `sample/feed-rss.njk` for an example JSON feed template.
109

1110
## [The full `eleventy-plugin-rss` documentation is on 11ty.dev](https://www.11ty.dev/docs/plugins/rss/).
1211

sample/feed-rss.njk

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---json
2+
{
3+
"permalink": "feed-rss.xml",
4+
"eleventyExcludeFromCollections": true,
5+
"metadata": {
6+
"title": "My Blog about Boats",
7+
"subtitle": "I am writing about my experiences as a naval navel-gazer.",
8+
"url": "https://example.com/",
9+
"feedUrl": "https://example.com/feed-rss.xml",
10+
"author": {
11+
"name": "Boaty McBoatFace",
12+
"email": "[email protected]"
13+
}
14+
}
15+
}
16+
---
17+
<?xml version="1.0" encoding="utf-8"?>
18+
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:base="{{ metadata.url }}">
19+
<channel>
20+
<title>{{ metadata.title }}</title>
21+
<link>{{ metadata.url }}</link>
22+
<description>{{ metadata.subtitle }}</description>
23+
<language>en</language>
24+
{%- for post in collections.posts | reverse %}
25+
{% set absolutePostUrl %}{{ post.url | url | absoluteUrl(metadata.url) }}{% endset %}
26+
<item>
27+
<title>{{ post.data.title }}</title>
28+
<link>{{ absolutePostUrl }}</link>
29+
<description>{{ post.templateContent | htmlToAbsoluteUrls(absolutePostUrl) }}</description>
30+
<pubDate>{{ post.date | dateToRfc822 }}</pubDate>
31+
<dc:creator>{{ metadata.author.name }}</dc:creator>
32+
<guid>{{ absolutePostUrl }}</guid>
33+
</item>
34+
{%- endfor %}
35+
</channel>
36+
</rss>

src/dateRfc822.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const decompose = require("./decomposeDate");
2+
3+
module.exports = function(date) {
4+
const { dayName, day, monthName, year, hours, minutes, seconds } = decompose(
5+
date
6+
);
7+
return `${dayName}, ${day} ${monthName} ${year} ${hours}:${minutes}:${seconds} +0000`;
8+
};

src/decomposeDate.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const pad = require("./padDate");
2+
3+
const shortDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
4+
5+
const shortMonthNames = [
6+
'Jan',
7+
'Feb',
8+
'Mar',
9+
'Apr',
10+
'May',
11+
'Jun',
12+
'Jul',
13+
'Aug',
14+
'Sep',
15+
'Oct',
16+
'Nov',
17+
'Dec'
18+
];
19+
20+
/**
21+
* Given a Date object returns decomposed values
22+
* @param {Date} date
23+
* @returns {Object}
24+
*/
25+
module.exports = function(date) {
26+
const day = pad(date.getDate(), 2);
27+
28+
const dayName = shortDayNames[date.getDay()];
29+
const month = pad(date.getMonth() + 1, 2);
30+
const monthName = shortMonthNames[date.getMonth()];
31+
const year = date.getFullYear();
32+
33+
const hours = pad(date.getHours(), 2);
34+
const minutes = pad(date.getMinutes(), 2);
35+
const seconds = pad(date.getSeconds(), 2);
36+
37+
return {
38+
day,
39+
dayName,
40+
month,
41+
monthName,
42+
year,
43+
hours,
44+
minutes,
45+
seconds
46+
};
47+
};

src/padDate.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Receives a number and a desired length, returns a padded string
3+
* @method pad
4+
* @param {Number} number
5+
* @param {Number} length
6+
* @returns {String}
7+
*/
8+
module.exports = function(number, length) {
9+
let result = number + '';
10+
while (result.length < length) result = '0' + result;
11+
return result;
12+
};

test/dateToRfc822.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const test = require("ava");
2+
const dateRfc822 = require("../src/dateRfc822.js");
3+
4+
test('parses date correctly', (t) => {
5+
const date = new Date(2015, 9, 21, 7, 28, 2, 450);
6+
const parsedDate = dateRfc822(date);
7+
t.is(parsedDate, 'Wed, 21 Oct 2015 07:28:02 +0000');
8+
});

0 commit comments

Comments
 (0)