diff options
Diffstat (limited to 'packages/astro-rss')
-rw-r--r-- | packages/astro-rss/CHANGELOG.md | 18 | ||||
-rw-r--r-- | packages/astro-rss/README.md | 26 | ||||
-rw-r--r-- | packages/astro-rss/package.json | 8 | ||||
-rw-r--r-- | packages/astro-rss/src/index.ts | 26 | ||||
-rw-r--r-- | packages/astro-rss/test/rss.test.js | 80 | ||||
-rw-r--r-- | packages/astro-rss/tsconfig.json | 2 |
6 files changed, 121 insertions, 39 deletions
diff --git a/packages/astro-rss/CHANGELOG.md b/packages/astro-rss/CHANGELOG.md index 1f06b449e..b5abcf7c6 100644 --- a/packages/astro-rss/CHANGELOG.md +++ b/packages/astro-rss/CHANGELOG.md @@ -1,5 +1,23 @@ # @astrojs/rss +## 3.0.0-rc.1 + +### Major Changes + +- [#8179](https://github.com/withastro/astro/pull/8179) [`6011d52d3`](https://github.com/withastro/astro/commit/6011d52d38e43c3e3d52bc3bc41a60e36061b7b7) Thanks [@matthewp](https://github.com/matthewp)! - Astro 3.0 Release Candidate + +### Patch Changes + +- [#8099](https://github.com/withastro/astro/pull/8099) [`732111cdc`](https://github.com/withastro/astro/commit/732111cdce441639db31f40f621df48442d00969) Thanks [@bluwy](https://github.com/bluwy)! - Deprecate the `markdown.drafts` configuration option. + + If you'd like to create draft pages that are visible in dev but not in production, you can [migrate to content collections](https://docs.astro.build/en/guides/content-collections/#migrating-from-file-based-routing) and [manually filter out pages](https://docs.astro.build/en/guides/content-collections/#filtering-collection-queries) with the `draft: true` frontmatter property instead. + +## 3.0.0-beta.0 + +### Major Changes + +- [`1eae2e3f7`](https://github.com/withastro/astro/commit/1eae2e3f7d693c9dfe91c8ccfbe606d32bf2fb81) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Remove support for Node 16. The lowest supported version by Astro and all integrations is now v18.14.1. As a reminder, Node 16 will be deprecated on the 11th September 2023. + ## 2.4.4 ### Patch Changes diff --git a/packages/astro-rss/README.md b/packages/astro-rss/README.md index 47dfc1747..4e7555a97 100644 --- a/packages/astro-rss/README.md +++ b/packages/astro-rss/README.md @@ -65,8 +65,6 @@ export function get(context) { site: context.site, // list of `<item>`s in output xml items: [...], - // 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 @@ -118,6 +116,8 @@ When providing a formatted RSS item list, see the [`RSSFeedItem` type reference] Type: `boolean (optional)` +**Deprecated**: Manually filter `items` instead. + 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 @@ -370,7 +370,27 @@ export async function get(context) { } ``` ---- +## `getRssString()` + +As `rss()` returns a `Response`, you can also use `getRssString()` to get the RSS string directly and use it in your own response: + +```ts "getRssString" +// src/pages/rss.xml.js +import { getRssString } from '@astrojs/rss'; + +export async function get(context) { + const rssString = await getRssString({ + title: 'Buzz’s Blog', + ... + }); + + return new Response(rssString, { + headers: { + 'Content-Type': 'application/xml', + }, + }); +} +``` For more on building with Astro, [visit the Astro docs][astro-rss]. diff --git a/packages/astro-rss/package.json b/packages/astro-rss/package.json index f3b7c9c12..2809a10f8 100644 --- a/packages/astro-rss/package.json +++ b/packages/astro-rss/package.json @@ -1,7 +1,7 @@ { "name": "@astrojs/rss", "description": "Add RSS feeds to your Astro projects", - "version": "2.4.4", + "version": "3.0.0-rc.1", "type": "module", "types": "./dist/index.d.ts", "author": "withastro", @@ -29,16 +29,16 @@ "devDependencies": { "@types/chai": "^4.3.5", "@types/chai-as-promised": "^7.1.5", - "@types/mocha": "^9.1.1", + "@types/mocha": "^10.0.1", "astro": "workspace:*", "astro-scripts": "workspace:*", "chai": "^4.3.7", "chai-as-promised": "^7.1.1", "chai-xml": "^0.4.1", - "mocha": "^9.2.2" + "mocha": "^10.2.0" }, "dependencies": { - "fast-xml-parser": "^4.2.5", + "fast-xml-parser": "^4.2.7", "kleur": "^4.1.5" } } diff --git a/packages/astro-rss/src/index.ts b/packages/astro-rss/src/index.ts index 4d97647f1..a611afc16 100644 --- a/packages/astro-rss/src/index.ts +++ b/packages/astro-rss/src/index.ts @@ -27,7 +27,10 @@ export type RSSOptions = { stylesheet?: z.infer<typeof rssOptionsValidator>['stylesheet']; /** Specify custom data in opening of file */ customData?: z.infer<typeof rssOptionsValidator>['customData']; - /** Whether to include drafts or not */ + /** + * Whether to include drafts or not + * @deprecated Deprecated since version 3.0. Use content collections instead. + */ drafts?: z.infer<typeof rssOptionsValidator>['drafts']; trailingSlash?: z.infer<typeof rssOptionsValidator>['trailingSlash']; }; @@ -45,7 +48,10 @@ export type RSSFeedItem = { description?: z.infer<typeof rssSchema>['description']; /** Append some other XML-valid data to this item */ customData?: z.infer<typeof rssSchema>['customData']; - /** Whether draft or not */ + /** + * Whether draft or not + * @deprecated Deprecated since version 3.0. Use content collections instead. + */ draft?: z.infer<typeof rssSchema>['draft']; /** Categories or tags related to the item */ categories?: z.infer<typeof rssSchema>['categories']; @@ -92,12 +98,18 @@ const rssOptionsValidator = z.object({ trailingSlash: z.boolean().default(true), }); -export default async function getRSS(rssOptions: RSSOptions) { - const validatedRssOptions = await validateRssOptions(rssOptions); +export default async function getRssResponse(rssOptions: RSSOptions): Promise<Response> { + const rssString = await getRssString(rssOptions); + return new Response(rssString, { + headers: { + 'Content-Type': 'application/xml', + }, + }); +} - return { - body: await generateRSS(validatedRssOptions), - }; +export async function getRssString(rssOptions: RSSOptions): Promise<string> { + const validatedRssOptions = await validateRssOptions(rssOptions); + return await generateRSS(validatedRssOptions); } async function validateRssOptions(rssOptions: RSSOptions) { diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index feca92546..5dfb48b32 100644 --- a/packages/astro-rss/test/rss.test.js +++ b/packages/astro-rss/test/rss.test.js @@ -1,4 +1,4 @@ -import rss from '../dist/index.js'; +import rss, { getRssString } from '../dist/index.js'; import { rssSchema } from '../dist/schema.js'; import chai from 'chai'; import chaiPromises from 'chai-as-promised'; @@ -36,41 +36,73 @@ const validXmlWithStylesheet = `<?xml version="1.0" encoding="UTF-8"?><?xml-styl const validXmlWithXSLStylesheet = `<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/feedstylesheet.xsl" type="text/xsl"?><rss version="2.0"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link></channel></rss>`; describe('rss', () => { + it('should return a response', async () => { + const response = await rss({ + title, + description, + items: [phpFeedItem, web1FeedItem], + site, + }); + + const str = await response.text(); + chai.expect(str).xml.to.equal(validXmlResult); + + const contentType = response.headers.get('Content-Type'); + chai.expect(contentType).to.equal('application/xml'); + }); + + it('should be the same string as getRssString', async () => { + const options = { + title, + description, + items: [phpFeedItem, web1FeedItem], + site, + }; + + const response = await rss(options); + const str1 = await response.text(); + const str2 = await getRssString(options); + + chai.expect(str1).to.equal(str2); + }); +}); + +describe('getRssString', () => { it('should generate on valid RSSFeedItem array', async () => { - const { body } = await rss({ + const str = await getRssString({ title, description, items: [phpFeedItem, web1FeedItem], site, }); - chai.expect(body).xml.to.equal(validXmlResult); + chai.expect(str).xml.to.equal(validXmlResult); }); it('should generate on valid RSSFeedItem array with HTML content included', async () => { - const { body } = await rss({ + const str = await getRssString({ title, description, items: [phpFeedItemWithContent, web1FeedItemWithContent], site, }); - chai.expect(body).xml.to.equal(validXmlWithContentResult); + chai.expect(str).xml.to.equal(validXmlWithContentResult); }); it('should generate on valid RSSFeedItem array with all RSS content included', async () => { - const { body } = await rss({ + const str = await getRssString({ title, description, items: [phpFeedItem, web1FeedItemWithAllData], site, }); - chai.expect(body).xml.to.equal(validXmlResultWithAllData); + chai.expect(str).xml.to.equal(validXmlResultWithAllData); }); it('should generate on valid RSSFeedItem array with custom data included', async () => { - const { body } = await rss({ + const str = await getRssString({ xmlns: { dc: 'http://purl.org/dc/elements/1.1/', }, @@ -80,11 +112,11 @@ describe('rss', () => { site, }); - chai.expect(body).xml.to.equal(validXmlWithCustomDataResult); + chai.expect(str).xml.to.equal(validXmlWithCustomDataResult); }); it('should include xml-stylesheet instruction when stylesheet is defined', async () => { - const { body } = await rss({ + const str = await getRssString({ title, description, items: [], @@ -92,11 +124,11 @@ describe('rss', () => { stylesheet: '/feedstylesheet.css', }); - chai.expect(body).xml.to.equal(validXmlWithStylesheet); + chai.expect(str).xml.to.equal(validXmlWithStylesheet); }); it('should include xml-stylesheet instruction with xsl type when stylesheet is set to xsl file', async () => { - const { body } = await rss({ + const str = await getRssString({ title, description, items: [], @@ -104,13 +136,13 @@ describe('rss', () => { stylesheet: '/feedstylesheet.xsl', }); - chai.expect(body).xml.to.equal(validXmlWithXSLStylesheet); + chai.expect(str).xml.to.equal(validXmlWithXSLStylesheet); }); it('should preserve self-closing tags on `customData`', async () => { const customData = '<atom:link href="https://example.com/feed.xml" rel="self" type="application/rss+xml"/>'; - const { body } = await rss({ + const str = await getRssString({ title, description, items: [], @@ -121,22 +153,22 @@ describe('rss', () => { customData, }); - chai.expect(body).to.contain(customData); + chai.expect(str).to.contain(customData); }); it('should filter out entries marked as `draft`', async () => { - const { body } = await rss({ + const str = await getRssString({ title, description, items: [phpFeedItem, { ...web1FeedItem, draft: true }], site, }); - chai.expect(body).xml.to.equal(validXmlWithoutWeb1FeedResult); + chai.expect(str).xml.to.equal(validXmlWithoutWeb1FeedResult); }); it('should respect drafts option', async () => { - const { body } = await rss({ + const str = await getRssString({ title, description, items: [phpFeedItem, { ...web1FeedItem, draft: true }], @@ -144,11 +176,11 @@ describe('rss', () => { drafts: true, }); - chai.expect(body).xml.to.equal(validXmlResult); + chai.expect(str).xml.to.equal(validXmlResult); }); it('should not append trailing slash to URLs with the given option', async () => { - const { body } = await rss({ + const str = await getRssString({ title, description, items: [phpFeedItem, { ...web1FeedItem, draft: true }], @@ -157,8 +189,8 @@ describe('rss', () => { trailingSlash: false, }); - chai.expect(body).xml.to.contain('https://example.com/<'); - chai.expect(body).xml.to.contain('https://example.com/php<'); + chai.expect(str).xml.to.contain('https://example.com/<'); + chai.expect(str).xml.to.contain('https://example.com/php<'); }); it('Deprecated import.meta.glob mapping still works', async () => { @@ -187,14 +219,14 @@ describe('rss', () => { ), }; - const { body } = await rss({ + const str = await getRssString({ title, description, items: globResult, site, }); - chai.expect(body).xml.to.equal(validXmlResult); + chai.expect(str).xml.to.equal(validXmlResult); }); it('should fail when an invalid date string is provided', async () => { diff --git a/packages/astro-rss/tsconfig.json b/packages/astro-rss/tsconfig.json index f9da2a5bd..d8efd2fec 100644 --- a/packages/astro-rss/tsconfig.json +++ b/packages/astro-rss/tsconfig.json @@ -5,7 +5,7 @@ "allowJs": true, "module": "ES2022", "outDir": "./dist", - "target": "ES2021", + "target": "ES2022", "strictNullChecks": true } } |