From a269cabf0c1a5d369dd5412893d3434442c4d7da Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Fri, 12 Jul 2024 15:34:02 -0500 Subject: [PATCH] Fixes #52 --- .eleventy.js | 65 ++++-------------------------------- package.json | 2 +- sample/config-sample.js | 2 +- sample/package.json | 2 +- src/rssPlugin.js | 62 ++++++++++++++++++++++++++++++++++ src/virtualTemplate.js | 13 ++++---- test/package.json | 15 +++++++++ test/virtualTemplatesTest.js | 52 +++++++++++++++++++++++++++++ 8 files changed, 146 insertions(+), 67 deletions(-) create mode 100644 src/rssPlugin.js create mode 100644 test/package.json create mode 100644 test/virtualTemplatesTest.js diff --git a/.eleventy.js b/.eleventy.js index ed0ea66..f2f6259 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -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 plugin bundled with Eleventy - eleventyConfig.addNunjucksFilter("absoluteUrl", absoluteUrl); - - // Deprecated in favor of the more efficient HTML 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; diff --git a/package.json b/package.json index 852594f..77e005e 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/sample/config-sample.js b/sample/config-sample.js index 723a2fe..6b05225 100644 --- a/sample/config-sample.js +++ b/sample/config-sample.js @@ -1,4 +1,4 @@ -const rssPlugin = require("../") +const rssPlugin = require("../"); module.exports = function(eleventyConfig) { // eleventyConfig.ignores.add("json.njk"); diff --git a/sample/package.json b/sample/package.json index 4702957..522986b 100644 --- a/sample/package.json +++ b/sample/package.json @@ -10,6 +10,6 @@ "author": "", "license": "ISC", "dependencies": { - "@11ty/eleventy": "3.0.0-alpha.13" + "@11ty/eleventy": "3.0.0-alpha.16" } } diff --git a/src/rssPlugin.js b/src/rssPlugin.js new file mode 100644 index 0000000..70d730c --- /dev/null +++ b/src/rssPlugin.js @@ -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 plugin bundled with Eleventy + eleventyConfig.addNunjucksFilter("absoluteUrl", absoluteUrl); + + // Deprecated in favor of the more efficient HTML 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; diff --git a/src/virtualTemplate.js b/src/virtualTemplate.js index cf1abd9..27beb00 100644 --- a/src/virtualTemplate.js +++ b/src/virtualTemplate.js @@ -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 }) { @@ -93,16 +96,15 @@ ${stylesheet ? `\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)}` : ""; @@ -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` }); diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..be6bcea --- /dev/null +++ b/test/package.json @@ -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" + } +} diff --git a/test/virtualTemplatesTest.js b/test/virtualTemplatesTest.js new file mode 100644 index 0000000..94a2f16 --- /dev/null +++ b/test/virtualTemplatesTest.js @@ -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(``)); +}); + +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(``)); +});