Skip to content

Commit a269cab

Browse files
committed
Fixes #52
1 parent 49b08a5 commit a269cab

File tree

8 files changed

+146
-67
lines changed

8 files changed

+146
-67
lines changed

.eleventy.js

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,13 @@
1-
const pkg = require("./package.json");
2-
const dateRfc3339 = require("./src/dateRfc3339");
3-
const dateRfc822 = require("./src/dateRfc822");
4-
const getNewestCollectionItemDate = require("./src/getNewestCollectionItemDate");
1+
const rssPlugin = require("./src/rssPlugin.js");
2+
const dateRfc3339 = require("./src/dateRfc3339.js");
3+
const dateRfc822 = require("./src/dateRfc822.js");
4+
const getNewestCollectionItemDate = require("./src/getNewestCollectionItemDate.js");
55
const virtualTemplate = require("./src/virtualTemplate.js");
66

7-
const absoluteUrl = require("./src/absoluteUrl");
8-
const convertHtmlToAbsoluteUrls = require("./src/htmlToAbsoluteUrls");
7+
const absoluteUrl = require("./src/absoluteUrl.js");
8+
const convertHtmlToAbsoluteUrls = require("./src/htmlToAbsoluteUrls.js");
99

10-
async function eleventyRssPlugin(eleventyConfig, options = {}) {
11-
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
12-
13-
// Guaranteed unique, first add wins
14-
const pluginHtmlBase = await eleventyConfig.resolvePlugin("@11ty/eleventy/html-base-plugin");
15-
eleventyConfig.addPlugin(pluginHtmlBase, options.htmlBasePluginOptions || {});
16-
17-
// Dates
18-
eleventyConfig.addNunjucksFilter("getNewestCollectionItemDate", getNewestCollectionItemDate);
19-
eleventyConfig.addNunjucksFilter("dateToRfc3339", dateRfc3339);
20-
eleventyConfig.addNunjucksFilter("dateToRfc822", dateRfc822);
21-
22-
// Deprecated in favor of the more efficient HTML <base> plugin bundled with Eleventy
23-
eleventyConfig.addNunjucksFilter("absoluteUrl", absoluteUrl);
24-
25-
// Deprecated in favor of the more efficient HTML <base> plugin bundled with Eleventy
26-
eleventyConfig.addNunjucksAsyncFilter("htmlToAbsoluteUrls", (htmlContent, base, callback) => {
27-
if(!htmlContent) {
28-
callback(null, "");
29-
return;
30-
}
31-
32-
let posthtmlOptions = Object.assign({
33-
// default PostHTML render options
34-
closingSingleTag: "slash"
35-
}, options.posthtmlRenderOptions);
36-
37-
convertHtmlToAbsoluteUrls(htmlContent, base, posthtmlOptions).then(html => {
38-
callback(null, html);
39-
});
40-
});
41-
42-
// These are removed, their names are incorrect! Issue #8, #21
43-
eleventyConfig.addNunjucksFilter("rssLastUpdatedDate", () => {
44-
throw new Error("The `rssLastUpdatedDate` filter was removed. Use `getNewestCollectionItemDate | dateToRfc3339` (for Atom) or `getNewestCollectionItemDate | dateToRfc822` (for RSS) instead.")
45-
});
46-
eleventyConfig.addNunjucksFilter("rssDate", () => {
47-
throw new Error("The `rssDate` filter was removed. Use `dateToRfc3339` (for Atom) or `dateToRfc822` (for RSS) instead.");
48-
});
49-
};
50-
51-
Object.defineProperty(eleventyRssPlugin, "eleventyPackage", {
52-
value: pkg.name
53-
});
54-
55-
Object.defineProperty(eleventyRssPlugin, "eleventyPluginOptions", {
56-
value: {
57-
unique: true
58-
}
59-
});
60-
61-
module.exports = eleventyRssPlugin;
10+
module.exports = rssPlugin;
6211
module.exports.feedPlugin = virtualTemplate;
6312
module.exports.dateToRfc3339 = dateRfc3339;
6413
module.exports.dateToRfc822 = dateRfc822;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"homepage": "https://www.11ty.dev/docs/plugins/rss/",
3636
"11ty": {
37-
"compatibility": ">=3.0.0-alpha.13"
37+
"compatibility": ">=3.0.0-alpha.15"
3838
},
3939
"devDependencies": {
4040
"ava": "^4.3.0"

sample/config-sample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const rssPlugin = require("../")
1+
const rssPlugin = require("../");
22

33
module.exports = function(eleventyConfig) {
44
// eleventyConfig.ignores.add("json.njk");

sample/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13-
"@11ty/eleventy": "3.0.0-alpha.13"
13+
"@11ty/eleventy": "3.0.0-alpha.16"
1414
}
1515
}

