Skip to content

Commit c76e1c8

Browse files
authored
Fix missing RSS item customData (#5591)
* Add failing test. * Fix it. * Add changeset.
1 parent 9f0fea0 commit c76e1c8

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

.changeset/ninety-socks-chew.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/rss': minor
3+
---
4+
5+
Fixes a bug that prevented an item’s `customData` from being included.

packages/astro-rss/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,9 @@ export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promi
163163
if (typeof result.content === 'string') {
164164
item['content:encoded'] = result.content;
165165
}
166-
if (typeof rssOptions.customData === 'string')
167-
Object.assign(item, parser.parse(`<item>${rssOptions.customData}</item>`).item);
166+
if (typeof result.customData === 'string') {
167+
Object.assign(item, parser.parse(`<item>${result.customData}</item>`).item);
168+
}
168169
return item;
169170
});
170171

packages/astro-rss/test/rss.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const phpFeedItemWithContent = {
2121
...phpFeedItem,
2222
content: `<h1>${phpFeedItem.title}</h1><p>${phpFeedItem.description}</p>`,
2323
};
24+
const phpFeedItemWithCustomData = {
25+
...phpFeedItem,
26+
customData: '<dc:creator><![CDATA[Buster Bluth]]></dc:creator>'
27+
};
2428

2529
const web1FeedItem = {
2630
// Should support empty string as a URL (possible for homepage route)
@@ -41,6 +45,8 @@ const web1FeedItemWithContent = {
4145
const validXmlResult = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link><item><title><![CDATA[${phpFeedItem.title}]]></title><link>${site}${phpFeedItem.link}/</link><guid>${site}${phpFeedItem.link}/</guid><description><![CDATA[${phpFeedItem.description}]]></description><pubDate>${new Date(phpFeedItem.pubDate).toUTCString()}</pubDate></item><item><title><![CDATA[${web1FeedItem.title}]]></title><link>${site}${web1FeedItem.link}/</link><guid>${site}${web1FeedItem.link}/</guid><description><![CDATA[${web1FeedItem.description}]]></description><pubDate>${new Date(web1FeedItem.pubDate).toUTCString()}</pubDate></item></channel></rss>`;
4246
// prettier-ignore
4347
const validXmlWithContentResult = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link><item><title><![CDATA[${phpFeedItemWithContent.title}]]></title><link>${site}${phpFeedItemWithContent.link}/</link><guid>${site}${phpFeedItemWithContent.link}/</guid><description><![CDATA[${phpFeedItemWithContent.description}]]></description><pubDate>${new Date(phpFeedItemWithContent.pubDate).toUTCString()}</pubDate><content:encoded><![CDATA[${phpFeedItemWithContent.content}]]></content:encoded></item><item><title><![CDATA[${web1FeedItemWithContent.title}]]></title><link>${site}${web1FeedItemWithContent.link}/</link><guid>${site}${web1FeedItemWithContent.link}/</guid><description><![CDATA[${web1FeedItemWithContent.description}]]></description><pubDate>${new Date(web1FeedItemWithContent.pubDate).toUTCString()}</pubDate><content:encoded><![CDATA[${web1FeedItemWithContent.content}]]></content:encoded></item></channel></rss>`;
48+
// prettier-ignore
49+
const validXmlWithCustomDataResult = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link><item><title><![CDATA[${phpFeedItemWithCustomData.title}]]></title><link>${site}${phpFeedItemWithCustomData.link}/</link><guid>${site}${phpFeedItemWithCustomData.link}/</guid><description><![CDATA[${phpFeedItemWithCustomData.description}]]></description><pubDate>${new Date(phpFeedItemWithCustomData.pubDate).toUTCString()}</pubDate>${phpFeedItemWithCustomData.customData}</item><item><title><![CDATA[${web1FeedItemWithContent.title}]]></title><link>${site}${web1FeedItemWithContent.link}/</link><guid>${site}${web1FeedItemWithContent.link}/</guid><description><![CDATA[${web1FeedItemWithContent.description}]]></description><pubDate>${new Date(web1FeedItemWithContent.pubDate).toUTCString()}</pubDate><content:encoded><![CDATA[${web1FeedItemWithContent.content}]]></content:encoded></item></channel></rss>`;
4450

4551
describe('rss', () => {
4652
it('should generate on valid RSSFeedItem array', async () => {
@@ -65,6 +71,20 @@ describe('rss', () => {
6571
chai.expect(body).xml.to.equal(validXmlWithContentResult);
6672
});
6773

74+
it('should generate on valid RSSFeedItem array with custom data included', async () => {
75+
const { body } = await rss({
76+
xmlns: {
77+
dc: 'http://purl.org/dc/elements/1.1/',
78+
},
79+
title,
80+
description,
81+
items: [phpFeedItemWithCustomData, web1FeedItemWithContent],
82+
site,
83+
});
84+
85+
chai.expect(body).xml.to.equal(validXmlWithCustomDataResult);
86+
});
87+
6888
describe('glob result', () => {
6989
it('should generate on valid result', async () => {
7090
const globResult = {

0 commit comments

Comments
 (0)