From 921a57ca9411c25733fd75bf77c982fff164ed0c Mon Sep 17 00:00:00 2001 From: John Reilly Date: Thu, 10 Nov 2022 18:03:45 +0000 Subject: [PATCH 01/17] fix: update historic g-tags --- .../version-2.0.1/api/plugins/plugin-google-gtag.md | 2 +- .../version-2.1.0/api/plugins/plugin-google-gtag.md | 2 +- .../version-2.2.0/api/plugins/plugin-google-gtag.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/website/versioned_docs/version-2.0.1/api/plugins/plugin-google-gtag.md b/website/versioned_docs/version-2.0.1/api/plugins/plugin-google-gtag.md index db414c4d0f21..fca251edd3bb 100644 --- a/website/versioned_docs/version-2.0.1/api/plugins/plugin-google-gtag.md +++ b/website/versioned_docs/version-2.0.1/api/plugins/plugin-google-gtag.md @@ -67,7 +67,7 @@ Most Docusaurus users configure this plugin through the preset options. // Plugin Options: @docusaurus/plugin-google-gtag const config = { - trackingID: 'G-226F0LR9KE', + trackingID: 'G-999X9XX9XX', anonymizeIP: true, }; ``` diff --git a/website/versioned_docs/version-2.1.0/api/plugins/plugin-google-gtag.md b/website/versioned_docs/version-2.1.0/api/plugins/plugin-google-gtag.md index db414c4d0f21..fca251edd3bb 100644 --- a/website/versioned_docs/version-2.1.0/api/plugins/plugin-google-gtag.md +++ b/website/versioned_docs/version-2.1.0/api/plugins/plugin-google-gtag.md @@ -67,7 +67,7 @@ Most Docusaurus users configure this plugin through the preset options. // Plugin Options: @docusaurus/plugin-google-gtag const config = { - trackingID: 'G-226F0LR9KE', + trackingID: 'G-999X9XX9XX', anonymizeIP: true, }; ``` diff --git a/website/versioned_docs/version-2.2.0/api/plugins/plugin-google-gtag.md b/website/versioned_docs/version-2.2.0/api/plugins/plugin-google-gtag.md index db414c4d0f21..fca251edd3bb 100644 --- a/website/versioned_docs/version-2.2.0/api/plugins/plugin-google-gtag.md +++ b/website/versioned_docs/version-2.2.0/api/plugins/plugin-google-gtag.md @@ -67,7 +67,7 @@ Most Docusaurus users configure this plugin through the preset options. // Plugin Options: @docusaurus/plugin-google-gtag const config = { - trackingID: 'G-226F0LR9KE', + trackingID: 'G-999X9XX9XX', anonymizeIP: true, }; ``` From 0bbe8fe195af23f854f42cf03233b27cbf5c4750 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Sat, 26 Nov 2022 08:48:10 +0000 Subject: [PATCH 02/17] feat: example implementation of blog filter --- .../__tests__/__snapshots__/feed.test.ts.snap | 129 ++++++++++++++++++ .../src/__tests__/feed.test.ts | 43 ++++++ .../src/feed.ts | 5 +- .../src/plugin-content-blog.d.ts | 2 + 4 files changed, 178 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap index bf7d608e30c6..8939a660425b 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap @@ -1,5 +1,47 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`atom filters to the first three entries 1`] = ` +[ + " + + https://docusaurus.io/myBaseUrl/blog + Hello Blog + 2021-03-06T00:00:00.000Z + https://github.com/jpmonette/feed + + Hello Blog + https://docusaurus.io/myBaseUrl/image/favicon.ico + Copyright + + <![CDATA[MDX Blog Sample with require calls]]> + /mdx-require-blog-post + + 2021-03-06T00:00:00.000Z + + Test MDX with require calls

]]>
+
+ + <![CDATA[Full Blog Sample]]> + /mdx-blog-post + + 2021-03-05T00:00:00.000Z + + HTML Heading 1

HTML Heading 2

HTML Paragraph

Import DOM

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
  • list1
  • list2
  • list3
  • list1
  • list2
  • list3

Normal Text Italics Text Bold Text

linkimage

]]>
+
+ + <![CDATA[Complex Slug]]> + /hey/my super path/héllô + + 2020-08-16T00:00:00.000Z + + complex url slug

]]>
+ + +
+
", +] +`; + exports[`atom has feed item for each post 1`] = ` [ " @@ -84,6 +126,49 @@ exports[`atom has feed item for each post 1`] = ` ] `; +exports[`json filters to the first three entries 1`] = ` +[ + "{ + "version": "https://jsonfeed.org/version/1", + "title": "Hello Blog", + "home_page_url": "https://docusaurus.io/myBaseUrl/blog", + "description": "Hello Blog", + "items": [ + { + "id": "/mdx-require-blog-post", + "content_html": "

Test MDX with require calls

", + "url": "https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post", + "title": "MDX Blog Sample with require calls", + "summary": "Test MDX with require calls", + "date_modified": "2021-03-06T00:00:00.000Z", + "tags": [] + }, + { + "id": "/mdx-blog-post", + "content_html": "

HTML Heading 1

HTML Heading 2

HTML Paragraph

Import DOM

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5

Normal Text Italics Text Bold Text

link\\"image\\"

", + "url": "https://docusaurus.io/myBaseUrl/blog/mdx-blog-post", + "title": "Full Blog Sample", + "summary": "HTML Heading 1", + "date_modified": "2021-03-05T00:00:00.000Z", + "tags": [] + }, + { + "id": "/hey/my super path/héllô", + "content_html": "

complex url slug

", + "url": "https://docusaurus.io/myBaseUrl/blog/hey/my super path/héllô", + "title": "Complex Slug", + "summary": "complex url slug", + "date_modified": "2020-08-16T00:00:00.000Z", + "tags": [ + "date", + "complex" + ] + } + ] +}", +] +`; + exports[`json has feed item for each post 1`] = ` [ "{ @@ -171,6 +256,50 @@ exports[`json has feed item for each post 1`] = ` ] `; +exports[`rss filters to the first three entries 1`] = ` +[ + " + + + Hello Blog + https://docusaurus.io/myBaseUrl/blog + Hello Blog + Sat, 06 Mar 2021 00:00:00 GMT + https://validator.w3.org/feed/docs/rss2.html + https://github.com/jpmonette/feed + en + Copyright + + <![CDATA[MDX Blog Sample with require calls]]> + https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post + /mdx-require-blog-post + Sat, 06 Mar 2021 00:00:00 GMT + + Test MDX with require calls

]]>
+
+ + <![CDATA[Full Blog Sample]]> + https://docusaurus.io/myBaseUrl/blog/mdx-blog-post + /mdx-blog-post + Fri, 05 Mar 2021 00:00:00 GMT + + HTML Heading 1

