From 68c20be66b197e6c525cd292823a3a728f238547 Mon Sep 17 00:00:00 2001 From: Leontopodium pusillum <17521736+equt@users.noreply.github.com> Date: Tue, 27 Dec 2022 23:50:11 +0800 Subject: [PATCH] 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 --- .changeset/modern-dodos-invent.md | 5 ++ packages/astro-rss/README.md | 8 +++ packages/astro-rss/src/index.ts | 8 +++ packages/astro-rss/test/rss.test.js | 77 +++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 .changeset/modern-dodos-invent.md diff --git a/.changeset/modern-dodos-invent.md b/.changeset/modern-dodos-invent.md new file mode 100644 index 0000000000..5a9a00a63d --- /dev/null +++ b/.changeset/modern-dodos-invent.md @@ -0,0 +1,5 @@ +--- +'@astrojs/rss': major +--- + +Filter out draft in RSS generation diff --git a/packages/astro-rss/README.md b/packages/astro-rss/README.md index 767a2bbcc1..3255aede2b 100644 --- a/packages/astro-rss/README.md +++ b/packages/astro-rss/README.md @@ -50,6 +50,8 @@ rss({ site: import.meta.env.SITE, // list of ``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 @@ -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)` diff --git a/packages/astro-rss/src/index.ts b/packages/astro-rss/src/index.ts index a138bebde9..667ab1c9dd 100644 --- a/packages/astro-rss/src/index.ts +++ b/packages/astro-rss/src/index.ts @@ -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 = { @@ -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 = { @@ -72,6 +76,7 @@ function mapGlobResult(items: GlobResult): Promise { pubDate: frontmatter.pubDate, description: frontmatter.description, customData: frontmatter.customData, + draft: frontmatter.draft, }; }) ); @@ -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 { diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index 95a0bbf10c..bd4c9cba78 100644 --- a/packages/astro-rss/test/rss.test.js +++ b/packages/astro-rss/test/rss.test.js @@ -44,6 +44,8 @@ const web1FeedItemWithContent = { // prettier-ignore const validXmlResult = `<![CDATA[${title}]]>${site}/<![CDATA[${phpFeedItem.title}]]>${site}${phpFeedItem.link}/${site}${phpFeedItem.link}/${new Date(phpFeedItem.pubDate).toUTCString()}<![CDATA[${web1FeedItem.title}]]>${site}${web1FeedItem.link}/${site}${web1FeedItem.link}/${new Date(web1FeedItem.pubDate).toUTCString()}`; // prettier-ignore +const validXmlWithoutWeb1FeedResult = `<![CDATA[${title}]]>${site}/<![CDATA[${phpFeedItem.title}]]>${site}${phpFeedItem.link}/${site}${phpFeedItem.link}/${new Date(phpFeedItem.pubDate).toUTCString()}`; +// prettier-ignore const validXmlWithContentResult = `<![CDATA[${title}]]>${site}/<![CDATA[${phpFeedItemWithContent.title}]]>${site}${phpFeedItemWithContent.link}/${site}${phpFeedItemWithContent.link}/${new Date(phpFeedItemWithContent.pubDate).toUTCString()}<![CDATA[${web1FeedItemWithContent.title}]]>${site}${web1FeedItemWithContent.link}/${site}${web1FeedItemWithContent.link}/${new Date(web1FeedItemWithContent.pubDate).toUTCString()}`; // prettier-ignore const validXmlWithCustomDataResult = `<![CDATA[${title}]]>${site}/<![CDATA[${phpFeedItemWithCustomData.title}]]>${site}${phpFeedItemWithCustomData.link}/${site}${phpFeedItemWithCustomData.link}/${new Date(phpFeedItemWithCustomData.pubDate).toUTCString()}${phpFeedItemWithCustomData.customData}<![CDATA[${web1FeedItemWithContent.title}]]>${site}${web1FeedItemWithContent.link}/${site}${web1FeedItemWithContent.link}/${new Date(web1FeedItemWithContent.pubDate).toUTCString()}`; @@ -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', () => {