1- import { createDebug } from "obug" ;
1+ // import { createDebug } from "obug";
22import pkg from "../package.json" with { type : "json" } ;
33
44import { DeepCopy } from "@11ty/eleventy-utils" ;
55
66import rssPlugin from "./rssPlugin.js" ;
77
8- const debug = createDebug ( "Eleventy:Rss:Feed" ) ;
8+ // const debug = createDebug("Eleventy:Rss:Feed");
99
1010function getFeedContent ( { type, stylesheet, collection, script } ) {
1111 // Note: page.lang comes from the i18n plugin: https://www.11ty.dev/docs/plugins/i18n/#page.lang
1212
13+ // "auto" reverses the (oldest first) collection, "ascending" and "descending" sort by date
14+ let sort = collection . sort === "auto" ? " | reverse" : ` | eleventyFeedSortByDate("${ collection . sort } ")` ;
15+
1316 if ( type === "rss" ) {
1417 // Nunjucks template
1518 return `<?xml version="1.0" encoding="utf-8"?>
@@ -22,7 +25,7 @@ ${stylesheet ? `<?xml-stylesheet href="${stylesheet}" type="text/xsl"?>\n` : ""}
2225 <description>{{ metadata.subtitle }}</description>
2326 <language>{{ metadata.language or page.lang }}</language>
2427 {%- if metadata.icon %}<image>{{ metadata.icon }}</image>{%- endif %}
25- {%- for post in collections.${ collection . name } | reverse | eleventyFeedHead(${ collection . limit } ) %}
28+ {%- for post in collections.${ collection . name } ${ sort } | eleventyFeedHead(${ collection . limit } ) %}
2629 {%- set absolutePostUrl = post.url | htmlBaseUrl(metadata.base) %}
2730 <item>
2831 <title>{{ post.data.title }}</title>
@@ -65,7 +68,7 @@ ${stylesheet ? `<?xml-stylesheet href="${stylesheet}" type="text/xsl"?>\n` : ""}
6568 <email>{{ metadata.author.email }}</email>
6669 {%- endif %}
6770 </author>
68- {%- for post in collections['${ collection . name } '] | reverse | eleventyFeedHead(${ collection . limit } ) %}
71+ {%- for post in collections['${ collection . name } ']${ sort } | eleventyFeedHead(${ collection . limit } ) %}
6972 {%- set absolutePostUrl %}{{ post.url | htmlBaseUrl(metadata.base) }}{% endset %}
7073 <entry>
7174 <title>{{ post.data.title }}</title>
@@ -97,7 +100,7 @@ ${stylesheet ? `<?xml-stylesheet href="${stylesheet}" type="text/xsl"?>\n` : ""}
97100 }
98101 ],
99102 "items": [
100- {%- for post in collections['${ collection . name } '] | reverse | eleventyFeedHead(${ collection . limit } ) %}
103+ {%- for post in collections['${ collection . name } ']${ sort } | eleventyFeedHead(${ collection . limit } ) %}
101104 {%- set absolutePostUrl %}{{ post.url | htmlBaseUrl(metadata.base) }}{% endset %}
102105 {
103106 "id": "{{ absolutePostUrl }}",
@@ -134,6 +137,7 @@ export default function eleventyFeedPlugin(eleventyConfig, options = {}) {
134137 collection : {
135138 name : false , // required
136139 limit : 0 , // limit number of entries, 0 means no limit
140+ sort : "auto" , // "auto" reverses the collection, "ascending" or "descending" sorts by date
137141 } ,
138142 outputPath : "/feed.xml" ,
139143 inputPath : `eleventy-plugin-feed${ inputPathSuffix } -${ options . type || "atom" } .njk` , // TODO make this more unique
@@ -157,6 +161,10 @@ export default function eleventyFeedPlugin(eleventyConfig, options = {}) {
157161 throw new Error ( "Only string is supported in `collection.name` option in feedPlugin from @11ty/eleventy-plugin-rss. Received: " + typeof options . collection ?. name ) ;
158162 }
159163
164+ if ( ! [ "auto" , "ascending" , "descending" ] . includes ( options . collection . sort ) ) {
165+ throw new Error ( "Invalid `collection.sort` option in feedPlugin from @11ty/eleventy-plugin-rss. Expected \"auto\", \"ascending\", or \"descending\", received: " + options . collection . sort ) ;
166+ }
167+
160168 let eleventyExcludeFromCollections ;
161169 let eleventyImport ;
162170 if ( options . collection . name === "all" ) {
@@ -178,6 +186,11 @@ export default function eleventyFeedPlugin(eleventyConfig, options = {}) {
178186 metadata : options . metadata ,
179187 } ;
180188
189+ // Sort a copy of a collection by date.
190+ eleventyConfig . addFilter ( "eleventyFeedSortByDate" , function ( array , direction ) {
191+ return [ ...array ] . sort ( ( a , b ) => direction === "ascending" ? a . date - b . date : b . date - a . date ) ;
192+ } ) ;
193+
181194 // Get the first `n` elements of a collection.
182195 eleventyConfig . addFilter ( "eleventyFeedHead" , function ( array , n ) {
183196 if ( ! n || n === 0 ) {
0 commit comments