Skip to content

Commit

Permalink
Fixes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jul 12, 2024
1 parent 49b08a5 commit a269cab
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 67 deletions.
65 changes: 7 additions & 58 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,13 @@
const pkg = require("./package.json");
const dateRfc3339 = require("./src/dateRfc3339");
const dateRfc822 = require("./src/dateRfc822");
const getNewestCollectionItemDate = require("./src/getNewestCollectionItemDate");
const rssPlugin = require("./src/rssPlugin.js");
const dateRfc3339 = require("./src/dateRfc3339.js");
const dateRfc822 = require("./src/dateRfc822.js");
const getNewestCollectionItemDate = require("./src/getNewestCollectionItemDate.js");
const virtualTemplate = require("./src/virtualTemplate.js");

const absoluteUrl = require("./src/absoluteUrl");
const convertHtmlToAbsoluteUrls = require("./src/htmlToAbsoluteUrls");
const absoluteUrl = require("./src/absoluteUrl.js");
const convertHtmlToAbsoluteUrls = require("./src/htmlToAbsoluteUrls.js");

async function eleventyRssPlugin(eleventyConfig, options = {}) {
eleventyConfig.versionCheck(pkg["11ty"].compatibility);

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

// Dates
eleventyConfig.addNunjucksFilter("getNewestCollectionItemDate", getNewestCollectionItemDate);
eleventyConfig.addNunjucksFilter("dateToRfc3339", dateRfc3339);
eleventyConfig.addNunjucksFilter("dateToRfc822", dateRfc822);

// Deprecated in favor of the more efficient HTML <base> plugin bundled with Eleventy
eleventyConfig.addNunjucksFilter("absoluteUrl", absoluteUrl);

// Deprecated in favor of the more efficient HTML <base> plugin bundled with Eleventy
eleventyConfig.addNunjucksAsyncFilter("htmlToAbsoluteUrls", (htmlContent, base, callback) => {
if(!htmlContent) {
callback(null, "");
return;
}

let posthtmlOptions = Object.assign({
// default PostHTML render options
closingSingleTag: "slash"
}, options.posthtmlRenderOptions);

convertHtmlToAbsoluteUrls(htmlContent, base, posthtmlOptions).then(html => {
callback(null, html);
});
});

// These are removed, their names are incorrect! Issue #8, #21
eleventyConfig.addNunjucksFilter("rssLastUpdatedDate", () => {
throw new Error("The `rssLastUpdatedDate` filter was removed. Use `getNewestCollectionItemDate | dateToRfc3339` (for Atom) or `getNewestCollectionItemDate | dateToRfc822` (for RSS) instead.")
});
eleventyConfig.addNunjucksFilter("rssDate", () => {
throw new Error("The `rssDate` filter was removed. Use `dateToRfc3339` (for Atom) or `dateToRfc822` (for RSS) instead.");
});
};

Object.defineProperty(eleventyRssPlugin, "eleventyPackage", {
value: pkg.name
});

Object.defineProperty(eleventyRssPlugin, "eleventyPluginOptions", {
value: {
unique: true
}
});

module.exports = eleventyRssPlugin;
module.exports = rssPlugin;
module.exports.feedPlugin = virtualTemplate;
module.exports.dateToRfc3339 = dateRfc3339;
module.exports.dateToRfc822 = dateRfc822;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://www.11ty.dev/docs/plugins/rss/",
"11ty": {
"compatibility": ">=3.0.0-alpha.13"
"compatibility": ">=3.0.0-alpha.15"
},
"devDependencies": {
"ava": "^4.3.0"
Expand Down
2 changes: 1 addition & 1 deletion sample/config-sample.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const rssPlugin = require("../")
const rssPlugin = require("../");

module.exports = function(eleventyConfig) {
// eleventyConfig.ignores.add("json.njk");
Expand Down
2 changes: 1 addition & 1 deletion sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"@11ty/eleventy": "3.0.0-alpha.13"
"@11ty/eleventy": "3.0.0-alpha.16"
}
}
62 changes: 62 additions & 0 deletions src/rssPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const pkg = require("../package.json");

const dateRfc3339 = require("./dateRfc3339.js");
const dateRfc822 = require("./dateRfc822.js");
const getNewestCollectionItemDate = require("./getNewestCollectionItemDate.js");

const absoluteUrl = require("./absoluteUrl.js");
const convertHtmlToAbsoluteUrls = require("./htmlToAbsoluteUrls.js");