HTML Heading 2

HTML Paragraph

Import DOM

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
  • list1
  • list2
  • list3
  • list1
  • list2
  • list3

Normal Text Italics Text Bold Text

linkimage

]]>
+
+ + <![CDATA[Complex Slug]]> + https://docusaurus.io/myBaseUrl/blog/hey/my super path/héllô + /hey/my super path/héllô + Sun, 16 Aug 2020 00:00:00 GMT + + complex url slug

]]>
+ date + complex +
+
+
", +] +`; + exports[`rss has feed item for each post 1`] = ` [ " diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts index 83d0af77157c..3a764d086a52 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts @@ -143,4 +143,47 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => { ).toMatchSnapshot(); fsMock.mockClear(); }); + + it('filters to the first three entries', async () => { + const siteDir = path.join(__dirname, '__fixtures__', 'website'); + const outDir = path.join(siteDir, 'build-snap'); + const siteConfig = { + title: 'Hello', + baseUrl: '/myBaseUrl/', + url: 'https://docusaurus.io', + favicon: 'image/favicon.ico', + }; + + // Build is quite difficult to mock, so we built the blog beforehand and + // copied the output to the fixture... + await testGenerateFeeds( + { + siteDir, + siteConfig, + i18n: DefaultI18N, + outDir, + } as LoadContext, + { + path: 'blog', + routeBasePath: 'blog', + tagsBasePath: 'tags', + authorsMapPath: 'authors.yml', + include: DEFAULT_OPTIONS.include, + exclude: DEFAULT_OPTIONS.exclude, + feedOptions: { + type: [feedType], + copyright: 'Copyright', + filter: (items, index) => index < 3, + }, + readingTime: ({content, defaultReadingTime}) => + defaultReadingTime({content}), + truncateMarker: //, + } as PluginOptions, + ); + + expect( + fsMock.mock.calls.map((call) => call[1] as string), + ).toMatchSnapshot(); + fsMock.mockClear(); + }); }); diff --git a/packages/docusaurus-plugin-content-blog/src/feed.ts b/packages/docusaurus-plugin-content-blog/src/feed.ts index 81d7d45fe99f..1eb8db9b59dd 100644 --- a/packages/docusaurus-plugin-content-blog/src/feed.ts +++ b/packages/docusaurus-plugin-content-blog/src/feed.ts @@ -154,9 +154,12 @@ export async function createBlogFeedFiles({ locale: string; }): Promise { const blogPosts = allBlogPosts.filter(shouldBeInFeed); + const filteredBlogPosts = options.feedOptions.filter + ? blogPosts.filter(options.feedOptions.filter) + : blogPosts; const feed = await generateBlogFeed({ - blogPosts, + blogPosts: filteredBlogPosts, options, siteConfig, outDir, diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 96c288d335f4..916b1e609a0c 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -263,6 +263,8 @@ declare module '@docusaurus/plugin-content-blog' { copyright: string; /** Language of the feed. */ language?: string; + /** Filter that can be applied to reduce the number of entries */ + filter: (blogPost: BlogPost, index: number) => boolean; }; /** From 915dc4565049f174572c18cc747a698baf07b3aa Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Sat, 26 Nov 2022 09:27:47 +0000 Subject: [PATCH 03/17] feat: demo on website --- .../__tests__/__snapshots__/feed.test.ts.snap | 38 ++----------------- .../src/__tests__/feed.test.ts | 4 +- .../src/plugin-content-blog.d.ts | 2 +- website/docusaurus.config.js | 1 + 4 files changed, 7 insertions(+), 38 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap index 8939a660425b..52900ac8955c 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`atom filters to the first three entries 1`] = ` +exports[`atom filters to the first two entries 1`] = ` [ " @@ -28,16 +28,6 @@ exports[`atom filters to the first three entries 1`] = ` HTML Heading 1

HTML Heading 2

HTML Paragraph

Import DOM

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
  • list1
  • list2
  • list3
  • list1
  • list2
  • list3

Normal Text Italics Text Bold Text

linkimage

]]>
- - <![CDATA[Complex Slug]]> - /hey/my super path/héllô - - 2020-08-16T00:00:00.000Z - - complex url slug

]]>
- - -
", ] `; @@ -126,7 +116,7 @@ exports[`atom has feed item for each post 1`] = ` ] `; -exports[`json filters to the first three entries 1`] = ` +exports[`json filters to the first two entries 1`] = ` [ "{ "version": "https://jsonfeed.org/version/1", @@ -151,18 +141,6 @@ exports[`json filters to the first three entries 1`] = ` "summary": "HTML Heading 1", "date_modified": "2021-03-05T00:00:00.000Z", "tags": [] - }, - { - "id": "/hey/my super path/héllô", - "content_html": "

complex url slug

", - "url": "https://docusaurus.io/myBaseUrl/blog/hey/my super path/héllô", - "title": "Complex Slug", - "summary": "complex url slug", - "date_modified": "2020-08-16T00:00:00.000Z", - "tags": [ - "date", - "complex" - ] } ] }", @@ -256,7 +234,7 @@ exports[`json has feed item for each post 1`] = ` ] `; -exports[`rss filters to the first three entries 1`] = ` +exports[`rss filters to the first two entries 1`] = ` [ " @@ -285,16 +263,6 @@ exports[`rss filters to the first three entries 1`] = ` HTML Heading 1

