@docusaurus/plugin-content-blog 0.0.0-4286 → 0.0.0-4290

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/feed.js CHANGED
@@ -51,23 +51,40 @@ async function generateBlogFeed({ blogPosts, options, siteConfig, }) {
51
51
  }
52
52
  blogPosts.forEach((post) => {
53
53
  const { id, metadata: { title: metadataTitle, permalink, date, description, authors }, } = post;
54
- feed.addItem({
54
+ const feedItem = {
55
55
  title: metadataTitle,
56
56
  id,
57
57
  link: (0, utils_1.normalizeUrl)([siteUrl, permalink]),
58
58
  date,
59
59
  description,
60
60
  content: mdxToFeedContent(post.content),
61
- author: authors.map(toFeedAuthor),
62
- });
61
+ };
62
+ // json1() method takes the first item of authors array
63
+ // it causes an error when authors array is empty
64
+ const feedItemAuthors = authors.map(toFeedAuthor);
65
+ if (feedItemAuthors.length > 0) {
66
+ feedItem.author = feedItemAuthors;
67
+ }
68
+ feed.addItem(feedItem);
63
69
  });
64
70
  return feed;
65
71
  }
66
72
  exports.generateBlogFeed = generateBlogFeed;
67
- async function createBlogFeedFile({ feed, feedType, filePath, }) {
68
- const feedContent = feedType === 'rss' ? feed.rss2() : feed.atom1();
73
+ async function createBlogFeedFile({ feed, feedType, generatePath, }) {
74
+ const [feedContent, feedPath] = (() => {
75
+ switch (feedType) {
76
+ case 'rss':
77
+ return [feed.rss2(), 'rss.xml'];
78
+ case 'json':
79
+ return [feed.json1(), 'feed.json'];
80
+ case 'atom':
81
+ return [feed.atom1(), 'atom.xml'];
82
+ default:
83
+ throw new Error(`Feed type ${feedType} not supported.`);
84
+ }
85
+ })();
69
86
  try {
70
- await fs_extra_1.default.outputFile(filePath, feedContent);
87
+ await fs_extra_1.default.outputFile(path_1.default.join(generatePath, feedPath), feedContent);
71
88
  }
72
89
  catch (err) {
73
90
  throw new Error(`Generating ${feedType} feed failed: ${err}.`);
@@ -79,12 +96,10 @@ async function createBlogFeedFiles({ blogPosts, options, siteConfig, outDir, })
79
96
  if (!feed || !feedTypes) {
80
97
  return;
81
98
  }
82
- await Promise.all(feedTypes.map(async (feedType) => {
83
- await createBlogFeedFile({
84
- feed,
85
- feedType,
86
- filePath: path_1.default.join(outDir, options.routeBasePath, `${feedType}.xml`),
87
- });
88
- }));
99
+ await Promise.all(feedTypes.map((feedType) => createBlogFeedFile({
100
+ feed,
101
+ feedType,
102
+ generatePath: path_1.default.join(outDir, options.routeBasePath),
103
+ })));
89
104
  }
90
105
  exports.createBlogFeedFiles = createBlogFeedFiles;
package/lib/index.js CHANGED
@@ -364,6 +364,11 @@ function pluginContentBlog(context, options) {
364
364
  path: 'atom.xml',
365
365
  title: `${feedTitle} Atom Feed`,
366
366
  },
367
+ json: {
368
+ type: 'application/json',
369
+ path: 'feed.json',
370
+ title: `${feedTitle} JSON Feed`,
371
+ },
367
372
  };
368
373
  const headTags = [];
369
374
  feedTypes.forEach((feedType) => {
@@ -74,8 +74,8 @@ exports.PluginOptionSchema = utils_validation_1.Joi.object({
74
74
  beforeDefaultRehypePlugins: utils_validation_1.RehypePluginsSchema.default(exports.DEFAULT_OPTIONS.beforeDefaultRehypePlugins),
75
75
  feedOptions: utils_validation_1.Joi.object({
76
76
  type: utils_validation_1.Joi.alternatives()
77
- .try(utils_validation_1.Joi.array().items(utils_validation_1.Joi.string()), utils_validation_1.Joi.alternatives().conditional(utils_validation_1.Joi.string().equal('all', 'rss', 'atom'), {
78
- then: utils_validation_1.Joi.custom((val) => val === 'all' ? ['rss', 'atom'] : [val]),
77
+ .try(utils_validation_1.Joi.array().items(utils_validation_1.Joi.string().equal('rss', 'atom', 'json')), utils_validation_1.Joi.alternatives().conditional(utils_validation_1.Joi.string().equal('all', 'rss', 'atom', 'json'), {
78
+ then: utils_validation_1.Joi.custom((val) => val === 'all' ? ['rss', 'atom', 'json'] : [val]),
79
79
  }))
80
80
  .allow(null)
81
81
  .default(exports.DEFAULT_OPTIONS.feedOptions.type),
package/lib/types.d.ts CHANGED
@@ -17,7 +17,7 @@ export interface BlogContent {
17
17
  blogTags: BlogTags;
18
18
  blogTagsListPath: string | null;
19
19
  }
20
- export declare type FeedType = 'rss' | 'atom';
20
+ export declare type FeedType = 'rss' | 'atom' | 'json';
21
21
  export declare type FeedOptions = {
22
22
  type?: FeedType[] | null;
23
23
  title?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-content-blog",
3
- "version": "0.0.0-4286",
3
+ "version": "0.0.0-4290",
4
4
  "description": "Blog plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/plugin-content-blog.d.ts",
@@ -18,10 +18,10 @@
18
18
  },
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@docusaurus/core": "0.0.0-4286",
22
- "@docusaurus/mdx-loader": "0.0.0-4286",
23
- "@docusaurus/utils": "0.0.0-4286",
24
- "@docusaurus/utils-validation": "0.0.0-4286",
21
+ "@docusaurus/core": "0.0.0-4290",
22
+ "@docusaurus/mdx-loader": "0.0.0-4290",
23
+ "@docusaurus/utils": "0.0.0-4290",
24
+ "@docusaurus/utils-validation": "0.0.0-4290",
25
25
  "chalk": "^4.1.2",
26
26
  "escape-string-regexp": "^4.0.0",
27
27
  "feed": "^4.2.2",
@@ -37,7 +37,7 @@
37
37
  "webpack": "^5.61.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@docusaurus/types": "0.0.0-4286"
40
+ "@docusaurus/types": "0.0.0-4290"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "react": "^16.8.4 || ^17.0.0",
@@ -46,5 +46,5 @@
46
46
  "engines": {
47
47
  "node": ">=14"
48
48
  },
49
- "gitHead": "2a87bcff19729fff3df8fe20ca79fcac87b31371"
49
+ "gitHead": "ba51a860b7928a3d120f8409ad1ced28727b27b2"
50
50
  }
package/src/feed.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import {Feed, Author as FeedAuthor} from 'feed';
8
+ import {Feed, Author as FeedAuthor, Item as FeedItem} from 'feed';
9
9
  import {PluginOptions, Author, BlogPost, FeedType} from './types';
10
10
  import {normalizeUrl, mdxToHtml} from '@docusaurus/utils';
11
11
  import {DocusaurusConfig} from '@docusaurus/types';
@@ -68,15 +68,24 @@ export async function generateBlogFeed({
68
68
  id,
69
69
  metadata: {title: metadataTitle, permalink, date, description, authors},
70
70
  } = post;
71
- feed.addItem({
71
+
72
+ const feedItem: FeedItem = {
72
73
  title: metadataTitle,
73
74
  id,
74
75
  link: normalizeUrl([siteUrl, permalink]),
75
76
  date,
76
77
  description,
77
78
  content: mdxToFeedContent(post.content),
78
- author: authors.map(toFeedAuthor),
79
- });
79
+ };
80
+
81
+ // json1() method takes the first item of authors array
82
+ // it causes an error when authors array is empty
83
+ const feedItemAuthors = authors.map(toFeedAuthor);
84
+ if (feedItemAuthors.length > 0) {
85
+ feedItem.author = feedItemAuthors;
86
+ }
87
+
88
+ feed.addItem(feedItem);
80
89
  });
81
90
 
82
91
  return feed;
@@ -85,15 +94,26 @@ export async function generateBlogFeed({
85
94
  async function createBlogFeedFile({
86
95
  feed,
87
96
  feedType,
88
- filePath,
97
+ generatePath,
89
98
  }: {
90
99
  feed: Feed;
91
100
  feedType: FeedType;
92
- filePath: string;
101
+ generatePath: string;
93
102
  }) {
94
- const feedContent = feedType === 'rss' ? feed.rss2() : feed.atom1();
103
+ const [feedContent, feedPath] = (() => {
104
+ switch (feedType) {
105
+ case 'rss':
106
+ return [feed.rss2(), 'rss.xml'];
107
+ case 'json':
108
+ return [feed.json1(), 'feed.json'];
109
+ case 'atom':
110
+ return [feed.atom1(), 'atom.xml'];
111
+ default:
112
+ throw new Error(`Feed type ${feedType} not supported.`);
113
+ }
114
+ })();
95
115
  try {
96
- await fs.outputFile(filePath, feedContent);
116
+ await fs.outputFile(path.join(generatePath, feedPath), feedContent);
97
117
  } catch (err) {
98
118
  throw new Error(`Generating ${feedType} feed failed: ${err}.`);
99
119
  }
@@ -118,12 +138,12 @@ export async function createBlogFeedFiles({
118
138
  }
119
139
 
120
140
  await Promise.all(
121
- feedTypes.map(async (feedType) => {
122
- await createBlogFeedFile({
141
+ feedTypes.map((feedType) =>
142
+ createBlogFeedFile({
123
143
  feed,
124
144
  feedType,
125
- filePath: path.join(outDir, options.routeBasePath, `${feedType}.xml`),
126
- });
127
- }),
145
+ generatePath: path.join(outDir, options.routeBasePath),
146
+ }),
147
+ ),
128
148
  );
129
149
  }
package/src/index.ts CHANGED
@@ -550,6 +550,11 @@ export default function pluginContentBlog(
550
550
  path: 'atom.xml',
551
551
  title: `${feedTitle} Atom Feed`,
552
552
  },
553
+ json: {
554
+ type: 'application/json',
555
+ path: 'feed.json',
556
+ title: `${feedTitle} JSON Feed`,
557
+ },
553
558
  };
554
559
  const headTags: HtmlTags = [];
555
560
 
@@ -90,12 +90,12 @@ export const PluginOptionSchema = Joi.object<PluginOptions>({
90
90
  feedOptions: Joi.object({
91
91
  type: Joi.alternatives()
92
92
  .try(
93
- Joi.array().items(Joi.string()),
93
+ Joi.array().items(Joi.string().equal('rss', 'atom', 'json')),
94
94
  Joi.alternatives().conditional(
95
- Joi.string().equal('all', 'rss', 'atom'),
95
+ Joi.string().equal('all', 'rss', 'atom', 'json'),
96
96
  {
97
97
  then: Joi.custom((val) =>
98
- val === 'all' ? ['rss', 'atom'] : [val],
98
+ val === 'all' ? ['rss', 'atom', 'json'] : [val],
99
99
  ),
100
100
  },
101
101
  ),
package/src/types.ts CHANGED
@@ -24,7 +24,7 @@ export interface BlogContent {
24
24
  blogTagsListPath: string | null;
25
25
  }
26
26
 
27
- export type FeedType = 'rss' | 'atom';
27
+ export type FeedType = 'rss' | 'atom' | 'json';
28
28
 
29
29
  export type FeedOptions = {
30
30
  type?: FeedType[] | null;