Skip to content

Commit

Permalink
Updates samples to use alpha.13. Require alpha.13+ compatibility. Aut…
Browse files Browse the repository at this point in the history
…omatically add base plugin (if not yet added)
  • Loading branch information
zachleat committed Jun 12, 2024
1 parent f6a88ad commit 38b3a21
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 162 deletions.
30 changes: 14 additions & 16 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ const virtualTemplate = require("./src/virtualTemplate.js");
const absoluteUrl = require("./src/absoluteUrl");
const convertHtmlToAbsoluteUrls = require("./src/htmlToAbsoluteUrls");

function eleventyRssPlugin(eleventyConfig, options = {}) {
try {
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
} catch(e) {
console.log( `WARN: Eleventy Plugin (${pkg.name}) Compatibility: ${e.message}` );
}
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 HTML <base> plugin bundled with Eleventy 2.0
// Deprecated in favor of the more efficient HTML <base> plugin bundled with Eleventy
eleventyConfig.addNunjucksFilter("absoluteUrl", absoluteUrl);

// Deprecated in favor of the HTML <base> plugin bundled with Eleventy 2.0
// Deprecated in favor of the more efficient HTML <base> plugin bundled with Eleventy
eleventyConfig.addNunjucksAsyncFilter("htmlToAbsoluteUrls", (htmlContent, base, callback) => {
if(!htmlContent) {
callback(null, "");
Expand All @@ -34,17 +39,10 @@ function eleventyRssPlugin(eleventyConfig, options = {}) {
});
});

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

// Removed, this name is incorrect! Issue #8, #21
// 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.")
});

