diff --git a/apps/site/util/__tests__/feeds.test.mjs b/apps/site/util/__tests__/feeds.test.mjs index 78152b992bbfa..a9fa80e74817a 100644 --- a/apps/site/util/__tests__/feeds.test.mjs +++ b/apps/site/util/__tests__/feeds.test.mjs @@ -1,12 +1,18 @@ import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; import { describe, it } from 'node:test'; +import matter from 'gray-matter'; + import { BASE_URL, BASE_PATH } from '#site/next.constants.mjs'; import { siteConfig } from '#site/next.json.mjs'; import { generateWebsiteFeeds } from '../feeds'; const base = `${BASE_URL}${BASE_PATH}/en`; +const dcNamespace = 'http://purl.org/dc/elements/1.1/'; + +const parseXml = xml => new DOMParser().parseFromString(xml, 'text/xml'); describe('generateWebsiteFeeds', () => { it('generates website feeds with correct data', () => { @@ -15,6 +21,7 @@ describe('generateWebsiteFeeds', () => { { slug: '/post-1', title: 'Post 1', + author: 'Jane Doe', date: '2025-04-18', categories: ['all'], }, @@ -40,7 +47,134 @@ describe('generateWebsiteFeeds', () => { guid: `${blogData.posts[0].slug}?${date.getTime()}`, date, link: `${base}${blogData.posts[0].slug}`, + author: [{ name: blogData.posts[0].author }], }, ]); + + const rss = parseXml(blogFeed.rss2()); + assert.equal(rss.querySelector('parsererror'), null); + assert.equal( + rss.querySelector('item').lookupNamespaceURI('dc'), + dcNamespace + ); + assert.deepEqual( + Array.from( + rss.getElementsByTagNameNS(dcNamespace, 'creator'), + creator => creator.textContent + ), + ['Jane Doe'] + ); + assert.equal(rss.getElementsByTagName('author').length, 0); + }); + + it('keeps each author of the official Discord announcement in byline order', () => { + const { data } = matter( + readFileSync( + new URL( + '../../pages/en/blog/announcements/official-discord-launch-announcement.md', + import.meta.url + ), + 'utf8' + ) + ); + assert.equal(data.author, 'Carl Vitullo, Claudio Wunder'); + + const feed = generateWebsiteFeeds({ + posts: [ + { + ...data, + slug: '/blog/announcements/official-discord-launch-announcement', + categories: ['all'], + }, + ], + }).get('blog.xml'); + const names = ['Carl Vitullo', 'Claudio Wunder']; + + assert.deepEqual(feed.items[0].author, [ + { name: 'Carl Vitullo' }, + { name: 'Claudio Wunder' }, + ]); + + const rss = parseXml(feed.rss2()); + assert.equal(rss.querySelector('parsererror'), null); + assert.deepEqual( + Array.from( + rss.getElementsByTagNameNS(dcNamespace, 'creator'), + creator => creator.textContent + ), + names + ); + assert.equal(rss.getElementsByTagName('author').length, 0); + + const atom = parseXml(feed.atom1()); + assert.equal(atom.querySelector('parsererror'), null); + assert.deepEqual( + Array.from( + atom.querySelectorAll('entry > author > name'), + name => name.textContent + ), + names + ); + assert.deepEqual(JSON.parse(feed.json1()).items[0], { + url: `${base}/blog/announcements/official-discord-launch-announcement`, + title: data.title, + date_modified: new Date(data.date).toISOString(), + author: { name: 'Carl Vitullo' }, + }); }); + + it('uses the website byline separators and escapes each RSS creator', () => { + const feed = generateWebsiteFeeds({ + posts: [ + { + slug: '/post-1', + title: 'Post 1', + author: + ' Máría (@maria) & O\'Connor > "editor" and Jane Doe; John Doe ', + date: '2025-04-18', + categories: ['all'], + }, + ], + }).get('blog.xml'); + const xml = feed.rss2(); + const rss = parseXml(xml); + + assert.equal(rss.querySelector('parsererror'), null); + assert.deepEqual( + Array.from( + rss.getElementsByTagNameNS(dcNamespace, 'creator'), + creator => creator.textContent + ), + ['Máría ', 'O\'Connor > "editor"', 'Jane Doe', 'John Doe'] + ); + assert.match(xml, /Máría <admin><\/dc:creator>/); + assert.equal(rss.getElementsByTagName('admin').length, 0); + assert.equal(rss.getElementsByTagName('author').length, 0); + }); + + for (const author of [undefined, '', ' ', ', ; & and ', '(@ghost)']) { + it(`omits author elements for an empty byline: ${JSON.stringify(author)}`, () => { + const feed = generateWebsiteFeeds({ + posts: [ + { + slug: '/post-1', + title: 'Post 1', + author, + date: '2025-04-18', + categories: ['all'], + }, + ], + }).get('blog.xml'); + const rss = parseXml(feed.rss2()); + + assert.equal(rss.querySelector('parsererror'), null); + assert.equal( + rss.getElementsByTagNameNS(dcNamespace, 'creator').length, + 0 + ); + assert.equal(rss.getElementsByTagName('author').length, 0); + assert.equal(parseXml(feed.atom1()).querySelectorAll('author').length, 0); + assert.equal(JSON.parse(feed.json1()).items[0].author, undefined); + }); + } }); diff --git a/apps/site/util/feeds.ts b/apps/site/util/feeds.ts index f2e1bdf5940f4..67802480e36f4 100644 --- a/apps/site/util/feeds.ts +++ b/apps/site/util/feeds.ts @@ -8,6 +8,7 @@ import { siteConfig } from '#site/next.json.mjs'; import type { BlogPostsRSC } from '#site/types'; import type { FeedOptions } from 'feed'; +import { mapAuthorToCardAuthors } from './author'; import { getBlogPosts } from './blog'; // This is the Base URL for the Node.js Website @@ -42,6 +43,11 @@ export const generateWebsiteFeeds = ({ posts }: BlogPostsRSC) => { .map(post => { const date = new Date(post.date); const time = date.getTime(); + const authors = mapAuthorToCardAuthors(post.author ?? '').map( + name => ({ + name, + }) + ); return { title: post.title, @@ -51,11 +57,33 @@ export const generateWebsiteFeeds = ({ posts }: BlogPostsRSC) => { time > guidTimestampStartDate ? `${post.slug}?${date.getTime()}` : post.slug, + author: authors.length ? authors : undefined, }; }); blogFeedEntries.forEach(entry => feed.addItem(entry)); + const rss2 = feed.rss2; + feed.rss2 = () => + rss2.call({ + ...feed, + items: feed.items.map(({ author, ...entry }) => ({ + ...entry, + extensions: author + ? [ + { + name: '_attributes', + objects: { 'xmlns:dc': 'http://purl.org/dc/elements/1.1/' }, + }, + { + name: 'dc:creator', + objects: author.map(({ name }) => ({ _text: name })), + }, + ] + : [], + })), + }); + return [file, feed]; } );