Skip to content

Commit

Permalink
fix: filter out draft item from glob result (#5612)
Browse files Browse the repository at this point in the history
* 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 <swithinbank@gmail.com>
  • Loading branch information
equt and delucis authored Dec 27, 2022
1 parent 9674cf5 commit 68c20be
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-dodos-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/rss': major
---

Filter out draft in RSS generation
8 changes: 8 additions & 0 deletions packages/astro-rss/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ rss({
site: import.meta.env.SITE,
// list of `<item>`s in output xml
items: import.meta.glob('./**/*.md'),
// include draft posts in the feed (default: false)
drafts: true,
// (optional) absolute path to XSL stylesheet in your project
stylesheet: '/rss-styles.xsl',
// (optional) inject custom xml
Expand Down Expand Up @@ -102,6 +104,12 @@ type RSSFeedItem = {
};
```

### drafts

Type: `boolean (optional)`

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.

### stylesheet

Type: `string (optional)`
Expand Down
8 changes: 8 additions & 0 deletions packages/astro-rss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type RSSOptions = {
stylesheet?: string | boolean;
/** Specify custom data in opening of file */
customData?: string;
/** Whether to include drafts or not */
drafts?: boolean;
};

type RSSFeedItem = {
Expand All @@ -43,6 +45,8 @@ type RSSFeedItem = {
content?: string;
/** Append some other XML-valid data to this item */
customData?: string;
/** Whether draft or not */
draft?: boolean;
};

type GenerateRSSArgs = {
Expand Down Expand Up @@ -72,6 +76,7 @@ function mapGlobResult(items: GlobResult): Promise<RSSFeedItem[]> {
pubDate: frontmatter.pubDate,
description: frontmatter.description,
customData: frontmatter.customData,
draft: frontmatter.draft,
};
})
);
Expand All @@ -87,6 +92,9 @@ export default async function getRSS(rssOptions: RSSOptions) {

if (isGlobResult(items)) {
items = await mapGlobResult(items);
if (!rssOptions.drafts) {
items = items.filter((item) => !item.draft);
}
}

return {
Expand Down
77 changes: 77 additions & 0 deletions packages/astro-rss/test/rss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const web1FeedItemWithContent = {
// prettier-ignore
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>`;
// prettier-ignore
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>`;
// prettier-ignore
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>`;
// prettier-ignore
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>`;
Expand Down Expand Up @@ -195,6 +197,81 @@ describe('rss', () => {
})
).to.be.rejected;
});

it('should filter out draft', async () => {
const globResult = {
'./posts/php.md': () =>
new Promise((resolve) =>
resolve({
url: phpFeedItem.link,
frontmatter: {
title: phpFeedItem.title,
pubDate: phpFeedItem.pubDate,
description: phpFeedItem.description,
},
})
),
'./posts/nested/web1.md': () =>
new Promise((resolve) =>
resolve({
url: web1FeedItem.link,
frontmatter: {
title: web1FeedItem.title,
pubDate: web1FeedItem.pubDate,
description: web1FeedItem.description,
draft: true,
},
})
),
};

const { body } = await rss({
title,
description,
items: globResult,
site,
});

chai.expect(body).xml.to.equal(validXmlWithoutWeb1FeedResult);
});

it('should respect drafts option', async () => {
const globResult = {
'./posts/php.md': () =>
new Promise((resolve) =>
resolve({
url: phpFeedItem.link,
frontmatter: {
title: phpFeedItem.title,
pubDate: phpFeedItem.pubDate,
description: phpFeedItem.description,
},
})
),
'./posts/nested/web1.md': () =>
new Promise((resolve) =>
resolve({
url: web1FeedItem.link,
frontmatter: {
title: web1FeedItem.title,
pubDate: web1FeedItem.pubDate,
description: web1FeedItem.description,
draft: true,
},
})
),
};

const { body } = await rss({
title,
description,
items: globResult,
site,
drafts: true,
});

chai.expect(body).xml.to.equal(validXmlResult);
});
});

describe('errors', () => {
Expand Down

0 comments on commit 68c20be

Please sign in to comment.