function eleventyRssPlugin(eleventyConfig, options = {}) {
eleventyConfig.versionCheck(pkg["11ty"].compatibility);

// Guaranteed unique, first add wins
const pluginHtmlBase = eleventyConfig.resolvePlugin("@11ty/eleventy/html-base-plugin");
eleventyConfig.addPlugin(pluginHtmlBase, options.htmlBasePluginOptions || {});

// Dates
eleventyConfig.addNunjucksFilter("getNewestCollectionItemDate", getNewestCollectionItemDate);
eleventyConfig.addNunjucksFilter("dateToRfc3339", dateRfc3339);
eleventyConfig.addNunjucksFilter("dateToRfc822", dateRfc822);

// Deprecated in favor of the more efficient HTML <base> plugin bundled with Eleventy
eleventyConfig.addNunjucksFilter("absoluteUrl", absoluteUrl);

// Deprecated in favor of the more efficient HTML <base> plugin bundled with Eleventy
eleventyConfig.addNunjucksAsyncFilter("htmlToAbsoluteUrls", (htmlContent, base, callback) => {
if(!htmlContent) {
callback(null, "");
return;
}

let posthtmlOptions = Object.assign({
// default PostHTML render options
closingSingleTag: "slash"
}, options.posthtmlRenderOptions);

convertHtmlToAbsoluteUrls(htmlContent, base, posthtmlOptions).then(html => {
callback(null, html);
});
});

// These are removed, their names are incorrect! Issue #8, #21
eleventyConfig.addNunjucksFilter("rssLastUpdatedDate", () => {
throw new Error("The `rssLastUpdatedDate` filter was removed. Use `getNewestCollectionItemDate | dateToRfc3339` (for Atom) or `getNewestCollectionItemDate | dateToRfc822` (for RSS) instead.")
});
eleventyConfig.addNunjucksFilter("rssDate", () => {
throw new Error("The `rssDate` filter was removed. Use `dateToRfc3339` (for Atom) or `dateToRfc822` (for RSS) instead.");
});
};

Object.defineProperty(eleventyRssPlugin, "eleventyPackage", {
value: pkg.name
});

Object.defineProperty(eleventyRssPlugin, "eleventyPluginOptions", {
value: {
unique: true
}
});

module.exports = eleventyRssPlugin;
13 changes: 7 additions & 6 deletions src/virtualTemplate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const pkg = require("../package.json");
const { DeepCopy } = require("@11ty/eleventy-utils");

const rssPlugin = require("./rssPlugin.js");

const debug = require("debug")("Eleventy:Rss:Feed");

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

async function eleventyFeedPlugin(eleventyConfig, options = {}) {
function eleventyFeedPlugin(eleventyConfig, options = {}) {
eleventyConfig.versionCheck(pkg["11ty"].compatibility);

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

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

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


Object.defineProperty(eleventyFeedPlugin, "eleventyPackage", {
value: `${pkg.name}/feed-plugin`
});
Expand Down
15 changes: 15 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "dateToRfc3339Test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@11ty/eleventy": "3.0.0-alpha.16"
}
}
52 changes: 52 additions & 0 deletions test/virtualTemplatesTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const test = require("ava");
const { feedPlugin } = require("../");

// https://github.com/11ty/eleventy-plugin-rss/issues/50
test("RSS virtual templates plugin", async (t) => {
const { default: Eleventy } = await import("@11ty/eleventy");

let elev = new Eleventy("./test", "./test/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`, { tag: "posts" })

eleventyConfig.addPlugin(feedPlugin, {
type: "atom", // or "rss", "json"
outputPath: "/feed.xml",
collection: {
name: "posts", // iterate over `collections.posts`
limit: 10, // 0 means no limit
},
});
},
});

let results = await elev.toJSON();

t.deepEqual(results.length, 2);
let [ feed ] = results.filter(entry => entry.outputPath.endsWith(".xml"));
t.truthy(feed.content.startsWith(`<?xml version="1.0" encoding="utf-8"?>`));
});

test("RSS virtual templates plugin with `all`", async (t) => {
const { default: Eleventy } = await import("@11ty/eleventy");

let elev = new Eleventy("./test", "./test/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual.md", `# Hello`, { tag: "posts" })

eleventyConfig.addPlugin(feedPlugin, {
type: "atom", // or "rss", "json"
outputPath: "/feed.xml",
collection: {
name: "all", // iterate over `collections.posts`
},
});
},
});

let results = await elev.toJSON();

t.deepEqual(results.length, 2);
let [ feed ] = results.filter(entry => entry.outputPath.endsWith(".xml"));
t.truthy(feed.content.startsWith(`<?xml version="1.0" encoding="utf-8"?>`));
});

0 comments on commit a269cab

Please sign in to comment.