HTML Heading 2

HTML Paragraph

Import DOM

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
  • list1
  • list2
  • list3
  • list1
  • list2
  • list3

Normal Text Italics Text Bold Text

linkimage

]]>
- - <![CDATA[Complex Slug]]> - https://docusaurus.io/myBaseUrl/blog/hey/my super path/héllô - /hey/my super path/héllô - Sun, 16 Aug 2020 00:00:00 GMT - - complex url slug

]]>
- date - complex -
", ] diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts index 3a764d086a52..2a5c7dfa879b 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts @@ -144,7 +144,7 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => { fsMock.mockClear(); }); - it('filters to the first three entries', async () => { + it('filters to the first two entries', async () => { const siteDir = path.join(__dirname, '__fixtures__', 'website'); const outDir = path.join(siteDir, 'build-snap'); const siteConfig = { @@ -173,7 +173,7 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => { feedOptions: { type: [feedType], copyright: 'Copyright', - filter: (items, index) => index < 3, + filter: (items, index) => index < 2, }, readingTime: ({content, defaultReadingTime}) => defaultReadingTime({content}), diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 916b1e609a0c..42cbe04ed6f6 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -264,7 +264,7 @@ declare module '@docusaurus/plugin-content-blog' { /** Language of the feed. */ language?: string; /** Filter that can be applied to reduce the number of entries */ - filter: (blogPost: BlogPost, index: number) => boolean; + filter?: (blogPost: BlogPost, index: number) => boolean; }; /** diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 6e00175fb995..f025aa2db35b 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -341,6 +341,7 @@ const config = { feedOptions: { type: 'all', copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`, + filter: (items, index) => index < 2, }, blogSidebarCount: 'ALL', blogSidebarTitle: 'All our posts', From fb9eb216df09f3d4f71b6a7984881862857cb0d7 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Sat, 26 Nov 2022 09:32:41 +0000 Subject: [PATCH 04/17] feat: function --- packages/docusaurus-plugin-content-blog/src/options.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/docusaurus-plugin-content-blog/src/options.ts b/packages/docusaurus-plugin-content-blog/src/options.ts index 99c4067a9ad8..a3ab0834bd7a 100644 --- a/packages/docusaurus-plugin-content-blog/src/options.ts +++ b/packages/docusaurus-plugin-content-blog/src/options.ts @@ -124,6 +124,7 @@ const PluginOptionSchema = Joi.object({ .default(DEFAULT_OPTIONS.feedOptions.copyright), }), language: Joi.string(), + filter: Joi.function(), }).default(DEFAULT_OPTIONS.feedOptions), authorsMapPath: Joi.string().default(DEFAULT_OPTIONS.authorsMapPath), readingTime: Joi.function().default(() => DEFAULT_OPTIONS.readingTime), From 2d88ab4a92279dd8cb1d92edbba30f44d5fcf693 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Sat, 26 Nov 2022 18:25:58 +0000 Subject: [PATCH 05/17] docs: add to docs --- .../docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts | 2 +- website/docs/blog.mdx | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 42cbe04ed6f6..65c748b5a360 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -264,7 +264,7 @@ declare module '@docusaurus/plugin-content-blog' { /** Language of the feed. */ language?: string; /** Filter that can be applied to reduce the number of entries */ - filter?: (blogPost: BlogPost, index: number) => boolean; + filter?: (blogPost: BlogPost, index?: number) => boolean; }; /** diff --git a/website/docs/blog.mdx b/website/docs/blog.mdx index 780a07dbca87..d176806e6532 100644 --- a/website/docs/blog.mdx +++ b/website/docs/blog.mdx @@ -511,6 +511,7 @@ type BlogOptions = { description?: string; copyright: string; language?: string; // possible values: http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes + filter?: (blogPost: BlogPost, index?: number) => boolean; }; }; ``` From aeeee383ea52ddcf98549e94fd119a96b4f30263 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Sun, 27 Nov 2022 11:21:15 +0000 Subject: [PATCH 06/17] fix: type --- .../docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 65c748b5a360..42cbe04ed6f6 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -264,7 +264,7 @@ declare module '@docusaurus/plugin-content-blog' { /** Language of the feed. */ language?: string; /** Filter that can be applied to reduce the number of entries */ - filter?: (blogPost: BlogPost, index?: number) => boolean; + filter?: (blogPost: BlogPost, index: number) => boolean; }; /** From d3317d98770ea99b10b15b044dffc40b289ed51d Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Tue, 27 Dec 2022 10:08:55 +0000 Subject: [PATCH 07/17] feat: replace filter with createFeedItems --- .../src/__tests__/feed.test.ts | 11 +++- .../src/feed.ts | 41 +++++++++---- .../src/options.ts | 2 +- .../src/plugin-content-blog.d.ts | 59 ++++++++++++++++++- website/docs/blog.mdx | 22 ++++++- website/docusaurus.config.js | 6 +- 6 files changed, 125 insertions(+), 16 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts index 2a5c7dfa879b..1552b0193f10 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts @@ -173,7 +173,16 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => { feedOptions: { type: [feedType], copyright: 'Copyright', - filter: (items, index) => index < 2, + createFeedItems: async (options) => { + const {blogPosts, defaultCreateFeedItems, ...others} = options; + const blogPostsFiltered = blogPosts.filter( + (item, index) => index < 2, + ); + return defaultCreateFeedItems({ + blogPosts: blogPostsFiltered, + ...others, + }); + }, }, readingTime: ({content, defaultReadingTime}) => defaultReadingTime({content}), diff --git a/packages/docusaurus-plugin-content-blog/src/feed.ts b/packages/docusaurus-plugin-content-blog/src/feed.ts index 1eb8db9b59dd..c5574d05f106 100644 --- a/packages/docusaurus-plugin-content-blog/src/feed.ts +++ b/packages/docusaurus-plugin-content-blog/src/feed.ts @@ -8,7 +8,7 @@ import path from 'path'; import fs from 'fs-extra'; import logger from '@docusaurus/logger'; -import {Feed, type Author as FeedAuthor, type Item as FeedItem} from 'feed'; +import {Feed, type Author as FeedAuthor} from 'feed'; import {normalizeUrl, readOutputHTMLFile} from '@docusaurus/utils'; import {blogPostContainerID} from '@docusaurus/utils-common'; import {load as cheerioLoad} from 'cheerio'; @@ -18,6 +18,7 @@ import type { PluginOptions, Author, BlogPost, + BlogFeedItem, } from '@docusaurus/plugin-content-blog'; async function generateBlogFeed({ @@ -54,11 +55,35 @@ async function generateBlogFeed({ copyright: feedOptions.copyright, }); + const feedItems = await (feedOptions.createFeedItems + ? feedOptions.createFeedItems({ + blogPosts, + siteConfig, + outDir, + defaultCreateFeedItems, + }) + : defaultCreateFeedItems({blogPosts, siteConfig, outDir})); + feedItems.forEach(feed.addItem); + + return feed; +} + +async function defaultCreateFeedItems({ + blogPosts, + siteConfig, + outDir, +}: { + blogPosts: BlogPost[]; + siteConfig: DocusaurusConfig; + outDir: string; +}): Promise { + const {url: siteUrl} = siteConfig; + function toFeedAuthor(author: Author): FeedAuthor { return {name: author.name, link: author.url, email: author.email}; } - await Promise.all( + const items = await Promise.all( blogPosts.map(async (post) => { const { id, @@ -79,7 +104,7 @@ async function generateBlogFeed({ ); const $ = cheerioLoad(content); - const feedItem: FeedItem = { + const feedItem: BlogFeedItem = { title: metadataTitle, id, link: normalizeUrl([siteUrl, permalink]), @@ -99,9 +124,8 @@ async function generateBlogFeed({ return feedItem; }), - ).then((items) => items.forEach(feed.addItem)); - - return feed; + ); + return items; } async function createBlogFeedFile({ @@ -154,12 +178,9 @@ export async function createBlogFeedFiles({ locale: string; }): Promise { const blogPosts = allBlogPosts.filter(shouldBeInFeed); - const filteredBlogPosts = options.feedOptions.filter - ? blogPosts.filter(options.feedOptions.filter) - : blogPosts; const feed = await generateBlogFeed({ - blogPosts: filteredBlogPosts, + blogPosts, options, siteConfig, outDir, diff --git a/packages/docusaurus-plugin-content-blog/src/options.ts b/packages/docusaurus-plugin-content-blog/src/options.ts index a3ab0834bd7a..895814a0c3ff 100644 --- a/packages/docusaurus-plugin-content-blog/src/options.ts +++ b/packages/docusaurus-plugin-content-blog/src/options.ts @@ -124,7 +124,7 @@ const PluginOptionSchema = Joi.object({ .default(DEFAULT_OPTIONS.feedOptions.copyright), }), language: Joi.string(), - filter: Joi.function(), + createFeedItems: Joi.function(), }).default(DEFAULT_OPTIONS.feedOptions), authorsMapPath: Joi.string().default(DEFAULT_OPTIONS.authorsMapPath), readingTime: Joi.function().default(() => DEFAULT_OPTIONS.readingTime), diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 42cbe04ed6f6..d3ade1f07228 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -263,8 +263,17 @@ declare module '@docusaurus/plugin-content-blog' { copyright: string; /** Language of the feed. */ language?: string; - /** Filter that can be applied to reduce the number of entries */ - filter?: (blogPost: BlogPost, index: number) => boolean; + /** Allow control over the construction of BlogFeedItems */ + createFeedItems?: (options: { + blogPosts: BlogPost[]; + siteConfig: DocusaurusConfig; + outDir: string; + defaultCreateFeedItems: (params: { + blogPosts: BlogPost[]; + siteConfig: DocusaurusConfig; + outDir: string; + }) => Promise; + }) => Promise; }; /** @@ -453,6 +462,52 @@ declare module '@docusaurus/plugin-content-blog' { content: string; }; + export type BlogFeedItem = { + title: string; + id?: string; + link: string; + date: Date; + description?: string; + content?: string; + category?: Category[]; + guid?: string; + image?: string | Enclosure; + audio?: string | Enclosure; + video?: string | Enclosure; + enclosure?: Enclosure; + author?: BlogFeedItemAuthor[]; + contributor?: BlogFeedItemAuthor[]; + published?: Date; + copyright?: string; + extensions?: Extension[]; + }; + + export type BlogFeedItemAuthor = { + name?: string; + email?: string; + link?: string; + }; + + export type Enclosure = { + url: string; + type?: string; + length?: number; + title?: string; + duration?: number; + }; + + export type Extension = { + name: string; + objects: unknown; + }; + + export type Category = { + name?: string; + domain?: string; + scheme?: string; + term?: string; + }; + export type BlogPaginatedMetadata = { /** Title of the entire blog. */ readonly blogTitle: string; diff --git a/website/docs/blog.mdx b/website/docs/blog.mdx index d176806e6532..13d468ee415c 100644 --- a/website/docs/blog.mdx +++ b/website/docs/blog.mdx @@ -511,7 +511,17 @@ type BlogOptions = { description?: string; copyright: string; language?: string; // possible values: http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes - filter?: (blogPost: BlogPost, index?: number) => boolean; + /** Allow control over the construction of BlogFeedItems */ + createFeedItems?: (options: { + blogPosts: BlogPost[]; + siteConfig: DocusaurusConfig; + outDir: string; + defaultCreateFeedItems: (params: { + blogPosts: BlogPost[]; + siteConfig: DocusaurusConfig; + outDir: string; + }) => Promise; + }) => Promise; }; }; ``` @@ -530,6 +540,16 @@ module.exports = { feedOptions: { type: 'all', copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`, + createFeedItems: async (options) => { + const {blogPosts, defaultCreateFeedItems, ...others} = options; + const blogPostsFiltered = blogPosts.filter( + (item, index) => index < 2, + ); + return defaultCreateFeedItems({ + blogPosts: blogPostsFiltered, + ...others, + }); + }, }, // highlight-end }, diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index f025aa2db35b..241c9dfa7a82 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -341,7 +341,11 @@ const config = { feedOptions: { type: 'all', copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`, - filter: (items, index) => index < 2, + createFeedItems: async (options) => { + const { blogPosts, defaultCreateFeedItems, ...others } = options; + const blogPostsFiltered = blogPosts.filter((item, index) => index < 2); + return defaultCreateFeedItems({ blogPosts: blogPostsFiltered, ...others }); + } }, blogSidebarCount: 'ALL', blogSidebarTitle: 'All our posts', From 941edccfd6716efe587a81470009eb93ea00edd7 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Tue, 27 Dec 2022 17:08:09 +0000 Subject: [PATCH 08/17] fix: update snapshots --- .../src/__tests__/__snapshots__/feed.test.ts.snap | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap index a68b3fd81df3..ae568922d131 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap @@ -14,7 +14,7 @@ exports[`atom filters to the first two entries 1`] = ` Copyright <![CDATA[MDX Blog Sample with require calls]]> - /mdx-require-blog-post + https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post 2021-03-06T00:00:00.000Z @@ -22,7 +22,7 @@ exports[`atom filters to the first two entries 1`] = ` <![CDATA[Full Blog Sample]]> - /mdx-blog-post + https://docusaurus.io/myBaseUrl/blog/mdx-blog-post 2021-03-05T00:00:00.000Z @@ -125,7 +125,7 @@ exports[`json filters to the first two entries 1`] = ` "description": "Hello Blog", "items": [ { - "id": "/mdx-require-blog-post", + "id": "https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post", "content_html": "

