mirror of
https://github.com/withastro/astro.git
synced 2025-04-07 23:41:43 -05:00
fix(@astrojs/rss): prevent an error when pubDate
is missing (#12137)
* fix(rss): prevent error when `pubDate` is missing * docs(rss): switch some RSS items to optional
This commit is contained in:
parent
2385d58389
commit
50dd88bc66
6 changed files with 37 additions and 10 deletions
.changeset
packages/astro-rss
5
.changeset/ninety-dolls-reply.md
Normal file
5
.changeset/ninety-dolls-reply.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/rss': patch
|
||||
---
|
||||
|
||||
Fixes an error that occurred when the optional `pubDate` property was missing in an item.
|
5
.changeset/plenty-kings-swim.md
Normal file
5
.changeset/plenty-kings-swim.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/rss': patch
|
||||
---
|
||||
|
||||
Fixes an error where docs incorrectly stated the `title`, `link` and `pubDate` properties of RSS items was required.
|
|
@ -198,19 +198,19 @@ const item = {
|
|||
|
||||
### `title`
|
||||
|
||||
Type: `string (required)`
|
||||
Type: `string (optional)`
|
||||
|
||||
The title of the item in the feed.
|
||||
The title of the item in the feed. Optional only if a description is set. Otherwise, required.
|
||||
|
||||
### `link`
|
||||
|
||||
Type: `string (required)`
|
||||
Type: `string (optional)`
|
||||
|
||||
The URL of the item on the web.
|
||||
|
||||
### `pubDate`
|
||||
|
||||
Type: `Date (required)`
|
||||
Type: `Date (optional)`
|
||||
|
||||
Indicates when the item was published.
|
||||
|
||||
|
@ -218,7 +218,7 @@ Indicates when the item was published.
|
|||
|
||||
Type: `string (optional)`
|
||||
|
||||
A synopsis of your item when you are publishing the full content of the item in the `content` field. The `description` may alternatively be the full content of the item in the feed if you are not using the `content` field (entity-coded HTML is permitted).
|
||||
A synopsis of your item when you are publishing the full content of the item in the `content` field. The `description` may alternatively be the full content of the item in the feed if you are not using the `content` field (entity-coded HTML is permitted). Optional only if a title is set. Otherwise, required.
|
||||
|
||||
### `content`
|
||||
|
||||
|
|
|
@ -5,9 +5,9 @@ export const rssSchema = z.object({
|
|||
description: z.string().optional(),
|
||||
pubDate: z
|
||||
.union([z.string(), z.number(), z.date()])
|
||||
.optional()
|
||||
.transform((value) => (value === undefined ? value : new Date(value)))
|
||||
.refine((value) => (value === undefined ? value : !isNaN(value.getTime()))),
|
||||
.transform((value) => new Date(value))
|
||||
.refine((value) => !isNaN(value.getTime()))
|
||||
.optional(),
|
||||
customData: z.string().optional(),
|
||||
categories: z.array(z.string()).optional(),
|
||||
author: z.string().optional(),
|
||||
|
|
|
@ -10,6 +10,7 @@ import {
|
|||
phpFeedItem,
|
||||
phpFeedItemWithContent,
|
||||
phpFeedItemWithCustomData,
|
||||
phpFeedItemWithoutDate,
|
||||
site,
|
||||
title,
|
||||
web1FeedItem,
|
||||
|
@ -25,6 +26,8 @@ const validXmlResult = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
|
|||
// biome-ignore format: keep in one line
|
||||
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 isPermaLink="true">${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 isPermaLink="true">${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>`;
|
||||
// biome-ignore format: keep in one line
|
||||
const validXmlResultWithMissingDate = `<?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[${phpFeedItemWithoutDate.title}]]></title><link>${site}${phpFeedItemWithoutDate.link}/</link><guid isPermaLink="true">${site}${phpFeedItemWithoutDate.link}/</guid><description><![CDATA[${phpFeedItemWithoutDate.description}]]></description></item><item><title><![CDATA[${phpFeedItem.title}]]></title><link>${site}${phpFeedItem.link}/</link><guid isPermaLink="true">${site}${phpFeedItem.link}/</guid><description><![CDATA[${phpFeedItem.description}]]></description><pubDate>${new Date(phpFeedItem.pubDate).toUTCString()}</pubDate></item></channel></rss>`;
|
||||
// biome-ignore format: keep in one line
|
||||
const validXmlResultWithAllData = `<?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 isPermaLink="true">${site}${phpFeedItem.link}/</guid><description><![CDATA[${phpFeedItem.description}]]></description><pubDate>${new Date(phpFeedItem.pubDate).toUTCString()}</pubDate></item><item><title><![CDATA[${web1FeedItemWithAllData.title}]]></title><link>${site}${web1FeedItemWithAllData.link}/</link><guid isPermaLink="true">${site}${web1FeedItemWithAllData.link}/</guid><description><![CDATA[${web1FeedItemWithAllData.description}]]></description><pubDate>${new Date(web1FeedItemWithAllData.pubDate).toUTCString()}</pubDate><category>${web1FeedItemWithAllData.categories[0]}</category><category>${web1FeedItemWithAllData.categories[1]}</category><author>${web1FeedItemWithAllData.author}</author><comments>${web1FeedItemWithAllData.commentsUrl}</comments><source url="${web1FeedItemWithAllData.source.url}">${web1FeedItemWithAllData.source.title}</source><enclosure url="${site}${web1FeedItemWithAllData.enclosure.url}" length="${web1FeedItemWithAllData.enclosure.length}" type="${web1FeedItemWithAllData.enclosure.type}"/></item></channel></rss>`;
|
||||
// biome-ignore format: keep in one line
|
||||
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 isPermaLink="true">${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 isPermaLink="true">${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>`;
|
||||
|
@ -101,6 +104,17 @@ describe('getRssString', () => {
|
|||
assertXmlDeepEqual(str, validXmlWithContentResult);
|
||||
});
|
||||
|
||||
it('should generate on valid RSSFeedItem array with missing date', async () => {
|
||||
const str = await getRssString({
|
||||
title,
|
||||
description,
|
||||
items: [phpFeedItemWithoutDate, phpFeedItem],
|
||||
site,
|
||||
});
|
||||
|
||||
assertXmlDeepEqual(str, validXmlResultWithMissingDate);
|
||||
});
|
||||
|
||||
it('should generate on valid RSSFeedItem array with all RSS content included', async () => {
|
||||
const str = await getRssString({
|
||||
title,
|
||||
|
|
|
@ -4,13 +4,16 @@ export const title = 'My RSS feed';
|
|||
export const description = 'This sure is a nice RSS feed';
|
||||
export const site = 'https://example.com';
|
||||
|
||||
export const phpFeedItem = {
|
||||
export const phpFeedItemWithoutDate = {
|
||||
link: '/php',
|
||||
title: 'Remember PHP?',
|
||||
pubDate: '1994-05-03',
|
||||
description:
|
||||
'PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994.',
|
||||
};
|
||||
export const phpFeedItem = {
|
||||
...phpFeedItemWithoutDate,
|
||||
pubDate: '1994-05-03',
|
||||
};
|
||||
export const phpFeedItemWithContent = {
|
||||
...phpFeedItem,
|
||||
content: `<h1>${phpFeedItem.title}</h1><p>${phpFeedItem.description}</p>`,
|
||||
|
|
Loading…
Add table
Reference in a new issue