Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 74 additions & 9 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import {
BlogItemsToModules,
TagsModule,
BlogPaginated,
FeedType,
} from './types';
import {
LoadContext,
PluginContentLoadedActions,
ConfigureWebpackUtils,
Props,
Plugin,
HtmlTags,
} from '@docusaurus/types';
import {Configuration, Loader} from 'webpack';
import {generateBlogFeed, generateBlogPosts} from './blogUtils';
Expand All @@ -42,6 +44,26 @@ const DEFAULT_OPTIONS: PluginOptions = {
truncateMarker: /<!--\s*(truncate)\s*-->/, // string or regex
};

function assertFeedTypes(val: any): asserts val is FeedType {
if (typeof val !== 'string' && !['rss', 'atom', 'all'].includes(val)) {
throw new Error(
`Invalid feedOptions type: ${val}. It must be either 'rss', 'atom', or 'all'`,
);
}
}

const getFeedTypes = (type?: FeedType) => {
assertFeedTypes(type);
let feedTypes: ('rss' | 'atom')[] = [];

if (type === 'all') {
feedTypes = ['rss', 'atom'];
} else {
feedTypes.push(type);
}
return feedTypes;
};

export default function pluginContentBlog(
context: LoadContext,
opts: Partial<PluginOptions>,
Expand Down Expand Up @@ -389,19 +411,13 @@ export default function pluginContentBlog(
return;
}

const {
feedOptions: {type: feedType},
} = options;
const feed = await generateBlogFeed(context, options);

if (!feed) {
return;
}
let feedTypes = [];
if (feedType === 'all') {
feedTypes = ['rss', 'atom'];
} else {
feedTypes.push(feedType);
}

const feedTypes = getFeedTypes(options.feedOptions?.type);

await Promise.all(
feedTypes.map(feedType => {
Expand All @@ -419,5 +435,54 @@ export default function pluginContentBlog(
}),
);
},

injectHtmlTags() {
if (!options.feedOptions) {
return {};
}

const feedTypes = getFeedTypes(options.feedOptions?.type);
const {
siteConfig: {title},
baseUrl,
} = context;
const feedsConfig = {
rss: {
type: 'application/rss+xml',
path: 'blog/rss.xml',
title: `${title} Blog RSS Feed`,
},
atom: {
type: 'application/atom+xml',
path: 'blog/atom.xml',
title: `${title} Blog Atom Feed`,
},
};
const headTags: HtmlTags = [];

feedTypes.map(feedType => {
const feedConfig = feedsConfig[feedType] || {};

if (!feedsConfig) {
return;
}

const {type, path, title} = feedConfig;

headTags.push({
tagName: 'link',
attributes: {
rel: 'alternate',
type,
href: normalizeUrl([baseUrl, path]),
title,
},
});
});

return {
headTags,
};
},
};
}
4 changes: 3 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface DateLink {
link: string;
}

export type FeedType = 'rss' | 'atom' | 'all';

export interface PluginOptions {
path: string;
routeBasePath: string;
Expand All @@ -30,7 +32,7 @@ export interface PluginOptions {
rehypePlugins: string[];
truncateMarker: RegExp | string;
feedOptions?: {
type: 'rss' | 'atom' | 'all';
type: FeedType;
title?: string;
description?: string;
copyright: string;
Expand Down