@11ty/eleventy-plugin-rss
Advanced tools
Comparing version 1.0.9 to 1.1.0
@@ -1,19 +0,9 @@ | ||
const dateToISO = require("./src/dateToISO"); | ||
const dateRfc3339 = require("./src/dateRfc3339"); | ||
const absoluteUrl = require("./src/absoluteUrl"); | ||
const htmlToAbsoluteUrls = require("./src/htmlToAbsoluteUrls"); | ||
const convertHtmlToAbsoluteUrls = require("./src/htmlToAbsoluteUrls"); | ||
const getNewestCollectionItemDate = require("./src/getNewestCollectionItemDate"); | ||
module.exports = function(eleventyConfig) { | ||
eleventyConfig.addNunjucksFilter("rssLastUpdatedDate", collection => { | ||
if( !collection || !collection.length ) { | ||
throw new Error( "Collection is empty in rssLastUpdatedDate filter." ); | ||
} | ||
module.exports = function(eleventyConfig, options = {}) { | ||
eleventyConfig.addNunjucksFilter("absoluteUrl", absoluteUrl); | ||
// Newest date in the collection | ||
return dateToISO(new Date(Math.max(...collection.map(item => {return item.date})))); | ||
}); | ||
eleventyConfig.addNunjucksFilter("rssDate", dateObj => dateToISO(dateObj)); | ||
eleventyConfig.addNunjucksFilter("absoluteUrl", (href, base) => absoluteUrl(href, base)); | ||
eleventyConfig.addNunjucksAsyncFilter("htmlToAbsoluteUrls", (htmlContent, base, callback) => { | ||
@@ -25,6 +15,28 @@ if(!htmlContent) { | ||
htmlToAbsoluteUrls(htmlContent, base).then(result => { | ||
callback(null, result.html); | ||
let posthtmlOptions = Object.assign({ | ||
// default PostHTML render options | ||
closingSingleTag: "slash" | ||
}, options.posthtmlRenderOptions); | ||
convertHtmlToAbsoluteUrls(htmlContent, base, posthtmlOptions).then(html => { | ||
callback(null, html); | ||
}); | ||
}); | ||
// Dates | ||
eleventyConfig.addNunjucksFilter("getNewestCollectionItemDate", getNewestCollectionItemDate); | ||
eleventyConfig.addNunjucksFilter("dateToRfc3339", dateRfc3339); | ||
// Deprecated, these names are incorrect! Issue #8 | ||
eleventyConfig.addNunjucksFilter("rssLastUpdatedDate", collection => { | ||
return dateRfc3339(getNewestCollectionItemDate(collection)); | ||
}); | ||
// Deprecated, this name is incorrect! Issue #8 | ||
eleventyConfig.addNunjucksFilter("rssDate", dateRfc3339); | ||
}; | ||
module.exports.dateToRfc3339 = dateRfc3339; | ||
module.exports.getNewestCollectionItemDate = getNewestCollectionItemDate; | ||
module.exports.absoluteUrl = absoluteUrl; | ||
module.exports.convertHtmlToAbsoluteUrls = convertHtmlToAbsoluteUrls; |
{ | ||
"name": "@11ty/eleventy-plugin-rss", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "A pack of Eleventy plugins for generating an RSS feed using the Nunjucks templating engine.", | ||
@@ -11,3 +11,3 @@ "publishConfig": { | ||
"test": "npx ava", | ||
"sample": "npx @11ty/eleventy --input=sample" | ||
"sample": "cd sample && npx @11ty/eleventy --config=config-sample.js" | ||
}, | ||
@@ -40,9 +40,9 @@ "repository": { | ||
"devDependencies": { | ||
"ava": "^1.0.1" | ||
"ava": "^3.15.0" | ||
}, | ||
"dependencies": { | ||
"debug": "^4.1.1", | ||
"posthtml": "^0.13.0", | ||
"debug": "^4.3.1", | ||
"posthtml": "^0.15.1", | ||
"posthtml-urls": "1.0.0" | ||
} | ||
} |
@@ -0,35 +1,35 @@ | ||
<p align="center"><img src="https://www.11ty.dev/img/logo-github.png" alt="eleventy Logo"></p> | ||
# eleventy-plugin-rss | ||
A pack of [Eleventy](https://github.com/11ty/eleventy) plugins for generating Atom and JSON feeds using the Nunjucks templating engine. | ||
A pack of [Eleventy](https://github.com/11ty/eleventy) filters for generating Atom and JSON feeds using the Nunjucks templating engine. | ||
_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!_ | ||
## Installation | ||
See `sample/feed.njk` for an example Atom feed template or `sample/feed.json` for an example JSON feed template. | ||
Available on [npm](https://www.npmjs.com/package/@11ty/eleventy-plugin-rss). | ||
## [The full `eleventy-plugin-rss` documentation is on 11ty.dev](https://www.11ty.dev/docs/plugins/rss/). | ||
``` | ||
npm install @11ty/eleventy-plugin-rss --save-dev | ||
``` | ||
* _This is a plugin for the [Eleventy static site generator](https://www.11ty.dev/)._ | ||
* Find more [Eleventy plugins](https://www.11ty.dev/docs/plugins/). | ||
* Please star [Eleventy on GitHub](https://github.com/11ty/eleventy/), follow [@eleven_ty](https://twitter.com/eleven_ty) on Twitter, and support [11ty on Open Collective](https://opencollective.com/11ty) | ||
Open up your Eleventy config file (probably `.eleventy.js`) and use `addPlugin`: | ||
[![npm Version](https://img.shields.io/npm/v/@11ty/eleventy-plugin-rss.svg?style=for-the-badge)](https://www.npmjs.com/package/@11ty/eleventy-plugin-rss) [![GitHub issues](https://img.shields.io/github/issues/11ty/eleventy-plugin-rss.svg?style=for-the-badge)](https://github.com/11ty/eleventy-plugin-rss/issues) | ||
## Installation | ||
``` | ||
const pluginRss = require("@11ty/eleventy-plugin-rss"); | ||
module.exports = function(eleventyConfig) { | ||
eleventyConfig.addPlugin(pluginRss); | ||
}; | ||
npm install @11ty/eleventy-plugin-rss | ||
``` | ||
Read more about [Eleventy plugins.](https://www.11ty.io/docs/plugins/) | ||
_[The full `eleventy-plugin-rss` documentation is on 11ty.dev](https://www.11ty.dev/docs/plugins/rss/)._ | ||
## Usage | ||
## Tests | ||
See `sample/feed.njk` for an example Atom feed template or `sample/feed.json` for an example JSON feed template. | ||
``` | ||
npm run test | ||
``` | ||
### Supplies: Nunjucks Filters | ||
- We use the [ava JavaScript test runner](https://github.com/avajs/ava) ([Assertions documentation](https://github.com/avajs/ava/blob/master/docs/03-assertions.md)) | ||
- ℹ️ To keep tests fast, thou shalt try to avoid writing files in tests. | ||
* `rssLastUpdatedDate`: Gets the most recently updated content in the collection and retrieves the properly formatted Date for the top-level `<updated>` element. | ||
* `rssDate`: format a Date to be used for individual `<entry><updated>` elements. | ||
* `absoluteUrl`: converts a single URL (relative or absolute path) to a full absolute URL including protocol, domain, full path. | ||
* `htmlToAbsoluteUrls`: transforms all of the URLs in a block of HTML with `absoluteUrl` above. Uses [posthtml-urls](https://github.com/posthtml/posthtml-urls) with `a[href]`, `video[src]`, `audio[src]`, `source`, `img[src]`, `[srcset]` and [a whole bunch more](https://github.com/posthtml/posthtml-urls/blob/307c91342a211b3f9fb22bc57264bbb31f235fbb/lib/defaultOptions.js). |
@@ -5,3 +5,3 @@ const posthtml = require('posthtml'); | ||
module.exports = function(htmlContent, base) { | ||
module.exports = async function(htmlContent, base, processOptions = {}) { | ||
if( !base ) { | ||
@@ -12,11 +12,4 @@ throw new Error( "eleventy-plugin-rss, htmlToAbsoluteUrls(absolutePostUrl) was missing the full URL base `absolutePostUrl` argument.") | ||
let options = { | ||
eachURL: function(url, attr, element) { | ||
url = url.trim(); | ||
// #anchor in-page | ||
if( url.indexOf("#") === 0 ) { | ||
return url; | ||
} | ||
return absoluteUrl(url, base); | ||
eachURL: function(url) { | ||
return absoluteUrl(url.trim(), base); | ||
} | ||
@@ -27,3 +20,4 @@ }; | ||
return modifier.process(htmlContent); | ||
let result = await modifier.process(htmlContent, processOptions); | ||
return result.html; | ||
}; |
@@ -1,13 +0,27 @@ | ||
import test from "ava"; | ||
import htmlToAbsUrls from "../src/htmlToAbsoluteUrls.js"; | ||
const test = require("ava"); | ||
const htmlToAbsUrls = require("../src/htmlToAbsoluteUrls.js"); | ||
test("Changes a link href", async t => { | ||
t.is((await htmlToAbsUrls(`<a href="#testanchor">Hello</a>`, "http://example.com/")).html, `<a href="#testanchor">Hello</a>`); | ||
t.is((await htmlToAbsUrls(`<a href="/test.html">Hello</a>`, "http://example.com/")).html, `<a href="http://example.com/test.html">Hello</a>`); | ||
t.is((await htmlToAbsUrls(`<img src="/test.png">`, "http://example.com/")).html, `<img src="http://example.com/test.png">`); | ||
t.is((await htmlToAbsUrls(`<a href="http://someotherdomain/">Hello</a>`, "http://example.com/")).html, `<a href="http://someotherdomain/">Hello</a>`); | ||
t.is(await htmlToAbsUrls(`<a href="#testanchor">Hello</a>`, "http://example.com/"), `<a href="http://example.com/#testanchor">Hello</a>`); | ||
t.is(await htmlToAbsUrls(`<a href="#testanchor">Hello</a>`, "http://example.com/web/"), `<a href="http://example.com/web/#testanchor">Hello</a>`); | ||
t.is(await htmlToAbsUrls(`<a href="/test.html">Hello</a>`, "http://example.com/"), `<a href="http://example.com/test.html">Hello</a>`); | ||
t.is(await htmlToAbsUrls(`<img src="/test.png">`, "http://example.com/"), `<img src="http://example.com/test.png">`); | ||
t.is(await htmlToAbsUrls(`<a href="http://someotherdomain/">Hello</a>`, "http://example.com/"), `<a href="http://someotherdomain/">Hello</a>`); | ||
}); | ||
test("Bad link href", async t => { | ||
t.is((await htmlToAbsUrls(`<a href="http://#">Hello</a>`, "http://example.com/")).html, `<a href="http://#">Hello</a>`); | ||
t.is(await htmlToAbsUrls(`<a href="http://#">Hello</a>`, "http://example.com/"), `<a href="http://#">Hello</a>`); | ||
}); | ||
test("Line breaks, Issue #12", async t => { | ||
let processOptions = { | ||
closingSingleTag: "slash" | ||
}; | ||
t.is(await htmlToAbsUrls(`<br/>`, "http://example.com/"), `<br>`); | ||
t.is(await htmlToAbsUrls(`<br/>`, "http://example.com/", processOptions), `<br />`); | ||
let processedHtml = await htmlToAbsUrls(`<img src="/test.png">`, "http://example.com/", processOptions); | ||
t.is(processedHtml, `<img src="http://example.com/test.png" />`); | ||
}); | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
14052
21
143
+ Added@one-ini/wasm@0.1.1(transitive)
+ Addedcross-spawn@7.0.3(transitive)
+ Addeddom-serializer@1.4.1(transitive)
+ Addeddomhandler@4.3.1(transitive)
+ Addeddomutils@2.8.0(transitive)
+ Addedforeground-child@3.1.1(transitive)
+ Addedhtmlparser2@6.1.0(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedjackspeak@2.3.6(transitive)
+ Addedlru-cache@10.2.2(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpath-scurry@1.11.1(transitive)
+ Addedposthtml@0.15.2(transitive)
+ Addedposthtml-parser@0.7.2(transitive)
+ Addedproto-list@1.2.4(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedsignal-exit@4.1.0(transitive)
+ Addedto-fast-properties@1.0.3(transitive)
+ Addedwhich@2.0.2(transitive)
- Removed@socket.io/component-emitter@3.1.2(transitive)
- Removed@types/cookie@0.4.1(transitive)
- Removed@types/cors@2.8.17(transitive)
- Removed@types/node@20.12.12(transitive)
- Removeddom-serializer@0.2.2(transitive)
- Removeddomelementtype@1.3.1(transitive)
- Removeddomhandler@2.4.2(transitive)
- Removeddomutils@1.7.0(transitive)
- Removedengine.io-client@6.5.3(transitive)
- Removedengine.io-parser@5.2.2(transitive)
- Removedentities@1.1.2(transitive)
- Removedhtmlparser2@3.10.1(transitive)
- Removedlimiter@1.1.5(transitive)
- Removedposthtml@0.13.4(transitive)
- Removedposthtml-parser@0.5.3(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedws@8.11.0(transitive)
Updateddebug@^4.3.1
Updatedposthtml@^0.15.1