Test MDX with require calls

", "url": "https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post", "title": "MDX Blog Sample with require calls", @@ -134,7 +134,7 @@ exports[`json filters to the first two entries 1`] = ` "tags": [] }, { - "id": "/mdx-blog-post", + "id": "https://docusaurus.io/myBaseUrl/blog/mdx-blog-post", "content_html": "

HTML Heading 1

HTML Heading 2

HTML Paragraph

Import DOM

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
  • list1
  • list2
  • list3
  • list1
  • list2
  • list3

Normal Text Italics Text Bold Text

link\\"image\\"

", "url": "https://docusaurus.io/myBaseUrl/blog/mdx-blog-post", "title": "Full Blog Sample", @@ -250,7 +250,7 @@ exports[`rss filters to the first two entries 1`] = ` <![CDATA[MDX Blog Sample with require calls]]> https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post - /mdx-require-blog-post + https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post Sat, 06 Mar 2021 00:00:00 GMT Test MDX with require calls

]]>
@@ -258,7 +258,7 @@ exports[`rss filters to the first two entries 1`] = ` <![CDATA[Full Blog Sample]]> https://docusaurus.io/myBaseUrl/blog/mdx-blog-post - /mdx-blog-post + https://docusaurus.io/myBaseUrl/blog/mdx-blog-post Fri, 05 Mar 2021 00:00:00 GMT HTML Heading 1

HTML Heading 2

HTML Paragraph

Import DOM

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
  • list1
  • list2
  • list3
  • list1
  • list2
  • list3

Normal Text Italics Text Bold Text

linkimage

]]>
From 3b984ce4f2d1202202ec465f7f917a89e950198b Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Tue, 27 Dec 2022 17:17:53 +0000 Subject: [PATCH 09/17] fix: import --- .../docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index d3ade1f07228..843f9a45f2f2 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -9,7 +9,7 @@ declare module '@docusaurus/plugin-content-blog' { import type {LoadedMDXContent} from '@docusaurus/mdx-loader'; import type {MDXOptions} from '@docusaurus/mdx-loader'; import type {FrontMatterTag, Tag} from '@docusaurus/utils'; - import type {Plugin, LoadContext} from '@docusaurus/types'; + import type {DocusaurusConfig, Plugin, LoadContext} from '@docusaurus/types'; import type {Overwrite} from 'utility-types'; export type Assets = { From 745b0ab0def0e14a13c386e23acc9a8e48dc2b2b Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Tue, 27 Dec 2022 19:57:41 +0000 Subject: [PATCH 10/17] fix: address feedback --- .../src/plugin-content-blog.d.ts | 72 +++++-------------- 1 file changed, 17 insertions(+), 55 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 843f9a45f2f2..ee2dfc40a033 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -10,6 +10,7 @@ declare module '@docusaurus/plugin-content-blog' { import type {MDXOptions} from '@docusaurus/mdx-loader'; import type {FrontMatterTag, Tag} from '@docusaurus/utils'; import type {DocusaurusConfig, Plugin, LoadContext} from '@docusaurus/types'; + import type {FeedItem} from 'feed'; import type {Overwrite} from 'utility-types'; export type Assets = { @@ -264,16 +265,21 @@ declare module '@docusaurus/plugin-content-blog' { /** Language of the feed. */ language?: string; /** Allow control over the construction of BlogFeedItems */ - createFeedItems?: (options: { - blogPosts: BlogPost[]; - siteConfig: DocusaurusConfig; - outDir: string; - defaultCreateFeedItems: (params: { - blogPosts: BlogPost[]; - siteConfig: DocusaurusConfig; - outDir: string; - }) => Promise; - }) => Promise; + createFeedItems?: ( + options: CreateFeedItemsOptions, + ) => Promise; + }; + + type DefaultCreateFeedItemsOptions = { + blogPosts: BlogPost[]; + siteConfig: DocusaurusConfig; + outDir: string; + }; + + type CreateFeedItemsOptions = DefaultCreateFeedItemsOptions & { + defaultCreateFeedItems: ( + params: DefaultCreateFeedItemsOptions, + ) => Promise; }; /** @@ -462,51 +468,7 @@ declare module '@docusaurus/plugin-content-blog' { content: string; }; - export type BlogFeedItem = { - title: string; - id?: string; - link: string; - date: Date; - description?: string; - content?: string; - category?: Category[]; - guid?: string; - image?: string | Enclosure; - audio?: string | Enclosure; - video?: string | Enclosure; - enclosure?: Enclosure; - author?: BlogFeedItemAuthor[]; - contributor?: BlogFeedItemAuthor[]; - published?: Date; - copyright?: string; - extensions?: Extension[]; - }; - - export type BlogFeedItemAuthor = { - name?: string; - email?: string; - link?: string; - }; - - export type Enclosure = { - url: string; - type?: string; - length?: number; - title?: string; - duration?: number; - }; - - export type Extension = { - name: string; - objects: unknown; - }; - - export type Category = { - name?: string; - domain?: string; - scheme?: string; - term?: string; - }; + export type BlogFeedItem = FeedItem; export type BlogPaginatedMetadata = { /** Title of the entire blog. */ From 64bc4425ab8b0dab9a5ddb0665a61f178da5d816 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Tue, 27 Dec 2022 20:43:48 +0000 Subject: [PATCH 11/17] fix: import --- .../src/plugin-content-blog.d.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index ee2dfc40a033..14b8fd3f0231 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -10,12 +10,18 @@ declare module '@docusaurus/plugin-content-blog' { import type {MDXOptions} from '@docusaurus/mdx-loader'; import type {FrontMatterTag, Tag} from '@docusaurus/utils'; import type {DocusaurusConfig, Plugin, LoadContext} from '@docusaurus/types'; - import type {FeedItem} from 'feed'; + import type {Item as FeedItem} from 'feed'; import type {Overwrite} from 'utility-types'; export type Assets = { /** - * If `metadata.image` is a collocated image path, this entry will be the + * If `metadata.yarn workspace website typecheck +4 +yarn workspace v1.22.19yarn workspace website typecheck +4 +yarn workspace v1.22.19yarn workspace website typecheck +4 +yarn workspace v1.22.19image` is a collocated image path, this entry will be the * bundler-generated image path. Otherwise, it's empty, and the image URL * should be accessed through `frontMatter.image`. */ From 5980c829ef21990160a0ffac26d65f79f5017ee2 Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Wed, 28 Dec 2022 12:11:50 +0100 Subject: [PATCH 12/17] minor simplifications --- .../src/feed.ts | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/feed.ts b/packages/docusaurus-plugin-content-blog/src/feed.ts index b7aa3e2c9f14..dc9a7b85f84c 100644 --- a/packages/docusaurus-plugin-content-blog/src/feed.ts +++ b/packages/docusaurus-plugin-content-blog/src/feed.ts @@ -55,14 +55,16 @@ async function generateBlogFeed({ copyright: feedOptions.copyright, }); - const feedItems = await (feedOptions.createFeedItems - ? feedOptions.createFeedItems({ - blogPosts, - siteConfig, - outDir, - defaultCreateFeedItems, - }) - : defaultCreateFeedItems({blogPosts, siteConfig, outDir})); + const createFeedItems = + options.feedOptions.createFeedItems ?? defaultCreateFeedItems; + + const feedItems = await createFeedItems({ + blogPosts, + siteConfig, + outDir, + defaultCreateFeedItems, + }); + feedItems.forEach(feed.addItem); return feed; @@ -83,7 +85,7 @@ async function defaultCreateFeedItems({ return {name: author.name, link: author.url, email: author.email}; } - const items = await Promise.all( + return Promise.all( blogPosts.map(async (post) => { const { metadata: { @@ -125,7 +127,6 @@ async function defaultCreateFeedItems({ return feedItem; }), ); - return items; } async function createBlogFeedFile({ From 3b900e98f048ed2f9223fa34d8acbe7f8a21b2c6 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Wed, 28 Dec 2022 19:19:46 +0000 Subject: [PATCH 13/17] docs: add docs --- website/docs/api/plugins/plugin-content-blog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/api/plugins/plugin-content-blog.md b/website/docs/api/plugins/plugin-content-blog.md index d31d00c00b3b..24f7b6c0d6b9 100644 --- a/website/docs/api/plugins/plugin-content-blog.md +++ b/website/docs/api/plugins/plugin-content-blog.md @@ -67,6 +67,7 @@ Accepted fields: | `authorsMapPath` | `string` | `'authors.yml'` | Path to the authors map file, relative to the blog content directory. | | `feedOptions` | _See below_ | `{type: ['rss', 'atom']}` | Blog feed. | | `feedOptions.type` | FeedType \| FeedType[] \| 'all' \| null | **Required** | Type of feed to be generated. Use `null` to disable generation. | +| `feedOptions.createFeedItems` | `(options: {{ blogPosts: BlogPost[]; siteConfig: DocusaurusConfig; outDir: string; }) => Promise` | `undefined` | An optional function which can be used to transform and / or filter the items in the feed. | | `feedOptions.title` | `string` | `siteConfig.title` | Title of the feed. | | `feedOptions.description` | `string` | \`${siteConfig.title} Blog\` | Description of the feed. | | `feedOptions.copyright` | `string` | `undefined` | Copyright message. | From 0ad75757916269656fd9d3b38efc31b9661d7b21 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Wed, 28 Dec 2022 19:22:51 +0000 Subject: [PATCH 14/17] feat: remove test --- website/docusaurus.config.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 4657b25c0a80..870cbff8b812 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -341,11 +341,6 @@ const config = { feedOptions: { type: 'all', copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`, - createFeedItems: async (options) => { - const { blogPosts, defaultCreateFeedItems, ...others } = options; - const blogPostsFiltered = blogPosts.filter((item, index) => index < 2); - return defaultCreateFeedItems({ blogPosts: blogPostsFiltered, ...others }); - } }, blogSidebarCount: 'ALL', blogSidebarTitle: 'All our posts', From 69485cdcacdce98361b6e606cae8517717b57682 Mon Sep 17 00:00:00 2001 From: johnnyreilly Date: Wed, 28 Dec 2022 19:32:03 +0000 Subject: [PATCH 15/17] fix: error --- website/docs/api/plugins/plugin-content-blog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/api/plugins/plugin-content-blog.md b/website/docs/api/plugins/plugin-content-blog.md index 24f7b6c0d6b9..7ff1304da466 100644 --- a/website/docs/api/plugins/plugin-content-blog.md +++ b/website/docs/api/plugins/plugin-content-blog.md @@ -67,7 +67,7 @@ Accepted fields: | `authorsMapPath` | `string` | `'authors.yml'` | Path to the authors map file, relative to the blog content directory. | | `feedOptions` | _See below_ | `{type: ['rss', 'atom']}` | Blog feed. | | `feedOptions.type` | FeedType \| FeedType[] \| 'all' \| null | **Required** | Type of feed to be generated. Use `null` to disable generation. | -| `feedOptions.createFeedItems` | `(options: {{ blogPosts: BlogPost[]; siteConfig: DocusaurusConfig; outDir: string; }) => Promise` | `undefined` | An optional function which can be used to transform and / or filter the items in the feed. | +| `feedOptions.createFeedItems` | `(options: { blogPosts: BlogPost[]; siteConfig: DocusaurusConfig; outDir: string; }) => Promise` | `undefined` | An optional function which can be used to transform and / or filter the items in the feed. | | `feedOptions.title` | `string` | `siteConfig.title` | Title of the feed. | | `feedOptions.description` | `string` | \`${siteConfig.title} Blog\` | Description of the feed. | | `feedOptions.copyright` | `string` | `undefined` | Copyright message. | From 1665b3de495aa10ff3ba2f4af29b9e4216175cab Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Thu, 29 Dec 2022 12:18:33 +0100 Subject: [PATCH 16/17] rename createFeedItems options => params --- .../src/__tests__/feed.test.ts | 6 +++--- .../src/plugin-content-blog.d.ts | 8 ++++---- website/docs/api/plugins/plugin-content-blog.md | 2 +- website/docs/blog.mdx | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts index 1552b0193f10..6592c09d7aa5 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts @@ -173,14 +173,14 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => { feedOptions: { type: [feedType], copyright: 'Copyright', - createFeedItems: async (options) => { - const {blogPosts, defaultCreateFeedItems, ...others} = options; + createFeedItems: async (params) => { + const {blogPosts, defaultCreateFeedItems, ...rest} = params; const blogPostsFiltered = blogPosts.filter( (item, index) => index < 2, ); return defaultCreateFeedItems({ blogPosts: blogPostsFiltered, - ...others, + ...rest, }); }, }, diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 14b8fd3f0231..7165ad7fb472 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -272,19 +272,19 @@ yarn workspace v1.22.19image` is a collocated image path, this entry will be the language?: string; /** Allow control over the construction of BlogFeedItems */ createFeedItems?: ( - options: CreateFeedItemsOptions, + params: CreateFeedItemsParams, ) => Promise; }; - type DefaultCreateFeedItemsOptions = { + type DefaultCreateFeedItemsParams = { blogPosts: BlogPost[]; siteConfig: DocusaurusConfig; outDir: string; }; - type CreateFeedItemsOptions = DefaultCreateFeedItemsOptions & { + type CreateFeedItemsParams = DefaultCreateFeedItemsParams & { defaultCreateFeedItems: ( - params: DefaultCreateFeedItemsOptions, + params: DefaultCreateFeedItemsParams, ) => Promise; }; diff --git a/website/docs/api/plugins/plugin-content-blog.md b/website/docs/api/plugins/plugin-content-blog.md index 7ff1304da466..4434c11bcd9a 100644 --- a/website/docs/api/plugins/plugin-content-blog.md +++ b/website/docs/api/plugins/plugin-content-blog.md @@ -67,7 +67,7 @@ Accepted fields: | `authorsMapPath` | `string` | `'authors.yml'` | Path to the authors map file, relative to the blog content directory. | | `feedOptions` | _See below_ | `{type: ['rss', 'atom']}` | Blog feed. | | `feedOptions.type` | FeedType \| FeedType[] \| 'all' \| null | **Required** | Type of feed to be generated. Use `null` to disable generation. | -| `feedOptions.createFeedItems` | `(options: { blogPosts: BlogPost[]; siteConfig: DocusaurusConfig; outDir: string; }) => Promise` | `undefined` | An optional function which can be used to transform and / or filter the items in the feed. | +| `feedOptions.createFeedItems` | `(params: { blogPosts: BlogPost[]; siteConfig: DocusaurusConfig; outDir: string; }) => Promise` | `undefined` | An optional function which can be used to transform and / or filter the items in the feed. | | `feedOptions.title` | `string` | `siteConfig.title` | Title of the feed. | | `feedOptions.description` | `string` | \`${siteConfig.title} Blog\` | Description of the feed. | | `feedOptions.copyright` | `string` | `undefined` | Copyright message. | diff --git a/website/docs/blog.mdx b/website/docs/blog.mdx index 13d468ee415c..c3ee0587666a 100644 --- a/website/docs/blog.mdx +++ b/website/docs/blog.mdx @@ -512,7 +512,7 @@ type BlogOptions = { copyright: string; language?: string; // possible values: http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes /** Allow control over the construction of BlogFeedItems */ - createFeedItems?: (options: { + createFeedItems?: (params: { blogPosts: BlogPost[]; siteConfig: DocusaurusConfig; outDir: string; @@ -540,14 +540,14 @@ module.exports = { feedOptions: { type: 'all', copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`, - createFeedItems: async (options) => { - const {blogPosts, defaultCreateFeedItems, ...others} = options; + createFeedItems: async (params) => { + const {blogPosts, defaultCreateFeedItems, ...rest} = params; const blogPostsFiltered = blogPosts.filter( (item, index) => index < 2, ); return defaultCreateFeedItems({ blogPosts: blogPostsFiltered, - ...others, + ...rest, }); }, }, From f5bb16757c6e533330b025a09e2e0dc9427f21bf Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Thu, 29 Dec 2022 12:38:34 +0100 Subject: [PATCH 17/17] rename createFeedItems options => params + better docs --- .../src/plugin-content-blog.d.ts | 8 ++++--- .../docs/api/plugins/plugin-content-blog.md | 21 ++++++++++++++++++- website/docs/blog.mdx | 6 ++---- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 7165ad7fb472..bcce2e7c70ec 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -271,9 +271,7 @@ yarn workspace v1.22.19image` is a collocated image path, this entry will be the /** Language of the feed. */ language?: string; /** Allow control over the construction of BlogFeedItems */ - createFeedItems?: ( - params: CreateFeedItemsParams, - ) => Promise; + createFeedItems?: CreateFeedItemsFn; }; type DefaultCreateFeedItemsParams = { @@ -282,6 +280,10 @@ yarn workspace v1.22.19image` is a collocated image path, this entry will be the outDir: string; }; + type CreateFeedItemsFn = ( + params: CreateFeedItemsParams, + ) => Promise; + type CreateFeedItemsParams = DefaultCreateFeedItemsParams & { defaultCreateFeedItems: ( params: DefaultCreateFeedItemsParams, diff --git a/website/docs/api/plugins/plugin-content-blog.md b/website/docs/api/plugins/plugin-content-blog.md index 4434c11bcd9a..4e1ab5fcd0e9 100644 --- a/website/docs/api/plugins/plugin-content-blog.md +++ b/website/docs/api/plugins/plugin-content-blog.md @@ -67,7 +67,7 @@ Accepted fields: | `authorsMapPath` | `string` | `'authors.yml'` | Path to the authors map file, relative to the blog content directory. | | `feedOptions` | _See below_ | `{type: ['rss', 'atom']}` | Blog feed. | | `feedOptions.type` | FeedType \| FeedType[] \| 'all' \| null | **Required** | Type of feed to be generated. Use `null` to disable generation. | -| `feedOptions.createFeedItems` | `(params: { blogPosts: BlogPost[]; siteConfig: DocusaurusConfig; outDir: string; }) => Promise` | `undefined` | An optional function which can be used to transform and / or filter the items in the feed. | +| `feedOptions.createFeedItems` | CreateFeedItemsFn \| undefined | `undefined` | An optional function which can be used to transform and / or filter the items in the feed. | | `feedOptions.title` | `string` | `siteConfig.title` | Title of the feed. | | `feedOptions.description` | `string` | \`${siteConfig.title} Blog\` | Description of the feed. | | `feedOptions.copyright` | `string` | `undefined` | Copyright message. | @@ -118,6 +118,17 @@ type ReadingTimeFn = (params: { type FeedType = 'rss' | 'atom' | 'json'; ``` +#### `CreateFeedItemsFn` {#CreateFeedItemsFn} + +```ts +type CreateFeedItemsFn = (params: { + blogPosts: BlogPost[]; + siteConfig: DocusaurusConfig; + outDir: string; + defaultCreateFeedItemsFn: CreateFeedItemsFn; +}) => Promise; +``` + ### Example configuration {#ex-config} You can configure this plugin through preset options or plugin options. @@ -169,6 +180,14 @@ const config = { description: '', copyright: '', language: undefined, + createFeedItems: async (params) => { + const {blogPosts, defaultCreateFeedItems, ...rest} = params; + return defaultCreateFeedItems({ + // keep only the 10 most recent blog posts in the feed + blogPosts: blogPosts.filter((item, index) => index < 10), + ...rest, + }); + }, }, }; ``` diff --git a/website/docs/blog.mdx b/website/docs/blog.mdx index c3ee0587666a..fc5fa935063f 100644 --- a/website/docs/blog.mdx +++ b/website/docs/blog.mdx @@ -542,11 +542,9 @@ module.exports = { copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`, createFeedItems: async (params) => { const {blogPosts, defaultCreateFeedItems, ...rest} = params; - const blogPostsFiltered = blogPosts.filter( - (item, index) => index < 2, - ); return defaultCreateFeedItems({ - blogPosts: blogPostsFiltered, + // keep only the 10 most recent blog posts in the feed + blogPosts: blogPosts.filter((item, index) => index < 10), ...rest, }); },