-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
146 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"?>`)); | ||
}); |