src/rssPlugin.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const pkg = require("../package.json");
2+
3+
const dateRfc3339 = require("./dateRfc3339.js");
4+
const dateRfc822 = require("./dateRfc822.js");
5+
const getNewestCollectionItemDate = require("./getNewestCollectionItemDate.js");
6+
7+
const absoluteUrl = require("./absoluteUrl.js");
8+
const convertHtmlToAbsoluteUrls = require("./htmlToAbsoluteUrls.js");
9+
10+
11+
function eleventyRssPlugin(eleventyConfig, options = {}) {
12+
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
13+
14+
// Guaranteed unique, first add wins
15+
const pluginHtmlBase = eleventyConfig.resolvePlugin("@11ty/eleventy/html-base-plugin");
16+
eleventyConfig.addPlugin(pluginHtmlBase, options.htmlBasePluginOptions || {});
17+
18+
// Dates
19+
eleventyConfig.addNunjucksFilter("getNewestCollectionItemDate", getNewestCollectionItemDate);
20+
eleventyConfig.addNunjucksFilter("dateToRfc3339", dateRfc3339);
21+
eleventyConfig.addNunjucksFilter("dateToRfc822", dateRfc822);
22+
23+
// Deprecated in favor of the more efficient HTML <base> plugin bundled with Eleventy
24+
eleventyConfig.addNunjucksFilter("absoluteUrl", absoluteUrl);
25+
26+
// Deprecated in favor of the more efficient HTML <base> plugin bundled with Eleventy
27+
eleventyConfig.addNunjucksAsyncFilter("htmlToAbsoluteUrls", (htmlContent, base, callback) => {
28+
if(!htmlContent) {
29+
callback(null, "");
30+
return;
31+
}
32+
33+
let posthtmlOptions = Object.assign({
34+
// default PostHTML render options
35+
closingSingleTag: "slash"
36+
}, options.posthtmlRenderOptions);
37+
38+
convertHtmlToAbsoluteUrls(htmlContent, base, posthtmlOptions).then(html => {
39+
callback(null, html);
40+
});
41+
});
42+
43+
// These are removed, their names are incorrect! Issue #8, #21
44+
eleventyConfig.addNunjucksFilter("rssLastUpdatedDate", () => {
45+
throw new Error("The `rssLastUpdatedDate` filter was removed. Use `getNewestCollectionItemDate | dateToRfc3339` (for Atom) or `getNewestCollectionItemDate | dateToRfc822` (for RSS) instead.")
46+
});
47+
eleventyConfig.addNunjucksFilter("rssDate", () => {
48+
throw new Error("The `rssDate` filter was removed. Use `dateToRfc3339` (for Atom) or `dateToRfc822` (for RSS) instead.");
49+
});
50+
};
51+
52+
Object.defineProperty(eleventyRssPlugin, "eleventyPackage", {
53+
value: pkg.name
54+
});
55+
56+
Object.defineProperty(eleventyRssPlugin, "eleventyPluginOptions", {
57+
value: {
58+
unique: true
59+
}
60+
});
61+
62+
module.exports = eleventyRssPlugin;

src/virtualTemplate.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const pkg = require("../package.json");
22
const { DeepCopy } = require("@11ty/eleventy-utils");
3+
4+
const rssPlugin = require("./rssPlugin.js");
5+
36
const debug = require("debug")("Eleventy:Rss:Feed");
47

