Add virtual Atom and JSON feeds. Example for https://github.com/11ty/eleventy-plugin-rss/issues/47

This commit is contained in:
Zach Leatherman 2024-06-08 20:58:22 -05:00
parent 855f27468e
commit 6b5d6bba1c
6 changed files with 38 additions and 66 deletions

1
content/feed/.virtual Normal file
View File

@ -0,0 +1 @@
For RSS feed, Atom Feed, and JSON feed templates, see the plugin in eleventy.config.js

View File

@ -1,31 +0,0 @@
---
# Metadata comes from _data/metadata.js
permalink: /feed/feed.xml
eleventyNavigation:
key: Feed
order: 3
---
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/feed/pretty-atom-feed.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="{{ metadata.language }}">
<title>{{ metadata.title }}</title>
<subtitle>{{ metadata.description }}</subtitle>
<link href="{{ permalink | htmlBaseUrl(metadata.url) }}" rel="self"/>
<link href="{{ metadata.url | addPathPrefixToFullUrl }}"/>
<updated>{{ collections.posts | getNewestCollectionItemDate | dateToRfc3339 }}</updated>
<id>{{ metadata.url }}</id>
<author>
<name>{{ metadata.author.name }}</name>
<email>{{ metadata.author.email }}</email>
</author>
{%- for post in collections.posts | reverse %}
{%- set absolutePostUrl %}{{ post.url | htmlBaseUrl(metadata.url) }}{% endset %}
<entry>
<title>{{ post.data.title }}</title>
<link href="{{ absolutePostUrl }}"/>
<updated>{{ post.date | dateToRfc3339 }}</updated>
<id>{{ absolutePostUrl }}</id>
<content type="html">{{ post.templateContent | transformWithHtmlBase(absolutePostUrl, post.url) }}</content>
</entry>
{%- endfor %}
</feed>

View File

@ -1,3 +0,0 @@
export default {
layout: false,
};

View File

@ -1,29 +0,0 @@
---
# Metadata comes from _data/metadata.js
permalink: /feed/feed.json
---
{
"version": "https://jsonfeed.org/version/1.1",
"title": "{{ metadata.title }}",
"language": "{{ metadata.language }}",
"home_page_url": "{{ metadata.url | addPathPrefixToFullUrl }}",
"feed_url": "{{ permalink | htmlBaseUrl(metadata.url) }}",
"description": "{{ metadata.description }}",
"author": {
"name": "{{ metadata.author.name }}",
"url": "{{ metadata.author.url }}"
},
"items": [
{%- for post in collections.posts | reverse %}
{%- set absolutePostUrl = post.url | htmlBaseUrl(metadata.url) %}
{
"id": "{{ absolutePostUrl }}",
"url": "{{ absolutePostUrl }}",
"title": "{{ post.data.title }}",
"content_html": {% if post.templateContent %}{{ post.templateContent | transformWithHtmlBase(absolutePostUrl, post.url) | dump | safe }}{% else %}""{% endif %},
"date_published": "{{ post.date | dateToRfc3339 }}"
}
{% if not loop.last %},{% endif %}
{%- endfor %}
]
}

View File

@ -1,7 +1,7 @@
import markdownItAnchor from "markdown-it-anchor"; import markdownItAnchor from "markdown-it-anchor";
import { InputPathToUrlTransformPlugin, HtmlBasePlugin } from "@11ty/eleventy"; import { InputPathToUrlTransformPlugin, HtmlBasePlugin } from "@11ty/eleventy";
import pluginRss from "@11ty/eleventy-plugin-rss"; import { feedPlugin } from "@11ty/eleventy-plugin-rss";
import pluginSyntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight"; import pluginSyntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight";
import pluginNavigation from "@11ty/eleventy-navigation"; import pluginNavigation from "@11ty/eleventy-navigation";
import { eleventyImageTransformPlugin } from "@11ty/eleventy-img"; import { eleventyImageTransformPlugin } from "@11ty/eleventy-img";
@ -32,7 +32,37 @@ export default async function(eleventyConfig) {
// eleventyConfig.addBundle("js"); // eleventyConfig.addBundle("js");
// Official plugins // Official plugins
eleventyConfig.addPlugin(pluginRss); eleventyConfig.addPlugin(feedPlugin, {
files: {
json: "/feed/feed.json",
atom: "/feed/feed.xml",
// "rss": "/feed/rss.xml",
},
stylesheet: {
atom: "pretty-atom-feed.xsl",
},
templateData: {
atom: {
eleventyNavigation: {
key: "Feed",
order: 3
}
}
},
collectionName: "posts",
limit: 10,
metadata: {
language: "en",
title: "Blog Title",
subtitle: "This is a longer description about your blog.",
base: "https://example.com/",
author: {
name: "Your Name",
email: "me@example.com"
}
}
});
eleventyConfig.addPlugin(pluginSyntaxHighlight, { eleventyConfig.addPlugin(pluginSyntaxHighlight, {
preAttributes: { tabindex: 0 } preAttributes: { tabindex: 0 }
}); });
@ -44,9 +74,12 @@ export default async function(eleventyConfig) {
eleventyConfig.addPlugin(eleventyImageTransformPlugin, { eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
// File extensions to process in _site folder // File extensions to process in _site folder
extensions: "html", extensions: "html",
// Output formats for each image. // Output formats for each image.
formats: ["avif", "webp", "auto"], formats: ["avif", "webp", "auto"],
// widths: ["auto"], // widths: ["auto"],
defaultAttributes: { defaultAttributes: {
// e.g. <img loading decoding> assigned on the HTML tag will override these values. // e.g. <img loading decoding> assigned on the HTML tag will override these values.
loading: "lazy", loading: "lazy",
@ -92,6 +125,7 @@ export const config = {
"njk", "njk",
"html", "html",
"liquid", "liquid",
"11ty.js",
], ],
// Pre-process *.md files with: (default: `liquid`) // Pre-process *.md files with: (default: `liquid`)

View File

@ -37,7 +37,7 @@
"@11ty/eleventy": "3.0.0-alpha.10", "@11ty/eleventy": "3.0.0-alpha.10",
"@11ty/eleventy-img": "5.0.0-beta.1", "@11ty/eleventy-img": "5.0.0-beta.1",
"@11ty/eleventy-navigation": "^0.3.5", "@11ty/eleventy-navigation": "^0.3.5",
"@11ty/eleventy-plugin-rss": "^1.2.0", "@11ty/eleventy-plugin-rss": "^2.0.0-beta.5",
"@11ty/eleventy-plugin-syntaxhighlight": "^5.0.0", "@11ty/eleventy-plugin-syntaxhighlight": "^5.0.0",
"luxon": "^3.4.4", "luxon": "^3.4.4",
"markdown-it-anchor": "^8.6.7", "markdown-it-anchor": "^8.6.7",