feat(content-blog): support json feed (#6126)

* feat(content-blog): support json feed

* feat(content-blog): support json feed

* feat(content-blog): add json type to default feed options

* Refactors, docs, validation

* Fix test

* Ammend docs

* Add API doc

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
This commit is contained in:
不郑 2021-12-18 22:47:40 +08:00 committed by GitHub
parent 06bd44c693
commit 7e5f6bb805
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 165 additions and 30 deletions

View file

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import {Feed, Author as FeedAuthor} from 'feed';
import {Feed, Author as FeedAuthor, Item as FeedItem} from 'feed';
import {PluginOptions, Author, BlogPost, FeedType} from './types';
import {normalizeUrl, mdxToHtml} from '@docusaurus/utils';
import {DocusaurusConfig} from '@docusaurus/types';
@ -68,15 +68,24 @@ export async function generateBlogFeed({
id,
metadata: {title: metadataTitle, permalink, date, description, authors},
} = post;
feed.addItem({
const feedItem: FeedItem = {
title: metadataTitle,
id,
link: normalizeUrl([siteUrl, permalink]),
date,
description,
content: mdxToFeedContent(post.content),
author: authors.map(toFeedAuthor),
});
};
// json1() method takes the first item of authors array
// it causes an error when authors array is empty
const feedItemAuthors = authors.map(toFeedAuthor);
if (feedItemAuthors.length > 0) {
feedItem.author = feedItemAuthors;
}
feed.addItem(feedItem);
});
return feed;
@ -85,15 +94,26 @@ export async function generateBlogFeed({
async function createBlogFeedFile({
feed,
feedType,
filePath,
generatePath,
}: {
feed: Feed;
feedType: FeedType;
filePath: string;
generatePath: string;
}) {
const feedContent = feedType === 'rss' ? feed.rss2() : feed.atom1();
const [feedContent, feedPath] = (() => {
switch (feedType) {
case 'rss':
return [feed.rss2(), 'rss.xml'];
case 'json':
return [feed.json1(), 'feed.json'];
case 'atom':
return [feed.atom1(), 'atom.xml'];
default:
throw new Error(`Feed type ${feedType} not supported.`);
}
})();
try {
await fs.outputFile(filePath, feedContent);
await fs.outputFile(path.join(generatePath, feedPath), feedContent);
} catch (err) {
throw new Error(`Generating ${feedType} feed failed: ${err}.`);
}
@ -118,12 +138,12 @@ export async function createBlogFeedFiles({
}
await Promise.all(
feedTypes.map(async (feedType) => {
await createBlogFeedFile({
feedTypes.map((feedType) =>
createBlogFeedFile({
feed,
feedType,
filePath: path.join(outDir, options.routeBasePath, `${feedType}.xml`),
});
}),
generatePath: path.join(outDir, options.routeBasePath),
}),
),
);
}