58
function getFeedContent({ type, stylesheet, collection }) {
@@ -93,16 +96,15 @@ ${stylesheet ? `<?xml-stylesheet href="${stylesheet}" type="text/xsl"?>\n` : ""}
9396
throw new Error("Missing or invalid feed type. Received: " + type);
9497
}
9598

96-
async function eleventyFeedPlugin(eleventyConfig, options = {}) {
99+
function eleventyFeedPlugin(eleventyConfig, options = {}) {
97100
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
98101

99102
// Guaranteed unique, first add wins
100-
const pluginRss = require("../.eleventy.js");
101-
eleventyConfig.addPlugin(pluginRss, options.rssPluginOptions || {});
103+
const pluginHtmlBase = eleventyConfig.resolvePlugin("@11ty/eleventy/html-base-plugin");
104+
eleventyConfig.addPlugin(pluginHtmlBase, options.htmlBasePluginOptions || {});
102105

103106
// Guaranteed unique, first add wins
104-
const pluginHtmlBase = await eleventyConfig.resolvePlugin("@11ty/eleventy/html-base-plugin");
105-
eleventyConfig.addPlugin(pluginHtmlBase, options.htmlBasePluginOptions || {});
107+
eleventyConfig.addPlugin(rssPlugin, options.rssPluginOptions || {});
106108

107109
let slugifyFilter = eleventyConfig.getFilter("slugify");
108110
let inputPathSuffix = options?.metadata?.title ? `-${slugifyFilter(options?.metadata?.title)}` : "";
@@ -168,7 +170,6 @@ async function eleventyFeedPlugin(eleventyConfig, options = {}) {
168170
eleventyConfig.addTemplate(options.inputPath, getFeedContent(options), templateData);
169171
};
170172

171-
172173
Object.defineProperty(eleventyFeedPlugin, "eleventyPackage", {
173174
value: `${pkg.name}/feed-plugin`
174175
});

test/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "test",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "dateToRfc3339Test.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"@11ty/eleventy": "3.0.0-alpha.16"
14+
}
15+
}

test/virtualTemplatesTest.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const test = require("ava");
2+
const { feedPlugin } = require("../");
3+
4+
// https://github.com/11ty/eleventy-plugin-rss/issues/50
5+
test("RSS virtual templates plugin", async (t) => {
6+
const { default: Eleventy } = await import("@11ty/eleventy");
7+
8+
let elev = new Eleventy("./test", "./test/_site", {
9+
config: function (eleventyConfig) {
10+
eleventyConfig.addTemplate("virtual.md", `# Hello`, { tag: "posts" })
11+
12+
eleventyConfig.addPlugin(feedPlugin, {
13+
type: "atom", // or "rss", "json"
14+
outputPath: "/feed.xml",
15+
collection: {
16+
name: "posts", // iterate over `collections.posts`
17+
limit: 10, // 0 means no limit
18+
},
19+
});
20+
},
21+
});
22+
23+
let results = await elev.toJSON();
24+
25+
t.deepEqual(results.length, 2);
26+
let [ feed ] = results.filter(entry => entry.outputPath.endsWith(".xml"));
27+
t.truthy(feed.content.startsWith(`<?xml version="1.0" encoding="utf-8"?>`));
28+
});
29+
30+
test("RSS virtual templates plugin with `all`", async (t) => {
31+
const { default: Eleventy } = await import("@11ty/eleventy");
32+
33+
let elev = new Eleventy("./test", "./test/_site", {
34+
config: function (eleventyConfig) {
35+
eleventyConfig.addTemplate("virtual.md", `# Hello`, { tag: "posts" })
36+
37+
eleventyConfig.addPlugin(feedPlugin, {
38+
type: "atom", // or "rss", "json"
39+
outputPath: "/feed.xml",
40+
collection: {
41+
name: "all", // iterate over `collections.posts`
42+
},
43+
});
44+
},
45+
});
46+
47+
let results = await elev.toJSON();
48+
49+
t.deepEqual(results.length, 2);
50+
let [ feed ] = results.filter(entry => entry.outputPath.endsWith(".xml"));
51+
t.truthy(feed.content.startsWith(`<?xml version="1.0" encoding="utf-8"?>`));
52+
});

0 commit comments

Comments
 (0)