Skip to content

Commit 68c20be

Browse files
equtdelucis
andauthored
fix: filter out draft item from glob result (#5612)
* fix: filter out draft item from glob result * test: rss should ignore draft * build: add changeset * build: major version * feat: add `drafts` option * Add README docs Co-authored-by: Chris Swithinbank <[email protected]>
1 parent 9674cf5 commit 68c20be

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

.changeset/modern-dodos-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/rss': major
3+
---
4+
5+
Filter out draft in RSS generation

packages/astro-rss/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ rss({
5050
site: import.meta.env.SITE,
5151
// list of `<item>`s in output xml
5252
items: import.meta.glob('./**/*.md'),
53+
// include draft posts in the feed (default: false)
54+
drafts: true,
5355
// (optional) absolute path to XSL stylesheet in your project
5456
stylesheet: '/rss-styles.xsl',
5557
// (optional) inject custom xml
@@ -102,6 +104,12 @@ type RSSFeedItem = {
102104
};
103105
```
104106

107+
### drafts
108+
109+
Type: `boolean (optional)`
110+
111+
Set `drafts: true` to include [draft posts](https://docs.astro.build/en/guides/markdown-content/#draft-pages) in the feed output. By default, this option is `false` and draft posts are not included.
112+
105113
### stylesheet
106114

107115
Type: `string (optional)`

packages/astro-rss/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type RSSOptions = {
2828
stylesheet?: string | boolean;
2929
/** Specify custom data in opening of file */
3030
customData?: string;
31+
/** Whether to include drafts or not */
32+
drafts?: boolean;
3133
};
3234

3335
type RSSFeedItem = {
@@ -43,6 +45,8 @@ type RSSFeedItem = {
4345
content?: string;
4446
/** Append some other XML-valid data to this item */
4547
customData?: string;
48+
/** Whether draft or not */
49+
draft?: boolean;
4650
};
4751

4852
type GenerateRSSArgs = {
@@ -72,6 +76,7 @@ function mapGlobResult(items: GlobResult): Promise<RSSFeedItem[]> {
7276
pubDate: frontmatter.pubDate,
7377
description: frontmatter.description,
7478
customData: frontmatter.customData,
79+
draft: frontmatter.draft,
7580
};
7681
})
7782
);
@@ -87,6 +92,9 @@ export default async function getRSS(rssOptions: RSSOptions) {
8792

8893
if (isGlobResult(items)) {
8994
items = await mapGlobResult(items);
95+
if (!rssOptions.drafts) {
96+
items = items.filter((item) => !item.draft);
97+
}
9098
}
9199

92100
return {

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const web1FeedItemWithContent = {
4444
// prettier-ignore
4545
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>`;
4646
// prettier-ignore
47+
const validXmlWithoutWeb1FeedResult = `<?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></channel></rss>`;
48+
// prettier-ignore
4749
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>`;
4850
// prettier-ignore
4951
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>`;
@@ -195,6 +197,81 @@ describe('rss', () => {
195197
})
196198
).to.be.rejected;
197199
});
200+
201+
it('should filter out draft', async () => {
202+
const globResult = {
203+
'./posts/php.md': () =>
204+
new Promise((resolve) =>
205+
resolve({
206+
url: phpFeedItem.link,
207+
frontmatter: {
208+
title: phpFeedItem.title,
209+
pubDate: phpFeedItem.pubDate,
210+
description: phpFeedItem.description,
211+
},
212+
})
213+
),
214+
'./posts/nested/web1.md': () =>
215+
new Promise((resolve) =>
216+
resolve({
217+
url: web1FeedItem.link,
218+
frontmatter: {
219+
title: web1FeedItem.title,
220+
pubDate: web1FeedItem.pubDate,
221+
description: web1FeedItem.description,
222+
draft: true,
223+
},
224+
})
225+
),
226+
};
227+
228+
const { body } = await rss({
229+
title,
230+
description,
231+
items: globResult,
232+
site,
233+
});
234+
235+
chai.expect(body).xml.to.equal(validXmlWithoutWeb1FeedResult);
236+
});
237+
238+
it('should respect drafts option', async () => {
239+
const globResult = {
240+
'./posts/php.md': () =>
241+
new Promise((resolve) =>
242+
resolve({
243+
url: phpFeedItem.link,
244+
frontmatter: {
245+
title: phpFeedItem.title,
246+
pubDate: phpFeedItem.pubDate,
247+
description: phpFeedItem.description,
248+
},
249+
})
250+
),
251+
'./posts/nested/web1.md': () =>
252+
new Promise((resolve) =>
253+
resolve({
254+
url: web1FeedItem.link,
255+
frontmatter: {
256+
title: web1FeedItem.title,
257+
pubDate: web1FeedItem.pubDate,
258+
description: web1FeedItem.description,
259+
draft: true,
260+
},
261+
})
262+
),
263+
};
264+
265+
const { body } = await rss({
266+
title,
267+
description,
268+
items: globResult,
269+
site,
270+
drafts: true,
271+
});
272+
273+
chai.expect(body).xml.to.equal(validXmlResult);
274+
});
198275
});
199276

200277
describe('errors', () => {

0 commit comments

Comments
 (0)