Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple feeds + fix permalink bug #59

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions src/virtualTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ${stylesheet ? `<?xml-stylesheet href="${stylesheet}" type="text/xsl"?>\n` : ""}
<atom:link href="{{ permalink | htmlBaseUrl(metadata.base) }}" rel="self" type="application/rss+xml" />
<description>{{ metadata.subtitle }}</description>
<language>{{ metadata.language or page.lang }}</language>
{%- for post in collections.${collection.name} | reverse | eleventyFeedHead(${collection.limit}) %}
{%- for post in collections.${collection.name} | eleventyFeedActive | reverse | eleventyFeedHead(${collection.limit}) %}
{%- set absolutePostUrl = post.url | htmlBaseUrl(metadata.base) %}
<item>
<title>{{ post.data.title }}</title>
Expand Down Expand Up @@ -49,7 +49,7 @@ ${stylesheet ? `<?xml-stylesheet href="${stylesheet}" type="text/xsl"?>\n` : ""}
<email>{{ metadata.author.email }}</email>
{%- endif %}
</author>
{%- for post in collections['${collection.name}'] | reverse | eleventyFeedHead(${collection.limit}) %}
{%- for post in collections['${collection.name}'] | eleventyFeedActive | reverse | eleventyFeedHead(${collection.limit}) %}
{%- set absolutePostUrl %}{{ post.url | htmlBaseUrl(metadata.base) }}{% endset %}
<entry>
<title>{{ post.data.title }}</title>
Expand Down Expand Up @@ -78,7 +78,7 @@ ${stylesheet ? `<?xml-stylesheet href="${stylesheet}" type="text/xsl"?>\n` : ""}
}
],
"items": [
{%- for post in collections['${collection.name}'] | reverse | eleventyFeedHead(${collection.limit}) %}
{%- for post in collections['${collection.name}'] | eleventyFeedActive | reverse | eleventyFeedHead(${collection.limit}) %}
{%- set absolutePostUrl %}{{ post.url | htmlBaseUrl(metadata.base) }}{% endset %}
{
"id": "{{ absolutePostUrl }}",
Expand Down Expand Up @@ -106,6 +106,31 @@ function eleventyFeedPlugin(eleventyConfig, options = {}) {
// Guaranteed unique, first add wins
eleventyConfig.addPlugin(rssPlugin, options.rssPluginOptions || {});

// Get the first `n` elements of a collection.
eleventyConfig.addFilter("eleventyFeedHead", function(array, n) {
if(!n || n === 0) {
return array;
}
if(n < 0) {
return array.slice(n);
}
return array.slice(0, n);
});

eleventyConfig.addFilter('eleventyFeedActive', function(items) {
return items.filter(item => item.url)
})

if (!Array.isArray(options.feeds)) {
options.feeds = [options]
}

for (const feed of options.feeds) {
addTemplate(eleventyConfig, feed)
}
}

function addTemplate(eleventyConfig, options = {}) {
let slugifyFilter = eleventyConfig.getFilter("slugify");
let inputPathSuffix = options?.metadata?.title ? `-${slugifyFilter(options?.metadata?.title)}` : "";

Expand Down Expand Up @@ -156,17 +181,6 @@ function eleventyFeedPlugin(eleventyConfig, options = {}) {
metadata: options.metadata,
};

// Get the first `n` elements of a collection.
eleventyConfig.addFilter("eleventyFeedHead", function(array, n) {
if(!n || n === 0) {
return array;
}
if(n < 0) {
return array.slice(n);
}
return array.slice(0, n);
});

eleventyConfig.addTemplate(options.inputPath, getFeedContent(options), templateData);
};

Expand Down
49 changes: 49 additions & 0 deletions test/virtualTemplatesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,52 @@ test("RSS virtual templates plugin with `all`", async (t) => {
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 with multple feeds", async t => {
const { default: Eleventy } = await import("@11ty/eleventy");

let elev = new Eleventy("./test", "./test/_site", {
config: function (eleventyConfig) {
eleventyConfig.addTemplate("virtual-a.md", `# Hello`, { tag: "posts" })
eleventyConfig.addTemplate("virtual-b.md", `# There`, { tag: "photos" })

eleventyConfig.addPlugin(feedPlugin, {
feeds: [
{
type: "atom", // or "rss", "json"
outputPath: "/posts.xml",
collection: {
name: "posts", // iterate over `collections.posts`
limit: 10, // 0 means no limit
},
metadata: {
title: "Posts" // required when there are multiple feeds
}
},
{
type: "atom", // or "rss", "json"
outputPath: "/photos.xml",
collection: {
name: "photos", // iterate over `collections.photos`
limit: 10, // 0 means no limit
},
metadata: {
title: "Photos" // required when there are multiple feeds
}
}
]
});
},
});

let results = await elev.toJSON();

t.deepEqual(results.length, 4);
let [ postsFeed ] = results.filter(entry => entry.outputPath.endsWith("/posts.xml"));
t.truthy(postsFeed.content.startsWith(`<?xml version="1.0" encoding="utf-8"?>`));
t.truthy(postsFeed.content.includes('<title>Posts</title>'))

let [ photosFeed ] = results.filter(entry => entry.outputPath.endsWith("/photos.xml"));
t.truthy(photosFeed.content.startsWith(`<?xml version="1.0" encoding="utf-8"?>`));
t.truthy(photosFeed.content.includes('<title>Photos</title>'))
})