// Removed, this name is incorrect! Issue #8, #21
eleventyConfig.addNunjucksFilter("rssDate", () => {
throw new Error("The `rssDate` filter was removed. Use `dateToRfc3339` (for Atom) or `dateToRfc822` (for RSS) instead.");
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"main": ".eleventy.js",
"scripts": {
"test": "npx ava",
"sample": "cd sample && npx @11ty/eleventy --config=config-sample.js",
"sample": "cd sample && npx @11ty/eleventy --config=config-sample.js --pathprefix=pathprefix",
"clean": "rm -rf sample/_site"
},
"repository": {
Expand All @@ -34,7 +34,7 @@
},
"homepage": "https://www.11ty.dev/docs/plugins/rss/",
"11ty": {
"compatibility": ">=2.0.0"
"compatibility": ">=3.0.0-alpha.13"
},
"devDependencies": {
"ava": "^4.3.0"
Expand Down
37 changes: 37 additions & 0 deletions sample/atom.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---json
{
"permalink": "atom.xml",
"eleventyExcludeFromCollections": true,
"metadata": {
"title": "My Blog about Boats",
"description": "I am writing about my experiences as a naval navel-gazer.",
"language": "en",
"base": "https://example.com/",
"author": {
"name": "Boaty McBoatFace"
}
}
}
---
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="{{ metadata.language or page.lang }}">
<title>{{ metadata.title }}</title>
<subtitle>{{ metadata.description }}</subtitle>
<link href="{{ permalink | htmlBaseUrl(metadata.base) }}" rel="self" />
<link href="{{ metadata.base | addPathPrefixToFullUrl }}" />
<updated>{{ collections.posts | getNewestCollectionItemDate | dateToRfc3339 }}</updated>
<id>{{ metadata.base | addPathPrefixToFullUrl }}</id>
<author>
<name>{{ metadata.author.name }}</name>
</author>
{%- for post in collections.posts | reverse %}
{%- set absolutePostUrl %}{{ post.url | htmlBaseUrl(metadata.base) }}{% endset %}
<entry>
<title>{{ post.data.title }}</title>
<link href="{{ absolutePostUrl }}" />
<updated>{{ post.date | dateToRfc3339 }}</updated>
<id>{{ absolutePostUrl }}</id>
<content type="html">{{ post.content | renderTransforms(post.data.page, metadata.base) }}</content>
</entry>
{%- endfor %}
</feed>
8 changes: 4 additions & 4 deletions sample/config-sample.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const rssPlugin = require("../")

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

eleventyConfig.addPlugin(rssPlugin, {
posthtmlRenderOptions: {}
});

eleventyConfig.addJavaScriptFunction("absoluteUrl", rssPlugin.absoluteUrl);
eleventyConfig.addJavaScriptFunction("htmlToAbsoluteUrls", rssPlugin.convertHtmlToAbsoluteUrls);
eleventyConfig.addJavaScriptFunction("dateToRfc3339", rssPlugin.dateToRfc3339);
};
50 changes: 0 additions & 50 deletions sample/feed-json.11ty.js

This file was deleted.

44 changes: 0 additions & 44 deletions sample/feed-rss.njk

This file was deleted.

46 changes: 0 additions & 46 deletions sample/feed.njk

This file was deleted.

41 changes: 41 additions & 0 deletions sample/json.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---json
{
"permalink": "feed.json",
"eleventyExcludeFromCollections": true,
"metadata": {
"title": "My Blog about Boats",
"description": "I am writing about my experiences as a naval navel-gazer.",
"language": "en",
"base": "https://example.com/",
"author": {
"name": "Boaty McBoatFace"
}
}
}
---
{
"version": "https://jsonfeed.org/version/1.1",
"title": "{{ metadata.title }}",
"language": "{{ metadata.language or page.lang }}",
"home_page_url": "{{ metadata.base | addPathPrefixToFullUrl }}",
"feed_url": "{{ permalink | htmlBaseUrl(metadata.base) }}",
"description": "{{ metadata.description }}",
"authors": [
{
"name": "{{ metadata.author.name }}"
}
],
"items": [
{%- for post in collections.posts | reverse %}
{%- set absolutePostUrl %}{{ post.url | htmlBaseUrl(metadata.base) }}{% endset %}
{
"id": "{{ absolutePostUrl }}",
"url": "{{ absolutePostUrl }}",
"title": "{{ post.data.title }}",
"content_html": {% if post.content %}{{ post.content | renderTransforms(post.data.page, metadata.base) | dump | safe }}{% else %}""{% endif %},
"date_published": "{{ post.date | dateToRfc3339 }}"
}
{% if not loop.last %},{% endif %}
{%- endfor %}
]
}
15 changes: 15 additions & 0 deletions sample/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "sample",
"version": "1.0.0",
"description": "",
"main": "config-sample.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@11ty/eleventy": "3.0.0-alpha.13"
}
}
36 changes: 36 additions & 0 deletions sample/rss.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---json
{
"permalink": "rss.xml",
"eleventyExcludeFromCollections": true,
"metadata": {
"title": "My Blog about Boats",
"description": "I am writing about my experiences as a naval navel-gazer.",
"language": "en",
"base": "https://example.com/",
"author": {
"name": "Boaty McBoatFace"
}
}
}
---
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:base="{{ metadata.base | addPathPrefixToFullUrl }}" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ metadata.title }}</title>
<link>{{ metadata.base | addPathPrefixToFullUrl }}</link>
<atom:link href="{{ permalink | htmlBaseUrl(metadata.base) }}" rel="self" type="application/rss+xml" />
<description>{{ metadata.description }}</description>
<language>{{ metadata.language or page.lang }}</language>
{%- for post in collections.posts | reverse %}
{%- set absolutePostUrl = post.url | htmlBaseUrl(metadata.base) %}
<item>
<title>{{ post.data.title }}</title>
<link>{{ absolutePostUrl }}</link>
<description>{{ post.content | renderTransforms(post.data.page, metadata.base) }}</description>
<pubDate>{{ post.date | dateToRfc822 }}</pubDate>
<dc:creator>{{ metadata.author.name }}</dc:creator>
<guid>{{ absolutePostUrl }}</guid>
</item>
{%- endfor %}
</channel>
</rss>

0 comments on commit 38b3a21

Please sign in to comment.